找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 2000|回复: 0
收起左侧

芯圣SDK-HC89F0541单片机的DS1302 RTC计时

[复制链接]
ID:776997 发表于 2020-7-11 08:18 | 显示全部楼层 |阅读模式
本帖最后由 jinglixixi 于 2020-7-11 08:21 编辑

DS1302计时模块是一种小巧的带后备电池的功能模块,可在掉电的情况下一直维持着计时器的运行。
DS1302SDK-HC89F0541开发板的连接关系为:
RST  --- P3_5
IO     --- P3_4
SCK  ---  P3_3

为便于观察计时结果,还使用串行通讯口来输出计时内容。其中,TXD P16,而RXD P17,整体的连接构成如图所示。
1.png
RTC计时构成图


串口发送字节的函数为:
  1. void SendByte(uchar dat)
  2. {
  3.            IE &=~ 0x10;
  4.            SBUF = dat;         
  5.            while(!(SCON & 0x02));
  6.            SCON &=~ 0x02;
  7.            IE |= 0x10;
  8. }
复制代码

串口发送计时值得函数为:
  1. void RTC_xs(void)
  2. {
  3.   SendByte(readtime[0]+'0');
  4.   SendByte(readtime[1]+'0');   
  5.   SendByte(readtime[2]+'0');
  6.   SendByte(readtime[3]+'0');   
  7.   SendByte('-');
  8.   SendByte(readtime[4]+'0');
  9.   SendByte(readtime[5]+'0');
  10.   SendByte('-');
  11.   SendByte(readtime[6]+'0');
  12.   SendByte(readtime[7]+'0');
  13.   SendByte(' ');      
  14.   SendByte(readtime[8]+'0');
  15.   SendByte(readtime[9]+'0');
  16.   SendByte(':');
  17.   SendByte(readtime[10]+'0');//s
  18.   SendByte(readtime[11]+'0');
  19.   SendByte(':');
  20.   SendByte(readtime[12]+'0');//s
  21.   SendByte(readtime[13]+'0');
  22.   SendByte('\r');
  23.   SendByte('\n');
  24. }
复制代码

使DS1302模块产生图示效果的主程序为:
  1. void main(void)
  2. {
  3.          WDTCCR = 0x00;
  4.          while((CLKCON&0x20)!=0x20);
  5.          CLKSWR = 0x51;
  6.          while((CLKSWR&0xC0)!=0x40);
  7.          CLKDIV = 0x01;
  8.          P3M3 = 0xC2;   
  9.          P3M4 = 0xC2;
  10.          P3M5 = 0xC2;
  11.          P1M6 = 0xC2;
  12.          P1M7 = 0x62;
  13.          TXD_MAP = 0x16;
  14.          RXD_MAP = 0x17;
  15.          T4CON = 0x06;        
  16.          TH4 = 0xFF;
  17.          TL4 = 0x98;               // 9600bps
  18.          SCON2 = 0x02;          // 8bit
  19.          SCON = 0x10;
  20.          IE |= 0x10;      
  21.          EA = 1;

  22.          ds1302_init();
  23.          time_buf[1]=0x20;
  24.          time_buf[2]=0x07;
  25.          time_buf[3]=0x07;
  26.          time_buf[4]=0x14;
  27.          time_buf[5]=0x01;
  28.          time_buf[6]=0x30;
  29.          time_buf[7]=0x02;

  30.         ds1302_write_time();
  31.         while(1)
  32.         {     
  33.          ds1302_read_time();
  34.          readtime[0]=(time_buf[0]>>4);  
  35.          readtime[1]=(time_buf[0]&0x0F);
  36.          readtime[2]=(time_buf[1]>>4);  
  37.          readtime[3]=(time_buf[1]&0x0F);
  38.          readtime[4]=(time_buf[2]>>4);
  39.          readtime[5]=(time_buf[2]&0x0F);
  40.          readtime[6]=(time_buf[3]>>4);  
  41.          readtime[7]=(time_buf[3]&0x0F);
  42.          readtime[8]=(time_buf[4]>>4);  
  43.          readtime[9]=(time_buf[4]&0x0F);
  44.          readtime[10]=(time_buf[5]>>4);  
  45.          readtime[11]=(time_buf[5]&0x0F);
  46.          readtime[12]=(time_buf[6]>>4);  
  47.          readtime[13]=(time_buf[6]&0x0F);
  48.          if(readtime[13]!=sec_buf)        
  49.          {
  50.              sec_flag=0;   
  51.              sec_buf=readtime[13];   
  52.              RTC_xs(); ;
  53.         }
  54.       }
  55. }
复制代码

2.png
计时效果图

评分

参与人数 1黑币 +50 收起 理由
admin + 50 共享资料的黑币奖励!

查看全部评分

回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表