1602液晶顯示器顯示當前時間,溫濕度和時間,通過按鍵可以設(shè)置時間和鬧鐘,當鬧鐘響起時按下增加或者減少按鍵取消鬧鐘
仿真原理圖如下(proteus仿真工程文件可到本帖附件中下載)
未命名1557969393.png (78.11 KB, 下載次數(shù): 84)
下載附件
2019-5-16 09:16 上傳
單片機源程序如下:
- #include<reg52.h>
- #include"ds1302.h"
- #include"lcd.h"
- #include "DHT111.h"
- #define uint unsigned int
- #define uchar unsigned char
- sbit Key_1 = P3^3;
- sbit Key_2 = P3^4;
- sbit Key_3 = P3^5;
- sbit Buzzer = P3^6;
- uchar KeyValue = 0; //按鍵值
- uchar Sec_temp;
- uchar Clock_Hour=8,Clock_Min=59;
- uchar Loc = 1;
- uchar Clock_Flag;
- void Key_Scan();
- void Delay10ms(uchar delaytime);
- void Main_DisPlay();
- void Display();
- void Time_Set();
- void Cursor_Set();
- void Set_Screen();
- void Set_Clock();
- void Clock_Check();
- void main()
- {
- Delay10ms(100);
- LcdInit();
- Ds1302Init();
- Ds1302ReadTime();
- Main_DisPlay();
- while(1)
- {
- Key_Scan();
- Display(); //檢查時間是否發(fā)生變化
- Set_Screen();
- Time_Set();
- Clock_Check(); //鬧鐘檢測
- }
- }
- void Key_Scan()
- {
- KeyValue = 0;
- if(Key_1 == 0) //增加按鍵按下
- {
- Delay10ms(1);
- if(Key_1 == 0)
- {
- KeyValue = 1;
- while(Key_1 == 0); //等待按鍵釋放
- }
- }
- if(Key_2 == 0) //減少按鍵按下
- {
- Delay10ms(1);
- if(Key_2 == 0)
- {
- KeyValue = 2;
- while(Key_2 == 0); //等待按鍵釋放
- }
- }
- if(Key_3 == 0) //設(shè)置按鍵按下
- {
- Delay10ms(1);
- if(Key_3 == 0)
- {
- KeyValue = 3;
- while(Key_3 == 0); //等待按鍵釋放
- }
- }
- }
- void Display()
- {
- RH();
- LcdWriteNum(2,6,U8T_data_H_temp);
- LcdWriteNum(2,12,U8RH_data_H_temp);
- Ds1302ReadTime();
- if(Sec_temp != TIME[0])
- {
- Sec_temp = TIME[0];
- LcdWriteNum(1,7,TIME[2]); //寫入時
- LcdWriteNum(1,10,TIME[1]); //寫入分
- LcdWriteNum(1,13,TIME[0]); //寫入秒
- LcdWriteNum(1,1,TIME[4]); //寫入月
- LcdWriteNum(1,4,TIME[3]); //寫入日
- }
- }
- void Main_DisPlay()
- {
- LcdClear();
- LcdWriteStr(1,1,"00-00 00:00:00");
- LcdWriteStr(2,1,"TEMP:00 RH:00");
- LcdWriteNum(1,7,TIME[2]); //寫入時
- LcdWriteNum(1,10,TIME[1]); //寫入分
- LcdWriteNum(1,13,TIME[0]); //寫入秒
- LcdWriteNum(1,1,TIME[4]); //寫入月
- LcdWriteNum(1,4,TIME[3]); //寫入日
- Sec_temp = TIME[0]; //讀取當前秒
- }
- void Time_Set()
- {
- if(KeyValue == 3)
- {
- LcdClear();
- Ds1302ReadTime();
- LcdWriteStr(1,1,"SET:00-00-00 ");
- LcdWriteStr(2,1," 00:00:00");
- LcdWriteNum(2,5,TIME[2]); //寫入時
- LcdWriteNum(2,8,TIME[1]); //寫入分
- LcdWriteNum(2,11,TIME[0]); //寫入秒
- LcdWriteNum(1,5,TIME[6]); //寫入年
- LcdWriteNum(1,8,TIME[4]); //寫入月
- LcdWriteNum(1,11,TIME[3]); //寫入日
- LcdWriteCom(0x0f); //開光標
- LcdWriteCom(0x80+4);
- while(1)
- {
- Key_Scan();
- if(KeyValue == 1)
- {
- switch(Loc)
- {
- case 1: //設(shè)置年
- TIME[6]++;
- LcdWriteNum(1,5,TIME[6]); //寫入年
- break;
- case 2: //設(shè)置月
- if( TIME[4] <12)
- TIME[4]++;
- else
- TIME[4] = 1;
- LcdWriteNum(1,8,TIME[4]); //寫入月
- break;
- case 3: //設(shè)置日
- if(TIME[3] < 31)
- TIME[3]++;
- else
- TIME[3] = 1;
- LcdWriteNum(1,11,TIME[3]); //寫入日
- break;
- case 4: //設(shè)置時
- if(TIME[2] < 24)
- TIME[2]++;
- else
- TIME[2] = 0;
- LcdWriteNum(2,5,TIME[2]); //寫入時
- break;
- case 5: //設(shè)置分
- if(TIME[1] < 59)
- TIME[1]++;
- else
- TIME[1] = 0;
- LcdWriteNum(2,8,TIME[1]); //寫入分
- break;
- case 6: //設(shè)置秒
- if(TIME[0] < 59)
- TIME[0]++;
- else
- TIME[0] = 0;
- LcdWriteNum(2,11,TIME[0]); //寫入秒
- break;
- }
- Cursor_Set();
- }
- if(KeyValue == 2)
- {
- switch(Loc)
- {
- case 1: //設(shè)置年
- TIME[6]--;
- LcdWriteNum(1,5,TIME[6]); //寫入年
- break;
- case 2: //設(shè)置月
- if( TIME[4] > 1)
- TIME[4]--;
- else
- TIME[4] = 12;
- LcdWriteNum(1,8,TIME[4]); //寫入月
- break;
- case 3: //設(shè)置日
- if(TIME[3] > 1)
- TIME[3]--;
- else
- TIME[3] = 31;
- LcdWriteNum(1,11,TIME[3]); //寫入日
- break;
- case 4: //設(shè)置時
- if(TIME[2] > 0)
- TIME[2]--;
- else
- TIME[2] = 24;
- LcdWriteNum(2,5,TIME[2]); //寫入時
- break;
- case 5: //設(shè)置分
- if(TIME[1] > 0)
- TIME[1]--;
- else
- TIME[1] = 59;
- LcdWriteNum(2,8,TIME[1]); //寫入分
- break;
- case 6: //設(shè)置秒
- if(TIME[0] > 0)
- TIME[0]--;
- else
- TIME[0] = 59;
- LcdWriteNum(2,11,TIME[0]); //寫入秒
- break;
- }
- Cursor_Set();
- }
- if(KeyValue == 3)
- {
- if(Loc < 6)
- Loc++;
- else
- {
- Ds1302WriteTime(); //寫入時間
- LcdWriteCom(0x0c); //開顯示不顯示光標
- return;
- }
- Cursor_Set();
- }
- }
- }
- }
- void Clock_Check()
- {
- static uchar i;
- if(KeyValue == 1 || KeyValue == 2)
- {
- Buzzer = 1;
- Clock_Flag = 2;
- }
- if(TIME[1] == Clock_Min) //分相等
- {
- if(Clock_Flag == 0)
- {
- if(TIME[2] == Clock_Hour) //時相等
- {
- i++;
- if(i > 5)
- {
- i = 0;
- Buzzer = ~Buzzer;
- }
- }
- }
- }
- else
- {
- i = 0;
- Clock_Flag = 0;
- Buzzer = 1;
- }
- }
- void Cursor_Set()
- {
- if(Loc <= 3)
- {
- LcdWriteCom(0x80+4+(Loc-1)*3);
- }
- else
- {
- LcdWriteCom(0xc0+4+(Loc-4)*3);
- }
- }
- void Set_Screen()
- {
- uchar Cursor;
- if(KeyValue == 3)
- {
- Cursor = 1; //設(shè)置光標
- LcdClear();
- LcdWriteStr(1,1,">");
- LcdWriteStr(1,3,"Set Time");
- LcdWriteStr(2,3,"Set Clock");
- while(1)
- {
- Key_Scan();
- if(KeyValue == 1 || KeyValue == 2)
- {
- if(KeyValue == 1)
- Cursor++;
- if(KeyValue == 2)
- Cursor--;
- if(Cursor == 0)
- Cursor=2;
- if(Cursor == 3)
- Cursor=1;
- if(Cursor == 1)
- {
- LcdWriteStr(1,1,">");
- LcdWriteStr(2,1," ");
- }
- if(Cursor == 2)
- {
- LcdWriteStr(1,1," ");
- LcdWriteStr(2,1,">");
- }
- }
- if(KeyValue == 3)
- {
- switch(Cursor)
- {
- case 1:
- Time_Set();
- break;
- case 2:
- Set_Clock();
- break;
- }
- Main_DisPlay();
- KeyValue = 0;
- return;
- }
- }
- }
- }
- void Set_Clock()
- {
- uchar Cursor=0;
- LcdClear();
- LcdWriteStr(1,3," Set Clock ");
- LcdWriteStr(2,5," : ");
- LcdWriteNum(2,5,Clock_Hour);
- LcdWriteNum(2,8,Clock_Min);
- LcdWriteCom(0xc0+5);
- LcdWriteCom(0x0f); //光標閃爍
- while(1)
- {
- Key_Scan();
- if(KeyValue == 1)
- {
- if(Cursor == 0)
- {
- if(Clock_Hour < 59)
- Clock_Hour++;
- LcdWriteNum(2,5,Clock_Hour);
- LcdWriteCom(0xc0+5);
- }
- if(Cursor == 1)
- {
- Clock_Min++;
- if(Clock_Hour < 59)
- Clock_Min++;
- LcdWriteNum(2,8,Clock_Min);
- LcdWriteCom(0xc0+8);
- }
- }
- if(KeyValue == 2)
- {
- if(Cursor == 0)
- ……………………
- …………限于本文篇幅 余下代碼請從51黑下載附件…………
復(fù)制代碼
所有資料51hei提供下載:
資料.zip
(157.43 KB, 下載次數(shù): 146)
2019-5-16 09:16 上傳
點擊文件名下載附件
仿真和代碼 下載積分: 黑幣 -5
|