#include<reg52.h> #include<intrins.h>
sbit RST=P2^4; sbit CLK=P2^1; sbit IO=P2^0; sbit RS=P1^0; sbit RW=P1^1; sbit E=P2^5; sbit DU=P2^6;
unsigned char num[]={"0123456789"};
void X() { DU=1; P0=0x00; DU=0; }
void Delay() { _nop_(); }
void Write_Bit_DS1302(unsigned char DAT) { unsigned char i; CLK=0; Delay(); for(i=0;i<8;i++) { IO=DAT&0x01; Delay(); CLK=1; Delay(); CLK=0; DAT>>=1; } }
void Write_DS1302(unsigned char CMD,unsigned char DAT) { RST=0; CLK=0; RST=1; Delay(); Write_Bit_DS1302(CMD); Write_Bit_DS1302(DAT); CLK=1; RST=0; }
unsigned char Read_Bit_DS1302() { unsigned char i,DAT; Delay(); for(i=0;i<8;i++) { DAT>>=1; if(IO==1) { DAT|=0x80; } CLK=1; Delay(); CLK=0; Delay(); } return DAT; }
unsigned char Read_DS1302(unsigned char CMD) { unsigned char DAT; RST=0; CLK=0; RST=1; Write_Bit_DS1302(CMD); DAT=Read_Bit_DS1302(); CLK=1; RST=0; return DAT; }
void Init_DS1302() { unsigned char X; X=Read_DS1302(0x81); if(X&0x80) { Write_DS1302(0x8e,0x00); Write_DS1302(0x80,((00/10)<<4|(00%10))); Write_DS1302(0x82,((00/10)<<4|(00%10))); Write_DS1302(0x84,((00/10)<<4|(00%10))); Write_DS1302(0x86,((11/10)<<4|(11%10))); Write_DS1302(0x88,((5/10)<<4|(5%10))); Write_DS1302(0x8c,((21/10)<<4|(21%10))); Write_DS1302(0x8e,0x80); } }
void Delay5ms() { unsigned char i,j; _nop_(); i=9; j=244; do { while(--j); } while(--i); }
int ReadBusy() { int temp; RS=0; RW=1; _nop_(); P0=0xff; _nop_(); E=1; _nop_(); temp=P0; _nop_(); E=0; return(temp&0x80); }
void Write_Com(char com) { while(ReadBusy()); RS=0; RW=0; E=0; _nop_(); P0=com; _nop_(); E=1; Delay5ms(); E=0; Delay5ms(); }
void Write_Dat(char dat) { while(ReadBusy()); RS=1; RW=0; E=0; _nop_(); P0=dat; _nop_(); E=1; Delay5ms(); E=0; Delay5ms(); }
void LCD1602_Init() { Delay5ms(); Delay5ms(); Delay5ms(); Write_Com(0x38); Delay5ms(); Write_Com(0x38); Delay5ms(); Write_Com(0x38); Delay5ms(); Write_Com(0x0c); Delay5ms(); Write_Com(0x06); Delay5ms(); Write_Com(0x01); Delay5ms(); }
void Display_Second(unsigned char x) { unsigned char i,j; i=x/10; j=x%10; Write_Com(0x80+0x49); Write_Dat(num[i]); Write_Dat(num[j]); Delay5ms(); }
void Display_Minute(unsigned char x) { unsigned char i,j; i=x/10; j=x%10; Write_Com(0x80+0x46); Write_Dat(num[i]); Write_Dat(num[j]); Delay5ms(); }
void Display_Hour(unsigned char x) { unsigned char i,j; i=x/10; j=x%10; Write_Com(0x80+0x43); Write_Dat(num[i]); Write_Dat(num[j]); Delay5ms(); }
void Display_Day(unsigned char x) { unsigned char i,j; i=x/10; j=x%10; Write_Com(0x80+0x0c); Write_Dat(num[i]); Write_Dat(num[j]); Delay5ms(); }
void Display_Month(unsigned char x) { unsigned char i,j; i=x/10; j=x%10; Write_Com(0x80+0x09); Write_Dat(num[i]); Write_Dat(num[j]); Delay5ms(); }
void Display_Year(unsigned char x) { unsigned char i,j; i=x/10; j=x%10; Write_Com(0x80+0x06); Write_Dat(num[i]); Write_Dat(num[j]); Delay5ms(); }
void main() { unsigned char second,minute,hour,day,month,year; unsigned char temp; X(); LCD1602_Init(); Write_Com(0x80+0x01); Write_Dat('D'); Write_Dat('A'); Write_Dat('T'); Write_Dat('E'); Write_Dat(':'); Delay5ms(); Write_Com(0x80+0x08); Write_Dat('-'); Delay5ms(); Write_Com(0x80+0x0b); Write_Dat('-'); Delay5ms(); Write_Com(0x80+0x45); Write_Dat(':'); Delay5ms(); Write_Com(0x80+0x48); Write_Dat(':'); Delay5ms(); Init_DS1302(); while(1) { temp=Read_DS1302(0x81); second=((temp&0x70)>>4)*10+(temp&0x0f); Display_Second(second); temp=Read_DS1302(0x83); minute=((temp&0x70)>>4)*10+(temp&0x0f); Display_Minute(minute); temp=Read_DS1302(0x85); hour=((temp&0x70)>>4)*10+(temp&0x0f); Display_Hour(hour); temp=Read_DS1302(0x87); day=((temp&0x70)>>4)*10+(temp&0x0f); Display_Day(day); temp=Read_DS1302(0x89); month=((temp&0x70)>>4)*10+(temp&0x0f); Display_Month(month); temp=Read_DS1302(0x8d); year=((temp&0x70)>>4)*10+(temp&0x0f); Display_Year(year); } }
|