本帖最后由 好食欲 于 2015-6-3 11:20 编辑
求大神指导下错在那里了
#include<reg51.h>
#include <string.h>
#define uint unsigned int
#define uchar unsigned char
#define Key P1
sbit lcd1=P3^0; //lcd灯
sbit bump=P2^2;//fengmingqi蜂
sbit relay=P2^3;//jidianqi
sbit lcden=P2^7;//LCD1602液晶控制端
sbit lcdrs=P2^6; //
sbit lcdrw=P2^5;
sbit sda=P2^0; //AT24C02数据端口
sbit scl=P2^1; //时钟端口
uchar key_state=0;
#define lcd1602 P0
uchar keyValue;
uchar num=0,count;
uchar code xianshi0[]="input password:";
uchar code xianshi1[]="Please come in!";
uchar code xianshi2[]="*";
uchar code xianshi3[]="error!";
uchar code xianshi4[]="new password";
uchar code xianshi5[]="Change Password";
uchar code xianshi6[]="Re-enter";
uchar code xianshi7[]="you are a thief";
uchar code xianshi8[]="welcome";
uchar mima[]={0};
uchar table[]={0};
void delay(uchar x);
void key();
void writecom(uchar com);
void writedate(uchar dat);
void lcdinit();
void writebyte(uchar date);
uchar readbyte();
void write_add(uchar addr,uchar date);
uchar read_add(uchar addr);
void SetPassword();
void input();
void compare();
void display();
/************delay()***************/
void delay(uchar x)
{
uchar i,j;
for(i=x;i>0;i--)
for(j=199;j>0;j--);
}
void key()
{
uint a=0;
Key=0x7f;
if(Key!=0x7f)
{
delay(1);
if(Key!=0x7f)
{
key_state=1;
switch(Key)
{
case(0x77):keyValue=0x31;
break;
case(0x7b):keyValue=0x32;
break;
case(0x7d):keyValue=0x33;
break;
case(0x7e):keyValue=0x34;
break;
}
while(Key!=0x7f&&a<50)
{
delay(1);
a++;
}
}
}
a=0;
Key=0xbf;
if(Key!=0xbf)
{
delay(1);
if(Key!=0xbf)
{
key_state=1;
switch(Key)
{
case(0xb7):keyValue=0x35;
break;
case(0xbb):keyValue=0x36;
break;
case(0xbd):keyValue=0x37;
break;
case(0xbe):keyValue=0x38;
break;
}
while(Key!=0xbf&&a<50)
{
delay(1);
a++;
}
}
}
a=0;
Key=0xdf;
if(Key!=0xdf)
{
delay(1);
if(Key!=0xdf)
{
key_state=1;
switch(Key)
{
case(0xd7):keyValue=0x39;
break;
case(0xdb):keyValue=0x30;
break;
case(0xdd):keyValue=0x41;
break;
case(0xde):keyValue=0x42;
break;
}
while(Key!=0xdf&&a<50)
{
delay(1);
a++;
}
}
}
a=0;
Key=0xef;
if(Key!=0xef)
{
delay(1);
if(Key!=0xef)
{
switch(Key)
{
case(0xe7):keyValue=0x43;
break;
case(0xeb):keyValue=0x44;
break;
case(0xed):keyValue=0x45;
break;
case(0xee):keyValue=0x46;
break;
}
while(Key!=0xdf&&a<50)
{
delay(1);
a++;
}
}
}
}
/******************lcd1602********************/
void writecom(uchar com)
{
lcden=0;
lcdrs=0;
lcdrw=0;
lcd1602=com;
delay(1);
lcden=1;
delay(5);
lcden=0;
}
void writedate(uchar dat)
{
lcden=0;
lcdrs=1;
lcdrw=0;
lcd1602=dat;
delay(1);
lcden=1;
delay(5);
lcden=0;
}
void lcdinit()
{
writecom(0x38);
writecom(0x0f);
writecom(0x06);
writecom(0x01);
writecom(0x80);
}
/**************AT24C02***************/
void delayus()
{
uchar a;
for(a=2;a>0;a--);
}
void start()
{
sda=1;
scl=1;
delayus();
sda=0;
delayus();
scl=0;
delayus();
}
void stop()
{
sda=0;
delayus();
scl=1;
delayus();
sda=1;
delayus();
}
void respons()
{
uchar i=0;
scl=1;
delayus();
while((sda==1)&&(i<255))
i++;
scl=0;
delayus();
}
void writebyte(uchar date)
{
uchar i,temp;
temp=date;
for(i=0;i<8;i++)
{
temp=temp<<1;
scl=0;
delayus();
sda=CY;
delayus();
scl=1;
delayus();
}
scl=0;
delayus();
sda=1;
delayus();
}
uchar readbyte()
{
uchar i,temp;
scl=0;
delayus();
sda=1;
delayus();
for(i=0;i<8;i++)
{
scl=1;
temp=(temp)<<1|sda;
scl=0;
delayus();
}
delayus();
return temp;
}
void write_add(uchar addr,uchar date)
{
start();
writebyte(0xa0);
respons();
writebyte(addr);
respons();
writebyte(date);
respons();
stop();
}
uchar read_add(uchar addr)
{
uchar date;
start();
writebyte(0xa0);
respons();
writebyte(addr);
respons();
start();
writebyte(0xa1);
respons();
date=readbyte();
stop();
return date;
}
/*************************************/
void SetPassword()
{
if(keyValue==0x43)//chongxinshuru
{
uchar i=0,j;
key_state=0;
lcdinit();
for(j=0;j<15;j++)
{
writedate(xianshi5[j]);
}
for(i=0;i<7;i++)
{
table
=keyValue;
}
}
delay(100);
if(keyValue==0x42||(keyValue==0x41&&num!=7))//1shiqueren 2shiquxiao
{
uchar k,h;
lcdinit();
for(k=0;k<8;k++)
{
write_add(2*k,0);
}
delay(10);
for(h=0;h<9;h++)
{
writedate(xianshi6[h]);
}
delay(10000);
}
}
void input()
{
if(keyValue==0x44)//shurumima
{
uchar i;
key_state=0;
lcdinit();
for(i=0;i<16;i++)
{
writedate(xianshi0);
}
if(key_state==1)
{
key_state=0;
lcdinit();
for(num=0;num<8;num++)
{
writedate(xianshi2[num]);
mima[num]=keyValue;
}
delay(1000);
}
}
}
void compare()
{
if(keyValue==0x45)//querenmima
{
uchar i,c,j,k,h;
lcdinit();
for(num=0;num<8;num++)
{
if(mima[num]==table[num])
{
c++;
}
}
if(7==c)
{
c=0;
for(j=0;j<16;j++)
{
writedate(xianshi1[j]);
count=0;
}
lcd1=0;
delay(500);
lcd1=1;
for(h=0;h<2;h++) //蜂鸣器响两声提示开锁成功
{
bump=0; delay(200);
bump=1; delay(200);
bump=0; delay(200);
bump=1; delay(200);
}
}
else
{
for(k=0;k<6;k++)
{
writedate(xianshi3[k]);
}
delay(1000);
count++;
lcdinit();
for(i=0;i<16;i++)
{
writedate(xianshi0);
}
delay(1000);
}
}
if(count==3)
{
uint h,j;
count=0;
lcdinit();
for(h=0;h<16;h++)
{
writedate(xianshi7[h]);
}
delay(1000);
for(j=0;j<10;j++) //蜂鸣器响两声提示开锁成功
{
bump=0; delay(100);
bump=1; delay(100);
bump=0; delay(100);
bump=1; delay(100);
}
}
}
void display()
{
if(keyValue==0x43)
{
for(num=0;num<7;num++)
{
writedate(table[num]);
}
}
if(keyValue==0x44)
{
for(num=0;num<7;num++)
{
writedate(mima[num]);
}
}
}
void main()
{
uchar i;
lcdinit();
for(i=0;i<7;i++)
{
writedate(xianshi8);
}
while(1)
{
key();
switch(keyValue)
{
case 0x43:SetPassword();
break;
case 0x44:input();
break;
case 0x45:compare();
break;
}
display();
}
}
-
-
电子密码锁.txt
下载积分: 积分 -1 分
7.03 KB, 下载次数: 2, 下载积分: 积分 -1 分
首先给按键定义键值,然后读取按键情况,再显示到1602上面
一周热门 更多>