#include<reg52.h> #include<intrins.h>
#define uchar unsigned char #define uint unsigned int #define NOP _nop_()
sbit LCD_RS=P2^6; sbit LCD_RW=P2^5; sbit LCD_EP=P2^7; sbit PCF_SCL=P2^0; sbit PCF_SDA=P2^1;
LCD_INIT(); LCD_WC(uchar dat); LCD_DELAY(uchar ms); bit LCD_BZ(); LCD_WD(uchar dat); dishow1(); void ConvertASCII (void); void Lcd(); unsigned int D[16]; unsigned char rd[6]; void GetTimer();
delay();delays(); IRcvStr(uchar suba,num); ISendStr(uchar suba,uchar mdata); ISendbyte(uchar c);void WaitACK(); void Ack_I2c(bit a); uchar RcvByte(); void SendByte(uchar c) ;void Stop_I2c(); void Start_I2c();
uchar code Cutime[]={"Current time is:"}; uchar code Cudate[]={"Current date is:"}; uchar code Cuweek[]={"Current week is:"}; uchar code time[]={"Time"};
uchar show[10]={0x30,0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39}; uchar dat,i; char temp[15]; uchar td[5]={0x32,0x30,0x31,0x30,'/'}; uchar week[7][3]={{'S','u','n'},{'M','o','n'},{'T','u','e'},{'W','e','d'},{'T','h','u'},{'F','r','i'},{'S','t','a'}}; void main() { LCD_INIT(); ISendStr(0,0);delays(); while(1) Lcd(); }
void Lcd() { GetTimer();
ConvertASCII (); LCD_WC(0xc8); LCD_DELAY(1); for (i=7;i<15;i++) { LCD_WD(temp[i]); } LCD_WC(0x85); for (i=1;i<6;i++) { LCD_WD(temp[i]); } LCD_WC(0x8d); for(i=0;i<3;i++) { LCD_WD(week[temp[0]][i]); } }
void GetTimer() { delays(); IRcvStr(2,6); //读现在的时、分、秒
rd[0]&=0x7f; rd[1]&=0x7f; rd[2]&=0x3f; rd[3]&=0x3f; rd[4]&=0x07; rd[5]&=0x1f; }
void ConvertASCII (void) { temp[0]=rd[5]&0x0f;
//*********月/日********
temp[1]=show[rd[4]>>4]; //月
temp[2]=show[rd[4]&0x0f];
temp[3]='/';
temp[4]=show[rd[3]>>4]; // 日
temp[5]=show[rd[3]&0x0f];
//*********时/分/秒********
temp[7]=show[rd[2]>>4]; //时
temp[8]=show[rd[2]&0x0f];
temp[9]=':';
temp[10]=show[rd[1]>>4]; //分
temp[11]=show[rd[1]&0x0f];
temp[12]=':';
temp[13]=show[rd[0]>>4]; // 秒
temp[14]=show[rd[0]&0x0f];
}
dishow1() { LCD_WC(0x07); LCD_WC(0x90); i=0; while(i<16) { LCD_WD( Cutime[i]); i++; LCD_DELAY(45); } }
LCD_WD(uchar dat) { while(LCD_BZ()); LCD_RS=1; LCD_RW=LCD_EP=0; NOP;NOP; P0=dat; NOP;NOP;NOP;NOP; LCD_EP=1; NOP;NOP;NOP;NOP; LCD_EP=0; }
LCD_INIT() { LCD_DELAY(5);
LCD_WC(0x38); LCD_DELAY(1);
LCD_WC(0x0c); LCD_DELAY(1);
LCD_WC(0x06); LCD_DELAY(1);
LCD_WC(0x01); LCD_DELAY(1); for (i=0;i<5;i++) { LCD_WD(td[i]); } }
bit LCD_BZ() { bit BZ; LCD_RS=0; LCD_RW=1; LCD_EP=1; NOP;NOP;NOP;NOP; BZ=(bit)(P0 & 0x80); LCD_EP=0; return BZ; }
LCD_DELAY(uchar ms) { uchar datams; while(ms--) for(datams=0;datams<250;datams++); }
LCD_WC(uchar dat) { while(LCD_BZ()); LCD_RS=LCD_RW=LCD_EP=0; NOP;NOP; P0=dat; NOP;NOP;NOP;NOP; LCD_EP=1; NOP;NOP;NOP;NOP; LCD_EP=0; }
IRcvStr(uchar suba,num) { uchar j; Start_I2c(); delay(); SendByte(0xa0); delay(); SendByte(suba); delay(); Start_I2c(); delay(); SendByte(0xa1); delay(); for(j=0;j<num;j++) { rd[j]=RcvByte(); delay(); if(j<num-1)Ack_I2c(0); } Ack_I2c(1); delay(); Stop_I2c(); delay(); }
ISendStr(uchar suba,uchar mdata) { Start_I2c(); SendByte(0xa0); delay(); SendByte(suba); delay(); SendByte(mdata); delay(); Stop_I2c(); delay(); }
void Ack_I2c(bit a) { PCF_SDA=a; delay(); PCF_SCL=1; delay(); PCF_SCL=0; delay(); }
uchar RcvByte() { uchar BitCnt,retc; PCF_SDA=1; for(BitCnt=0;BitCnt<8;BitCnt++) { PCF_SCL=1; delay(); retc=retc<<1; retc|=PCF_SDA; PCF_SCL=0; delay(); } delay(); return(retc); delay(); }
void SendByte(uchar c) { uchar BitCnt; for(BitCnt=0;BitCnt<8;BitCnt++) { if(c&0x80)PCF_SDA=1; else PCF_SDA=0; c=c<<1; PCF_SCL=1; delay(); PCF_SCL=0; delay(); } delay(); WaitACK(); }
void WaitACK() { uchar errtime=20; PCF_SDA=1; delay(); PCF_SCL=1; delay(); while(PCF_SDA) { errtime--; delay(); if(!errtime) Stop_I2c(); } PCF_SCL=0; delay(); }
void Stop_I2c() { PCF_SDA=0; delay(); PCF_SCL=1; delay(); PCF_SDA=1; delay(); PCF_SCL=0; }
void Start_I2c() { PCF_SDA=1; delay(); PCF_SCL=1; delay(); PCF_SDA=0; delay(); PCF_SCL=0; NOP;NOP; }
delay() { uchar aa; for(aa=0;aa<50;aa++); }
delays() { uchar bb,aa; for(aa=0;aa<10;aa++) for(bb=0;bb<100;bb++); }
|