#include<reg52.h>
#include<intrinS.h>
#include<absacc.h>
#include "keycl.h"//见3.12
#include "LCD1602.h"//见2.2
#include "24C02.h"//见4.1
#define uchar unsigned char
#define C02_write 0xa0
#define C02_read 0xa1
#define enter 0x0C//确定
#define backspace 0x0F//退格
#define A 0x0a //输入密码
#define B 0x0b //修改密码
uchar idata temp5_password[6]=
{0x01,0x02,0x03,0x04,0x05,0x06};//设置初始密码为“123456”
sbit SET=P3^2;//恢复出厂密码设置键
sbit Lock_BIT=P3^3;//锁控制信号,输出低电平
sbit SPK=P3^4;//报警信号输出端
uchar int_count_1;
unsigned int int_count_2;
//================================================
void longdelay(unsigned char i)
{
unsigned int j;
for(;i>0;i--)
for(j=10000;j>0;j--);
}
//==========================================
bit compare_string(uchar *string1,uchar *string2)
{
uchar count;
for(count=0;count<6;count++)
{
if(string1[count]!=string2[count]) return(0);
}
return(1);
}
//---------------------------------------
uchar step_choose(void)
{
unsigned int i;
uchar key;
do{
WR_CMD(0x01);
WR_CMD(0x06);
GotoXY(0,0);
Show_Char("input password");
GotoXY(0,1);
Show_Char("please press A");
for(i=0;i<30000;i++)
{
key=keycl();
if((key==A)||(key==B)) break;
}
if((key!=A)&&(key!=B))
{
WR_CMD(0x01);
WR_CMD(0x06);
GotoXY(0,0);
Show_Char("change password");
GotoXY(0,1);
Show_Char("please press B");
for(i=0;i<30000;i++)
{
key=keycl();
if((key==A)||(key==B)) break;
}
}
}
while((key!=A)&&(key!=B));
return(key);
}
//==========================================================
bit input_password(uchar *password)
{
uchar count;
uchar key;
GotoXY(0,1);
for(count=0;count<7;count++)
{
longdelay(3);
if(count<6)
{
do{key=keycl();}
while(key==0xff);
if((key!=backspace)&&(key!=enter))
{
Show_1_Char('*');
password[count]=key;
}
if(key==backspace)
{
if(count>0)
{
GotoXY(--count,1);
Show_1_Char(' ');
password[count]=' ';
GotoXY(count,1);
count--;
}
}
if(key==enter)
{
GotoXY(0,0);
return( 0);
}
}
if(count==6)
{
do{key=keycl();}
while((key!=backspace)&&(key!=enter));
if(key==backspace)
{
GotoXY(--count,1);
Show_1_Char(' ');
password[count]=' ';
GotoXY(count,1);
count--;
}
if(key==enter)
{return(1);}
}
}
}
//============================================================
void password()
{uchar key;
uchar idata temp1_password[6]={' ',' ',' ',' ',' ',' '};
uchar idata temp3_password[6]={' ',' ',' ',' ',' ',' '};
uchar idata temp2_password[6]={' ',' ',' ',' ',' ',' '};
uchar idata temp4_password[6]={' ',' ',' ',' ',' ',' '};
key=step_choose();
if(key==A)
{Read_Flash(temp1_password,0x00,6);//I2C_receive_string(temp1_password,6,0x00);
WR_CMD(0x01);
WR_CMD(0x06);
GotoXY(2,0);
Show_Char("input password");
if(input_password(temp2_password))
{if(compare_string(temp1_password,temp2_password))
{GotoXY(0,1);
Show_Char("password correct!");
Lock_BIT=0;
SPK=0;
TR0=1;
longdelay(10);
Lock_BIT=1;
SPK=1;
TR0=0;
return;
}
else
{GotoXY(0,1);
Show_Char("wrong password");
longdelay(10);
return;
}
}
else
{GotoXY(0,1);
Show_Char("error! ");
longdelay(6);
}
}
else
{Read_Flash(temp1_password,0x00,6);//I2C_receive_string(temp1_password,6,0x00);
WR_CMD(0x01);
WR_CMD(0x06);
GotoXY(0,0);
Show_Char("input old password");
if(input_password(temp2_password))
{if(compare_string(temp1_password,temp2_password))
{GotoXY(0,1);
Show_Char("password correct!");
longdelay(6);
WR_CMD(0x01);
WR_CMD(0x06);
GotoXY(0,0);
Show_Char("input new password");
if(input_password(temp3_password))
{WR_CMD(0x01);
WR_CMD(0x06);
GotoXY(0,0);
Show_Char("input password again");
if(input_password(temp4_password))
{if(compare_string(temp3_password,temp4_password))
{Write_Flash(temp3_password,0x00,6);//I2C_send_string(temp3_password,6,0x00);
WR_CMD(0x01);
WR_CMD(0x06);
GotoXY(0,0);
Show_Char("password has");
GotoXY(0,1);
Show_Char("been changed");
longdelay(10);
return;
}
else
{WR_CMD(0x01);
WR_CMD(0x06);
GotoXY(0,0);
Show_Char("twice input");
GotoXY(0,1);
Show_Char("is different");
longdelay(10);
return;
}
}
else
{GotoXY(0,1);
Show_Char("error! ");
longdelay(10);
return;
}
}
else
{GotoXY(0,1);
Show_Char("error! ");
longdelay(10);
return;
}
}
else
{GotoXY(0,1);
Show_Char("wrong password");
longdelay(6);
return;
}
}
else
{GotoXY(0,1);
Show_Char("error! ");
longdelay(6);
return;
}
}
}
//===================================================
void int_service(void) using 0 interrupt 0
{EA=0;
EX0=0;
for(int_count_1=10;int_count_1>0;int_count_1--)
{for(int_count_2=0xffff;int_count_2>0;int_count_2--)
{if(SET)
{EA=1;
EX0=1;
return;
}
}
}
WR_CMD(0x01);
WR_CMD(0x06);
Show_Char("rest success!");
longdelay(12);//延时
Write_Flash(temp5_password,0x00,6);//I2C_send_string(temp5_password,6,0x00);
(*(void(*)())0)();
}
//=========================================================
void t0(void) interrupt 1 using 0
{
TH0=(65536-1000)/256;
TL0=(65536-1000)%256;
SPK=~SPK;
}
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
void main(void)
{ IT0=1;
EX0=1;
TMOD=0x01;
TH0=(65536-1000)/256;
TL0=(65536-1000)%256;
TR0=0;
ET0=1;
EA=1;
LCD_Initial2();
while(1)
{
password();
}
}
基于51单片机的c语言程序 电子密码锁
4星 · 超过85%的资源 需积分: 50 114 浏览量
2012-05-06
15:52:24
上传
评论 3
收藏 207KB ZIP 举报
kaidada
- 粉丝: 4
- 资源: 16