随着电子产品向智能化和微型化的不断发展,单片机已成为电子产品研制和开发中首选的控制器。随着人们生活水平的提高,如何实现家庭防盗这一问题也变的尤其的突出,传统的机械锁由于其构造的简单,安全性能低,无法满足人们的需要。
本文从经济实用的角度出发,采用美国Atmel公司的单片机AT89S51与低功耗CMOS型E2PROM AT24C02作为主控芯片与数据存储器单元,结合外围的键盘输入、显示、报警、开锁等电路,用汇编语言编写主控芯片的控制程序,设计了一款可以多次更改密码具有报警功能的电子密码锁。
经实验证明,该密码锁具有设计方法合理,简单易行,成本低,安全实用等特点,符合住宅、办公室用锁要求,具有推广价值。
//包含头文件
#include <REG51.h>
#include<intrins.h>
//宏定义
#define LCM_Data P0 //将P0口定义为LCM_Data
#define uchar unsigned char
#define uint unsigned int
//1602的控制脚
sbit lcd1602_rs=P2^5;
sbit lcd1602_rw=P2^6;
sbit lcd1602_en=P2^7;
sbit Scl=P3^4; //24C02串行时钟
sbit Sda=P3^5; //24C02串行数据
sbit ALAM = P2^1; //报警
sbit KEY = P3^6; //开锁
bit pass=0; //密码正确标志
bit ReInputEn=0; //重置输入允许标志
bit s3_keydown=0; //3秒按键标志位
bit key_disable=0; //锁定键盘标志
unsigned char countt0,second; //t0中断计数器,秒计数器
void Delay5Ms(void); //声明延时函数
unsigned char code a[]={0xFE,0xFD,0xFB,0xF7}; //控盘扫描控制表
//液晶显示数据数组
unsigned char code start_line[] = {"Password: "};
unsigned char code name[] = {"===Coded Lock==="}; //显示名称
unsigned char code Correct[] = {" correct "}; //输入正确
unsigned char code Error[] = {" error "}; //输入错误
unsigned char code codepass[] = {" pass "};
unsigned char code LockOpen[] = {" open "}; //OPEN
unsigned char code SetNew[] = {"SetNewWordEnable"};
unsigned char code Input[] = {"Input: "}; //INPUT
unsigned char code ResetOK[] = {"ResetPasswordOK "};
unsigned char code initword[] = {"Init password..."};
unsigned char code Er_try[] = {"error,try again!"};
unsigned char code again[] = {"Input again "};
unsigned char InputData[6]; //输入密码暂存区
unsigned char CurrentPassword[6]={0,0,0,0,0,0}; //读取EEPROM密码暂存数组
unsigned char TempPassword[6];
unsigned char N=0; //密码输入位数记数
unsigned char ErrorCont; //错误次数计数
unsigned char CorrectCont; //正确输入计数
unsigned char ReInputCont; //重新输入计数
unsigned char code initpassword[6]={1,2,3,4,5,6}; //输入管理员密码后将密码初始为000000
unsigned char code adminpassword[6]={1,3,1,4,2,0}; //输入管理员密码后将密码初始为000000