文档库

最新最全的文档下载
当前位置:文档库 > Digital_lock(电子密码锁程序——完整版)

Digital_lock(电子密码锁程序——完整版)

//////////////////////////////////////////////////////////

/////////////////////////////////////////////////////////
#include
#include
#include
#include"KEY_4x4.h"
#include"LCD_1602.h"
#include"I2C.h"
#define uint unsigned int
#define uchar unsigned char

uchar code table0[]=" ";
uchar code table1[]="PLESSE ID";
uchar code table2[]="WELCOME";
uchar code table3[]="HAPPY";
uchar code table4[]="ERROR";
uchar code table5[]="****";
uchar code table6[]="ERROR AGAIN";
uchar code table8[]="SUCCEED";
uchar code table9[]="OK";
uchar code table10[]="Change Password";
uchar code table11[]="Y/N";
uchar code table12[]="New Password";
uchar code table13[]="Succeed";
uchar code table14[]="NOT FORGET";
uchar code table15[]="Keyboard Locked";
uchar code table16[]="S_users Y/N";
uchar code table17[]="YOU NOT S_user";
uchar code table18[]="S_user Password";
uchar code table19[]="S_user Password";
uchar code table20[]="S_user ID";
uchar code table21[]="Remember:";
uchar code table22[]="Init Password";

sbit yellow=P2^5; // 上锁 指示灯
sbit red=P2^6; //密码错误信号
sbit green=P2^7; //已开锁指示信号
sbit blue=P2^4; //修改密码指示信号

bit keyscan_flag;//键盘有键按下标志
bit BJCB=0; //密码正确错误标志
bit BJ=0; //报警标志位
bit SJ=0; //锁键盘标志位
bit super_flag=0;//超级用户密码是否正确标志

bit come_flag=0; //用户输入标志位
bit finish_flag=0; //密码输完标志位
bit error_alarm_flag=0;// 密码错误报警标志
bit right_alarm_flag=0;//密码正确报警标志

uchar PSWD0[10]=0; //密码输入缓存存储单元
uchar PSWD1[10]=0; //读出密码缓存存储单元
uchar PSWD2[4]={8,8,8,8};//用户初始密码存储单元
uchar PSWD3[4]={1,1,1,1};//超级用户密码存储单元
uchar m,num,wo,count=0;

void delay1(uint x)
{
uchar i;
for(;x>0;x--)
for(i=200;i>0;i--);
}
/***************************************************************
函数功能:第一次烧片时将初始密码烧到24c02
****************************************************************/
void burn_frist(void) //
{
uchar i;
for(i=0;i<4;i++)
{
I2C_write_data(i,PSWD2[i]);
delay1(20);
}
delay1(20);
for(i=0;i<4;i++)
{
I2C_write_data(i+10,PSWD3[i]);
delay1(20);
}
}
/***************************************************************
函数功能:报警声音
***************************************************************/
void alarm_error(uint t)
{
uint n;
for(;t>0;t--)
{
for(n=0;n<300;n++);
BEEP=~BEEP; //取反输出到喇叭的信号
}
}
void alarm_right(uint t)
{
uint n;
for(;t>0;t--)
{
for(n=0;n<500;n++);
BEEP=~BEEP; //取反输出到喇叭的信号
}
}
/**************************************************************
函数功能:

显示各个提示
**************************************************************/
void display0(void)/* 显示输入的数字 */
{
write_1602_com(0x80+0x45+wo);
write_1602_data(num+48);
delay1(1800);
write_1602_com(0x80+0x45+ wo++);
write_1602_data('*');
}
void display1(void) /* 上电时显示 */
{
uchar length=0;
length=strlen(table0);
write_1602_com(0x80);
display(table0,length); // PLESSE ID
length=strlen(table0);
write_1602_com(0x80+0x40);
display(table0,length);

length=strlen(table1);
write_1602_com(0x83);
display(table1,length);
}
void display2(void) /* 第1、2次密码错误显示 */
{
uchar length=0;
length=strlen(table0);
write_1602_com(0x80);
display(table0,length);
length=strlen(table6); //ERROR AGAIN
write_1602_com(0x82);
display(table6,length);

// length=strlen(table5); // ****
// write_1602_com(0x80+0x45);
// display(table5,length);
}
void display3(void) /* 第3次密码输入错误显示 */
{
uchar length=0;
length=strlen(table0);
write_1602_com(0x80);
display(table0,length);
length=strlen(table4); // ERROR
write_1602_com(0x85);
display(table4,length);

//length=strlen(table5); //****
//write_1602_com(0x80+0x45);
//display(table5,length);
}
void display4(void) /* 密码输入正确显示 */
{
uchar length=0;
length=strlen(table0);
write_1602_com(0x80);
display(table0,length);
length=strlen(table2); //WELCOME
write_1602_com(0x84);
display(table2,length);

length=strlen(table3); //HAPPY
write_1602_com(0x80+0x45);
display(table3,length);
}
void display5(void) /*提示修改密码显示 */
{
uchar length=0;
length=strlen(table0);
write_1602_com(0x80);
display(table0,length);
length=strlen(table10);// change password
write_1602_com(0x80);
display(table10,length);

length=strlen(table0);
write_1602_com(0x80+0x40);
display(table0,length);
length=strlen(table11);// Y/N
write_1602_com(0x80+0x46);
display(table11,length);
}
void display6(void) /*提示新密码输入显示 */
{
uchar length=0;
length=strlen(table0);
write_1602_com(0x80);
display(table0,length);
length=strlen(table12);// new password
write_1602_com(0x82);
display(table12,length);
length=strlen(table0);
write_1602_com(0x80+0x40);
display(table0,length);
}
void display7(void)/* 提示密码修改正确显示 */
{
uchar length=0;
length=strlen(table0);
write_1602_com(0x80);
display(table0,length);
length=strlen(table13);//succeed
write_1602_com(0x84);
display(table13,length);

length=strlen(table0);
write_1602_com(0x80+0x40);
display(table0,length);
length=strlen(table14);//NOT FORGET
write_1602_com(0x80+0x42);
display(table14,length);
}
void display8(void)/* 3次输入密码错误后,进入超级用户提示显示 */
{
ucha

r length=0;
length=strlen(table0);
write_1602_com(0x80);
display(table0,length);
length=strlen(table15); //Keyboard Locked
write_1602_com(0x80);
display(table15,length);

length=strlen(table16); //S_users Y/N
write_1602_com(0x80+0x41);
display(table16,length);
}
void display9(void) /* 非法超级用户 */
{
uchar length=0;
length=strlen(table0);
write_1602_com(0x80);
display(table0,length);
length=strlen(table17); //YOU NOT S_user
write_1602_com(0x80);
display(table17,length);

length=strlen(table0);
write_1602_com(0x80);
display(table0,length);
length=strlen(table15); //Keyboard Locked
write_1602_com(0x80);
display(table15,length);
}
void display10(void) /* 正确超级用户进入显示 */
{
uchar length=0;
length=strlen(table0);
write_1602_com(0x80);
display(table0,length);
length=strlen(table18); //S_user Password
write_1602_com(0x80);
display(table18,length);

length=strlen(table0);
write_1602_com(0x80+0x40);
display(table0,length);
}
void display11(void) /* 超级用户密码输入提示 */
{
uchar length=0;
length=strlen(table0);
write_1602_com(0x80);
display(table0,length); // S_user ID
length=strlen(table20);
write_1602_com(0x82);
display(table20,length);

length=strlen(table0);
write_1602_com(0x80+0x40);
display(table0,length);
}
void display12(void) /* 显示初始密码 提示用户记住密码*/
{
uchar i,length;

length=strlen(table0);
write_1602_com(0x80);
display(table0,length); //Init Password
length=strlen(table22);
write_1602_com(0x80);
display(table22,length);

length=strlen(table21);
write_1602_com(0x80+0x40);//Remember:
display(table21,length);

length=strlen(PSWD2);
write_1602_com(0x80+0x4c);
for(i=0;i{
write_1602_data(PSWD2[i]+48);
}
}
/*************************************************************
函数功能:从键盘扫描密码
**************************************************************/
void key(void)
{
if(keyscan_flag==1 && num<=9)
{
write_1602_com(0x80+0x45+wo);
write_1602_data(num+48);
delay1(1800);
write_1602_com(0x80+0x45+ wo++);
write_1602_data('*');

PSWD0[m++]=num;
//*password=num;
//password++;
}
}
/*********************************************************************
函数功能: 密码判断
*********************************************************************/
void judge_password(void)
{
uchar i;
if(keyscan_flag==1 && num==15)
{
keyscan_flag=0;
m=0;
for(i=0;i<4;i++) //从24c02里读出密码
{
PSWD1[i]=I2C_read_data(i);
delay1(10);
}
for(i=0;i<4;i++)
{
if(PSWD1[i]!=PSWD0[i])
{
BJCB=1;
break;
}
else
BJCB=0;
}
wo=0;
}
}
void super_judge_password(void)
{
uchar i;
if(keyscan_flag==1 && num==15)


{
keyscan_flag=0;
for(i=0;i<4;i++) //从24c02里读出密码
{
PSWD1[i]=I2C_read_data(i+10);
delay1(10);
}
for(i=0;i<4;i++)
{
if(PSWD1[i]!=PSWD0[i])
{
super_flag=1;
break;
}
else
super_flag=0;
}
wo=0;
}
}
/**********************************************************************
函数功能:判断密码是否正确 以及修改密码
***********************************************************************/
void pass_word(void)
{
uchar i;
uchar m=0,length=0;
//uchar *password;
//password=PSWD0;

loop: yellow=0;
green=1;
red=1;
display1();

if(SJ==0)
{
m=0;
do
{
// key();
keyscan_flag=0; //键盘标志位
num=keyscan();
if(keyscan_flag==1 && num<=9)
{
display0();
PSWD0[m++]=num;
}
if(keyscan_flag==1 && num==10||num==11||num==12||num==13)
{
red=0;
delay1(2000);
red=1;
goto loop;
}
}while(num!=15);

if(m!=4)
{
for(i=m;i<4;i++)
{
PSWD0[i]=0;
}
}

judge_password(); //密码判断

/* 密码错误相关处理 */
if(BJCB)
{
count++;
if(count!=3)
{
yellow=1;
red=0;
BJCB=0; //清第一次和第二次报警
error_alarm_flag=1;
display2();
/* 密码错误报警声 */
for(i=0;i<3;i++)
{
alarm_error(200);
delay1(2000);
}

red=1;
goto loop;
}
else if(count==3)
{
red=0;
count=0;
BJCB=0;
yellow=1;

error_alarm_flag=1;
display3(); //第3次密码输入错误显示
/* 密码错误报警声 */
for(i=0;i<5;i++)
{
alarm_error(200);
delay1(2000);
}
display8();//进入超级用户提示显示
do
{
keyscan_flag=0;
num=keyscan();
if(num==14)
{ // 15表示 Y
break; // 14表示 N
}
}while(num!=15);
/* Y 进入超级密码设置 N 跳出超级密码设置 但键盘被锁 */
if(keyscan_flag && num==15)
{
display11();// 超级用户密码输入提示
m=0;
do
{
keyscan_flag=0;
num=keyscan();
if(keyscan_flag==1 && num<=9)
{
display0();
PSWD0[m++]=num;
}
}while(num!=15);
super_judge_password(); //密码判断
if(super_flag)

{
display9(); //非法超级用户
SJ=1; //锁键盘
delay1(4000);
}
else //是超级用户
{
display10(); //显示超级用户
display12(); // 显示初始密码 提示用户记住密码
/* 再次将初始密码写入 */
delay1(50);
for(i=0;i<4;i++)
{
I2C_write_data(i,PSWD2[i]);
delay1(20);
}
delay1(50);
do
{
keyscan_flag=0;
num=keyscan();
}while(num!=15);
num=0;
}
}
if(keyscan_flag && num==14)
{
SJ=1; //锁键盘
}

red=1;
m=0;
delay1(4000);
goto loop;
}
}
/* 密码正确处理 */
else
{
red=1;
green=0; //表示密码正确
yellow=1;
right_alarm_flag=1;
m=0; //密码个数清零

display4();
alarm_right(700);
}

LOOP1: do
{
keyscan_flag=0;
num=keyscan();
if(keyscan_flag)
{
keyscan_flag=0;
switch(num)
{
case 11:
{
blue=0; //修改密码标记
green=1;
display5(); //提示修改密码

do
{
keyscan_flag=0;
num=keyscan();
if(num==14)
{ // 14表示 N
break; // 15表示 Y
}
}while(num!=15);
/* Y 从新向24c02写新密码 N跳出 */
if(keyscan_flag==1 && num==15)
{
display6();
m=0;
wo=0;
do
{
keyscan_flag=0;
num=keyscan();
if(keyscan_flag==1 && num<=9)
{
PSWD0[m++]=num;
display0();
}
if(m==10)
{
goto LOOP1; //超出10位密码范围
}
}while(num!=15);

if(m!=10 && num==15) //
{
for(i=m;i<10;i++)
{
PSWD0[i]=0;
}
}

if(keyscan_flag && num==15)
{
keyscan_flag=0;
for(i=0;i<4;i++) //一个向24c02里写
{

I2C_write_data(i,PSWD0[i]);
delay1(20);
}
display7();//提示修改密码成功
}
do
{
keyscan_flag=0;
num=keyscan();
}while(num!=15); //锁锁返回
wo=0;
blue=1;
goto loop;
}
/* 不修改密码 跳到初始界面 */
else if(keyscan_flag && num==14)
{
blue=1;
wo=0;
goto loop;
}

}break;

case 14:{
goto loop;
}break;
default: goto loop;
}

}

}while(keyscan_flag!=0);//如果设置密码键按下

goto LOOP1;

}
}
void init_timer(void)
{
TMOD=0x01; //定时器工作在方式1
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
ET0=1;
EA=1;
}
void main()
{
//uchar i;
//uchar m=0,length=0;
//uchar *password;
//password=PSWD0;
m=0;
num=0;
wo=0;
count=0;
init_timer();
I2C_init();
init_1602();

// burn_frist(); //第一次烧片时用
// while(1)
// {
pass_word();
// }

for(;;);
}
void timer0() interrupt 1
{
uint i;
TL0=(65536-50000)%256; // 重装计数初值
TH0=(65536-50000)/256; // 对TH0 TL0赋值5MS定时

if(error_alarm_flag)
{
for(i=0;i<3;i++)
{
alarm_error(200);
}
}
if(right_alarm_flag)
{
//for(i=0;i<3;i++)
//{
alarm_right(1000);
// }
}

}