Sunteți pe pagina 1din 3

#include<reg51.

h>
sbit rs=P3^4;
sbit rw=P3^5;
sbit en=P3^6;
sbit SDA=P1^0;
sbit SCL=P1^1;
sbit SW=P1^7;
char ack;
char array[7];
void I2CStart(){SDA=1;SCL=1,SDA=0,SCL=0;}
void I2CStop(){SDA=0,SCL=1,SDA=1;}
unsigned char I2CSend(unsigned char Data)
{
char i;
static bit ack_bit;
for(i=0;i<8;i++)
{
if(Data & 0x80) SDA=1;
else SDA=0;
SCL=1;
Data<<=1;
SCL=0;
}
SDA=1,SCL=1;
ack_bit=SDA;
SCL=0;
return ack_bit;
}
unsigned char I2CRead(char ack)
{
unsigned char i, Data=0;
SDA=1;
for(i=0;i<8;i++)
{
Data<<=1;
do{SCL=1;}
while(SCL==0);
if(SDA) Data|=1;
SCL=0;
}
if(ack)SDA=0;
else SDA=1;
SCL=1;
SCL=0;
SDA=1;
return Data;
}
void delay(int time)
{unsigned int i,j;
for(i=0;i<time;i++)
for(j=0;j<1000;j++);}
void LCD_command(unsigned char cmd)
{
P0=cmd;
rw=0;rs=0,en=1;
delay(5),en=0;
}
void LCD_senddata(unsigned char item)
{
P0=item;
rw=0;rs=1,en=1;
delay(1),en=0;
}
void LCD_init()
{
LCD_command(0x38);
LCD_command(0x06);
LCD_command(0x0c);
}
void LCD_sendnum(unsigned char khan)
{
LCD_senddata(khan/16+48);
LCD_senddata(khan%16+48);
}
void print(char *str)
{
while(*str!='\0')
{
LCD_senddata(*str);
str++;
}
}
void day(char d)
{
if(d==01)
print("SUN");
if(d==02)
print("MON");
if(d==03)
print("TUE");
if(d==04)
print("WED");
if(d==05)

print("THU");
if(d==06)
print("FRI");
if(d==07)
print("SAT");
}
void main()
{
int s;
//SW=1;//SW=0;
LCD_init();
LCD_command(0x80);
print("RTC");
delay(200);
LCD_command(0x01);
I2CStart();
I2CSend(0xD0);
I2CSend(0x00);
I2CSend(0x01);
I2CSend(0x50); //MIN
I2CSend(0x22); //HR
I2CSend(0x03); //week
I2CSend(0x15); //DAY
I2CSend(0x10); //MON
I2CSend(0x17); //YR
I2CSend(0x80);
I2CStop();
while(1)
{
I2CStart();
I2CSend(0xD0);
I2CSend(0x00);
I2CStart();
I2CSend(0xD1);
for(s=0;s<7;s++)
{array[s]=I2CRead(1);}
I2CStop();
LCD_command(0x80);
LCD_sendnum(array[4]);
print("-");
LCD_sendnum(array[5]);
print("-20");
LCD_sendnum(array[6]);
LCD_command(0x8c);
day(array[3]);
LCD_command(0xc0);
LCD_sendnum(array[2]);
print(":");
LCD_sendnum(array[1]);
print(":");
LCD_sendnum(array[0]);
LCD_command(0xc9);
}
}

S-ar putea să vă placă și