主控芯片为STC12
源程序:
- #include "common.h"
- #include "delay.h"
- #include "keyscan.h"
- #include "at24c02.h"
- #include "lcd1602.h"
- #include "buzzer_relay.h"
- #include "timer.h"
- #include "ds1302.h"
- #define STATE_INPUT 1
- #define STATE_CHECK 2
- #define STATE_OPEN 3
- #define STATE_SET 4
- #define STATE_ERROR 5
- #define STATE_CHECK_OLD 6
- #define STATE_INPUT_NEW 7
- #define STATE_INPUT_NEW_AGAIN 8
- #define STATE_CHECK_NEW 9
- #define STATE_AUTO 1
- #define STATE_MANUAL 2
- sbit hongwai = P1^0;
- void init_all();
- void auto_sys();
- void pro();
- void state_input();
- void state_check();
- void state_open();
- void state_set();
- void state_error();
- void state_check_old();
- void state_input_new();
- void state_input_new_again();
- void state_check_new();
- uint8 password[6];
- uint8 password_dis[] = "------";
- uint8 data_buf[] = "123456";
- uint8 time_dis_buf[] = "18:59:50";
- uint8 error_count = 0;
- uint8 sec_flag = 0;
- uint8 relay_count = 0;
- bit close_flag = 0;
- uint8 state_pointer = 0;
- uint8 all_pointer = 0;
- void main()
- {
- init_all();
- while(1)
- {
- DS1302BurstRead(time_buf);
- time_dis_buf[0] = time_buf[2]/10+'0';
- time_dis_buf[1] = time_buf[2]%10+'0';
- time_dis_buf[3] = time_buf[1]/10+'0';
- time_dis_buf[4] = time_buf[1]%10+'0';
- time_dis_buf[6] = time_buf[0]/10+'0';
- time_dis_buf[7] = time_buf[0]%10+'0';
-
- switch(all_pointer)
- {
- case STATE_AUTO : auto_sys();
- break;
- case STATE_MANUAL : pro();
- break;
- default: break;
- }
- }
- }
- void init_all()
- {
- buzzer_off();
- relay_off();
- Timer0Init();
- LCD1602_init();
- DS1302_Init();
- DS1302BurstRead(time_buf);
- DS1302BurstRead(time_buf);
-
- // at24c02_write(0x00,password[0]);
- // at24c02_write(0x01,password[1]);
- // at24c02_write(0x02,password[2]);
- // at24c02_write(0x03,password[3]);
- // at24c02_write(0x04,password[4]);
- // at24c02_write(0x05,password[5]);
- password[0] = at24c02_read(0x00);
- password[1] = at24c02_read(0x01);
- password[2] = at24c02_read(0x02);
- password[3] = at24c02_read(0x03);
- password[4] = at24c02_read(0x04);
- password[5] = at24c02_read(0x05);
-
- delay_ms(5000);
- LCD1602_write(0x01, 0); //清除显示
- all_pointer = STATE_AUTO;
- state_pointer = STATE_INPUT;
- }
- void auto_sys()
- {
- if(hongwai == 1)
- {
- relay_on();
- LCD1602_WriteString(0x80+0x40+5,"Please");
- LCD1602_WriteString(0x84,time_dis_buf);
- }
- if(hongwai == 0)
- {
- relay_off();
- LCD1602_WriteString(0x84,time_dis_buf);
- }
- if(( (time_buf[2] >= 0)&&(time_buf[2] < 7) ) ||( (time_buf[2] >= 19)&&(time_buf[2] <= 23) ) )
- {
- relay_off();
- LCD1602_write(0x01, 0); //清除显示
- LCD1602_WriteString(0x80,"Please input:");
- all_pointer = STATE_MANUAL;
- }
-
- }
- void pro()
- {
- key_read();
- switch(state_pointer)
- {
- case STATE_INPUT: state_input(); break;
- case STATE_CHECK: state_check(); break;
- case STATE_OPEN: state_open(); break;
- case STATE_SET: state_set(); break;
- case STATE_ERROR: state_error(); break;
- case STATE_CHECK_OLD: state_check_old( ); break;
- case STATE_INPUT_NEW: state_input_new( ); break;
- case STATE_INPUT_NEW_AGAIN: state_input_new_again(); break;
- case STATE_CHECK_NEW: state_check_new(); break;
- default:break;
- }
-
- if( (time_buf[2] >= 7)&&(time_buf[2] < 19) )
- {
- LCD1602_write(0x01, 0); //清除显示
- all_pointer = STATE_AUTO;
- }
- }
- void state_input()
- {
- LCD1602_WriteString(0x80+0x40,password_dis);
- if(key_use < 10)
- {
- if(key_count > 7)
- {
- key_count = 0;
- state_pointer = STATE_CHECK;
- }
- switch(key_count)
- {
- case 1: password_dis[0] = key_use+'0';
- break;
- case 2: password_dis[1] = key_use+'0';
- break;
- case 3: password_dis[2] = key_use+'0';
- break;
- case 4: password_dis[3] = key_use+'0';
- break;
- case 5: password_dis[4] = key_use+'0';
- break;
- case 6: password_dis[5] = key_use+'0';
- state_pointer = STATE_CHECK;
- break;
- default:break;
- }
- }
- if(Exit_flag == 1)
- {
- Exit_flag = 0;
- password_dis[0] = '-'; password_dis[1] = '-'; password_dis[2] = '-';
- password_dis[3] = '-'; password_dis[4] = '-'; password_dis[5] = '-';
- key_count = 0;
- }
- if(key_use == 11) //复位
- {
- password[0] = '1'; password[1] = '2'; password[2] = '3';
- password[3] = '4'; password[4] = '5'; password[5] = '6';
- password_dis[0] = '-'; password_dis[1] = '-'; password_dis[2] = '-';
- password_dis[3] = '-'; password_dis[4] = '-'; password_dis[5] = '-';
- at24c02_write(0x00,password[0]);
- at24c02_write(0x01,password[1]);
- at24c02_write(0x02,password[2]);
- at24c02_write(0x03,password[3]);
- at24c02_write(0x04,password[4]);
- at24c02_write(0x05,password[5]);
- key_count = 0;
- state_pointer = STATE_INPUT;
- }
- if( (key_use == 13) && (key_count == 0) ) //设置
- {
- key_use = 55;
- state_pointer = STATE_SET;
- LCD1602_write(0x01, 0); //清除显示
- LCD1602_WriteString(0x80,"Please input old");
- }
- }
- void state_check()
- {
- key_count = 0;
- if(Exit_flag == 1)
- {
- Exit_flag = 0;
- password_dis[0] = '-'; password_dis[1] = '-'; password_dis[2] = '-';
- password_dis[3] = '-'; password_dis[4] = '-'; password_dis[5] = '-';
- key_count = 0;
- state_pointer = STATE_INPUT;
- }
- LCD1602_WriteString(0x80+0x40,password_dis);
- if(key_use == 10) //OK
- {
- key_use = 55;
- if( (password_dis[0] == password[0])&&(password_dis[1] == password[1])&&(password_dis[2] == password[2])&&
- (password_dis[3] == password[3])&&(password_dis[4] == password[4])&&(password_dis[5] == password[5]) )
- {
- LCD1602_write(0x01, 0); //清除显示
- state_pointer = STATE_OPEN;
- sec_flag = 0;
- }
- else if( (password_dis[0] != password[0])||(password_dis[1] != password[1])||(password_dis[2] != password[2])||
- (password_dis[3] != password[3])||(password_dis[4] != password[4])||(password_dis[5] != password[5]) )
- {
- password_dis[0] = '-'; password_dis[1] = '-'; password_dis[2] = '-';
- password_dis[3] = '-'; password_dis[4] = '-'; password_dis[5] = '-';
-
- error_count++;
- if(error_count <= 2)
- {
- state_pointer = STATE_INPUT;
- }
- if(error_count >= 3)
- {
- error_count = 0;
- LCD1602_write(0x01, 0); //清除显示
- state_pointer = STATE_ERROR;
- }
- }
- }
- }
- void state_open()
- {
- key_count = 0;
- Exit_flag = 0;
- if(close_flag == 1)
- {
- close_flag = 0;
- relay_off();
- password_dis[0] = '-'; password_dis[1] = '-'; password_dis[2] = '-';
- password_dis[3] = '-'; password_dis[4] = '-'; password_dis[5] = '-';
- LCD1602_write(0x01, 0); //清除显示
- LCD1602_WriteString(0x80,"Please input:");
- key_count = 0;
- state_pointer = STATE_INPUT;
- }
- else if(close_flag == 0)
- {
- relay_on();
- LCD1602_WriteString(0x84,"Welcome!");
- LCD1602_WriteString(0x81+0x40+4,"Please");
- }
- }
- void state_set()
- {
- if(key_use < 10)
- {
- if(key_count > 7)
- {
- key_count = 0;
- state_pointer = STATE_CHECK_OLD;
- }
- switch(key_count)
- {
- // case 0: password_dis[0] = key_use+'0'; break;
- case 1: password_dis[0] = key_use+'0'; break;
- case 2: password_dis[1] = key_use+'0'; break;
- case 3: password_dis[2] = key_use+'0'; break;
- case 4: password_dis[3] = key_use+'0'; break;
- case 5: password_dis[4] = key_use+'0'; break;
- case 6: password_dis[5] = key_use+'0';
- key_count = 0;
- state_pointer = STATE_CHECK_OLD;
- break;
- default:break;
- }
- }
- if(Exit_flag == 1)
- {
- Exit_flag = 0;
- password_dis[0] = '-'; password_dis[1] = '-'; password_dis[2] = '-';
- password_dis[3] = '-'; password_dis[4] = '-'; password_dis[5] = '-';
- key_count = 0;
- }
- LCD1602_WriteString(0x80+0x40,password_dis);
- }
-
- void state_error()
- {
- Exit_flag = 0;
- if(close_flag == 1)
- {
- close_flag = 0;
- buzzer_off();
- password_dis[0] = '-'; password_dis[1] = '-'; password_dis[2] = '-';
- password_dis[3] = '-'; password_dis[4] = '-'; password_dis[5] = '-';
- LCD1602_write(0x01, 0); //清除显示
- LCD1602_WriteString(0x80,"Please input:");
- state_pointer = STATE_INPUT;
- }
- else if(close_flag == 0)
- {
- buzzer_on();
- LCD1602_WriteString(0x85,"Error!");
- }
- }
- void state_check_old()
- {
- key_count = 0;
- if(Exit_flag == 1)
- {
- Exit_flag = 0;
- password_dis[0] = '-'; password_dis[1] = '-'; password_dis[2] = '-';
- password_dis[3] = '-'; password_dis[4] = '-'; password_dis[5] = '-';
- key_count = 0;
- state_pointer = STATE_SET;
- }
- if(key_use == 10) //OK
- {
- key_use = 55;
- if( (password_dis[0] == password[0])&&(password_dis[1] == password[1])&&(password_dis[2] == password[2])&&
- (password_dis[3] == password[3])&&(password_dis[4] == password[4])&&(password_dis[5] == password[5]) )
- {
- password_dis[0] = '-'; password_dis[1] = '-'; password_dis[2] = '-';
- password_dis[3] = '-'; password_dis[4] = '-'; password_dis[5] = '-';
- LCD1602_write(0x01, 0); //清除显示
- LCD1602_WriteString(0x80,"Please input new");
- state_pointer = STATE_INPUT_NEW;
- }
- else if( (password_dis[0] != password[0])||(password_dis[1] != password[1])||(password_dis[2] != password[2])||
- (password_dis[3] != password[3])||(password_dis[4] != password[4])||(password_dis[5] != password[5]) )
- {
- password_dis[0] = '-'; password_dis[1] = '-'; password_dis[2] = '-';
- password_dis[3] = '-'; password_dis[4] = '-'; password_dis[5] = '-';
-
- state_pointer = STATE_SET;
- }
- }
- }
- void state_input_new()
- {
- LCD1602_WriteString(0x80+0x40,password_dis);
- if(key_use < 10)
- {
- switch(key_count)
- {
- // case 0: password_dis[0] = key_use+'0'; break;
- case 1: password_dis[0] = key_use+'0'; break;
- case 2: password_dis[1] = key_use+'0'; break;
- case 3: password_dis[2] = key_use+'0'; break;
- case 4: password_dis[3] = key_use+'0'; break;
- case 5: password_dis[4] = key_use+'0'; break;
- case 6: password_dis[5] = key_use+'0';
- key_count = 0;
- data_buf[0] = password_dis[0]; data_buf[1] = password_dis[1]; data_buf[2] = password_dis[2];
- data_buf[3] = password_dis[3]; data_buf[4] = password_dis[4]; data_buf[5] = password_dis[5];
- password_dis[0] = '-'; password_dis[1] = '-'; password_dis[2] = '-';
- password_dis[3] = '-'; password_dis[4] = '-'; password_dis[5] = '-';
- LCD1602_write(0x01, 0); //清除显示
- LCD1602_WriteString(0x80,"Pleaset again:");
- state_pointer = STATE_INPUT_NEW_AGAIN;
- break;
- default:break;
- }
- }
- if(Exit_flag == 1)
- {
- Exit_flag = 0;
- password_dis[0] = '-'; password_dis[1] = '-'; password_dis[2] = '-';
- password_dis[3] = '-'; password_dis[4] = '-'; password_dis[5] = '-';
- key_count = 0;
- }
- }
- void state_input_new_again()
- {
- LCD1602_WriteString(0x80+0x40,password_dis);
- if(key_use < 10)
- {
- switch(key_count)
- {
- case 1: password_dis[0] = key_use+'0'; break;
- case 2: password_dis[1] = key_use+'0'; break;
- case 3: password_dis[2] = key_use+'0'; break;
- case 4: password_dis[3] = key_use+'0'; break;
- case 5: password_dis[4] = key_use+'0'; break;
- case 6: password_dis[5] = key_use+'0';
- key_count = 0;
- state_pointer = STATE_CHECK_NEW;
- break;
- default:break;
- }
- }
- if(Exit_flag == 1)
- {
- Exit_flag = 0;
- password_dis[0] = '-'; password_dis[1] = '-'; password_dis[2] = '-';
- password_dis[3] = '-'; password_dis[4] = '-'; password_dis[5] = '-';
- key_count = 0;
- }
- }
- void state_check_new()
- {
- Exit_flag = 0;
- if( (password_dis[0] == data_buf[0])&&(password_dis[1] == data_buf[1])&&(password_dis[2] == data_buf[2])&&
- (password_dis[3] == data_buf[3])&&(password_dis[4] == data_buf[4])&&(password_dis[5] == data_buf[5]) )
- {
- password[0] = password_dis[0]; password[1] = password_dis[1]; password[2] = password_dis[2];
- password[3] = password_dis[3]; password[4] = password_dis[4]; password[5] = password_dis[5];
- password_dis[0] = '-'; password_dis[1] = '-'; password_dis[2] = '-';
- password_dis[3] = '-'; password_dis[4] = '-'; password_dis[5] = '-';
- at24c02_write(0x00,password[0]);
- at24c02_write(0x01,password[1]);
- at24c02_write(0x02,password[2]);
- at24c02_write(0x03,password[3]);
- at24c02_write(0x04,password[4]);
- at24c02_write(0x05,password[5]);
- LCD1602_write(0x01, 0); //清除显示
- LCD1602_WriteString(0x80,"Please input:");
- state_pointer = STATE_INPUT; // 设置完成
- }
- else if( (password_dis[0] != password[0])||(password_dis[1] != password[1])||(password_dis[2] != password[2])||
- (password_dis[3] != password[3])||(password_dis[4] != password[4])||(password_dis[5] != password[5]) )
- {
- password_dis[0] = '-'; password_dis[1] = '-'; password_dis[2] = '-';
- password_dis[3] = '-'; password_dis[4] = '-'; password_dis[5] = '-';
-
- LCD1602_write(0x01, 0); //清除显示
- LCD1602_WriteString(0x80,"Please input:");
- state_pointer = STATE_SET;
- }
- }
- void timer0() interrupt 1
- {
- TL0 = 0xE0; //设置定时初值
- TH0 = 0xB1; //设置定时初值
- if( (state_pointer == STATE_OPEN)||(state_pointer == STATE_ERROR) )
- {
- sec_flag++;
- if(sec_flag >= 50)
- {
- sec_flag = 0;
- relay_count++;
- if(relay_count >=3)
- {
- relay_count = 0;
- close_flag = 1;
- }
- }
- }
- }
复制代码
完整代码下载:
程序.zip
(122.62 KB, 下载次数: 26)
|