所用模块含:EEPROM,LCD1602,矩阵按键,无源蜂鸣器,LED
单片机源程序如下:
- #include <reg52.h>
- #include <string.h>
- #include "eeprom.h"
- #include "iic.h"
- #include "keyboard.h"
- #include "lcd1602.h"
- #include "sound.h"
- sbit led2=P2^3; //连D2
- sbit led3=P2^2; //连D4
- typedef unsigned char uchar;
- typedef unsigned int uint;
- void Init_Timer1(void);
- uchar code password[]={'1','0','0','8','6'}; //注意要以字符形式存
- bit sign;
- #define Clear {length=0;password_length=0;num=0;i=0;input_times=0;flag=0;sign=0;led=1;}
-
- void main()
- {
- uchar length,password_length; //实际密码长度,输入密码长度
- uchar num,i;
- uchar password_temp[16]={0}; //输入密码
- uchar input_times; //记录输入错误次数
- bit flag;
-
- label:
- Clear
- length = sizeof(password) / sizeof(password[0]);
- EEPROM_Write(password, 1, length);
- EEPROM_Read(password, 1,length);
- LCD_Init();
- Init_Time0();
- LCD_Clear();
- LCD_Write_String(0,0,"LCD Cipher Lock");
- LCD_Write_String(0,1," --Yanglizidi ");
- while(1)
- {
- num=KeyDriver();
-
- if(num!=0xff) //如果扫描是按键有效值则进行处理
- {
- led2=1;
- if(i==0) //输入是第一个字符的时候需要把改行清空,方便观看密码
- {
- LCD_Clear();
- LCD_Write_String(0,0," Yanglizidi ");
- }
- if(i<16)
- {
- password_temp[i]=num;
- LCD_Write_Char(i,1,'*'); //输入的密码用"*"代替
- }
- i++;
- if( (num=='f') || (i==16) ) // 'f'为确认键,当按下确认键和密码长度达到16,则开始比对
- {
- password_length=i-1; // 输入的密码长度计算
- i=0;
- if(password_length == length)//长度相等则比较,否则直接输出错误
- {
- flag=0;
- if(strncmp(password_temp,password,length) == 0)
- {
- flag=1;
- }
- }
- if(flag)
- {
- LCD_Clear();
- LCD_Write_String(0,0," Yanglizidi ");
- LCD_Write_String(0,1,"Right-->Open!>>> ");
- input_times=0;
- led2=0;
- flag=0;
- }
- else
- {
- LCD_Clear();
- LCD_Write_String(0,0," Yanglizidi ");
- LCD_Write_String(0,1,"Wrong ! Retry ! ");
- input_times++;
- if(input_times==3)
- {
- LCD_Clear();
- LCD_Write_String(0,0," Wrong 3 times !");
- LCD_Write_String(0,1," Wait a minute !");
- Init_Timer1();
- while(1)
- {
- Sound_DIDA();
- if(sign==0)
- goto label;
- }
- }
- }
- }
- }
- }
- }
- void Init_Timer1()
- {
- TMOD |=0x10;
- TH1=0x4c;
- TL1=0x00; // 定时50ms
- ET1=1;
- sign=1;
- EA=1;
- TR1=1;
- led3=0;
- }
- void Timer1() interrupt 3
- {
- static uint k;
- TH1=0x4c;
- TL1=0x00; // 定时50ms
- k++;
- if(k==1200) //1 min
- {
- k=0;
- TR1=0;
- sign=0;
- led3=1;
- }
- }
复制代码
所有代码51hei附件下载:
LCD1602密码锁.7z
(37.76 KB, 下载次数: 26)
|