通过4个按键来控制时间的调整和闹钟的设置,一键开关闹钟,时间通过ds1302 来读取设置
单片机源程序如下:
- #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位定时器,使用"|"符号可以在使用多个定时器时不受影响
- TH0=(65536-50000)/256; //给定初值
- TL0=(65536-50000)/256;
- EA=1; //总中断打开
- ET0=1; //定时器中断打开
- TR0=1; //定时器开关打开
- }
- void Alarm_Clock()
- {
- if(flag_100ms == 1) //100ms执行一次
- {
- 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(); //先写入时钟日历寄存器起始地址再设置时钟日历初值
- init_eeprom(); //开始初始化保存的数据
- 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(); //先写入时钟日历寄存器起始地址再读出时钟日历写入到TimeData数组中
-
- 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]); //时
- 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;
- }
- }
复制代码
所有资料51hei提供下载:
闹钟.zip
(60.1 KB, 下载次数: 22)
|