記得剛入門51時,在51hei上學習到了很多,這一切都源于論壇和大家的無私奉獻。最近有空寫了個基于51單片機的LCD1602多功能時玩玩,主要使用任務輪詢的思路來進行設計的,我也來分享一下代碼,并做個視頻講解講解,視頻不知道怎么放上來,今日頭條搜索“單片機程序零或壹”可以查看哦
51時鐘1602.png (600.8 KB, 下載次數: 62)
下載附件
2020-4-20 07:44 上傳
基于51單片機的LCD1602多功能時鐘代碼分析.png (158.26 KB, 下載次數: 44)
下載附件
2020-4-20 07:44 上傳
單片機源程序如下:
- #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)
2020-4-20 07:45 上傳
點擊文件名下載附件
下載積分: 黑幣 -5
|