单片机实现电子密码锁功能
这个时我尝试实现论坛上的一个大佬文档的电子密码锁,希望将其的stc15芯片换成52单片机,所以修改了部分和删除了部分,但是为什么无法重现那个大佬的功能,问题是出在哪里?如果是都有错,请问我应该如何修改
- #include <reg52.h> //包含52头文件
- #include "intrins.h"
- #define uint unsigned int //宏定义uint代替unsigned int
- #define uchar unsigned char //宏定义uchar代替unsigned char
- #define out P0
- sbit lcdrs=P2^4;//液晶的RS端接P2.4口
- sbit lcdrw=P2^5;//液晶的RW端接P2.5
- sbit lcden=P2^6;// 液晶的EN使能端接P2.6口
- sbit buzzer =P1^0;//蜂鸣器
- uchar code keyvalue[]={0xee,0xde,0xbe,0x7e,0xed,0xdd,0xbd,0x7d,
- 0xeb,0xdb,0xbb,0x7b,0xe7,0xd7,0xb7,0x77};
- uint ver=0;
- void beep();
- void lcd_init();//初始化1602
- void write_data(uchar dat);
- void check_busy(void);
- void write_cmd(char cmd); //写指令函数
- void lcd_clear();
- void xianshi(void);//LCD显示函数
- void right1(void); //输入密码正确的显示函数
- void wrong1(void); //输入密码错误后的显示函数
- void verify(); //验证密码
- void copy(); //复制密码
- void input();//输入密码
- void reset();//重置密码
- uchar keyscan(void);
- uchar RIGHT[]="pass";
- uchar WRONG[]="Password Error";
- uchar SHURU[]="aaaaaaaaaa";
- uchar SHUZU[]="aaaaaaaaaa";
- uchar MIMA[]="000aaaaaaa";
- void delayms (uint xms) //毫秒函数定义
- {
- uint i,j;
- for(i=0;i<xms;i++)
- for(j=0;j<120;j++);
- }
- void main()
- { lcd_init();
- lcd_clear();
- while(1)
- { P1=0x0f;
-
- if((P1&0x0f)!=0x0f)
- { delayms(15);
- if((P1&0x0f)!=0x0f)
- { lcd_clear();
- copy(); //先对显示的数组进行赋值,以免出现显示错误
- input(); //输入密码
- if(keyscan()==12)
- lcd_clear();
- copy();
- input(); //若按下重置键,则重新输入
- if(keyscan()==13)
- { verify(); //按下确认键,对密码进行验证
- if(ver==0)
- {lcd_clear();
- right1(); //密码正确
- }
- else
- { lcd_clear();
- copy();
- wrong1(); //密码错误
- }
- }
- if(keyscan()==15)
- {
- lcd_clear();
- copy();
- verify(); //按下重置密码键之后,先确认之前输入的密码是否正确
- if(ver==0)
- reset(); //正确则重置密码
- else
- {lcd_clear();
- wrong1();
- }
- }
- P1=0xf0;
- while((P1&0xf0)!=0xf0)
- { P1=0xf0;
- beep();
- }
- }
- }
- }
-
- }
- uchar keyscan(void)
- {
- uchar j,temp,i;
- uchar t=0xfe;
- P1=0xf0;
- if((P1&0xf0)!=0xf0)
- {
- for(i=0;i<4;i++)
- { P1=t;
- temp=P1;
- for(j=0;j<16;j++)
- { if(temp==keyvalue[j]) return(j);
- }
- t=_crol_(t,1);
- }
-
- }
- return(16);
- }
- void beep(void)
- {
- uchar i;
- for(i=0;i<10;i++)
- {
- delayms(50);
- buzzer=~buzzer;
- }
- buzzer=1;
- }
- /**********************检查忙标志函数************************/
- void check_busy(void)
- {
- uchar dt;
- out=0xff;
- do
- {
- lcden=0;
- lcdrs=0;
- lcdrw=1;
- lcden=1;
- dt=out;
- }while(dt&0x80);
- lcden=0;
- }
- /**************************1602显示****************************/
- void write_cmd(char cmd) //写指令函数
- {
- check_busy();
- lcden=0;//再把EN拉低
- lcdrs=0;
- lcdrw=0;//先将RW拉低
- out=cmd;
- lcden=1;
- lcden=0;
- }
- void write_data(uchar dat) //写数据函数
- {
- check_busy();
- lcden=0;//再把EN拉低
- lcdrs=1;
- lcdrw=0;//先将RW拉低
- out=dat;
- lcden=1;
- lcden=0;
- }
- /***************************初始化******************************/
- void lcd_init()//初始化1602
- {
- write_cmd(0x38);//显示模式设置
- write_cmd(0x0c);//显示开关,光标没有闪烁
- write_cmd(0x06);//显示光标移动设置
- write_cmd(0x01);//清除屏幕
- delayms(1);
- }
- void lcd_clear()
- { write_cmd(0x01);
- delayms(5);
- }
- void input()
- {
- uchar i=0,key;
- for(;i<10;)
- { P1=0xf0;
- if((P1&0xf0)!=0xf0)
- { delayms(15);
- if((P1&0xf0)!=0xf0)
- { if (keyscan()>=10) break; //确认有键按下后,如果按下的不是数字键0-9,则跳出循环
- key=keyscan();
- SHURU[i]=key+'0'; //将输入的数字转化为字符变量存入‘SHURU’和‘SHUZU'两个数组中
- SHUZU[i]=SHURU[i];
- SHURU[i+1]='a'; //对"SHURU"数组的下一位赋值为a,避免在验证密码时出错
- xianshi(); //显示函数
- i++;
-
- P1=0xf0;
- while((P1&0xf0)!=0xf0)
- { P1=0xf0;
- beep();
- }
- }
- }
-
- }
-
- }
- void xianshi(void)
- { uchar m;
- uchar n=0;
- for (m=0x80;m<0x90;m++)
- {
- write_cmd(m);
- if(SHUZU[n]=='a'|| SHUZU[n]=='\0') break;
- write_data(SHUZU[n]);
- n++;
- }
- }
- void right1(void)
- { uchar m;
- uchar n=0;
- for (m=0x80;m<0x90;m++)
- {
- write_cmd(m);
- if(RIGHT[n]=='\0') break;
- write_data(RIGHT[n]);
- n++;
- }
- }
- void wrong1(void)
- { uchar m;
- uchar n=0;
- for (m=0x80;m<0x90;m++)
- {
- write_cmd(m);
- if(WRONG[n]=='\0') break;
- write_data(WRONG[n]);
- n++;
- }
- }
- void verify()
- { uchar i;
- ver=0;
- for(i=0;MIMA[i]!='*' && MIMA[i]!='\0';i++)
- { if(SHURU[i]!=MIMA[i] || SHURU[i+1]!=MIMA[i+1]) //验证密码是否正确
- {ver=1;
- break;
- }
- }
- }
- void copy()
- { uchar i=0;
- for(;i<10;i++)
- { SHUZU[i]='*';
- }
- }
- void reset()
- { uchar i,key;
- for(i=0;i<10;)
- { P1=0xf0;
- if((P1&0xf0)!=0xf0)
- { delayms(15);
- if((P1&0xf0)!=0xf0 )
- { if(keyscan()!=15)
- {
- if (keyscan()==13) break; //再确认要重置密码后,把输入的新密码存入'SHUZU'数组中,若按下确认键则跳出
- key=keyscan();
- SHUZU[i]=key+'0';
- xianshi();
- i++;
- }
- P1=0xf0;
- while((P1&0xf0)!=0xf0)
- { P1=0xf0;
- beep();
- }
- }
- }
-
- }
- for(i=0;i<9;i++)
- {MIMA[i]=SHUZU[i];//将密码存入“MIMA”数组中
- }
- lcd_clear();
- copy();
- }
复制代码
|