本帖最后由 jinglixixi 于 2020-7-11 08:21 编辑
DS1302计时模块是一种小巧的带后备电池的功能模块,可在掉电的情况下一直维持着计时器的运行。 DS1302与SDK-HC89F0541开发板的连接关系为: RST --- P3_5 IO --- P3_4 SCK --- P3_3
为便于观察计时结果,还使用串行通讯口来输出计时内容。其中,TXD为 P16,而RXD 为P17,整体的连接构成如图所示。 RTC计时构成图
串口发送字节的函数为: - void SendByte(uchar dat)
- {
- IE &=~ 0x10;
- SBUF = dat;
- while(!(SCON & 0x02));
- SCON &=~ 0x02;
- IE |= 0x10;
- }
复制代码
串口发送计时值得函数为: - void RTC_xs(void)
- {
- SendByte(readtime[0]+'0');
- SendByte(readtime[1]+'0');
- SendByte(readtime[2]+'0');
- SendByte(readtime[3]+'0');
- SendByte('-');
- SendByte(readtime[4]+'0');
- SendByte(readtime[5]+'0');
- SendByte('-');
- SendByte(readtime[6]+'0');
- SendByte(readtime[7]+'0');
- SendByte(' ');
- SendByte(readtime[8]+'0');
- SendByte(readtime[9]+'0');
- SendByte(':');
- SendByte(readtime[10]+'0');//s
- SendByte(readtime[11]+'0');
- SendByte(':');
- SendByte(readtime[12]+'0');//s
- SendByte(readtime[13]+'0');
- SendByte('\r');
- SendByte('\n');
- }
复制代码
使DS1302 模块产生图示效果的主程序为:- void main(void)
- {
- WDTCCR = 0x00;
- while((CLKCON&0x20)!=0x20);
- CLKSWR = 0x51;
- while((CLKSWR&0xC0)!=0x40);
- CLKDIV = 0x01;
- P3M3 = 0xC2;
- P3M4 = 0xC2;
- P3M5 = 0xC2;
- P1M6 = 0xC2;
- P1M7 = 0x62;
- TXD_MAP = 0x16;
- RXD_MAP = 0x17;
- T4CON = 0x06;
- TH4 = 0xFF;
- TL4 = 0x98; // 9600bps
- SCON2 = 0x02; // 8bit
- SCON = 0x10;
- IE |= 0x10;
- EA = 1;
-
- ds1302_init();
- time_buf[1]=0x20;
- time_buf[2]=0x07;
- time_buf[3]=0x07;
- time_buf[4]=0x14;
- time_buf[5]=0x01;
- time_buf[6]=0x30;
- time_buf[7]=0x02;
-
- ds1302_write_time();
- while(1)
- {
- ds1302_read_time();
- readtime[0]=(time_buf[0]>>4);
- readtime[1]=(time_buf[0]&0x0F);
- readtime[2]=(time_buf[1]>>4);
- readtime[3]=(time_buf[1]&0x0F);
- readtime[4]=(time_buf[2]>>4);
- readtime[5]=(time_buf[2]&0x0F);
- readtime[6]=(time_buf[3]>>4);
- readtime[7]=(time_buf[3]&0x0F);
- readtime[8]=(time_buf[4]>>4);
- readtime[9]=(time_buf[4]&0x0F);
- readtime[10]=(time_buf[5]>>4);
- readtime[11]=(time_buf[5]&0x0F);
- readtime[12]=(time_buf[6]>>4);
- readtime[13]=(time_buf[6]&0x0F);
- if(readtime[13]!=sec_buf)
- {
- sec_flag=0;
- sec_buf=readtime[13];
- RTC_xs(); ;
- }
- }
- }
复制代码
计时效果图
|