|
通過(guò)4個(gè)按鍵來(lái)控制時(shí)間的調(diào)整和鬧鐘的設(shè)置,一鍵開(kāi)關(guān)鬧鐘,時(shí)間通過(guò)ds1302 來(lái)讀取設(shè)置
單片機(jī)源程序如下:
- #include <reg52.h>
- #include "ds1302.h"
- #include "lcd1602.h"
- #include "key.h"
- #include "eeprom52.h"
- static uchar value;
- uchar flag_100ms,flag_200ms;
- sbit LED = P1^5;
- sbit ElectricRelay = P1^6;
- void Init_Timer0(void)
- {
- TMOD |= 0x01; //使用模式1,16位定時(shí)器,使用"|"符號(hào)可以在使用多個(gè)定時(shí)器時(shí)不受影響
- TH0=(65536-50000)/256; //給定初值
- TL0=(65536-50000)/256;
- EA=1; //總中斷打開(kāi)
- ET0=1; //定時(shí)器中斷打開(kāi)
- TR0=1; //定時(shí)器開(kāi)關(guān)打開(kāi)
- }
- void Alarm_Clock()
- {
- if(flag_100ms == 1) //100ms執(zhí)行一次
- {
- flag_100ms = 0;
- if(alarm_switch)
- if(TimeData[0] == 0 && TimeData[1] == TimeAlarm[1] && TimeData[2] == TimeAlarm[2])
- {
- alarmflag = 1;
- ElectricRelay = 0;
- }
- if(alarmflag)
- LED = ~LED;
- }
- }
- void main()
- {
- Init_Timer0();
- InitLcd1602(); //1602初始化
- DS1302Init(); //先寫入時(shí)鐘日歷寄存器起始地址再設(shè)置時(shí)鐘日歷初值
- init_eeprom(); //開(kāi)始初始化保存的數(shù)據(jù)
- while(1)
- {
-
- key();
- key_switch();
-
- if(menu_1 == 0)
- {
- Alarm_Clock();
- if(value == 0)
- {
- if(alarm_switch)
- {
- LcdShowStr (14,1,"ON");
- DisplayOneChar(12,1,0);
- }
- else
- {
- LcdShowStr (13,1,"OFF");
- DisplayOneChar(12,1,1);
- }
-
- DS1302ReadTime(); //先寫入時(shí)鐘日歷寄存器起始地址再讀出時(shí)鐘日歷寫入到TimeData數(shù)組中
-
- LcdShowStr (0,0,"20"); //年
- DisplayOneStr (2,0,TimeData[6]); //年
- DisplayOneChar (4,0,'-'); //-
- DisplayOneStr (5,0,TimeData[4]); //月
- DisplayOneChar (7,0,'-'); //-
- DisplayOneStr (8,0,TimeData[3]);//日
-
- LcdShowWeek(11,0,TimeData[5]%10); //星期
- DisplayOneStr (14,0,TimeData[5]);//星期
-
- DisplayOneStr (1,1,TimeData[2]); //時(shí)
- DisplayOneChar (3,1,':'); //:
- DisplayOneStr (4,1,TimeData[1]); //分
- DisplayOneChar (6,1,':'); //:
- DisplayOneStr (7,1,TimeData[0]);//秒
-
- }
- }
- }
- }
- void Timer0(void) interrupt 1
- {
- TH0=(65536-50000)/256; //給定初值
- TL0=(65536-50000)/256;
- value ++;
- if((value % 2) == 0) //100ms
- {
- flag_100ms = 1;
- }
- if(value >= 6) //200ms
- {
- value = 0;
- flag_200ms = 1;
- }
- }
復(fù)制代碼
所有資料51hei提供下載:
鬧鐘.zip
(60.1 KB, 下載次數(shù): 22)
2018-12-27 13:24 上傳
點(diǎn)擊文件名下載附件
下載積分: 黑幣 -5
|
|