找回密码
 立即注册

QQ登录

只需一步,快速开始

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

基于51单片机的LCD1602多功能时钟,代码分享,视频讲解

[复制链接]
ID:179958 发表于 2020-4-20 07:49 | 显示全部楼层 |阅读模式
记得刚入门51时,在51hei上学习到了很多,这一切都源于论坛和大家的无私奉献。最近有空写了个基于51单片机的LCD1602多功能时玩玩,主要使用任务轮询的思路来进行设计的,我也来分享一下代码,并做个视频讲解讲解,视频不知道怎么放上来,今日头条搜索“单片机程序零或壹”可以查看哦
51时钟1602.png

基于51单片机的LCD1602多功能时钟代码分析.png

单片机源程序如下:
  1. #include "sys.h"
  2. #include "keyfunc.h"
  3. #include "e2stc89.h"

  4. sbit LEDALARM = P1^7;
  5. sbit BUZZER = P3^7;

  6. //全局系统变量
  7. stSysDef stSys;
  8. //E2PROM保存的变量
  9. stSaveVerDef stSaveVer;

  10. void Timer0Init();
  11. void InitPageMainVar();
  12. void UpdateDs1302Time();
  13. void UpdateDs18b20Temp();
  14. void KeyProcess();

  15. void main()
  16. {
  17.     //硬件初始化
  18.     LcdInit();
  19.     Start18B20();
  20.     InitDS1302();
  21.    
  22.     GetSetFromE2prom();
  23.     InitPageMainVar();
  24.     GotoPageMain();

  25.     Timer0Init();
  26.     while(1)
  27.     {
  28.         UpdateDs1302Time();
  29.         UpdateDs18b20Temp();
  30.         KeyProcess();
  31.     }
  32. }

  33. void InitPageMainVar()
  34. {
  35.     memset(&stSys, 0, sizeof(stSys));
  36.     stSys.SecBkp = 88;
  37.     stSys.DayBkp = 88;
  38. }

  39. void KeyProcess()
  40. {
  41.     if(stSys.Flag10ms != 0)
  42.     {
  43.         stSys.Flag10ms = 0;
  44.         KeyAction();
  45.     }
  46. }

  47. void UpdateDs18b20Temp()
  48. {
  49.     s8 res = 0;

  50.     //只在主界面时更新显示
  51.     if(stSys.PageNum != defPageMain)    return;

  52.     if(stSys.Flag1s != 0)
  53.     {
  54.         stSys.Flag1s = 0;
  55.         res = Get18B20Temp(&stSys.Temp);
  56.         if(res != 0){
  57.             LcdShowTemp(10, 1, stSys.Temp);
  58.         }
  59.         else{
  60.             LcdShowStr(10, 1, " **.*");
  61.         }
  62.         Start18B20();
  63.     }
  64. }

  65. void AlarmClockCheck()
  66. {
  67.     static u8 cnt10s = 0;
  68.     if(stSaveVer.AlarmSwitch != 0)
  69.     {
  70.         if(stSys.stTime.hour == stSaveVer.stAlarm.hour && \
  71.         stSys.stTime.min == stSaveVer.stAlarm.min && \
  72.         stSys.stTime.sec == stSaveVer.stAlarm.sec)
  73.         {
  74.             stSys.FlagAlarm = 1;
  75.         }
  76.     }

  77.     if(stSys.FlagAlarm != 0)
  78.     {
  79.         LEDALARM = !LEDALARM;
  80.         cnt10s++;
  81.         if(cnt10s >= 10)
  82.         {
  83.             cnt10s = 0;
  84.             stSys.FlagAlarm = 0;
  85.             LEDALARM = 1;
  86.         }
  87.     }
  88. }

  89. void UpdateDs1302Time()
  90. {
  91.     //只在主界面时更新显示
  92.     if(stSys.PageNum != defPageMain)    return;

  93.     if(stSys.Flag200ms != 0)
  94.     {
  95.         stSys.Flag200ms = 0;
  96.         GetRealTime(&stSys.stTime);
  97.         if(stSys.SecBkp != stSys.stTime.sec)
  98.         {
  99.             stSys.SecBkp = stSys.stTime.sec;
  100.             LcdShowTime(1, 1, stSys.stTime);

  101.             if(stSys.DayBkp != stSys.stTime.day){
  102.                 stSys.DayBkp = stSys.stTime.day;
  103.                 LcdShowDate(0, 0, stSys.stTime);
  104.             }

  105.             AlarmClockCheck();
  106.         }
  107.     }
  108. }

  109. void Timer10MsAction()
  110. {
  111.     static s8 cnt200ms = 0;
  112.     static s8 cnt1s = 0;

  113.     cnt200ms++;
  114.     if(cnt200ms >= 20){
  115.         cnt200ms = 0;
  116.         stSys.Flag200ms = 1;
  117.     }

  118.     cnt1s++;
  119.     if(cnt1s >= 100){
  120.         cnt1s = 0;
  121.         stSys.Flag1s = 1;
  122.     }

  123.     stSys.Flag10ms = 1;
  124. }

  125. void Timer0Init()
  126. {
  127.     TMOD &= 0xF0;
  128.     TMOD |= 0x01;
  129.     TH0 = 0xFC;
  130.     TL0 = 0x66;
  131.     ET0 = 1;
  132.     TR0 = 1;
  133.     EA = 1;
  134. }

  135. //定时器中断函数,1ms一次
  136. void Timer0_ISR(void) interrupt 1
  137. {
  138.     static u8 cnt10ms = 0;
  139.     TH0 = 0xFC;
  140.     TL0 = 0x66;
  141.    
  142.     cnt10ms++;
  143.     if(cnt10ms >= 10){
  144.         cnt10ms = 0;
  145.         Timer10MsAction();
  146.     }
  147.    
  148.     if(LEDALARM == 0) {
  149.         BUZZER = !BUZZER;
  150.     }
  151. }
复制代码

所有资料51hei提供下载:
基于51单片机的LCD1602多功能时钟.rar (155.41 KB, 下载次数: 42)

评分

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

查看全部评分

回复

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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