基于51单片机密码锁课程设计报告Word格式.docx
《基于51单片机密码锁课程设计报告Word格式.docx》由会员分享,可在线阅读,更多相关《基于51单片机密码锁课程设计报告Word格式.docx(37页珍藏版)》请在冰豆网上搜索。
芯片工作电压
工作电流
字符尺寸
16*2个字符
4.5~5.5V
2.0mA(5v)
3.95*4.35mm
时刻显示输入输出的内容以及操作过程。
6.报警电路
当输入密码错误次数大于等于3次时,会发出声响报警。
7.开锁电路
四.软件设计
1.程序设计流程图
2.程序代码
#include<
REG52.h>
#include<
intrins.h>
#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};
//控盘扫描控制表
unsignedcharcodeb[]={"
0123456789"
};
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_();
/*起始条件*/
voidStart(void)
Sda=1;
Scl=1;
Nop();
Sda=0;
/*停止条件*/
voidStop(void)
/*应答位*/
voidAck(void)
Scl=0;
/*反向应答位*/
voidNoAck(void)
/*发送数据子程序,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);
/*读一字节的数据,并返回该字节值*/
ucharRead(void)
uchartemp=0;
uchartemp1=0;
do{
if(Sda)
temp=temp|0x01;
else
temp=temp&
0xfe;
if(BitCounter-1)
temp1=temp<
temp=temp1;
BitCounter--;
return(temp);
voidWrToROM(ucharData[],ucharAddress,ucharNum)
uchari;
uchar*PData;
PData=Data;
for(i=0;
Num;
Start();
Send(0xa0);
Ack();
Send(Address+i);
Send(*(PData+i));
Stop();
mDelay(20);
voidRdFromROM(ucharData[],ucharAddress,ucharNum)
Send(0xa1);
*(PData+i)=Read();
Scl=0;
NoAck();
//=======================================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>
y--);
//--------------------------写指令---------------------------
write_1602com(ucharcom)//****液晶写入指令函数****
lcd1602_rs=0;
//数据/指令选择置为指令......
lcd1602_rw=0;
//读写选择置为写
P0=com;
//送入数据
delay
(1);
lcd1602_en=1;
//拉高使能端,为制造有效的下降沿做准备
lcd1602_en=0;
//en由高变低,产生下降沿,液晶执行命令
//-------------------------写数据-----------------------------
write_1602dat(uchardat)//***液晶写入数据函数****
lcd1602_rs=1;
//数据/指令选择置为数据
P0=dat;
//en置高电平,为制造下降沿做准备
//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;
case(0x48):
k=3;
case(0x88):
k='
A'
;
case(0x14):
k=4;
case(0x24):
k=5;
case(0x44):
k=6;
case(0x84):
B'
case(0x12):
k=7;
case(0x22):
k=8;
case(0x42):
k=9;
case(0x82):
C'
case(0x11):
*'
case(0x21):
k=0;
case(0x41):
#'
case(0x81):
D'
return(k);
//=====================按键检测并返回按键值===============================
unsignedcharkeynum(void)
unsignedcharrow,col,i;
P1=0xf0;
if((P1&
0xf0)!
=0xf0)
Delay5Ms();
Delay5Ms();
if((P1&
{
row=P1^0xf0;
//确定行线
i=0;
P1=a[i];
//精确定位
while(i<
4)
{
if((P1&
{
col=~(P1&
0xff);
//确定列线
break;
//已定位后提前退出
}
else
i++;
P1=a[i];
}
}
return0;
while((P1&
=0xf0);
return(row|col);
//行线与列线组合后返回
elsereturn0;
//无键按下时返回0
//=======================一声提示音,表示有效输入========================
voidOneAlam(void)
ALAM=0;
Delay5Ms();
ALAM=1;
//========================二声提示音,表示操作成功========================
voidTwoAlam(void)
//========================三声提示音,表示错误========================
voidThreeAlam(void)
//=====================显示输入的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);
16;
write_1602dat(LockOpen[i]);
if(N==0)
//DisplayListChar(0,1,Error);
write_1602com(er);
for(i=0;
write_1602dat(Error[i]);
//DisplayListChar(0,1,start_line);
write_1602dat(start_line[i]);
voidDisplayInput(void)
if(CorrectCont==1)
//DisplayListChar(0,0,Input);
write_1602dat(Input[i]);
//========================重置密码==================================================
//==================================================================================
voidResetPassword(void)
unsignedcharj;
if(pass==0)
pass=0;
DisplayChar();
ThreeAlam();
if(ReInputEn==1)
if(N==6)
ReInputCont++;
if(ReInputCont==2)
for(i=0;
6;
)
{
if(TempPassword[i]==InputData[i])//将两次输入的新密码作对比
i++;
else
{
//DisplayListChar(0,1,Error);
write_1602com(er);
for(j=0;
j<
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;
write_1602dat(ResetOK[j]);
TwoAlam();
//操作成功提示
WrToROM(TempPassword,0,6);
//将新密码写入24C02存储
ReInputEn=0;
}
ReInputCont=0;
CorrectCont=0;
OneAlam();
//DisplayListChar(0,1,again);