记得刚入门51时,在51hei上学习到了很多,这一切都源于论坛和大家的无私奉献。最近有空写了个基于51单片机的LCD1602多功能时玩玩,主要使用任务轮询的思路来进行设计的,我也来分享一下代码,并做个视频讲解讲解,视频不知道怎么放上来,今日头条搜索“单片机程序零或壹”可以查看哦
单片机源程序如下:
- #include "sys.h"
- #include "keyfunc.h"
- #include "e2stc89.h"
- sbit LEDALARM = P1^7;
- sbit BUZZER = P3^7;
- //全局系统变量
- stSysDef stSys;
- //E2PROM保存的变量
- stSaveVerDef stSaveVer;
- void Timer0Init();
- void InitPageMainVar();
- void UpdateDs1302Time();
- void UpdateDs18b20Temp();
- void KeyProcess();
- void main()
- {
- //硬件初始化
- LcdInit();
- Start18B20();
- InitDS1302();
-
- GetSetFromE2prom();
- InitPageMainVar();
- GotoPageMain();
- Timer0Init();
- while(1)
- {
- UpdateDs1302Time();
- UpdateDs18b20Temp();
- KeyProcess();
- }
- }
- void InitPageMainVar()
- {
- memset(&stSys, 0, sizeof(stSys));
- stSys.SecBkp = 88;
- stSys.DayBkp = 88;
- }
- void KeyProcess()
- {
- if(stSys.Flag10ms != 0)
- {
- stSys.Flag10ms = 0;
- KeyAction();
- }
- }
- void UpdateDs18b20Temp()
- {
- s8 res = 0;
- //只在主界面时更新显示
- if(stSys.PageNum != defPageMain) return;
- if(stSys.Flag1s != 0)
- {
- stSys.Flag1s = 0;
- res = Get18B20Temp(&stSys.Temp);
- if(res != 0){
- LcdShowTemp(10, 1, stSys.Temp);
- }
- else{
- LcdShowStr(10, 1, " **.*");
- }
- Start18B20();
- }
- }
- void AlarmClockCheck()
- {
- static u8 cnt10s = 0;
- if(stSaveVer.AlarmSwitch != 0)
- {
- if(stSys.stTime.hour == stSaveVer.stAlarm.hour && \
- stSys.stTime.min == stSaveVer.stAlarm.min && \
- stSys.stTime.sec == stSaveVer.stAlarm.sec)
- {
- stSys.FlagAlarm = 1;
- }
- }
- if(stSys.FlagAlarm != 0)
- {
- LEDALARM = !LEDALARM;
- cnt10s++;
- if(cnt10s >= 10)
- {
- cnt10s = 0;
- stSys.FlagAlarm = 0;
- LEDALARM = 1;
- }
- }
- }
- void UpdateDs1302Time()
- {
- //只在主界面时更新显示
- if(stSys.PageNum != defPageMain) return;
- if(stSys.Flag200ms != 0)
- {
- stSys.Flag200ms = 0;
- GetRealTime(&stSys.stTime);
- if(stSys.SecBkp != stSys.stTime.sec)
- {
- stSys.SecBkp = stSys.stTime.sec;
- LcdShowTime(1, 1, stSys.stTime);
- if(stSys.DayBkp != stSys.stTime.day){
- stSys.DayBkp = stSys.stTime.day;
- LcdShowDate(0, 0, stSys.stTime);
- }
- AlarmClockCheck();
- }
- }
- }
- void Timer10MsAction()
- {
- static s8 cnt200ms = 0;
- static s8 cnt1s = 0;
- cnt200ms++;
- if(cnt200ms >= 20){
- cnt200ms = 0;
- stSys.Flag200ms = 1;
- }
- cnt1s++;
- if(cnt1s >= 100){
- cnt1s = 0;
- stSys.Flag1s = 1;
- }
- stSys.Flag10ms = 1;
- }
- void Timer0Init()
- {
- TMOD &= 0xF0;
- TMOD |= 0x01;
- TH0 = 0xFC;
- TL0 = 0x66;
- ET0 = 1;
- TR0 = 1;
- EA = 1;
- }
- //定时器中断函数,1ms一次
- void Timer0_ISR(void) interrupt 1
- {
- static u8 cnt10ms = 0;
- TH0 = 0xFC;
- TL0 = 0x66;
-
- cnt10ms++;
- if(cnt10ms >= 10){
- cnt10ms = 0;
- Timer10MsAction();
- }
-
- if(LEDALARM == 0) {
- BUZZER = !BUZZER;
- }
- }
复制代码
所有资料51hei提供下载:
基于51单片机的LCD1602多功能时钟.rar
(155.41 KB, 下载次数: 42)
|