求51单片机C语言编的简易密码锁程序
简易电子密码锁(基于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单片机简易密码锁学号: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单片机简易密码锁设计

易密码锁设计摘要:本设计以单片机STC89C52RC作为密码锁监控装置的检测和控制核心,分为主机控制和从机执行机构(本设重点介绍主机设计),实现钥匙信息在主机上的初步认证注册、密码信息的加密、钥匙丢失报废等功能。
根据51单片机之间的串行通信原理,这便于对密码信息的随机加密和保护。
而且采用键盘输入的电子密码锁具有较高的优势。
采用数字信号编码和二次调制方式,不仅可以实现多路信息的控制,提高信号传输的抗干扰性,减少错误动作,而且功率消耗低;反应速度快、传输效率高、工作稳定可靠等。
软件设计采用自上而下的模块化设计思想,以使系统朝着分布式、小型化方向发展,增强系统的可扩展性和运行的稳定性。
测试结果表明,本系统各项功能已达到本设计的所有要求。
关键词:单片机;智能密码锁;串行通信The Design Of The Simple Password LockAbstract: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.Key Words : singlechip; intelligent password lock; Serial communication;目录概述 (1)1 设计目标 (1)2硬件设计与原理 (2)2.1 设计总框图 (2)2.2 硬件设计分析 (2)2.2.1 电源的设计 (2)2.2.2 单片机最小系统 (3)2.2.3 显示系统 (8)2.2.4 矩阵按键模块 (8)2.2.5 蜂鸣器和指示灯电路 (9)3软件设计与分析 (9)3.1 软件设计的组成 (9)3.2 各部分软件分析 (9)3.2.1 延时子函数 (9)3.2.2 矩阵键盘扫描子函数 (10)3.2.3 检验密码正误子函数 (10)3.2.4锁定,鸣笛程序 (11)3.2.5显示子程序 (11)4软件仿真 (14)4.1 PROTEUS简介 (14)4.2仿真图 (15)总结 (19)参考文献 (20)致谢 (28)附录 (29)述随着人们生活水平的提高,传统的机械锁由于其构造的简单,被撬开的事件屡见不鲜,电子锁保密性高,能够防止不法分子多次试探密码;性价比高,因此,电子锁受到了广大的亲昵。
电子密码锁完整代码

#include<reg51.h>#include <intrins.h>#include <absacc.h>#include "VIIC_C51.h"#define uchar unsigned char#define uint unsigned int#define CSI24WC02 0XA0#define uchar unsigned charsbit lcdrs=P2^0;sbit lcdrw=P2^1;sbit lcden=P2^2;sbit alarm=P2^3;sbit red=P2^7;sbit green=P2^6;sbit KEY_INT=P3^2;//led testsbit LED1=P3^5;sbit LED2=P3^1;uchar hour,min,sec,us;uchar flag=1,canscan=0,lock=0;uchar password[8]={0,0,0,0,0,0,0,0};uchar input[8]={0,1,2,3,4,5,6,7};uchar code a[]={0xF7,0xFB,0xFD,0xFE};unsigned char DelayNS(unsigned char no) {unsigned char i,j;for(; no>0; no--){for(i=0; i<100; i++)for(j=0; j<100; j++);}return 0;}unsigned char delay_eep(unsigned char j) {unsigned char k,l;for(l=0;l<=j;l++)for(k=0;k<=250;k++);return 0;}void t0_init(){TMOD=0x01;TL0=0xb0;TH0=0x3c;TR0=0;EA=1;ET0=1;}void delay(uchar n){uchar i;while(n--)for(i=125;i>0;i--);}//lcd 写命令void write_com(uchar com) {lcdrs=0;lcdrw=0;P0=com;delay(2);lcden=1;delay(2);lcden=0;}//lcd 写数据void write_data(uchar date) {lcdrs=1;lcdrw=0;P0=date;delay(2);lcden=1;delay(2);lcden=0;//lcd 的初始化void init_lcd(){write_com(0x38);write_com(0x0c);write_com(0x06);write_com(0x01);}// lcd 清屏void clearscn(){write_com(0x06);write_com(0x01);}//lcd 显示坐标void gotoxy(uchar x,uchar y) {if(x==1)write_com(0x80+y);if(x==2)write_com(0xc0+y);}//写lcd字符串void write_str(uchar *str) {while(*str!='\0'){write_data(*str);delay(2);str++;}}//写一位数字void write_num(uchar num) {write_data(0x30+num);}//写两位数void write_num2(uchar num)uchar x,y;x=num/10;y=num%10;write_num(x);write_num(y);}// 矩阵键盘扫描函数uchar scan(void){uchar row,col;uchar j,m;P1=0xF0;if((P1&0xF0)!=0xF0){delay(1);if((P1&0xF0)!=0xF0)col=~(P1|0x0F);j=0;P1=a[j];while(j<=3){if((P1&0xF0)!=0xF0){row=~a[j];break;}else{j++;P1=a[j];}}m=row+col;return(m);}elsereturn(0);}uchar coding(uchar m){uchar k;switch(m){case(0x08+0x80):k=0;break;case(0x08+0x40):k=1;break;case(0x08+0x20):k=2;break;case(0x08+0x10):k=3;break;case(0x04+0x80):k=4;break;case(0x04+0x40):k=5;break;case(0x04+0x20):k=6;break;case(0x04+0x10):k=7;break;case(0x02+0x80):k=8;break;case(0x02+0x40):k=9;break;case(0x02+0x20):k=10;break;case(0x02+0x10):k=11;break;case(0x01+0x80):k=12;break;case(0x01+0x40):k=13;break;case(0x01+0x20):k=14;break;case(0x01+0x10):k=15;break;}return(k);}//c重新设置密码void setpassword(){uchar tmp,key,i=0;write_com(0x38);write_com(0x0c);write_com(0x06);write_com(0x01);gotoxy(1,0);write_str(" ");gotoxy(1,10);write_str("SET ");while(1){tmp=scan();if(tmp!=0){key=coding(tmp);if(key<=9&&i<8){if(i<8){password[i]=key;gotoxy(1,i);write_data(0x2a);}i++;alarm=0;delay(250);alarm=1;}if(i>=8&&(key==11)){gotoxy(1,10);write_str("SET OK");gotoxy(1,0);write_str(" ");break;}delay(250);}}}//蜂鸣器报警void buzzeralarm(){alarm=0;delay(250);alarm=1;delay(250);alarm=0;delay(250);alarm=1;delay(250);alarm=0;delay(250);alarm=1;delay(250);}//定时器0中断服务函数//作用给输入三次错误倒计时用void time0() interrupt 1{TL0=0xb0;TH0=0x3c;if(++us==20){us=0;gotoxy(1,6);write_num2(sec);gotoxy(1,3);write_num2(min);gotoxy(1,0);write_num2(hour);if(++sec==60){sec=0;if(++min==60){min=0;if(++hour==1){hour=0;TR0=0;lock=0;gotoxy(2,10);write_str("UNLOCKED");}}}}}//主函数void main(void){uchar tmp,key,i=0,j,scannum=0;uchar td[8]={0x1,0x1,0x1,0x1,0x1,0x1,0x1,0x1}; //eeprom send buff uchar rd[8]={0x00,0x01,0x02,0x03,0x04}; //eeprom read buffgreen=0;red=0;t0_init();init_lcd();LED1=0;LED2=0;while(1){tmp=scan(); //读入键值if(tmp!=0){key=coding(tmp);if((key<=9)&&(i<8)&&(lock==0)&&(green!=1))//判断输入次数(i),和锁定情况(lock) 和有效输入(key){red=0;gotoxy(1,10);write_str(" ");gotoxy(1,i);input[i]=key;i++;alarm=0;write_data(0x2a);delay(250);alarm=1;}if(key==10)//{gotoxy(1,i-1);write_data(0x20);if(i>0)i--;delay(250);}if(i==8&&key==11)//确认键{i=0;#if 1IRcvStr(CSI24WC02,0,rd,0x8); //?EEPROMdelay_eep(200);for(i=0; i<8; i++) //?password{password[i] = rd[i];} i = 0;#endiffor(j=0;j<=7;j++){if(input[j]!=password[j])flag=1;else flag=0;}if(lock==0){if(flag){flag = 0;scannum++;gotoxy(2,10);write_str("ERR! ");write_num(scannum);red=1;buzzeralarm();if(scannum==3){lock=1;TR0=1;}else{clearscn();gotoxy(2,10);write_str("again ");continue;}}else{gotoxy(2,10);write_str("OK! ");green=1;}}if(lock){gotoxy(2,10);write_str("LOCKED");}gotoxy(1,0);write_str(" ");canscan=0;delay(250);}if((green==1)&&(key==13)){LED1=1;green=0;delay(200);LED1=0;}if((key==12)&&(green==1))//{setpassword();ISendStr(CSI24WC02,0,password,0x8); //}}}}/********************************************************************VIIC_C51.C此程序是I2C操作平台(主方式的软件平台)的底层的C子程序,如发送数据及接收数据,应答位发送,并提供了几个直接面对器件的操作函数,它很方便的与用户程序连接并扩展.....注意:函数是采用软件延时的方法产生SCL脉冲,固对高晶振频率要作一定的修改....(本例是1us机器周期,即晶振频率要小于12MHZ)********************************************************************/#include <reg51.h> /*头文件的包含*/#include <intrins.h>#define uchar unsigned char /*宏定义*/#define uint unsigned int#define _Nop() _nop_() /*定义空指令*//* 常,变量定义区*//*端口位定义*/sbit SDA=P3^7; /*模拟I2C数据传送位*/sbit SCL=P3^6; /*模拟I2C时钟控制位*//*状态标志*/bit ack; /*应答标志位*//*******************************************************************起动总线函数函数原型: void Start_I2c();功能: 启动I2C总线,即发送I2C起始条件.********************************************************************/ void Start_I2c(){SDA=1; /*发送起始条件的数据信号*/_Nop();SCL=1;_Nop(); /*起始条件建立时间大于4.7us,延时*/_Nop();_Nop();_Nop();_Nop();SDA=0; /*发送起始信号*/_Nop(); /* 起始条件锁定时间大于4μs*/_Nop();_Nop();_Nop();_Nop();SCL=0; /*钳住I2C总线,准备发送或接收数据*/_Nop();_Nop();}/*******************************************************************结束总线函数函数原型: void Stop_I2c();功能: 结束I2C总线,即发送I2C结束条件.********************************************************************/ void Stop_I2c(){SDA=0; /*发送结束条件的数据信号*/_Nop(); /*发送结束条件的时钟信号*/SCL=1; /*结束条件建立时间大于4μs*/_Nop();_Nop();_Nop();_Nop();SDA=1; /*发送I2C总线结束信号*/_Nop();_Nop();_Nop();_Nop();}/*******************************************************************字节数据传送函数函数原型: void SendByte(uchar c);功能: 将数据c发送出去,可以是地址,也可以是数据,发完后等待应答,并对此状态位进行操作.(不应答或非应答都使ack=0 假)发送数据正常,ack=1; ack=0表示被控器无应答或损坏。
51单片机电子密码锁

目录第1节引言 (1)1.1 电子密码锁述 (1)1.2 本设计主要任务 (1)1.3 系统主要功能 (2)第2节系统硬件设计 (3)2.1 系统的硬件构成及功能 (3)2.2 AT89C2051单片机及其引脚说明 (3)第3节系统软件设计 (5)3.1 系统主程序设计(流程图) (5)3.2 软件设计思想 (5)3.3 储单元的分配 (5)3.4 系统源程序 (6)3.5 系统应用说明 (9)3.6 小结 (9)结束语 (10)参考文献 (11)附录 (12)电子密码锁第1节引言1.1 电子密码锁概述随着社会物质财富的日益增长和人们生活水平的提高,安全成为现代居民最关心的问题之一。
而锁自古以来就是把守门的铁将军,人们对它要求甚高,即要求可靠地防盗,又要使用方便,这也是制锁者长期以来研制的主题。
传统的门锁既要备有大量的钥匙,又要担心钥匙丢失后的麻烦。
另外,如:宾馆、办公大楼、仓库、保险柜等,由于装修施工等人住时也要把原有的锁胆更换,况且钥匙随身携带也诸多便。
随着单片机的问世,出现了带微处理器的密码锁,它除具有电子密码锁的功能外,还引入了智能化、科技化等功能。
从而使密码锁具有很高的安全性、可靠性。
目前西方发达国家已经大量应用智能门禁系统,可以通过多种的更加安全更加方便可靠的方法来实现大门的管理。
但电子密码锁在我国的应用还不广泛,成本还很高,希望通过不断地努力使电子密码锁能够在我国及居民日常生活中得到广泛应用,这也是一个国家生活水平的体现。
很多行业的许多地方都要用到密码锁,随着人们生活水平的提高,如何实现家庭或公司的防盗这一问题也变的尤其突出,传统的机械锁由于其构造简单,被撬的事件屡见不鲜,再者,普通密码锁的密码容易被多次试探而破译,所以,考虑到单片机的优越性,一种基于单片机的电子密码锁应运而生。
电子密码锁由于其保密性高,使用灵活性好,安全系数高,受到了广大用户的亲睐。
设计本课题时构思的方案:采用以AT89C2051为核心的单片机控制方案;能防止多次试探而不被破译,从而有效地克服了现实生活中存在的许多缺点。
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单片机电子密码锁#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单片机密码锁程序
sbit beep=P2^0; //蜂鸣器,低电平叫
sbit red=P2^1; //红灯,低电平亮
sbit close=P2^7; //锁合指示灯,黄色。低电平亮
1 2 3 确定
0
---------------------------------------------------------------------------------------------------------------------------------------------------------*/
初始密码为四位,1,2,3,4 如果想修改密码的长度,可将程序中的密码长度4改为你想设置的长度值
初始密码1,2,3,4也可在函数init_main()函数中修改
密码锁上电后,锁合灯亮,初始密码为1,2,3,4 。
当输入的四位密码按确定键后,密码正确时锁开灯亮,锁合灯灭。当不正确时,锁合灯继续亮,蜂鸣器短报警
pwflag = 0; // 密码标志先置为0
last_pwflag=0;
close=0; //锁合灯亮,其它灯灭
delay();
red=1;
delay();
delay();
delay();
delay();
red=0;
delay();
delay();
delay();
delay();
red=1;
}
//快闪红灯,重新输入自定义密码
void flashred_S(void)
case 0xdb: return 8;break;
单片机课程设计——可改密码的电子密码锁设计
百度文库- 让每个人平等地提升自我目录课程设计(论文)任务书 (1)可改密码电子密码锁设计报告 (1)第一章概述 (2)第二章系统整体方案设计 (2)第三章硬件电路设计 (3)键盘电路设计 (3)显示电路 (4)开锁电路 (6)报警电路 (6)密码掉电保留电路 (6)第四章软件设计 (8)软件设计思路 (8)程序设计流程图 (9)程序设计 (10)第五章系统调试 (10)3、开锁状态: (10)第六章心得体会 (12)致谢 (13)参考文献: (13)课程设计(论文)任务书设计题目:可改密码电子密码锁设计任务:以89C52单片机为核心,制作一个能够更改6位密码的模拟电子密码锁。
设计要求:密码位数为6位。
显示利用1602液晶显示。
更改的密码支持断电保留。
支持持续输入三次错误密码报警。
密码输入状态动态显示。
别离利用两个发光二极管表示输入正确和错误密码。
可改密码电子密码锁设计报告摘要本文介绍基于MCS-51单片机的可更改密码的电子密码锁设计,并支持掉电保留密码,程序利用了C语言编写。
关键词单片机电子密码锁改变Abstract:This article describes the MCS-51 microcontroller based on the password change the design of electronic locks, and to support power-down save the password, the program uses the C language.Key word: MCU Electronic code lock Change第一章概述密码锁是一种通过密码输入来控制电路或是芯片工作,从而控制机械开关的闭合,完成开锁、闭锁任务的电子产品。
它的种类很多,有简易的电路产品,也有基于芯片的性价比较高的产品。
此刻应用较广的数字密码锁是以芯片为核心,通过编程来实现的。
51单片机设计电子锁密码锁含代码
电子锁设计报告一,实验目的1. 学习8051定时器时间计时处理,键盘扫描及LED数码管显示的设计方法。
2. 设计任务及要求利用实验平台上8个LED数码管,设计带有报警功能的可掉电保存的电子密码锁。
3.通过本次实验,加强对所学知识的理解,增强编程能力及实践能力。
二,实验要求A.基本要求:1:用4×4矩阵键盘组成0-9数字键及确认键和删除键。
2:可以自行设定或删除8位密码,能够掉电保存。
3:用5位数码管组成显示电路提示信息,当输入密码时,只显示“8.”,当密码位数输入完毕按下确认键时,对输入的密码与设定的密码进行比较,若密码正确,则门开,此处用绿色led发光二极管亮一秒钟做为提示,若密码不正确,禁止按键输入3秒,同时用红色led发光二极管亮三秒钟做为提示;若在3秒之内仍有按键按下,则禁止按键输入3秒被重新禁止。
4:自由发挥其他功能.5:要求有单片机硬件系统框图,电路原理图,软件流程图B.拓展部分:无三,实验基本原理单片机密码锁是集计算机技术、电子技术、数字密码技术为一体的机电一体化高科技产品,具有安全性高,使用方便等优点。
本系统考虑到单片机密码锁成本及体积因素,在设计单片机密码锁部分时,以AT89S52单片机为核心,24C04、LED等构成外围电路。
本系统单片机密码锁硬件部分结构简单、成本低,软件部分使用电子加密提高锁的安全性,具有比较好的市场前景。
同时,由于本电子密码锁可以实现掉电保存,而且可以自行设计或者删除8位密码,所以具有较高的实用价值。
本密码锁采用5位数码管组成显示电路提示信息,当输入密码时,只显示“8.”,当密码位数输入完毕按下确认键时,对输入的密码与设定的密码进行比较,若密码正确,则门开,此处用绿色led 发光二极管亮一秒钟做为提示,若密码不正确,禁止按键输入3秒,同时用红色led 发光二极管亮三秒钟做为提示;若在3秒之内仍有按键按下,则禁止按键输入3秒被重新禁止。
此项功能方便用户使用。
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
求51单片机C语言编的简易密码锁程序
2011-12-01 20:06 提问者:_yi_feng_|浏览次数:535次
设计一个一位简易密码锁,其基本功能如下:输入一位密码,为0~3之间的数字,密码输入正确显示字符“P”约3秒钟,并通过P3.0端口将锁打开;否则显示字符“E”约3秒钟,锁继续保持锁定状态,等待密码的再次输入。
根据题目要求,用一位数码管即可显示,采用静态连接方式;四个数字键通过P0口的低四位P0.0~P0.3来连接,设P0.0表示0数字键、P0.1表示1数字键~P0.3表示3数字键;锁的开、关电路用P3.0控制一个发光二极管替代,发光二极管表示锁打开,灭表示锁定。
(设初始密码为213)
我来帮他解答
满意回答
2011-12-02 09:28
首先得说明我这个可是自己原创手打的,但是没去仿真了,程序可能有错误,你自己修改下吧
#include<reg52.h> //51单片机头文件
typedef unsigned char uchar; // 宏定义
typedef unsigned int uint; // 宏定义
sbit key1=P0^0; // 位定义
sbit key2=P0^1; // 位定义
sbit key3=P0^2; // 位定义
sbit key4=P0^3; // 位定义
sbit wela=P2^0; //定义位锁存端
#define SMG P1 //定义数据输入
sbit LED=P3^0; // 低电平亮
uchar code table[]={0x8d,0x86}; //共阳数码管P,E
uchar chushi_mima[]={2,1,3};
uchar shuru_mima[3];
uchar index; //控制输入密码的位数
uchar flag_3s=0; //3s标志位
uchar keydown; //确定按键变量
#define times 15 //去抖时间15Ms
uchar key1_count,key2_count,key3_count,key4_count;
void init()
{
wela=0; 数码管初始化
SMG=0xff;
TMOD=0x01; 定时中断
TH0=(65536-1000)/256;
TL0=(65536-1000)%256;
ET0=1;
EA=1;
TR0=1;
LED=1;发光二级管初始化(灭)}
void main() 主函数
{
init();
while(1) 大循环,
{
switch(keydown) 检测按键
{
if(index>2)index=0;
case 1:
shuru_mima[index]=0;
index++;
break;
case 2:
shuru_mima[index]=1;
index++;
break;
case 3:
shuru_mima[index]=2;
index++;
break;
case 4:
shuru_mima[index]=3;
index++;
break;
}
flag_3s=0;
for(i=0;i<3;i++)
{
if(shuru_mima[i]==chushi_mima[i])
{
LED=0;
wela=1;
SMG=table[0];
if(flag_3s)
{
flag_3s=0;
wela=0;
}
}
else
{
LED=1;
wela=1;
SMG=table[1];
if(flag_3s)
{
flag_3s=0;
wela=0;
}
}
}
}
}
void timer0() interrupt 1
{
uchar count;
TH0=(65536-1000)/256;
TL0=(65536-1000)%256;
if(++count>=600)
{
count=0;
flag_3s=1;
}
/*********1ms中断扫描按键(包含去抖程序)********/ if(!key1&&key1_count!=0)
{
key1_count--;
if(key1_count==0)
{
keydown=1;
}
}
else if(!key2&&key2_count!=0)
{
key2_count--;
if(key2_count==0)
{
keydown=1;
}
}
else if(!key3&&key3_count!=0)
{
key3_count--;
if(key3_count==0)
{
keydown=1;
}
}
}。