找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 3383|回复: 2
打印 上一主题 下一主题
收起左侧

LCD1602显示DS1302时钟

[复制链接]
跳转到指定楼层
楼主
本人正在学习单片机,已超过2个月,此程序适合学习过单片机的新手借鉴和讨论,注释详尽。
本程序不可通过按键设置时间,可按键设置时间的程序正在创作。详细程序可下载附件。
  1. #include <reg52.h>
  2. #include"inc/lcd.h"

  3. #define uchar unsigned char
  4. #define uint unsigned int

  5. sbit IO = P3^6;
  6. sbit SCLK = P3^5;
  7. sbit RST = P3^7;
  8. sbit ACC_0 = ACC^0;//累加器第一位
  9. sbit ACC_7 = ACC^7;//累加器最后一位

  10. //写的地址
  11. uchar code write_addr[7]=

  12. {0x80,0x82,0x84,0x86,0x88,0x8a,0x8c};   
  13. //读的地址
  14. uchar code read_addr[7]=

  15. {0x81,0x83,0x85,0x87,0x89,0x8b,0x8d};   

  16. /*形式上为十六进,数值是BCD码*/
  17. //存储格式是BCD码秒   分   时   日   月   周   年
  18. uchar

  19. time[7]={0x00,0x14,0x11,0x13,0x12,0x04,0x19};

  20. /*如果数值时间是十进制,写入时间时要先转化为BCD码*/
  21. //uchar time[7]={0,25,15,15,12,6,18}; //十进制

  22. void write_byte(uchar dat)//写一个字节
  23. {        
  24.         uchar i;
  25.         ACC=dat;
  26.         for(i=8;i>0;i--)
  27.         {
  28.                 IO=ACC_0;
  29.                 SCLK=0;
  30.                 SCLK=1;
  31.                 ACC=ACC>>1;
  32.         }
  33.          /*
  34.         uchar i;
  35.         for(i=0;i<8;i++)
  36.         {
  37.                 IO = (bit)(dat & 0x01);
  38.                 SCLK = 0;
  39.                 SCLK = 1;
  40.                 dat >>= 1;
  41.         }
  42.          */         
  43. }


  44. uchar read_byte()                       //读一个字节
  45. {        
  46.         uchar i;                                       

  47.          
  48.         for(i=0;i<8;i++)
  49.         {
  50.                 ACC_7=IO;
  51.                 SCLK=1;
  52.                 SCLK=0;
  53.                 ACC=ACC>>1;
  54.         }
  55.         IO=0;
  56.         return (ACC);
  57.         /*
  58.         uchar dat,i;
  59.         for(i=0;i<8;i++)
  60.         {
  61.         

  62.         if(IO == 1)
  63.                 {
  64.                         

  65. dat = dat|0x80;
  66.                 }
  67.                 SCLK = 1;
  68.                 SCLK = 0;
  69.                 dat >>= 1;        
  70.         }
  71.         IO = 0;
  72.         return (dat);
  73.         */
  74. }

  75. void write_1302(uchar add,uchar dat)    //向1302芯片写

  76. 函数,指定写入地址,数据
  77. {

  78.         RST=0;
  79.         SCLK=0;
  80.         RST=1;
  81.         write_byte(add);
  82.         write_byte(dat);
  83.         SCLK=1;
  84.         RST=0;
  85. }


  86. uchar read_1302(uchar add)                   //从1302

  87. 读数据函数,指定读取数据来源地址
  88. {
  89.         uchar temp;
  90.         RST=0;
  91.         SCLK=0;
  92.         RST=1;
  93.         write_byte(add);
  94.         temp=read_byte();
  95.         SCLK=1;
  96.         RST=0;
  97.         return(temp);
  98. }


  99. void ds1302_init()
  100. {
  101.         uchar k;
  102.     write_1302(0x8e,0x00);  //禁止写保

  103. 护,即允许数据写入
  104.     for(k=0;k<7;k++)   //写入7个字节

  105. 的时钟信号:分秒时日月周年
  106.     {
  107.       write_1302

  108. (write_addr[k],time[k]);
  109.     }
  110.     write_1302

  111. (0x8e,0x80);  //打开写保护

  112.         /*//写入时间时要先转化为BCD码
  113.         uchar i,tmp;
  114.         write_1302(0x8e,0x00);  //禁止写保护,即允许数

  115. 据写入
  116.         for (i=0; i<7; i++)  
  117.         {
  118.                 tmp = time[i] / 10;
  119.                 time[i] = time[i] % 10;
  120.                 time[i] = time[i] + tmp*16;        // 十

  121. 进制转化为BCD格式
  122.                 write_1302(write_addr[i],time[i]);
  123.                 //写入7个字节的时钟信号:分秒时日月周


  124.         }
  125.     write_1302(0x8e,0x80);  //打开写保护
  126.         */
  127. }

  128. void BCD_STRING(uchar bcd, uchar *str) //BCD转化为字符


  129. {         
  130.          *str = (bcd >> 4) + '0';
  131.         *(str+1) = (bcd & 0x0f) + '0';
  132. }

  133. void read_time(uchar *timedata)
  134. {
  135.         uchar n;
  136.         for(n=0;n<7;n++)
  137.         {
  138.                  timedata[n]=read_1302(read_addr[n]);  

  139. //读取分秒时日月周年
  140.         }        
  141.         BCD_STRING(timedata[6], LCD_TIME+0);//转化后年

  142. ,存放在LCD_TIME
  143.         BCD_STRING(timedata[4], LCD_TIME+2);//转化后月
  144.         BCD_STRING(timedata[3], LCD_TIME+4);//转化后日
  145.         BCD_STRING(timedata[5], LCD_TIME+6);//转化后周
  146.         BCD_STRING(timedata[2], LCD_TIME+8);//转化后时
  147.         BCD_STRING(timedata[1], LCD_TIME+10);//转化后


  148.         BCD_STRING(timedata[0], LCD_TIME+12);//转化后


  149. }

  150. void main()
  151. {
  152.         ds1302_init();//1302初始化,设定时间
  153.         Lcd_init();         //        lcd初始化
  154.         while(1)
  155.         {
  156.                 read_time(&time); //读取时间
  157.                 lcd_dis();         //        显示在lcd
  158.         }
  159. }
复制代码

}4SPVY4XKK)9U[K18KL5{_B.png (44.67 KB, 下载次数: 44)

}4SPVY4XKK)9U[K18KL5{_B.png

LCD显示时钟.rar

35.36 KB, 下载次数: 59, 下载积分: 黑币 -5

分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏1 分享淘帖 顶 踩
回复

使用道具 举报

沙发
ID:721769 发表于 2021-4-1 16:51 | 只看该作者
请问博主, 你用的是C52芯片吗?
回复

使用道具 举报

板凳
ID:900518 发表于 2021-4-17 10:27 | 只看该作者
一直想搞一个DS1302时钟 学习一下了  谢谢
回复

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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