找回密码
 立即注册

QQ登录

只需一步,快速开始

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

单片机实时时钟 仿真电路原理图程序 ds1302+lcd1602

[复制链接]
跳转到指定楼层
楼主
实时时钟 仿真电路原理图程序
可以直接拿去用


单片机源程序如下:
  1. /*******************************************************************************
  2. * 实验名: 用单片机实现的实时时钟系统研究
  3. * 使用的IO:
  4. * 实验效果       :1602显示时钟
  5. *        注意                                         :
  6. *******************************************************************************/

  7. #include<reg51.h>
  8. #include"lcd.h"
  9. #include"ds1302.h"

  10. sbit SET_KEY=P3^7;
  11. sbit ADD_KEY=P3^6;
  12. sbit SUB_KEY=P3^5;
  13. sbit DETERMINE_KEY=P3^4;
  14. unsigned char Set_Step = 0;
  15. #define NormalRun 0
  16. #define SetYear 1
  17. #define SetMon 2
  18. #define SetDay 3
  19. #define SetHour 4
  20. #define SetMin 5
  21. #define SetSec 6
  22. #define SetWeek 7

  23. void LcdDisplay();
  24. extern unsigned char TIME[7];        //加入全局变量

  25. void Key_Scan();


  26. /*******************************************************************************
  27. * 函数名         : main
  28. * 函数功能                   : 主函数
  29. * 输入           : 无
  30. * 输出                  : 无
  31. *******************************************************************************/

  32. void main()
  33. {
  34.         Set_Step = NormalRun;
  35.         LcdInit();
  36.         //Ds1302Init();

  37.         while(1)
  38.         {       
  39.                 Key_Scan();
  40.                
  41.                 if(Set_Step == NormalRun)
  42.                 {
  43.                         Ds1302ReadTime();
  44.                         LcdDisplay();
  45.                 }
  46.         }
  47.        
  48. }
  49. /*******************************************************************************
  50. * 函数名         : LcdDisplay()
  51. * 函数功能                   : 显示函数
  52. * 输入           : 无
  53. * 输出                  : 无
  54. *******************************************************************************/
  55. void LcdDisplay()
  56. {
  57.         LcdWriteCom(0x80+0X40);
  58.         LcdWriteData('0'+TIME[2]/16);                                //时
  59.         LcdWriteData('0'+((TIME[2]%16)&0x0f));                         
  60.         LcdWriteData(':');
  61.         LcdWriteData('0'+TIME[1]/16);                                //分
  62.         LcdWriteData('0'+((TIME[1]%16)&0x0f));       
  63.         LcdWriteData(':');
  64.         LcdWriteData('0'+TIME[0]/16);                                //秒
  65.         LcdWriteData('0'+((TIME[0]%16)&0x0f));

  66.         LcdWriteCom(0x80);
  67.         LcdWriteData('2');
  68.         LcdWriteData('0');
  69.         LcdWriteData('0'+TIME[6]/16);                        //年
  70.         LcdWriteData('0'+((TIME[6]%16)&0x0f));
  71.         LcdWriteData('-');
  72.         LcdWriteData('0'+TIME[4]/16);                        //月
  73.         LcdWriteData('0'+((TIME[4]%16)&0x0f));
  74.         LcdWriteData('-');
  75.         LcdWriteData('0'+TIME[3]/16);                        //日
  76.         LcdWriteData('0'+((TIME[3]%16)&0x0f));
  77.         LcdWriteCom(0x8D);
  78.         LcdWriteData('W');        //星期
  79.         if(TIME[5]==1)
  80.                 LcdWriteData('7');       
  81.         else
  82.                 LcdWriteData('0'+((TIME[5]-1)&0x07));        //星期                         
  83. }

  84. void Key_Scan()
  85. {
  86.         if(SET_KEY == 0)
  87.         {
  88.                 Delay10us(5000);
  89.                
  90.                 if(SET_KEY == 0)
  91.                 {
  92.                         LcdWriteCom(0x0f);//显示光标
  93.                         switch(Set_Step)
  94.                         {
  95.                                 case NormalRun:
  96.                                         LcdWriteCom(0x83);  //设置光标
  97.                                         Set_Step = SetYear;
  98.                                         break;
  99.                                 case SetYear:
  100.                                         LcdWriteCom(0x86);  //设置光标
  101.                                         Set_Step = SetMon;
  102.                                         break;
  103.                                 case SetMon:
  104.                                         LcdWriteCom(0x89);  //设置光标
  105.                                         Set_Step = SetDay;
  106.                                         break;
  107.                                 case SetDay:
  108.                                         LcdWriteCom(0xc1);  //设置光标
  109.                                         Set_Step = SetHour;
  110.                                         break;
  111.                                 case SetHour:
  112.                                         LcdWriteCom(0xc4);  //设置光标
  113.                                         Set_Step = SetMin;
  114.                                         break;
  115.                                 case SetMin:
  116.                                         LcdWriteCom(0xc7);  //设置光标
  117.                                         Set_Step = SetSec;
  118.                                         break;
  119.                                 case SetSec:
  120.                                         LcdWriteCom(0x8E);  //设置光标
  121.                                         Set_Step = SetWeek;
  122.                                         break;
  123.                                 case SetWeek:
  124.                                         LcdWriteCom(0x83);  //设置光标
  125.                                         Set_Step = SetYear;
  126.                                         break;
  127.                                        
  128.                         }
  129.                        
  130.                 }
  131.         }

  132.         if(Set_Step == NormalRun)
  133.                         return ;
  134.                
  135.         if(ADD_KEY == 0)
  136.         {
  137.                 Delay10us(5000);
  138.                 if(ADD_KEY == 0)
  139.                 {
  140.                         if(Set_Step == SetYear)
  141.                         {
  142.                                 if(TIME[6] == 0x99)
  143.                                         TIME[6] = 0x0;
  144.                                 else
  145.                                 {
  146.                                         TIME[6]=(TIME[6]/16)*10+(TIME[6]%16);
  147.                                         TIME[6]=TIME[6]+1;
  148.                                         TIME[6]=(TIME[6]/10)*16+(TIME[6]%10);
  149.                                 }
  150.                                 LcdDisplay();
  151.                                 LcdWriteCom(0x83);  //设置光标
  152.                         }  
  153.                         else if(Set_Step == SetMon)
  154.                         {
  155.                                 if(TIME[4] == 0x12)
  156.                                         TIME[4] = 0x1;
  157.                                 else
  158.                                 {
  159.                                         TIME[4]=(TIME[4]/16)*10+(TIME[4]%16);
  160.                                         TIME[4]=TIME[4]+1;
  161.                                         TIME[4]=(TIME[4]/10)*16+(TIME[4]%10);       
  162.                                 }
  163.                                 LcdDisplay();
  164.                                 LcdWriteCom(0x86);  //设置光标
  165.                         }
  166.                         else if(Set_Step == SetDay)
  167.                         {
  168.                                 TIME[3]=(TIME[3]/16)*10+(TIME[3]%16);
  169.                                 TIME[3]=TIME[3]+1;
  170.                                 TIME[3]=(TIME[3]/10)*16+(TIME[3]%10);
  171.                                
  172.                                 LcdDisplay();
  173.                                 LcdWriteCom(0x89);  //设置光标
  174.                         }
  175.                         else if(Set_Step == SetHour)
  176.                         {
  177.                                 if(TIME[2] == 0x23)
  178.                                         TIME[2] = 0x0;
  179.                                 else
  180.                                 {
  181.                                         TIME[2]=(TIME[2]/16)*10+(TIME[2]%16);
  182.                                         TIME[2]=TIME[2]+1;
  183.                                         TIME[2]=(TIME[2]/10)*16+(TIME[2]%10);       
  184.                                 }
  185.                                 LcdDisplay();
  186.                                 LcdWriteCom(0xc1);  //设置光标
  187.                         }
  188.                         else if(Set_Step == SetMin)
  189.                         {
  190.                                 if(TIME[1] == 0x59)
  191.                                         TIME[1] = 0x0;
  192.                                 else
  193.                                 {
  194.                                         TIME[1]=(TIME[1]/16)*10+(TIME[1]%16);
  195.                                         TIME[1]=TIME[1]+1;
  196.                                         TIME[1]=(TIME[1]/10)*16+(TIME[1]%10);       
  197.                                 }
  198.                                 LcdDisplay();
  199.                                 LcdWriteCom(0xc4);  //设置光标
  200.                         }
  201.                         else if(Set_Step == SetSec)
  202.                         {
  203.                                 if(TIME[0] == 0x59)
  204.                                         TIME[0] = 0x0;
  205.                                 else
  206.                                 {
  207.                                         TIME[0]=(TIME[0]/16)*10+(TIME[0]%16);
  208.                                         TIME[0]=TIME[0]+1;
  209.                                         TIME[0]=(TIME[0]/10)*16+(TIME[0]%10);       
  210.                                 }
  211.                                 LcdDisplay();
  212.                                 LcdWriteCom(0xc7);  //设置光标
  213.                         }
  214.                         else if(Set_Step == SetWeek)
  215.                         {
  216.                                 if(TIME[5] == 7)
  217.                                         TIME[5] = 1;
  218.                                 else
  219.                                         TIME[5] = TIME[5]+1;
  220.                                 LcdDisplay();
  221.                                 LcdWriteCom(0x8E);  //设置光标
  222.                         }
  223.                 }
  224.         }
  225.        
  226.         if(SUB_KEY == 0)
  227.         {
  228.                 Delay10us(5000);
  229.                 if(SUB_KEY == 0)
  230.                 {
  231.                         if(Set_Step == SetYear)
  232.                         {
  233.                                 if(TIME[6] == 0x00)
  234.                                         TIME[6] = 0x99;
  235.                                 else
  236.                                 {
  237.                                         TIME[6]=(TIME[6]/16)*10+(TIME[6]%16);
  238.                                         TIME[6]=TIME[6]-1;
  239.                                         TIME[6]=(TIME[6]/10)*16+(TIME[6]%10);       
  240.                                 }
  241.                                 LcdDisplay();
  242.                                 LcdWriteCom(0x83);  //设置光标
  243.                         }  
  244.                         else if(Set_Step == SetMon)
  245.                         {
  246.                                 if(TIME[4] == 0x01)
  247.                                         TIME[4] = 0x12;
  248.                                 else
  249.                                 {
  250.                                         TIME[4]=(TIME[4]/16)*10+(TIME[4]%16);
  251.                                         TIME[4]=TIME[4]-1;
  252.                                         TIME[4]=(TIME[4]/10)*16+(TIME[4]%10);       
  253.                                 }
  254.                                 LcdDisplay();
  255.                                 LcdWriteCom(0x86);  //设置光标
  256.                         }
  257.                         else if(Set_Step == SetDay)
  258.                         {
  259.                                 TIME[3]=(TIME[3]/16)*10+(TIME[3]%16);
  260.                                 TIME[3]=TIME[3]-1;
  261.                                 TIME[3]=(TIME[3]/10)*16+(TIME[3]%10);
  262.                                 LcdDisplay();
  263.                                 LcdWriteCom(0x89);  //设置光标
  264.                         }
  265.                         else if(Set_Step == SetHour)
  266.                         {
  267.                                 if(TIME[2] == 0x00)
  268.                                         TIME[2] = 0x23;
  269.                                 else
  270.                                 {
  271.                                         TIME[2]=(TIME[2]/16)*10+(TIME[2]%16);
  272.                                         TIME[2]=TIME[2]-1;
  273.                                         TIME[2]=(TIME[2]/10)*16+(TIME[2]%10);       
  274.                                 }
  275.                                
  276.                                 LcdDisplay();
  277.                                 LcdWriteCom(0xc1);  //设置光标
  278.                         }
  279.                         else if(Set_Step == SetMin)
  280.                         {
  281.                                 if(TIME[1] == 0x00)
  282.                                         TIME[1] = 0x59;
  283.                                 else
  284.                                 {
  285.                                         TIME[1]=(TIME[1]/16)*10+(TIME[1]%16);
  286.                                         TIME[1]=TIME[1]-1;
  287.                                         TIME[1]=(TIME[1]/10)*16+(TIME[1]%10);       
  288.                                 }
  289.                        
  290. ……………………

  291. …………限于本文篇幅 余下代码请从51黑下载附件…………
复制代码

所有资料51hei提供下载:
实时时钟.rar (318.13 KB, 下载次数: 23)


评分

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

查看全部评分

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

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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