51单片机密码锁程序

合集下载

简易电子密码锁(基于51单片机)报告

简易电子密码锁(基于51单片机)报告

简易电子密码锁设计&我的设计思想联想到日前在安全技术防范领域,具有防盗报警功能的电子密码控制系统逐渐代替传统的机械式密码控制系统,并结合近期的学习过程和一些参考书籍,完成了简易的电子密码锁设计学习。

电子密码控制是一种通过密码输入来控制电路或是芯片工作,从而控制机械开关的闭合,完成开锁、闭锁任务的电子产品。

电子密码控制不论性能还是安全性都已大大超过了机械类结,具有良好的应用前景。

一、设计目的与内容设计了一个简易电子密码锁,可按要求从矩阵键盘输入6位数密码如“080874”,输入过程中有按键音提示。

当密码输入正确并按下确认键(“OK”键)后,发光二极管被点亮。

二、工作原理与基本操作过程介绍采用80C51为核心的单片机控制。

利用单片机灵活的编程设计和丰富的IO端口,及其控制的准确性,进行电子密码锁的设计。

(1)键盘的人工编码给每个按键指定一个按键值,报告设定按键S1~S9对应的按键值分别为“1~9”,S10为数字“0”,S11为“OK”,S12~S16对应的按键值分别为12~16。

(2)根据按键值,指定每个按键对应的输入数字和信息。

如下表为每个按键代表的数字和输入信息。

当键盘扫描程序扫描到S10键被按下时,将其代表的按键值“0”通知CPU,CPU根据事先的规定,就会知道输入的数字是“0”。

矩阵键盘中每个按键所代表的数字和输入信息(3)输入数字和密码对比。

先将设定的密码用一个数组保存,报告中用的密码“080874”和“OK”确认信息可以用如下数组保存:Unsigned char D[ ]={0,8,0,8,7,4,11};在主程序接收到数字和信息后,通过逐位对比的方法进行判断。

输入的数字经对比正确时,程序才会继续顺序执行,否则,程序拒绝继续执行。

(4)执行预期功能。

如果输入密码正确,执行预期功能,报告设计为点亮P3.0口引脚LED。

三、电路图设计(Proteus绘制)四、程序设计(C语言)矩阵式键盘实现的电子密码锁程序#include<reg51.h> //包含51单片机寄存器定义的头文件sbit P14=P1^4; //将P14位定义为P1.4引脚sbit P15=P1^5; //将P15位定义为P1.5引脚sbit P16=P1^6; //将P16位定义为P1.6引脚sbit P17=P1^7; //将P17位定义为P1.7引脚sbit sound=P3^7; //将sound位定义为P3.7unsigned char keyval; //储存按键值/************************************************************** 函数功能:延时输出音频**************************************************************/ void delay(void){unsigned char i;for(i=0;i<200;i++);}/************************************************************** 函数功能:软件延时子程序**************************************************************/ void delay20ms(void){unsigned char i,j;for(i=0;i<100;i++)for(j=0;j<60;j++);}/************************************************************** 函数功能:主函数**************************************************************/ void main(void){unsigned char D[ ]={0,8,0,8,7,4,11}; //设定密码EA=1; //开总中断ET0=1; //定时器T0中断允许TMOD=0x01; //使用定时器T0的模式1TH0=(65536-500)/256; //定时器T0的高8位赋初值TL0=(65536-500)%256; //定时器T0的高8位赋初值TR0=1; //启动定时器T0keyval=0xff; //按键值初始化while(keyval!=D[0]) //第一位密码输入不正确,等待;while(keyval!=D[1]) //第二位密码输入不正确,等待;while(keyval!=D[2]) //第三位密码输入不正确,等待;while(keyval!=D[3]) //第四位密码输入不正确,等待;while(keyval!=D[4]) //第五位密码输入不正确,等待;while(keyval!=D[5]) //第六位密码输入不正确,等待;while(keyval!=D[6]) //没有输入“OK”,等待;P3=0xfe; //P3.0引脚输出低电平,点亮LED}/**************************************************************函数功能:定时器0的中断服务子程序,进行键盘扫描,判断键位**************************************************************/void time0_interserve(void) interrupt 1 using 1 //定时器T0的中断编号为1,使用第一组寄存器{unsigned char i;TR0=0; //关闭定时器T0P1=0xf0; //所有行线置为低电平“0”,所有列线置为高电平“1”if((P1&0xf0)!=0xf0) //列线中有一位为低电平“0”,说明有键按下delay20ms(); //延时一段时间、软件消抖if((P1&0xf0)!=0xf0) //确实有键按下{P1=0xfe; //第一行置为低电平“0”(P1.0输出低电平“0”)if(P14==0) //如果检测到接P1.4引脚的列线为低电平“0”keyval=1; //可判断是S1键被按下if(P15==0) //如果检测到接P1.5引脚的列线为低电平“0”keyval=2; //可判断是S2键被按下if(P16==0) //如果检测到接P1.6引脚的列线为低电平“0”keyval=3; //可判断是S3键被按下if(P17==0) //如果检测到接P1.7引脚的列线为低电平“0”keyval=4; //可判断是S4键被按下P1=0xfd; //第二行置为低电平“0”(P1.1输出低电平“0”)if(P14==0) //如果检测到接P1.4引脚的列线为低电平“0”keyval=5; //可判断是S5键被按下if(P15==0) //如果检测到接P1.5引脚的列线为低电平“0”keyval=6; //可判断是S6键被按下if(P16==0) //如果检测到接P1.6引脚的列线为低电平“0”keyval=7; //可判断是S7键被按下if(P17==0) //如果检测到接P1.7引脚的列线为低电平“0”keyval=8; //可判断是S8键被按下P1=0xfb; //第三行置为低电平“0”(P1.2输出低电平“0”)if(P14==0) //如果检测到接P1.4引脚的列线为低电平“0”keyval=9; //可判断是S9键被按下if(P15==0) //如果检测到接P1.5引脚的列线为低电平“0”keyval=0; //可判断是S10键被按下if(P16==0) //如果检测到接P1.6引脚的列线为低电平“0”keyval=11; //可判断是S11键被按下if(P17==0) //如果检测到接P1.7引脚的列线为低电平“0”keyval=12; //可判断是S12键被按下P1=0xf7; //第四行置为低电平“0”(P1.3输出低电平“0”)if(P14==0) //如果检测到接P1.4引脚的列线为低电平“0”keyval=13; //可判断是S13键被按下if(P15==0) //如果检测到接P1.5引脚的列线为低电平“0”keyval=14; //可判断是S14键被按下if(P16==0) //如果检测到接P1.6引脚的列线为低电平“0”keyval=15; //可判断是S15键被按下if(P17==0) //如果检测到接P1.7引脚的列线为低电平“0”keyval=16; //可判断是S16键被按下for(i=0;i<200;i++) //让P3.7引脚电平不断取反输出音频{sound=0;delay();sound=1;delay();}}TR0=1; //开启定时器T0TH0=(65536-500)/256; //定时器T0的高8位赋初值TL0=(65536-500)%256; //定时器T0的高8位赋初值}五、用Proteus软件进行仿真利用Keil软件进行编译通过后,生成hex文件。

51单片机简易密码锁

51单片机简易密码锁

51单片机简易密码锁学号:201114040215HEBEI UNITED UNIVERSITY单片机课程设计说明书设计题目:简易电子密码锁学生姓名:李红辉专业班级:测控技术及仪器2学院:电气工程学院指导教师:曹晓华2014年06月05日成绩评定表AbstractIn daily life and work, the department of housing and security, unit documents, financial statements and some personal information to save more in order to lock the ways to solve. If use the traditional mechanical key to open the lock, people often need to carry multiple keys, use very convenient, and the key missing after security is compromised. With the continuous development of science and technology, people in daily life the demand is higher and higher safety insurance device. To meet the requirements of people on the use of the lock, increase its safety, use the password instead of the key combination lock arises at the historic moment. Combination lock with high safety, low cost, low power consumption, easy operation, etc.In the field of security technology to guard against, with electronic combination lock anti-theft alarm function gradually replace the traditional mechanical combination lock, overcoming the mechanical combination lock password quantity is little, the shortcomings of poor safety performance, make the combination lock both in technology and step in performance are improved greatly. With the development of large scale integrated circuit technology, especially the single chip microcomputer, appeared with the intelligent combination of the microprocessor, it besides has the function of electronic combination lock, also introduced the intelligent management, expert analysis system, and other functions, so that the combination lock of high security, reliability, and increasingly widely used.The course design of electronic combination lock based on MCU is introduced, the design of the hardware is mainly composed of AT89C52 minimum system, matrix circuit, 1602 LCD display circuit, power circuit and alarm circuit and so on several parts. Software is mainly composed of C language programming. The combination lock by the single chip microcomputer technology, through the matrix circuit setting, change passwords, passwordprotection, and by the LCD display password input, so as to realize the password Settings, protection.Key words: single chip microcomputer, trick lock, the 1602, the smallest system, matrix keyboard目录摘要 ............................................................................................ 错误!未定义书签。

基于51单片机的电子密码锁—1

基于51单片机的电子密码锁—1

基于51单⽚机的电⼦密码锁—1这个程序是为了实现基于51单⽚机的电⼦密码锁,⽬前,程序解决了最重要之⼀的输⼊的密码和保存的正确密码相⽐较的问题。

通过定义了两个数组:uchar table2[6]; //临时密码保存uchar password[6]="123456"; //进门密码将输⼊的密码写⼊到table2[]中有⼀点需要特别注意:因为我写到table2[]数组内的是ASCII值的0-9,⽽ASCII值的0-9对应的符号却是NUT,SOH... (省略)所以在刚开始调试时,LCD1602屏幕输出的总是奇怪的字符,⽽不是我想要的0-9,通过查询ASCLL码表可以知道字符(0-9)对应的数值是48-57,所以我通过定义了⼀个新的数组,uchar smgduan[10]={48,49,50,51,52,53,54,55,56,57};以及lcd_write_data(smgduan[table2[i]]);的⽅式,实现了在LCD上输出字符0-9的功能。

在最后做两个数组⽐较时,开始同样出现了这个情况,因为数字1和字符1对应的ASCII值不同,所以password[i]不等于table2[i],需要进⾏转换,我的⽅式的是password[i]==smgduan[table2[i]];罗⾥吧嗦这个多,主要还是给未来的⾃⼰看看,当初犯得错误多么低级。

---------------------------------------------------分割线-----------------------------------------------------------------------------------下⼀版改进考虑把重复按键选择数字改成矩阵按键,加进些其他的功能。

---------------------------------------------------分割线-----------------------------------------------------------------------------------程序部分:/*这个⽅案是我写基于51单⽚机的电⼦密码锁过程中,未完成全部功能的程序。

51单片机简易密码锁

51单片机简易密码锁

学号:单片机课程设计说明书设计题目:简易电子密码锁成绩评定表摘要在日常的生活和工作中, 住宅与部门的安全防范、单位的文件档案、财务报表以及一些个人资料的保存多以加锁的办法来解决。

若使用传统的机械式钥匙开锁,人们常需携带多把钥匙, 使用极不方便, 且钥匙丢失后安全性即大打折扣。

随着科学技术的不断发展,人们对日常生活中的安全保险器件的要求越来越高。

为满足人们对锁的使用要求,增加其安全性,用密码代替钥匙的密码锁应运而生。

密码锁具有安全性高、成本低、功耗低、易操作等优点。

在安全技术防范领域,具有防盗报警功能的电子密码锁逐渐代替传统的机械式密码锁,克服了机械式密码锁密码量少、安全性能差的缺点,使密码锁无论在技术上还是在性能上都大大提高一步。

随着大规模集成电路技术的发展,特别是单片机的问世,出现了带微处理器的智能密码锁,它除具有电子密码锁的功能外,还引入了智能化管理、专家分析系统等功能,从而使密码锁具有很高的安全性、可靠性,应用日益广泛。

本课程设计介绍了基于单片机电子密码锁的设计,该设计硬件主要由AT89C52最小系统、矩阵电路、1602LCD显示电路、电源电路和报警电路等几部分组成。

软件主要由C语言编程。

该密码锁由单片机技术,通过矩阵电路设置、修改密码、保护密码,并由LCD显示密码输入,从而实现对密码的设置、保护。

关键词:单片机,密码锁, 1602,最小系统,矩阵键盘AbstractIn daily life and work, the department of housing and security, unit documents, financial statements and some personal information to save more in order to lock the ways to solve. If use the traditional mechanical key to open the lock, people often need to carry multiple keys, use very convenient, and the key missing after security is compromised. With the continuous development of science and technology, people in daily life the demand is higher and higher safety insurance device. To meet the requirements of people on the use of the lock, increase its safety, use the password instead of the key combination lock arises at the historic moment. Combination lock with high safety, low cost, low power consumption, easy operation, etc.In the field of security technology to guard against, with electronic combination lock anti-theft alarm function gradually replace the traditional mechanical combination lock, overcoming the mechanical combination lock password quantity is little, the shortcomings of poor safety performance, make the combination lock both in technology and step in performance are improved greatly. With the development of large scale integrated circuit technology, especially the single chip microcomputer, appeared with the intelligent combination of the microprocessor, it besides has the function of electronic combination lock, also introduced the intelligent management, expert analysis system, and other functions, so that the combination lock of high security, reliability, and increasingly widely used.The course design of electronic combination lock based on MCU is introduced, the design of the hardware is mainly composed of AT89C52 minimum system, matrix circuit, 1602 LCD display circuit, power circuit and alarm circuit and so on several parts. Software is mainly composed of C language programming. The combination lock by the single chip microcomputer technology, through the matrix circuit setting, change passwords, password protection, and by the LCD display password input, so as to realize the password Settings, protection.Key words: single chip microcomputer, trick lock, the 1602, the smallest system, matrix keyboard目录摘要 ......................................................................................................................... I II ABSTRACT .. (4)第1章绪论 (7)1.1电子密码锁的现状与发展 (7)1.1.1电子密码锁的现状 (7)1.1.2电子密码锁的发展 (7)1.2电子密码锁的特色 (7)第2章电子密码锁的工作原理及总体设计 (8)2.1电子密码锁的工作原理 (8)2.2总体结构的设计 (8)第3章硬件系统设计 ............................................................... 错误!未定义书签。

AT89C51单片机电子密码锁

AT89C51单片机电子密码锁

AT89C51单片机电子密码锁#include"main.h"/******************** LCD PART START *******************************///5ms延时void Delay5Ms(void){unsigned int TempCyc = 5552;while(TempCyc--);}//读状态unsigned char ReadStatusLCM(void){LCM_Data = 0xFF;LCM_RS = 0;LCM_RW = 1;LCM_E = 0;LCM_E = 0;LCM_E = 1;Delay5Ms();while (LCM_Data & Busy); //检测忙信号return(LCM_Data);}//写数据void WriteDataLCM(unsigned char WDLCM){ReadStatusLCM(); //检测忙LCM_Data = WDLCM;LCM_RS = 1;LCM_RW = 0;LCM_E = 0; //若晶振速度太高能够在这后加小的延时LCM_E = 0; //延时LCM_E = 1;}//写指令void WriteCommandLCM(unsigned char WCLCM,BuysC) //BuysC为0时忽略忙检测{if (BuysC) ReadStatusLCM(); //根据需要检测忙LCM_Data = WCLCM;LCM_RS = 0;LCM_RW = 0;LCM_E = 0;LCM_E = 0;LCM_E = 1;}//读数据unsigned char ReadDataLCM(void){LCM_RS = 1;LCM_RW = 1;LCM_E = 0;LCM_E = 0;LCM_E = 1;return(LCM_Data);}void LCMInit(void) //LCM初始化{LCM_Data = 0;WriteCommandLCM(0x38,0); //三次显示模式设置,不检测忙信号Delay5Ms();WriteCommandLCM(0x38,0);Delay5Ms();WriteCommandLCM(0x38,0);Delay5Ms();WriteCommandLCM(0x38,1); //显示模式设置,开始要求每次检测忙信号WriteCommandLCM(0x08,1); //关闭显示WriteCommandLCM(0x01,1); //显示清屏WriteCommandLCM(0x06,1); // 显示光标移动设置WriteCommandLCM(0x0C,1); // 显示开及光标设置}//按指定位置显示一个字符,x表示列,Y表示行void LCD_write_char(unsigned char X,unsigned char Y, unsigned char DData){Y &= 0x1;X &= 0xF; //限制X不能大于15,Y不能大于1if (Y)X |= 0x40; //当要显示第二行时地址码+0x40;X |= 0x80; // 算出指令码WriteCommandLCM(X, 0); //这里不检测忙信号,发送地址码WriteDataLCM(DData);}//按指定位置显示一串字符void LCD_write_string(unsigned char X,unsigned char Y, unsigned char code *DData){unsigned char ListLength;ListLength = 0;Y &= 0x1;X &= 0xF; //限制X不能大于15,Y不能大于1while (*DData) //若到达字串尾则退出'\0'就是0{if (X <= 0xF) //X坐标应小于0xF{LCD_write_char(X,Y, *DData); //显示单个字符DData++;X++;}}}/************/#include<reg52.h>#include<math.h>#define uchar unsigned char#define uint unsigned int/******************** LCD PART START *******************************/void delay(uint z) //延时{uint x,y;for(x=z;x>0;x--)for(y=124;y>0;y--);}sbit e=P2^5;sbit rw=P2^6;sbit sr=P2^7;uchar code name[]="zxs";uchar code name1[]="zcf";uchar code name2[]="zx";void write_com(uchar com){int rs;rs=0;P0=com;delay(5);e=1;delay(5);e=0;}void write_data(uchar date){int rs;rs=1;P0=date;delay(5);e=1;delay(5);e=0;}void init(){e=0;rw=0;write_com(0x38);write_com(0x0c); write_com(0x06);write_com(0x01);write_com(0x80);}void display(){uchar i;for(i=0;i<3;i++){write_data(name[i]);}write_com(0x80+0x40);for(i=0;i<3;i++){write_data(name1[i]);}}void main(){init();display();}#include <AT89X51.h>#define uchar unsigned charuchar starbuf[10];uchar wordbuf[8];uchar pw[8]={1,2,3,4,5,6,7,8};uchar pwbuf[8];uchar count=0;// 初始没有输入密码,计数器设为0 uchar inputflag=0;// 先处于密码输入状态,非密码修改状态bit enterflag=0; // 没有按下确认键bit pwflag=0;// 密码标志先置为0sbit warn=P3^6;#define lcd_data P0sbit rs=P2^7;sbit rw=P2^6;sbit e=P2^5;///////////////////////LCD1602驱动程序///////////////////////void delay_1602(unsigned int i){while(i--);}void enrw(){rs=0;rw=0;e=0;delay_1602(250);e=1;}write_data(uchar c){lcd_data=c;rs=1;rw=0;e=0;delay_1602(250);e=1;}init_lcd(void)//初始化{lcd_data=0x01;//清屏幕enrw();lcd_data=0x38;//数据长度为8位,双行显示,5*7字符。

毕业设计-基于51单片机电子密码锁设计

毕业设计-基于51单片机电子密码锁设计

基于51单片机电子锁设计摘要随着科技和人们的生活水平的提高,如何实现家庭防盗这一问题也变的尤其的突出,传统机械锁由于构造简单,被撬事件屡见不鲜;电子锁由于其保密性高,使用灵活性好,安全系数高,受到了广大用户的青睐。

本设计以单片机AT89C51作为密码锁监控装置的检测和控制核心,分为主机控制和从机执行机构(本设重点介绍主机设计),实现钥匙信息在主机上的初步认证注册、密码信息的加密、钥匙丢失报废等功能。

根据51单片机之间的串行通信原理,这便于对密码信息的随机加密和保护。

而且采用键盘输入的电子密码锁具有较高的优势。

采用数字信号编码和二次调制方式,不仅可以实现多路信息的控制,提高信号传输的抗干扰性,减少错误动作,而且功率消耗低;反应速度快、传输效率高、工作稳定可靠等。

软件设计采用自上而下的模块化设计思想,以使系统朝着分布式、小型化方向发展,增强系统的可扩展性和运行的稳定性。

测试结果表明,本系统各项功能已达到本设计的所有要求。

关键词:单片机;密码锁;单片机设计,电子锁。

Electronic Lock Design with 51 Serires Single Chip ControllerAbstractAlong with the exaltation of social science and the living level of people, how carry out the family to guard against theft, this problem also change particularly outstanding.Because of the simple construct of traditional machine lock,the affairs of theft is hackneyed.the electronics lock is safer because of its confidentiality, using the vivid good, the safe coefficient is high, being subjected to the large customer close.It can carry out the key information to register in the main on board initial attestation, the password information encrypt etc. Go to correspond by letter the principle according to the string between 51 machines, this is easy to encrypt and protect to the passwords information random. Adopt the numerical signal codes,not only can carry out many controls of the road information, raise the anti- interference that signal deliver, reduce the mistake action,but also the power consume is low, Respond quickly,the efficiency deliver is high, work stable credibility etc. The software design adoption the design thought from top to bottom, to make the system toward wear distribute type,turn to the direction development of small, strengthen the system and can expand the stability and circulate.Test the result enunciation, various functions of this system are already all request of this design.keyword:singlechip;cryptogram lock;singlechip design; electronics lock.目录1 绪论 (1)1.1 引言 (1)1.2 电子密码锁的背景 (1)1.3 电子锁设计的意义的本设计特点 (2)2.系统设计 (3)2.1系统总设计结构图 (3)2.2.开锁机构设计 (3)2.2.1主控芯片AT89C51单片机的简介 (4)3系统硬件设计 (6)3.1键盘设计 (6)3.2系统电路设计: (8)3.2.1 晶振时钟电路 (8)3.2.2复位电路设计 (8)3.2.3串口引脚功能介绍 (8)3.2.4 其它引脚 (9)3.3电路图的绘制 (9)3.3.1 PROTEL 99 SE简介: (12)3.4原器件采购 (14)3.5电路焊接 (14)4.软件设计 (17)4.1 系统软件设计整体思路 (17)4.2系统软件设计流程图 (18)5 程序调试 (19)5.1 程序调试用到的软件及工具 (19)5.2 KEIL C51简介 (19)5.3 调试过程 (19)6 设计总结与展望 (22)致谢 (23)参考文献 (24)附录 (25)1 绪论1.1 引言随着人们生活水平的提高,如何实现家庭防盗这一问题也变的尤其的突出,传统的机械锁由于其构造的简单,被撬的事件屡见不鲜,电子锁由于其保密性高,使用灵活性好,安全系数高,受到了广大用户的喜爱。

51单片机电子密码锁报告

51单片机电子密码锁报告

信息工程学院51单片机的密码锁控制器的设计实验报告专业:电气工程及其自动化班级:10040921基于51单片机的密码锁控制器设计一、设计目的:要求设计的电子密码锁的密码用键盘上的数字按键产生的6位数字码构成的密码。

如果输入密码正确开锁(发光二极管量),如果密码不正确,发出报警信号。

二、实验要求:1、显示位数:6位密码显示2、键盘设置密码三、设计方案:本设计包括矩阵键盘接口电路、密码锁的控制电路和输出显示电路等三部分。

键盘部分包括键盘扫描时序产生电路;键盘扫描;弹跳消除;键盘译码;按键存储。

程序控制包括数字按键的数字输入;存储及清除;功能按键的功能设计;移位寄存器的设计与控制;密码清除、变更、存储;激活开锁电路;密码核对;解除电锁电路。

输出显示电路的设计包括:数据选择;BCD对显示译码;七段显示扫描。

(1)密码数据输入:每按一个数字键,在显示器上显示一个“-”最多可设置6位密码。

(2)密码设置:每按一个数字键,就输入一个数值,并在显示器上的最右方显示出该数值,并将先前已经输入的数据依序左移一个数字位置。

注意:密码设置必须是在开锁状态下设置。

(3)数码清除:按下此键可清除前面所有的输入值,清除成为“000000”。

(4)密码更改:按下此键时将目前的数字设定成新的密码。

(5)激活电锁:按下此键可将密码锁上锁。

(6)解除电锁:按下此键会检查输入的密码是否正确,密码正确即开锁。

(7)密码错误:声光报警四、实验电路及连线:1、实验接线2、LED电平显示电路实验仪上装有8只发光二极管及相应驱动电路。

见下图,L0―L7为相应发光二极管驱动信号输入端,该输入端为高电压电平“1”时发光二极管点亮。

我们可以通过P1口对其直接进行控制,点亮或者熄灭发光二极管。

LED电平显示电路3、键盘及LED显示电路键盘和LED显示的地址译码见下图,做键盘和LED实验时,需要将KEY/LED CS接到相应的地址译码上。

位码输出的地址为0X002H,段码输出的地址为0X004H,键盘行码读回的地址为0X001H,此处X是由KEY/LED CS决定,参见地址译码。

基于51单片机多功能电子密码锁

基于51单片机多功能电子密码锁

#include <reg52.h>#define uint unsigned int#define uchar unsigned char#define KEY P3 //键盘输入端口#define No_key 20 //无按键时的返回值#define lcddata P2 //1602的数据输入端口sbit lcden= P1^2。

sbit lcdrs= P1^0。

sbit lcdrw= P1^1。

sbit light= P1^3。

sbit light1= P1^4。

uchar j 。

//用来统计输入个数的全局变量uchar aa。

//用来在定时器中计数的全局变量uchar code table[]= " Hello!"。

uchar code table1[]=" OK! " 。

uchar code table2[]="Enter please:" 。

uchar code key_table[16] ={1,2,3,10,4,5,6,11,7,8,9,12,0,13,14,15}。

uchar password[]={2,0,1,0,9,3} 。

//设定初始密码uchar save[6]。

//保存输入的数据uchar conflag 。

//确认标志uchar lockflag。

//锁键盘标志uchar startflag。

//开始标志void delay(uint z>。

//延时子函数void wright_com(uchar com>。

//写指令函数void wright_data(uchar date> 。

//写数据函数void init(>。

//初始化void display_OK(>。

// 显示OKvoid delete(>。

//删除输入的最后一个数uchar keyscan(> 。

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

自己做的基于单片机的密码锁设计(c语言设计)时间:2011-08-16 21:08:01 来源:作者:自己做的基于单片机的密码锁设计(c语言设计)系统说明该系统如图所示使用了80C51单片机、普通键盘、排阻、1602液晶。

该系统的功能:①该系统输入正确的密码LED灯会亮(其实就是很多操作都可以,用LED只是代表作用,主要是操作简单,效果明显。

)②系统可以修改密码;(系统断电后重启后必须用初始密码(初始密码是:000000)重新改密,当然修改的密码可以与掉电前的一样。

)具体操作:①系统开机时显示请输入密码的提示,直接输入密码#键确认②系统会自动提醒密码错误,数秒后系统会再次提醒重新输密码。

③在系统提醒输入密码的界面可以按*键修改密码,系统会提醒输入两次密码,并且会检查两次密码是否相同。

同则修改成功。

否则在数秒后可以重新按*键修改。

④在改密前(就是按了*键后系统)会提醒身份识别,请输入旧密码,系统判断旧密码正确方可更改密码。

源程序#include<reg52.h>#define uchar unsigned char#define uint unsigned intsbit lcdrs=P3^0;sbit lcdrw=P3^1;sbit lcden=P3^2;sbit h1=P1^0;sbit h2=P1^1;sbit h3=P1^2;sbit sa=P1^3;sbit sb=P1^4;sbit sc=P1^5;sbit sd=P1^6;sbit kd=P3^7;uchar code table[]=" PLEASE INPUT ";uchar code table1[]="CODE: ";uchar code table2[]=" *****WIN*****";uchar code table3[]=" PLEASE AFFIRM";uchar code table4[]=" *****FAIL*****";uchar code table5[]=" MODIFICATION";uchar code table6[]=" IDENTITY LIMITS ";uchar unm,M1,M2,M3,M4,M5,M6,sex,k1,k2,k3,k4,k5,k6,flge,flge1;uchar q1,q2,q3,q4,q5,q6,w1,w2,w3,w4,w5,w6,g;void delay(int z){int x,y;for(x=z;x>0;x--)for(y=110;y>0;y--);}void delay1(int z){int x,y;for(x=z;x>0;x--)for(y=20000;y>0;y--);}void writen_com(uchar com) //1602写命令{lcdrs=0;P0=com;delay(1);lcden=1;delay(1);lcden=0;}void writen_dat(uchar dat) //1602写数据{lcdrs=1;P0=dat;delay(1);lcden=1;delay(1);lcden=0;}void main();void cheak_mima() //检查第二次确认密码是否与第一次相同{if(q1==w1){if(q2==w2){if(q3==w3){if(q4==w4){if(q5==w5){if(q6==w6){writen_com(0x01);writen_com(0x0f);writen_com(0x06);writen_com(0x80);for(unm=0;unm<14;unm++){writen_dat(table5[unm]);delay(5);}writen_com(0x80+0x40+1);for(unm=0;unm<14;unm++){writen_dat(table2[unm]);delay(5);}M1=w1;M2=w2;M3=w3;M4=w4;M5=w5;M6=w6;delay1(6);main();}/*-----------------------------------*/ //密码确认错误返回writen_com(0x01);writen_com(0x80);for(unm=0;unm<14;unm++){writen_dat(table4[unm]);delay(5);}delay1(6);main();}writen_com(0x01);writen_com(0x80);for(unm=0;unm<14;unm++){writen_dat(table4[unm]);delay(5);}delay1(6);main();}writen_com(0x01);writen_com(0x80);for(unm=0;unm<14;unm++){writen_dat(table4[unm]);delay(5);}delay1(6);main();}writen_com(0x01);writen_com(0x80);for(unm=0;unm<14;unm++){writen_dat(table4[unm]);delay(5);}delay1(6);main();}writen_com(0x01);writen_com(0x80);for(unm=0;unm<14;unm++){writen_dat(table4[unm]);delay(5);}delay1(6);main();}writen_com(0x01);writen_com(0x80);for(unm=0;unm<14;unm++){writen_dat(table4[unm]);delay(5);}delay1(6);main();}/*------------------------------------*/ //第二次确认密码void affirm_code(){P1=0x07;if(P1!=0x07)delay(1);if(P1!=0x07){P1=0x77;if(h1==0){delay(1);if(h1==0){while(!h1);writen_com(0x80+0x45+sex);writen_dat('*');switch(sex){case 0:w1=1;break;case 1:w2=1;break;case 2:w3=1;break;case 3:w4=1;break;case 4:w5=1;break;case 5:w6=1;break;}sex++;}}if(h2==0){delay(1);if(h2==0){while(!h2);writen_com(0x80+0x45+sex);writen_dat('*');{case 0:w1=2;break;case 1:w2=2;break;case 2:w3=2;break;case 3:w4=2;break;case 4:w5=2;break;case 5:w6=2;break;}sex++;}}if(h3==0){delay(1);if(h3==0){while(!h3);writen_com(0x80+0x45+sex); writen_dat('*');switch(sex){case 0:w1=3;break;case 1:w2=3;break;case 2:w3=3;break;case 3:w4=3;break;case 4:w5=3;break;case 5:w6=3;break;}sex++;}}/*-------------------------------*/ P1=0x6f;if(h1==0){delay(1);if(h1==0){while(!h1);writen_com(0x80+0x45+sex); writen_dat('*');{case 0:w1=4;break;case 1:w2=4;break;case 2:w3=4;break;case 3:w4=4;break;case 4:w5=4;break;case 5:w6=4;break;}sex++;}}if(h2==0){delay(1);if(h2==0){while(!h2);writen_com(0x80+0x45+sex); writen_dat('*');switch(sex){case 0:w1=5;break;case 1:w2=5;break;case 2:w3=5;break;case 3:w4=5;break;case 4:w5=5;break;case 5:w6=5;break;}sex++;}}if(h3==0){delay(1);if(h3==0){while(!h3);writen_com(0x80+0x45+sex); writen_dat('*');switch(sex){case 1:w2=6;break;case 2:w3=6;break;case 3:w4=6;break;case 4:w5=6;break;case 5:w6=6;break;}sex++;}}/*--------------------------------*/ P1=0x5f;if(h1==0){delay(1);if(h1==0){while(!h1);writen_com(0x80+0x45+sex); writen_dat('*');switch(sex){case 0:w1=7;break;case 1:w2=7;break;case 2:w3=7;break;case 3:w4=7;break;case 4:w5=7;break;case 5:w6=7;break;}sex++;}}if(h2==0){delay(1);if(h2==0){while(!h2);writen_com(0x80+0x45+sex); writen_dat('*');switch(sex){case 1:w2=8;break;case 2:w3=8;break;case 3:w4=8;break;case 4:w5=8;break;case 5:w6=8;break;}sex++;}}if(h3==0){delay(1);if(h3==0){while(!h3);writen_com(0x80+0x45+sex); writen_dat('*');switch(sex){case 0:w1=9;break;case 1:w2=9;break;case 2:w3=9;break;case 3:w4=9;break;case 4:w5=9;break;case 5:w6=9;break;}sex++;}}/*-----------------------------------*/ P1=0x3f;if(h2==0) //0键扫描{delay(1);if(h2==0){while(!h2);writen_com(0x80+0x45+sex); writen_dat('*');switch(sex){case 1:w2=0;break;case 2:w3=0;break;case 3:w4=0;break;case 4:w5=0;break;case 5:w6=0;break;}sex++;}}if(h3==0) //#键扫描{delay(1);if(h3==0){while(!h3);while(!flge1)cheak_mima();}}}}/*--------------------------------------*/ //修改密码自己做的基于单片机的密码锁设计(c语言设计)时间:2011-08-16 21:08:01 来源:作者:void set_mima(){P1=0x07;if(P1!=0x07)delay(1);if(P1!=0x07){P1=0x77;if(h1==0){delay(1);if(h1==0){while(!h1);writen_com(0x80+0x45+sex);writen_dat('*');switch(sex){case 0:q1=1;break;case 1:q2=1;break;case 2:q3=1;break;case 3:q4=1;break;case 4:q5=1;break;case 5:q6=1;break;}sex++;}}if(h2==0){delay(1);if(h2==0){while(!h2);writen_com(0x80+0x45+sex); writen_dat('*');switch(sex){case 0:q1=2;break;case 1:q2=2;break;case 2:q3=2;break;case 3:q4=2;break;case 4:q5=2;break;case 5:q6=2;break;}sex++;}}if(h3==0){delay(1);if(h3==0){while(!h3);writen_com(0x80+0x45+sex); writen_dat('*');switch(sex){case 1:q2=3;break;case 2:q3=3;break;case 3:q4=3;break;case 4:q5=3;break;case 5:q6=3;break;}sex++;}}P1=0x6f;if(h1==0){delay(1);if(h1==0){while(!h1);writen_com(0x80+0x45+sex); writen_dat('*');switch(sex){case 0:q1=4;break;case 1:q2=4;break;case 2:q3=4;break;case 3:q4=4;break;case 4:q5=4;break;case 5:q6=4;break;}sex++;}}if(h2==0){delay(1);if(h2==0){while(!h2);writen_com(0x80+0x45+sex); writen_dat('*');switch(sex){case 0:q1=5;break;case 2:q3=5;break;case 3:q4=5;break;case 4:q5=5;break;case 5:q6=5;break;}sex++;}}if(h3==0){delay(1);if(h3==0){while(!h3);writen_com(0x80+0x45+sex); writen_dat('*');switch(sex){case 0:q1=6;break;case 1:q2=6;break;case 2:q3=6;break;case 3:q4=6;break;case 4:q5=6;break;case 5:q6=6;break;}sex++;}}P1=0x5f;if(h1==0){delay(1);if(h1==0){while(!h1);writen_com(0x80+0x45+sex); writen_dat('*');switch(sex){case 0:q1=7;break;case 1:q2=7;break;case 3:q4=7;break;case 4:q5=7;break;case 5:q6=7;break;}sex++;}}if(h2==0){delay(1);if(h2==0){while(!h2);writen_com(0x80+0x45+sex); writen_dat('*');switch(sex){case 0:q1=8;break;case 1:q2=8;break;case 2:q3=8;break;case 3:q4=8;break;case 4:q5=8;break;case 5:q6=8;break;}sex++;}}if(h3==0){delay(1);if(h3==0){while(!h3);writen_com(0x80+0x45+sex); writen_dat('*');switch(sex){case 0:q1=9;break;case 1:q2=9;break;case 2:q3=9;break;case 3:q4=9;break;case 5:q6=9;break;}sex++;}}P1=0x3f;if(h2==0) //0键扫描{delay(1);if(h2==0){while(!h2);writen_com(0x80+0x45+sex); writen_dat('*');switch(sex){case 0:q1=0;break;case 1:q2=0;break;case 2:q3=0;break;case 3:q4=0;break;case 4:q5=0;break;case 5:q6=0;break;}sex++;}}if(h3==0) //#键扫描{delay(1);if(h3==0){while(!h3);writen_com(0x01);writen_com(0x0f);writen_com(0x06);writen_com(0x80);for(unm=0;unm<14;unm++){writen_dat(table3[unm]);delay(5);}writen_com(0x80+0x40);for(unm=0;unm<5;unm++) {writen_dat(table1[unm]);delay(5);}sex=0;while(!flge1)affirm_code();}}}}void init();void cheak_identity2(){if(k1==M1){if(k2==M2){if(k3==M3){if(k4==M4){if(k5==M5){if(k6==M6){sex=0;init();while(!flge1)set_mima();}writen_com(0x01);writen_com(0x0c);writen_com(0x06);writen_com(0x80);for(unm=0;unm<14;unm++){writen_dat(table4[unm]);delay(5);}delay1(6);main();}writen_com(0x01);writen_com(0x0c);writen_com(0x06);writen_com(0x80);for(unm=0;unm<14;unm++) {writen_dat(table4[unm]); delay(5);}delay1(6);main();}writen_com(0x01);writen_com(0x0c);writen_com(0x06);writen_com(0x80);for(unm=0;unm<14;unm++) {writen_dat(table4[unm]); delay(5);}delay1(6);main();}writen_com(0x01);writen_com(0x0c);writen_com(0x06);writen_com(0x80);for(unm=0;unm<14;unm++) {writen_dat(table4[unm]); delay(5);}delay1(6);main();}writen_com(0x01);writen_com(0x0c);writen_com(0x06);writen_com(0x80);for(unm=0;unm<14;unm++){writen_dat(table4[unm]); delay(5);}delay1(6);main();}void cheak_identity(){P1=0x07;if(P1!=0x07)delay(1);if(P1!=0x07){P1=0x77;if(h1==0){delay(1);if(h1==0){while(!h1);writen_com(0x80+0x45+sex); writen_dat('*');switch(sex){case 0:k1=1;break;case 1:k2=1;break;case 2:k3=1;break;case 3:k4=1;break;case 4:k5=1;break;case 5:k6=1;break;}sex++;}}if(h2==0)delay(1);if(h2==0){while(!h2);writen_com(0x80+0x45+sex);writen_dat('*');switch(sex){case 0:k1=2;break;case 1:k2=2;break;case 2:k3=2;break;case 3:k4=2;break;case 4:k5=2;break;case 5:k6=2;break;}sex++;}}if(h3==0){delay(1);if(h3==0){while(!h3);writen_com(0x80+0x45+sex);writen_dat('*');switch(sex){case 0:k1=3;break;case 1:k2=3;break;case 2:k3=3;break;case 3:k4=3;break;case 4:k5=3;break;case 5:k6=3;break;}sex++;}}/*-------------------------------*/P1=0x6f;if(h1==0){delay(1);if(h1==0){while(!h1);writen_com(0x80+0x45+sex); writen_dat('*');switch(sex){case 0:k1=4;break;case 1:k2=4;break;case 2:k3=4;break;case 3:k4=4;break;case 4:k5=4;break;case 5:k6=4;break;}sex++;}}if(h2==0){delay(1);if(h2==0){while(!h2);writen_com(0x80+0x45+sex); writen_dat('*');switch(sex){case 0:k1=5;break;case 1:k2=5;break;case 2:k3=5;break;case 3:k4=5;break;case 4:k5=5;break;case 5:k6=5;break;}sex++;}}if(h3==0){delay(1);if(h3==0){while(!h3);writen_com(0x80+0x45+sex); writen_dat('*');switch(sex){case 0:k1=6;break;case 1:k2=6;break;case 2:k3=6;break;case 3:k4=6;break;case 4:k5=6;break;case 5:k6=6;break;}sex++;}}/*--------------------------------*/ P1=0x5f;if(h1==0){delay(1);if(h1==0){while(!h1);writen_com(0x80+0x45+sex); writen_dat('*');switch(sex){case 0:k1=7;break;case 1:k2=7;break;case 2:k3=7;break;case 3:k4=7;break;case 4:k5=7;break;case 5:k6=7;break;}sex++;}}if(h2==0){delay(1);if(h2==0){while(!h2);writen_com(0x80+0x45+sex);writen_dat('*');switch(sex){case 0:k1=8;break;case 1:k2=8;break;case 2:k3=8;break;case 3:k4=8;break;case 4:k5=8;break;case 5:k6=8;break;}sex++;}}if(h3==0){delay(1);if(h3==0){while(!h3);writen_com(0x80+0x45+sex); writen_dat('*');switch(sex){case 0:k1=9;break;case 1:k2=9;break;case 2:k3=9;break;case 3:k4=9;break;case 4:k5=9;break;case 5:k6=9;break;}sex++;}}/*-----------------------------------*/ P1=0x3f;if(h2==0) //0键扫描{delay(1);if(h2==0){while(!h2);writen_com(0x80+0x45+sex); writen_dat('*');switch(sex){case 0:k1=0;break;case 1:k2=0;break;case 2:k3=0;break;case 3:k4=0;break;case 4:k5=0;break;case 5:k6=0;break;}sex++;}}if(h3==0) //#键扫描{delay(1);if(h3==0){while(!h3);while(!flge1)cheak_identity2();}}}}/*---------------------------------------*/ //键盘扫描void keyscan(){P1=0x07;if(P1!=0x07)delay(1);if(P1!=0x07){P1=0x77;if(h1==0){delay(1);if(h1==0){while(!h1);writen_com(0x80+0x45+sex);writen_dat('*');switch(sex){case 0:k1=1;break;case 1:k2=1;break;case 2:k3=1;break;case 3:k4=1;break;case 4:k5=1;break;case 5:k6=1;break;}sex++;}}if(h2==0){delay(1);if(h2==0){while(!h2);writen_com(0x80+0x45+sex); writen_dat('*');switch(sex){case 0:k1=2;break;case 1:k2=2;break;case 2:k3=2;break;case 3:k4=2;break;case 4:k5=2;break;case 5:k6=2;break;}sex++;}}if(h3==0){delay(1);if(h3==0){while(!h3);writen_com(0x80+0x45+sex); writen_dat('*');switch(sex){case 0:k1=3;break;case 1:k2=3;break;case 2:k3=3;break;case 3:k4=3;break;case 4:k5=3;break;case 5:k6=3;break;}sex++;}}P1=0x6f;if(h1==0){delay(1);if(h1==0){while(!h1);writen_com(0x80+0x45+sex); writen_dat('*');switch(sex){case 0:k1=4;break;case 1:k2=4;break;case 2:k3=4;break;case 3:k4=4;break;case 4:k5=4;break;case 5:k6=4;break;}sex++;}}if(h2==0){delay(1);if(h2==0){while(!h2);writen_com(0x80+0x45+sex); writen_dat('*');switch(sex){case 0:k1=5;break;case 1:k2=5;break;case 2:k3=5;break;case 3:k4=5;break;case 4:k5=5;break;case 5:k6=5;break;}}}if(h3==0){delay(1);if(h3==0){while(!h3);writen_com(0x80+0x45+sex); writen_dat('*');switch(sex){case 0:k1=6;break;case 1:k2=6;break;case 2:k3=6;break;case 3:k4=6;break;case 4:k5=6;break;case 5:k6=6;break;}sex++;}}/*--------------------------------*/ P1=0x5f;if(h1==0){delay(1);if(h1==0){while(!h1);writen_com(0x80+0x45+sex); writen_dat('*');switch(sex){case 0:k1=7;break;case 1:k2=7;break;case 2:k3=7;break;case 3:k4=7;break;case 4:k5=7;break;case 5:k6=7;break;}}}if(h2==0){delay(1);if(h2==0){while(!h2);writen_com(0x80+0x45+sex); writen_dat('*');switch(sex){case 0:k1=8;break;case 1:k2=8;break;case 2:k3=8;break;case 3:k4=8;break;case 4:k5=8;break;case 5:k6=8;break;}sex++;}}if(h3==0){delay(1);if(h3==0){while(!h3);writen_com(0x80+0x45+sex); writen_dat('*');switch(sex){case 0:k1=9;break;case 1:k2=9;break;case 2:k3=9;break;case 3:k4=9;break;case 4:k5=9;break;case 5:k6=9;break;}sex++;}/*-----------------------------------*/ P1=0x3f;if(h1==0) //*键扫描{delay(1);if(h1==0){while(!h1);writen_com(0x0f);writen_com(0x06);writen_com(0x80);for(unm=0;unm<16;unm++){writen_dat(table6[unm]);delay(5);}while(!flge1)cheak_identity();}}if(h2==0) //0键扫描{delay(1);if(h2==0){while(!h2);writen_com(0x80+0x45+sex); writen_dat('*');switch(sex){case 0:k1=0;break;case 1:k2=0;break;case 2:k3=0;break;case 3:k4=0;break;case 4:k5=0;break;case 5:k6=0;break;}sex++;}}if(h3==0) //#键扫描delay(1);if(h3==0){while(!h3);flge=1;}}}}/*-------------------------------------*/ //密码检查void init();void check_code(){if(k1==M1){if(k2==M2){if(k3==M3){if(k4==M4){if(k5==M5){if(k6==M6){writen_com(0x01);kd=0;writen_com(0x0c);writen_com(0x06);writen_com(0x80);for(unm=0;unm<14;unm++){writen_dat(table2[unm]);delay(5);}delay1(6);kd=1;main();writen_com(0x0c);writen_com(0x06);writen_com(0x80);for(unm=0;unm<14;unm++) {writen_dat(table4[unm]); delay(5);}delay1(6);main();}writen_com(0x0c);writen_com(0x06);writen_com(0x80);for(unm=0;unm<14;unm++) {writen_dat(table4[unm]); delay(5);}delay1(6);main();}writen_com(0x0c);writen_com(0x06);writen_com(0x80);for(unm=0;unm<14;unm++) {writen_dat(table4[unm]); delay(5);}delay1(6);main();}writen_com(0x0c);writen_com(0x06);writen_com(0x80);for(unm=0;unm<14;unm++) {writen_dat(table4[unm]); delay(5);}delay1(6);main();}writen_com(0x0c);writen_com(0x06);writen_com(0x80);for(unm=0;unm<14;unm++) {writen_dat(table4[unm]); delay(5);}delay1(6);main();}}writen_com(0x0c);writen_com(0x06);writen_com(0x80);for(unm=0;unm<14;unm++) {writen_dat(table4[unm]); delay(5);}delay1(6);main();}void now_mima(){M1=0;M2=0;M3=0;M4=0;M5=0;M6=0;g=0;}void init(){if(g==0)now_mima();g=1;flge1=0;flge=0;sex=0;lcdrw=0;lcden=0;writen_com(0x01);writen_com(0x38);writen_com(0x0f);writen_com(0x06);writen_com(0x80);for(unm=0;unm<14;unm++){writen_dat(table[unm]); delay(5);}writen_com(0x80+0x40);for(unm=0;unm<5;unm++) {writen_dat(table1[unm]); delay(5);}}void main(){while(1){init();set_mima();while(!flge)keyscan();check_code();}}。

相关文档
最新文档