上一帖:
http://www.51hei.com/bbs/dpj-162303-1.html
仿真原理图如下(proteus仿真工程文件可到本帖附件中下载)
单片机源程序如下:
- #include <EEPROM.h>
- #include <LiquidCrystal.h>
- #include <DS1302.h>
- LiquidCrystal lcd(2,3,4,5,6,7); //LCD定义
- DS1302 rtc(A3,A2,A1); //DS1302定义
- typedef unsigned char uchar;
- //数据存储位置
- #define switch_on_hour 0x00
- #define switch_on_min 0x01
- #define switch_off_hour 0x02
- #define switch_off_min 0x03
- //按键
- #define t_set 8
- #define io_set 9
- #define up 10
- #define down 11
- uchar switch_on_hour_tmp,switch_on_min_tmp,
- switch_off_hour_tmp,switch_off_min_tmp;Time t; //定义数据存储位置临时区
- #define relay A0
- //开/关字符
- const uchar switch_on_char[]={
- B01000,
- B01100,
- B01000,
- B01000,
- B01000,
- B01000,
- B01000,
- B00000
- };
- const uchar switch_off_char[]={
- B01000,
- B01000,
- B00100,
- B00010,
- B00000,
- B01000,
- B01000,
- B00000
- };
- void timeset()
- {
- uchar hour_tmp,min_tmp,sec_tmp;
- t=rtc.getTime();
- hour_tmp=t.hour;
- min_tmp=t.min;
- sec_tmp=t.sec;
- lcd.clear();
- while(1){
- lcd.setCursor(0,0);
- lcd.print("Time Hour");
- lcd.setCursor(0,1);
- lcd.print(hour_tmp%100/10,DEC);
- lcd.print(hour_tmp%10,DEC);
- if(digitalRead(up)==0){
- delayMicroseconds(10); //消抖
- if(digitalRead(up)==0){
- while(!digitalRead(up));
- ++hour_tmp;
- }
- }
- if(digitalRead(down)==0){
- delayMicroseconds(10); //消抖
- if(digitalRead(down)==0){
- while(!digitalRead(down));
- --hour_tmp;
- }
- }
- if(hour_tmp==255)hour_tmp=23;
- if(hour_tmp==24)hour_tmp=0;
- if(digitalRead(t_set)==0){
- delayMicroseconds(10); //消抖
- if(digitalRead(t_set)==0){
- while(!digitalRead(t_set));
- while(1){
- lcd.setCursor(0,0);
- lcd.print("Time Minute");
- lcd.setCursor(0,1);
- lcd.print(min_tmp%100/10,DEC);
- lcd.print(min_tmp%10,DEC);
- if(digitalRead(up)==0){
- delayMicroseconds(10); //消抖
- if(digitalRead(up)==0){
- while(!digitalRead(up));
- ++min_tmp;
- }
- }
- if(digitalRead(down)==0){
- delayMicroseconds(10); //消抖
- if(digitalRead(down)==0){
- while(!digitalRead(down));
- --min_tmp;
- }
- }
- if(min_tmp==255)min_tmp=59;
- if(min_tmp==60)min_tmp=0;
- if(digitalRead(t_set)==0){
- delayMicroseconds(10); //消抖
- if(digitalRead(t_set)==0){
- while(!digitalRead(t_set));
- while(1){
- lcd.setCursor(0,0);
- lcd.print("Time Second");
- lcd.setCursor(0,1);
- lcd.print(sec_tmp%100/10,DEC);
- lcd.print(sec_tmp%10,DEC);
- if(digitalRead(up)==0){
- delayMicroseconds(10); //消抖
- if(digitalRead(up)==0){
- while(!digitalRead(up));
- ++sec_tmp;
- }
- }
- if(digitalRead(down)==0){
- delayMicroseconds(10); //消抖
- if(digitalRead(down)==0){
- while(!digitalRead(down));
- --sec_tmp;
- }
- }
- if(sec_tmp==255)sec_tmp=59;
- if(sec_tmp==60)sec_tmp=0;
- if(digitalRead(t_set)==0){
- delayMicroseconds(10); //消抖
- if(digitalRead(t_set)==0){
- while(!digitalRead(t_set));
- rtc.setTime(hour_tmp,min_tmp,sec_tmp);
- lcd.clear();
- return;
- }
- }
- }
- }
- }
- }
- }
- }
- }
- }
- void ioset()
- {
- onset();
- offset();
- }
- void onset()
- {
- uchar hour_tmp,min_tmp;
- hour_tmp=EEPROM.read(switch_on_hour);
- min_tmp=EEPROM.read(switch_on_min);
- lcd.clear();
- while(1){
- lcd.setCursor(0,0);
- lcd.print("SwitchOn Hour");
- lcd.setCursor(0,1);
- lcd.print(hour_tmp%100/10,DEC);
- lcd.print(hour_tmp%10,DEC);
- if(digitalRead(up)==0){
- delayMicroseconds(10); //消抖
- if(digitalRead(up)==0){
- while(!digitalRead(up));
- ++hour_tmp;
- }
- }
- if(digitalRead(down)==0){
- delayMicroseconds(10); //消抖
- if(digitalRead(down)==0){
- while(!digitalRead(down));
- --hour_tmp;
- }
- }
- if(hour_tmp==255)hour_tmp=23;
- if(hour_tmp==24)hour_tmp=0;
- if(digitalRead(io_set)==0){
- delayMicroseconds(10); //消抖
- if(digitalRead(io_set)==0){
- while(!digitalRead(io_set));
- while(1){
- lcd.setCursor(0,0);
- lcd.print("SwitchOn Minute");
- lcd.setCursor(0,1);
- lcd.print(min_tmp%100/10,DEC);
- lcd.print(min_tmp%10,DEC);
- if(digitalRead(up)==0){
- delayMicroseconds(10); //消抖
- if(digitalRead(up)==0){
- while(!digitalRead(up));
- ++min_tmp;
- }
- }
- if(digitalRead(down)==0){
- delayMicroseconds(10); //消抖
- if(digitalRead(down)==0){
- while(!digitalRead(down));
- --min_tmp;
- }
- }
- if(min_tmp==255)min_tmp=59;
- if(min_tmp==60)min_tmp=0;
- if(digitalRead(io_set)==0){
- delayMicroseconds(10); //消抖
- if(digitalRead(io_set)==0){
- while(!digitalRead(io_set));
- EEPROM.write(switch_on_hour,hour_tmp);
- EEPROM.write(switch_on_min,min_tmp);
- lcd.clear();
- return;
- }
- }
- }
- }
- }
- }
- }
- void offset()
- {
- uchar hour_tmp,min_tmp;
- hour_tmp=EEPROM.read(switch_off_hour);
- min_tmp=EEPROM.read(switch_off_min);
- lcd.clear();
- while(1){
- lcd.setCursor(0,0);
- lcd.print("SwitchOff Hour");
- lcd.setCursor(0,1);
- lcd.print(hour_tmp%100/10,DEC);
- lcd.print(hour_tmp%10,DEC);
- if(digitalRead(up)==0){
- delayMicroseconds(10); //消抖
- if(digitalRead(up)==0){
- while(!digitalRead(up));
- ++hour_tmp;
- }
- }
- if(digitalRead(down)==0){
- delayMicroseconds(10); //消抖
- if(digitalRead(down)==0){
- while(!digitalRead(down));
- --hour_tmp;
- }
- }
- if(hour_tmp==255)hour_tmp=23;
- if(hour_tmp==24)hour_tmp=0;
- if(digitalRead(io_set)==0){
- delayMicroseconds(10); //消抖
- if(digitalRead(io_set)==0){
- while(!digitalRead(io_set));
- while(1){
- lcd.setCursor(0,0);
- lcd.print("SwitchOff Minute");
- lcd.setCursor(0,1);
- lcd.print(min_tmp%100/10,DEC);
- lcd.print(min_tmp%10,DEC);
- if(digitalRead(up)==0){
- delayMicroseconds(10); //消抖
- if(digitalRead(up)==0){
- while(!digitalRead(up));
- ++min_tmp;
- }
- }
- if(digitalRead(down)==0){
- delayMicroseconds(10); //消抖
- if(digitalRead(down)==0){
- while(!digitalRead(down));
- --min_tmp;
- }
- }
- if(min_tmp==255)min_tmp=59;
- if(min_tmp==60)min_tmp=0;
- if(digitalRead(io_set)==0){
- delayMicroseconds(10); //消抖
- if(digitalRead(io_set)==0){
- while(!digitalRead(io_set));
- EEPROM.write(switch_off_hour,hour_tmp);
- EEPROM.write(switch_off_min,min_tmp);
- lcd.clear();
- return;
- }
- }
- }
- }
- }
- }
- }
- void setup() //初始化并显示字符
- {
- rtc.halt(false);
- rtc.writeProtect(false);
- lcd.begin(16,2);
- lcd.createChar(0,switch_on_char);
- lcd.createChar(1,switch_off_char);
- pinMode(t_set,INPUT_PULLUP);
- pinMode(io_set,INPUT_PULLUP);
- pinMode(up,INPUT_PULLUP);
- pinMode(down,INPUT_PULLUP);
- pinMode(relay,OUTPUT);
- digitalWrite(relay,HIGH);
- lcd.setCursor(0,0);
- lcd.print("The With");
- lcd.setCursor(0,1);
- lcd.print("Commands");
- delay(2000);
- lcd.clear();
- lcd.print("Enjoy");
- lcd.setCursor(0,1);
- lcd.print("Program Life");
- delay(2000);
- lcd.clear();
- }
- void loop()
- {
- t=rtc.getTime();
- lcd.setCursor(4,0);
- lcd.print(rtc.getTimeStr());
- switch_on_hour_tmp=EEPROM.read(switch_on_hour);
- switch_on_min_tmp=EEPROM.read(switch_on_min);
- switch_off_hour_tmp=EEPROM.read(switch_off_hour);
- switch_off_min_tmp=EEPROM.read(switch_off_min);
- lcd.setCursor(1,1);
- lcd.write((uchar)0);
- lcd.print(":");
- lcd.print(switch_on_hour_tmp%100/10,DEC);
- lcd.print(switch_on_hour_tmp%10,DEC);
- lcd.print(":");
- lcd.print(switch_on_min_tmp%100/10,DEC);
- lcd.print(switch_on_min_tmp%10,DEC);
- lcd.write((uchar)1);
- lcd.print(":");
- lcd.print(switch_off_hour_tmp%100/10,DEC);
- lcd.print(switch_off_hour_tmp%10,DEC);
- lcd.print(":");
- lcd.print(switch_off_min_tmp%100/10,DEC);
- lcd.print(switch_off_min_tmp%10,DEC);
- if(digitalRead(t_set)==0){
- delayMicroseconds(10); //消抖
- if(digitalRead(t_set)==0){
- while(!digitalRead(t_set));
- timeset();
- }
- }
- if(digitalRead(io_set)==0){
- delayMicroseconds(10); //消抖
- if(digitalRead(io_set)==0){
- while(!digitalRead(io_set));
- ioset();
- }
- }
- if(t.hour==switch_on_hour_tmp){
- if(t.min==switch_on_min_tmp){
- digitalWrite(relay,LOW);
- }
- }
- if(t.hour==switch_off_hour_tmp){
- if(t.min==switch_off_min_tmp){
- digitalWrite(relay,HIGH);
- }
- }
- }
复制代码
所有资料51hei提供下载:
TimeSwitch.zip
(64.44 KB, 下载次数: 28)
|