如何實(shí)現(xiàn)DS1302實(shí)時(shí)時(shí)鐘表顯示在1602LCD液晶
#include
#include
sbit SCLK=P3^6;
sbit SDA=P3^4;
sbit RST = P3^5;
//unsigned char code Write_Address[]={0x80,0x82,0x84};
unsigned char code Read_Address[]={0x85,0x83,0x81};
unsigned char Time_Table[3];
void Write_Byte(unsigned char Data)//寫(xiě)一個(gè)字節(jié): 地址或數(shù)據(jù)
{
unsigned char i;
for(i=0; i<8; i++)
{
SCLK=0;
SDA=Data&0x01;
Data=Data>>1;
SCLK=1;
}
}
void Write_RTC(unsigned char Address, unsigned char Data)
{
RST=0;
_nop_();
SCLK=0;
_nop_();
RST=1;
Write_Byte(Address);
Write_Byte(Data);
RST=0;
}
unsigned char Read_Byte() //讀一個(gè)字節(jié): 數(shù)據(jù)
{
unsigned char Data=0;
unsigned char i;
for(i=0; i<8; i++)
{
if(SDA==1)
{
Data=Data|0x80;
}
SCLK=0;
Data=Data>>1;
_nop_();
SCLK=1;
}
return Data;
}
unsigned char Read_RTC(unsigned char Address) //
{
unsigned char Data=0;
unsigned char Data1,Data2;
RST=0;
_nop_();
SCLK=0;
_nop_();
RST=1;
Write_Byte(Address);
Data=Read_Byte();
RST=0;
Data1=Data/16;
Data2=Data%16;
Data=Data2+Data1*10;
return Data;
}
void Init_RTC() //初始化實(shí)時(shí) 時(shí)鐘表RTC
{
Write_RTC(0x8e, 0x00);
Write_RTC(0x80, 0x56);
Write_RTC(0x82, 0x48);
Write_RTC(0x84, 0x12);
Write_RTC(0x8e, 0x80)
}
void DisplayTime() //把時(shí)間顯示1602液晶
{
unsigned char i;
InitLCD();
Write_Command(0x80+0x03);
for(i=0; i<3; i++)
{
Time_Table[i]=Read_RTC(Read_Address[i]);
Write_Data(0x30+Time_Table[i]/10);
Write_Data(0x30+Time_Table[i]%10);
if(i!=2)
Write_Data(:);
}
}
void main()
{
Init_RTC();
DisplayTime();
while(1);
}
其實(shí),寫(xiě)這個(gè)程序,也是比較簡(jiǎn)單的, 比在24C02 芯片里讀寫(xiě)容易了。因?yàn)樵?4C02讀寫(xiě)里,是使用I2C總線傳輸數(shù)據(jù)的,關(guān)鍵要把握好時(shí)序讀寫(xiě),至今仍然未解決這個(gè)24C02芯片的讀寫(xiě),也不知道是,芯片出問(wèn)題,還是自己程序的時(shí)序出問(wèn)題。
評(píng)論