#include <reg51.h>
#include "lcd.h"
#include "i2c.h"
#define KEY_DATA P3 //矩阵键盘IO口
#define open 1
#define close 0
sbit buzz = P1^4;
sbit men_here = P2^2;
sbit LA = P1^0;
sbit LB = P1^1;
sbit LC = P1^2;
sbit LD = P1^3;
void Timer0Init(void); //10毫秒@12.000MHz
void delay(int z);
void step_motor(bit dir, int speed);
char key_scan();
unsigned char At24c02Read(unsigned char addr);
void At24c02Write(unsigned char addr,unsigned char dat);
void input_password(); //输入密码
void set_password(); //设置密码
void save_password(); //将密码存入24c02
void check_password(); //校验密码
void set_mode(); //设置模式
void password_mode(); //密码模式
void auto_mode(); //自动模式
void close_mode(); //常关模式
void door_control(bit dir); // 控制门开关
enum mode_index
{
PasswordMode,
AutoMode,
CloseMode,
};
enum mode_index mode;
static char key_val; //存储按键的值
unsigned char set_flag = 0; //设置密码标志,默认为0,表示不设置密码
unsigned char alter_password_index = 0; //修改密码层索引,0输入旧密码,1输入新密码,2确认密码
unsigned char password_is_alter = 0; //密码是否已经修改的标志
unsigned char door_is_open = 0; //门状态标志位
unsigned char open_or_close = 0;
unsigned char sec = 0, flag_5s = 0, flag_10s = 0, password_error_flag = 0, error_index = 0;
unsigned char code motor_0[8] = {0x08, 0x0c, 0x04, 0x06, 0x02, 0x03, 0x01, 0x09};
unsigned char code motor_1[8] = {0x09, 0x01, 0x03, 0x02, 0x06, 0x04, 0x0c, 0x08};
char password[7] = {'1', '8', '0', '3', '1', '6', '\0'};
char password_buff[7]; //密码缓冲区,修改密码时的密码预先存在这里
char key_password[7]; //存放按键输入的密码
char const key_Map[16] =
{
'1', '2', '3', 'A',
'4', '5', '6', 'B',
'7', '8', '9', 'C',
'F', '0', 'E', 'D',
};
void main()
{
P1 = 0x00;
buzz = 0;
men_here = 0;
Timer0Init();
LcdInit();
//save_password();
door_is_open = At24c02Read(8);
door_control(open_or_close);
while(1)
{
switch(mode)
{
case PasswordMode: password_mode(); break;
case AutoMode: auto_mode(); break;
case CloseMode: close_mode(); break;
}
door_control(open_or_close);
}
}
/******设置模式******/
void set_mode()
{
if(key_scan() == 'A')
mode = PasswordMode;
else if(key_scan() == 'B')
mode = AutoMode;
else if(key_scan() == 'C')
mode = CloseMode;
}
void Timer0Init(void) //10毫秒@12.000MHz
{
TMOD = 0x01; //设置定时器模式
TL0 = 0xF0; //设置定时初值
TH0 = 0xD8; //设置定时初值
EA = 1;
ET0 = 1;
}
void Timer0_interrupt() interrupt 1
{
static int cnt = 0;
TL0 = 0xF0; //设置定时初值
TH0 = 0xD8; //设置定时初值
cnt ++;
if(cnt > 100)
{
cnt = 0;
sec++;
if(mode == PasswordMode)
{
if(password_error_flag)
{
LcdWriteStr(1, 0, " Try after ");
LcdWriteByte(7, 1, (10 - sec) + '0');
}
else
LcdWriteByte(7, 1, (5 - sec) + '0');
}
if(sec%5 == 0)
{
flag_5s = 1;
}
if(sec == 10)
{
sec = 0;
flag_10s = 1;
}
}
}
/******密码模式******/
void password_mode()
{
switch(set_flag)
{
case 0: if(password_error_flag)
{
LcdWriteStr(1, 0, " Try after ");
LcdWriteStr(5, 1, " s ");
while(flag_10s == 0);
flag_10s = 0;
TR0 = 0;
password_error_flag = 0;
}
else
{
LcdWriteStr(1, 0, "input password");
LcdWriteStr(5, 1, "______ ");
input_password(); //输入密码
}
break;
case 1: set_password(); //设置密码
break;
}
}
/******自动模式******/
void auto_mode()
{
LcdWriteStr(1, 0, " auto mode ");
LcdWriteStr(0, 1, " ");
set_mode();
if(men_here == 1)
{
open_or_close = open;
}
else
{
open_or_close = close;
}
}
/******常关模式******/
void close_mode()
{
LcdWriteStr(1, 0, " close mode ");
LcdWriteStr(0, 1, " ");
open_or_close = 0;
door_control(open_or_close);
while(key_scan() == -1);
set_mode();
}
/******保存密码******/
void save_password()
{
unsigned char i;
if(password_is_alter == 1)
{
for (i = 7; i > 0; i--)
{
At24c02Write(i, password_buff[i - 1]);
delay(10);
}
}
else
{
for (i = 1; i < 7; i++)
{
At24c02Write(i, password[i - 1]);
delay(10);
}
}
}
/******输入密码******/
void input_password()
{
unsigned char i, input_is_correct = 0;
for (i = 0; i < 7; i++)
{
while(key_scan() == -1); //等待按键被按下
if (('0' <= key_scan()) && (key_scan() <= '9') && (i < 6))
{
if(alter_password_index == 1)
password_buff[i] = key_scan();
else
key_password[i] = key_scan();
LcdWriteByte(i + 5, 1, key_scan());
input_is_correct = 1;
}
else
{
input_is_correct = 0;
if (key_scan() == 'E')
{
if(i <= 1)
i = 1;
LcdWriteByte(i + 4, 1, '_');
i = i - 2;
}
else if((key_scan() == 'F') && (i == 6))
{
i = 8;
if(alter_password_index == 1)
{
password_buff[6] = '\0';
alter_password_index = 2;
break;
}
else
{
key_password[6] = '\0';
check_password();
break;
}
}
else if(key_scan() == 'A')
{
set_flag = 0;
break;
}
else if(key_scan() == 'B')
{
mode = AutoMode;
break;
}
else if(key_scan() == 'C')
{
mode = CloseMode;
break;
}
else if(key_scan() == 'D')
{
set_flag = 1;
break;
}
else
i--;
}
while(key_scan() != -1); //等待按键弹起
if (input_is_correct)
LcdWriteByte(i + 5, 1, '*');
}
}
/******设置密码******/
void set_password()
{
switch(alter_password_index)
{
case 0: LcdWriteStr(1, 0, " Old Password ");
LcdWriteStr(5, 1, "______");
input_password();
break;
case 1: LcdWriteStr(1, 0, " New Password ");
LcdWriteStr(5, 1, "______");
input_password();
break;
case 2: LcdWriteStr(1, 0, " input again ");
LcdWriteStr(5, 1, "______");
input_password();
break;
}
}
/******校验密码******/
void check_password()
{
unsigned char i, x, index = 0;
for (i = 0; i < 6; i++)
{
if(alter_password_index == 2)
{
if(key_password[i] == password_buff[i])
{
index ++;
}
if((index == 6) && (set_flag == 1))
{
password_is_alter = 1; //密码修改成功
At24c02Write(9, password_is_alter);
save_password();
LcdWriteStr(5, 1, "succeed");
set_flag = 0;
alter_password_index = 0;
buzz = 1;
delay(600);
buzz = 0;
}
if((i == 5) && (index != 6))
{
LcdWriteStr(5, 1, "error ");
buzz = 1;
delay(500);
buzz = 0;
}
}
else
{
if(key_password[i] == At24c02Read(i + 1))
{
index ++;
}
if((index == 6) && (set_flag == 0))
{
LcdWriteStr(5, 1, " pass ");
error_index = 0;
open_or_close = open;
door_control(open_or_close);
LcdWriteStr(1, 0, " closed after ");
LcdWriteStr(5, 1, " s ");
LcdWriteByte(7, 1, (5 - sec) + '0');
TR0 = 1;
while(flag_5s == 0);
open_or_close = close;
TR0 = 0;
sec = 0;
flag_5s = 0;
break;
}
else if((index == 6) && (set_flag))
{
alter_password_index = 1;
}
if((i == 5) && (index != 6))
{
LcdWriteStr(5, 1, "error ");
buzz = 1;
delay(500);
buzz = 0;
error_index ++;
if(error_index == 3)
{
error_index = 0;
TR0 = 1;
password_error_flag = 1;
mode = CloseMode;
}
}
}
}
}
void door_control(bit dir)
{
unsigned char x;
if((door_is_open == 0) && (dir == 1))
{
for(x = 200; x > 0; x--)
step_motor(open, 1);
door_is_o
评论0