完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
函数如下: main.c #include #include ***it DS1302_CE=P3^5; ***it DS1302_CK=P3^6; ***it DS1302_IO=P3^4; unsigned char T0RH=0;T0RL=0; bit flag200ms=0; unsigned char LED[]= {0x3f,0x06,0x5B,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71}; unsigned char DS1302SingleRead(unsigned char reg); void InitLCD1602(); void LCDshowstr(unsigned char x,unsigned char y,unsigned char *p); void DS1302SingleWrite(unsigned char reg,unsigned char dat); void Configetimer0(unsigned char ms) { unsigned int tmp; tmp=65536-12000000*ms/12/1000; T0RH=(unsigned char )(tmp>>8); T0RL=(unsigned char )tmp; TMOD&=0xF0; TMOD|=0x01; TH0=T0RH; TL0=T0RL; ET0=1; TR0=1; } unsigned char DS1302BitRead() { unsigned char dat=0; unsigned char mask; for (mask=0;mask<8;mask++,dat<<1) { if (DS1302_IO!=0) {dat=dat|0x01;} DS1302_CK=1; _nop_(); DS1302_CK=0; _nop_(); } return dat; } void DS1302BitWrite(unsigned char dat) { unsigned char mask; for (mask=0;mask<8;mask++,dat>>1) { if ((dat&0x01)!=0) {DS1302_IO=1;} else {DS1302_IO=0;} DS1302_CK=1; _nop_(); DS1302_CK=0; _nop_(); } } void InitDS1302() { unsigned char code InitTime[7]={0x00,0x30,0x09,0x18,0x03,0x06,0x13}; unsigned char i; DS1302_CK=0; DS1302_CE=0; i=DS1302SingleRead(0); if ((i&0x80)!=0) { DS1302SingleWrite(7,0x00); for(i=0;i<7;i++) { DS1302SingleWrite(i,InitTime[i]); } } } unsigned char DS1302SingleRead(unsigned char reg) { unsigned char dat; DS1302_IO=0xff; DS1302_CE=1; DS1302BitWrite((reg<<1)+0x81); dat=DS1302BitRead(); DS1302_CE=0; return dat; } void DS1302SingleWrite(unsigned char reg,unsigned char dat) { DS1302_CE=1; DS1302BitWrite((reg<<1)|0x80); DS1302BitWrite(dat); DS1302_CE=0; } void main() { unsigned char Psec=0xaa; unsigned char Time[8]; unsigned char str[10]; unsigned char i; EA=1; ConfigeTimer0(2); InitLCD1602(); InitDS1302(); while(1) { if (flag200ms) { flag200ms=0; for (i=0;i<7;i++) { Time[i]=DS1302SingleRead(i); } if (Psec!=Time[0]) { str[0]='2'; str[1]='0'; str[2]=(Time[6]>>4)+'0'; str[3]=(Time[6]&0x0f)+'0'; str[4]='-'; str[5]=(Time[4]>>4)+'0'; str[6]=(Time[4]&0x0f)+'0'; str[7]='-'; str[8]=(Time[3]>>4)+'0'; str[9]=(Time[3]&0x0f)+'0'; str[10]=' |