#include<intrins.h>
#include<reg51.h>
sbit rs=P0^1;
sbit rw=P0^2;
sbit en=P0^3;
sbit en1=P2^7;
sbit sda=P1^0;
sbit scl=P1^1;
void i2c_start();
void lcd_init1();
void i2c_stop();
void data_init();
void display_routine(unsigned char);
void slave_address(unsigned char);
void data_write(unsigned char);
unsigned char data_read();
void ack();
void ack1();
void m_ack();
void m_nack();
void lcd_init();
void data_mode1(unsigned char );
void data_mode(unsigned char);
void command_mode(unsigned char);
void busy();
unsigned char ch1[][5]={"sun","mon","tue","wed","thur","fri","sat"};
unsigned char ch[7],x;
int main()
{
unsigned char i=0,b=0,c=0,z=0;
P2=0x00;
rs=0;
rw=0;
en=0;
en1=1;
scl=0;
sda=0;
i2c_start();
slave_address(0xd0);
ack();
data_write(0x00);
ack();
data_write(0x00);
ack();
data_write(0x08);
ack();
data_write(0x07);
ack();
data_write(0x4);
ack();
data_write(0x6);
ack();
data_write(0x6);
ack();
data_write(0x12);
i2c_stop();
lcd_init();
//while(1)
//{
command_mode(0x83);
i2c_start();
slave_address(0xd0);
ack();
data_write(0x00);
ack();
i2c_start();
slave_address(0xd1);
ack();
for(i=0;i<7;i++)
{
ch[i]=data_read();
if(i==7)
{
m_nack();
}
else{
m_ack();
}
}
i2c_stop();
display_routine(ch[2]);
data_mode(':');
display_routine(ch[1]);
data_mode(':');
display_routine(ch[0]);
command_mode(0xc0);
data_mode1(ch[3]);
command_mode(0xc7);
display_routine(ch[4]);
data_mode('-');
display_routine(ch[5]);
data_mode('-');
display_routine(ch[6]);
//}
return 0;
}
void data_write(unsigned char a)
{
unsigned char i=0,x=0;
for(i=0;i<8;i++)
{
if(a&0x80)
{
sda=1;
}
else
{
sda=0;
}
scl=1;
_nop_();
_nop_();
scl=0;
a=a<<1;
}
}
unsigned char data_read()
{
unsigned char a=0,b=0,c=0,i;
sda=1;
for(i=0;i<8;i++)
{
a<<=1;
if(sda)
{
a=a|0x01;
}
scl=1;
_nop_();
_nop_();
//_nop_();
scl=0;
}
return a;
}
void slave_address(unsigned char a)
{
data_write(a);
}
void ack()
{
sda=0;
scl=1;
_nop_();
_nop_();
//_nop_();
scl=0;
if(sda==0)
{
;
}
else
{
;
}
}
void m_ack()
{
sda=0;
scl=1;
_nop_();
_nop_();
//_nop_();
scl=0;
}
void m_nack()
{
sda=1;
scl=1;
_nop_();
_nop_();
//_nop_();
scl=0;
}
void i2c_start()
{
sda=1;
scl=1;
_nop_();
_nop_();
//_nop_();
sda=0;
scl=0;
}
void i2c_stop()
{
sda=0;
scl=1;
_nop_();
_nop_();
sda=1;
scl=0;
}
void command_mode(unsigned char a)
{
//delay();
busy();
P2=a;
rs=0;
rw=0;
en=1;
_nop_();
_nop_();
en=0;
}
void data_mode(unsigned char a)
{
busy();
P2=a;
data_init();
}
void data_mode1(unsigned char p)
{
unsigned char j=0;
busy();
p=p-1;
while(ch1[p][j]!='\0')
{
P2=ch1[p][j];
data_init();
j++;
}
}
void data_init()
{
rs=1;
rw=0;
en=1;
_nop_();
_nop_();
en=0;
}
void lcd_init()
{
command_mode(0x01);
command_mode(0x38);
command_mode(0x06);
command_mode(0x0c);
command_mode(0x80);
}
void busy()
{
en1=1;
rs=0;
rw=1;
while(en1)
{
en=0;
_nop_();
_nop_();
en=1;
}
}
void display_routine(unsigned char ch)
{
unsigned char b=0,c=0;
b=ch&0x0f;
b=b|0x30;
c=ch>>4;
c=c|0x30;
data_mode(c);
data_mode(b);
}