|
單片機控制的DS1302+102*65液晶顯示C51程序
 void write_ds1302(unsigned char Writeaddr,unsigned char Writedata) { unsigned char i;
TIMECLK=0; delayus(1); TIMERST=1; delayus(1); for(i=8;i>0;i--) { TIMECLK=0; TIMEDATA=Writeaddr&0x01; delayus(1); // TIMECLK=1; Writeaddr=Writeaddr>>1; }
for(i=8;i>0;i--) { TIMECLK=0; TIMEDATA=Writedata&0x01; delayus(1); // TIMECLK=1; Writedata=Writedata>>1; }
TIMERST=0; delayus(1); TIMECLK=0; delayus(1); }
/**************************************************************************** 從DS1302讀出一個字節(jié)的數(shù)據(jù) ****************************************************************************/ unsigned char read_ds1302(unsigned char Readaddr) { unsigned char i; unsigned char value=0; TIMECLK=0; delayus(2); TIMERST=1; delayus(2); for(i=8;i>0;i--) { TIMECLK=0; //0 TIMEDATA=Readaddr&0x01; delayus(1); // TIMECLK=1; Readaddr=Readaddr>>1; }
for(i=8;i>0;i--) { value=value>>1; TIMECLK=1; delayus(1); // TIMECLK=0; if(TIMEDATA==1) { value=value|0x80; } delayus(1); }
TIMERST=0; delayus(1); TIMECLK=0; delayus(1);
return value; }
/**************************************************************************** DS1302初始化 ****************************************************************************/ void init_ds1302(void) { if(read_ds1302(0x0cd)!=0x01) { TIMECLK=0; TIMERST=0;
write_ds1302(0x8e,0x00);
write_ds1302(0x82,INIT_MINUTE); write_ds1302(0x84,INIT_HOUR); write_ds1302(0x86,INIT_DAY); write_ds1302(0x88,INIT_MONTH); write_ds1302(0x8a,INIT_WEEK); write_ds1302(0x8c,INIT_YEAR);
write_ds1302(0x90,0xa6); write_ds1302(0x0cc,0x01); write_ds1302(0x80,INIT_SECOND); } } /***********************************************************/ display_time() { uccurtime[0]=read_ds1302(0x81); //秒 disp_time(); uccurtime[1]=read_ds1302(0x83); //分 disp_time(); uccurtime[2]=read_ds1302(0x85); //時 disp_time(); uccurtime[3]=read_ds1302(0x87); //日 disp_time(); uccurtime[4]=read_ds1302(0x89); //月 disp_time(); uccurtime[5]=read_ds1302(0x8d); //年 disp_time(); uccurtime[6]=read_ds1302(0x8b); //周 disp_time(); // delay100ms(); // delay100ms(); // delay100ms(); } |