电子防盗密码锁程序.docx
《电子防盗密码锁程序.docx》由会员分享,可在线阅读,更多相关《电子防盗密码锁程序.docx(26页珍藏版)》请在冰豆网上搜索。
电子防盗密码锁程序
#include
#include
#defineLCM_DataP0
#defineucharunsignedchar
#defineuintunsignedint
#definew6//定义密码位数
sbitlcd1602_rs=P2^5;
sbitlcd1602_rw=P2^6;
sbitlcd1602_en=P2^7;
sbitScl=P3^4;//24C02串行时钟
sbitSda=P3^5;//24C02串行数据
sbitALAM=P2^1;//报警
sbitKEY=P2^0;//开锁
sbitopen_led=P2^2;//开锁指示灯
bitoperation=0;//操作标志位
bitpass=0;//密码正确标志
bitReInputEn=0;//重置输入充许标志
bits3_keydown=0;//3秒按键标志位
bitkey_disable=0;//锁定键盘标志
unsignedcharcountt0,second;//t0中断计数器,秒计数器
voidDelay5Ms(void);
unsignedcharcodea[]={0xFE,0xFD,0xFB,0xF7};//控盘扫描控制表
unsignedcharcodestart_line[]={"password:
"};
unsignedcharcodename[]={"===CodedLock==="};//显示名称
unsignedcharcodeCorrect[]={"correct"};//输入正确
unsignedcharcodeError[]={"error"};//输入错误
unsignedcharcodecodepass[]={"pass"};
unsignedcharcodeLockOpen[]={"open"};//OPEN
unsignedcharcodeSetNew[]={"SetNewWordEnable"};
unsignedcharcodeInput[]={"input:
"};//INPUT
unsignedcharcodeResetOK[]={"ResetPasswordOK"};
unsignedcharcodeinitword[]={"Initpassword..."};
unsignedcharcodeEr_try[]={"error,tryagain!
"};
unsignedcharcodeagain[]={"inputagain"};
unsignedcharInputData[6];//输入密码暂存区
unsignedcharCurrentPassword[6]={1,3,1,4,2,0};//当前密码值
unsignedcharTempPassword[6];
unsignedcharN=0;//密码输入位数记数
unsignedcharErrorCont;//错误次数计数
unsignedcharCorrectCont;//正确输入计数
unsignedcharReInputCont;//重新输入计数
unsignedcharcodeinitpassword[6]={0,0,0,0,0,0};
//=====================5ms延时==============================
voidDelay5Ms(void)
{
unsignedintTempCyc=5552;
while(TempCyc--);
}
//===================400ms延时==============================
voidDelay400Ms(void)
{
unsignedcharTempCycA=5;
unsignedintTempCycB;
while(TempCycA--)
{
TempCycB=7269;
while(TempCycB--);
}
}
//=============================================================================================
//================================24C02========================================================
//=============================================================================================
voidmDelay(uintt)//延时
{
uchari;
while(t--)
{
for(i=0;i<125;i++)
{;}
}
}
voidNop(void)//空操作
{
_nop_();
_nop_();
_nop_();
_nop_();
}
/*起始条件*/
voidStart(void)
{
Sda=1;
Scl=1;
Nop();
Sda=0;
Nop();
}
/*停止条件*/
voidStop(void)
{
Sda=0;
Scl=1;
Nop();
Sda=1;
Nop();
}
/*应答位*/
voidAck(void)
{
Sda=0;
Nop();
Scl=1;
Nop();
Scl=0;
}
/*反向应答位*/
voidNoAck(void)
{
Sda=1;
Nop();
Scl=1;
Nop();
Scl=0;
}
/*发送数据子程序,Data为要求发送的数据*/
voidSend(ucharData)
{
ucharBitCounter=8;
uchartemp;
do
{
temp=Data;
Scl=0;
Nop();
if((temp&0x80)==0x80)
Sda=1;
else
Sda=0;
Scl=1;
temp=Data<<1;
Data=temp;
BitCounter--;
}
while(BitCounter);
Scl=0;
}
/*读一字节的数据,并返回该字节值*/
ucharRead(void)
{
uchartemp=0;
uchartemp1=0;
ucharBitCounter=8;
Sda=1;
do{
Scl=0;
Nop();
Scl=1;
Nop();
if(Sda)
temp=temp|0x01;
else
temp=temp&0xfe;
if(BitCounter-1)
{
temp1=temp<<1;
temp=temp1;
}
BitCounter--;
}
while(BitCounter);
return(temp);
}
voidWrToROM(ucharData[],ucharAddress,ucharNum)
{
uchari;
uchar*PData;
PData=Data;
for(i=0;i{
Start();
Send(0xa0);
Ack();
Send(Address+i);
Ack();
Send(*(PData+i));
Ack();
Stop();
mDelay(20);
}
}
voidRdFromROM(ucharData[],ucharAddress,ucharNum)
{
uchari;
uchar*PData;
PData=Data;
for(i=0;i{
Start();
Send(0xa0);
Ack();
Send(Address+i);
Ack();
Start();
Send(0xa1);
Ack();
*(PData+i)=Read();
Scl=0;
NoAck();
Stop();
}
}
//==================================================================================================
//=======================================LCD1602====================================================
//==================================================================================================
#defineyi0x80//LCD第一行的初始位置,因为LCD1602字符地址首位D7恒定为1(100000000=80)
#defineer0x80+0x40//LCD第二行初始位置(因为第二行第一个字符位置地址是0x40)
//----------------延时函数,后面经常调用----------------------
voiddelay(uintxms)//延时函数,有参函数
{
uintx,y;
for(x=xms;x>0;x--)
for(y=110;y>0;y--);
}
//--------------------------写指令---------------------------
write_1602com(ucharcom)//****液晶写入指令函数****
{
lcd1602_rs=0;//数据/指令选择置为指令
lcd1602_rw=0;//读写选择置为写
P0=com;//送入数据
delay
(1);
lcd1602_en=1;//拉高使能端,为制造有效的下降沿做准备
delay
(1);
lcd1602_en=0;//en由高变低,产生下降沿,液晶执行命令
}
//-------------------------写数据-----------------------------
write_1602dat(uchardat)//***液晶写入数据函数****
{
lcd1602_rs=1;//数据/指令选择置为数据
lcd1602_rw=0;//读写选择置为写
P0=dat;//送入数据
delay
(1);
lcd1602_en=1;//en置高电平,为制造下降沿做准备
delay
(1);
lcd1602_en=0;//en由高变低,产生下降沿,液晶执行命令
}
//-------------------------初始化-------------------------
voidlcd_init(void)
{
write_1602com(0x38);//设置液晶工作模式,意思:
16*2行显示,5*7点阵,8位数据
write_1602com(0x0c);//开显示不显示光标
write_1602com(0x06);//整屏不移动,光标自动右移
write_1602com(0x01);//清显示
}
//========================================================================================
//=========================================================================================
//==============将按键值编码为数值=========================
unsignedcharcoding(unsignedcharm)
{
unsignedchark;
switch(m)
{
case(0x18):
k=1;break;
case(0x28):
k=2;break;
case(0x48):
k=3;break;
case(0x88):
k='A';break;
case(0x14):
k=4;break;
case(0x24):
k=5;break;
case(0x44):
k=6;break;
case(0x84):
k='B';break;
case(0x12):
k=7;break;
case(0x22):
k=8;break;
case(0x42):
k=9;break;
case(0x82):
k='C';break;
case(0x11):
k='*';break;
case(0x21):
k=0;break;
case(0x41):
k='#';break;
case(0x81):
k='D';break;
}
return(k);
}
//=====================按键检测并返回按键值===============================
unsignedcharkeynum(void)
{
unsignedcharrow,col,i;
P1=0xf0;
if((P1&0xf0)!
=0xf0)
{
Delay5Ms();
Delay5Ms();
if((P1&0xf0)!
=0xf0)
{
row=P1^0xf0;//确定行线
i=0;
P1=a[i];//精确定位
while(i<4)
{
if((P1&0xf0)!
=0xf0)
{
col=~(P1&0xff);//确定列线
break;//已定位后提前退出
}
else
{
i++;
P1=a[i];
}
}
}
else
{
return0;
}
while((P1&0xf0)!
=0xf0);
return(row|col);//行线与列线组合后返回
}
elsereturn0;//无键按下时返回0
}
//=======================一声提示音,表示有效输入========================
voidOneAlam(void)
{
ALAM=0;
Delay5Ms();
ALAM=1;
}
//========================二声提示音,表示操作成功========================
voidTwoAlam(void)
{
ALAM=0;
Delay5Ms();
ALAM=1;
Delay5Ms();
ALAM=0;
Delay5Ms();
ALAM=1;
}
//========================三声提示音,表示错误========================
voidThreeAlam(void)
{
ALAM=0;
Delay5Ms();
ALAM=1;
Delay5Ms();
ALAM=0;
Delay5Ms();
ALAM=1;
Delay5Ms();
ALAM=0;
Delay5Ms();
ALAM=1;
}
//=====================显示输入的N个数字,用H代替以便隐藏============================
voidDisplayOne(void)
{
//DisplayOneChar(9+N,1,'*');
write_1602com(yi+5+N);
write_1602dat('*');
}
//=======================显示提示输入=========================
voidDisplayChar(void)
{
unsignedchari;
if(pass==1)
{
//DisplayListChar(0,1,LockOpen);
write_1602com(er);
for(i=0;i<16;i++)
{
write_1602dat(LockOpen[i]);
}
}
else
{
if(N==0)
{
//DisplayListChar(0,1,Error);
write_1602com(er);
for(i=0;i<16;i++)
{
write_1602dat(Error[i]);
}
}
else
{
//DisplayListChar(0,1,start_line);
write_1602com(er);
for(i=0;i<16;i++)
{
write_1602dat(start_line[i]);
}
}
}
}
voidDisplayInput(void)
{
unsignedchari;
if(CorrectCont==1)
{
//DisplayListChar(0,0,Input);
write_1602com(er);
for(i=0;i<16;i++)
{
write_1602dat(Input[i]);
}
}
}
//========================重置密码==================================================
//==================================================================================
voidResetPassword(void)
{
unsignedchari;
unsignedcharj;
if(pass==0)
{
pass=0;
DisplayChar();
ThreeAlam();
}
else
{
if(ReInputEn==1)
{
if(N==6)
{
ReInputCont++;
if(ReInputCont==2)
{
for(i=0;i<6;)
{
if(TempPassword[i]==InputData[i])//将两次输入的新密码作对比
i++;
else
{
//DisplayListChar(0,1,Error);
write_1602com(er);
for(j=0;j<16;j++)
{
write_1602dat(Error[j]);
}
ThreeAlam();//错误提示
pass=0;
ReInputEn=0;//关闭重置功能,
ReInputCont=0;
DisplayChar();
break;
}
}
if(i==6)
{
//DisplayListChar(0,1,ResetOK);
write_1602com(er);
for(j=0;j<16;j++)
{
write_1602dat(ResetOK[j]);
}
TwoAlam();//操作成功提示
WrToROM(TempPassword,0,6);//将新密码写入24C02存储
ReInputEn=0;
}
ReInputCont=0;
CorrectCont=0;
}
else
{
OneAlam();
//DisplayListChar(0,1,again);//显示再次输入一次
write_1602com(er);
for(j=0;j<16;j++)
{
write_1602dat(again[j]);
}
for(i=0;i<6;i++)
{
TempPassword[i]=InputData[i];//将第一次输入的数据暂存起来
}
}
N=0;//输入数据位数计数器清零
}
}
}
}
//=======================输入密码错误超过三过,报警并锁死键盘======================
voidAlam_KeyUnable(void)
{
P1=0x00;
{
ALAM=~ALAM;
Delay5Ms();
}
}
//=======================取消所有操作============================================
voidCancel(void)
{
unsignedchari;
unsignedcharj;
//DisplayListChar(0,1,start_line);
write_160