AT89C51单片机电子密码锁.docx_第1页
AT89C51单片机电子密码锁.docx_第2页
AT89C51单片机电子密码锁.docx_第3页
AT89C51单片机电子密码锁.docx_第4页
AT89C51单片机电子密码锁.docx_第5页
已阅读5页,还剩26页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

基于AT89C51单片机电子密码锁#includemain.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就是0if (X = 0xF) /X坐标应小于0xF LCD_write_char(X,Y, *DData); /显示单个字符 DData+; X+;/*/#include#include#define uchar unsigned char#define uint unsigned int /* LCD PART START */void delay(uint z) /延时uint x,y;for(x=z;x0;x-)for(y=124;y0;y-);sbit e=P25;sbit rw=P26;sbit sr=P27;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;i3;i+)write_data(namei);write_com(0x80+0x40);for(i=0;i3;i+)write_data(name1i);void main()init();display();#include #define uchar unsigned charuchar starbuf10;uchar wordbuf8;uchar pw8=1,2,3,4,5,6,7,8;uchar pwbuf8;uchar count=0; / 初始没有输入密码,计数器设为0uchar inputflag=0; / 先处于密码输入状态,非密码修改状态bit enterflag=0; / 没有按下确认键bit pwflag=0; / 密码标志先置为0sbit warn=P36;#define lcd_data P0sbit rs=P27;sbit rw=P26;sbit e=P25;/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字符。 enrw(); lcd_data=0x0c;/打开显示开关 enrw(); lcd_data=0x06;/地址计数递增,显示屏不移动 enrw();write_cmd(uchar m)/写命令,注意与写数据的区别 lcd_data=m; enrw();display(uchar row,uchar colum,uchar *s)/行 列 字符 写字符串,简单的指针应用 uchar p; if(row=1) p=0x82+colum-1; else p=0xC0+colum-1; write_cmd(p); for(;*s!=0;s+) write_data(*s);void lcd_display( unsigned char a, unsigned char b,unsigned char i) /行 列 数 switch (i)case 0: display( a,b, 0) ;break;/* 0 */case 1: display( a,b, 1) ;break;/* 1 */case 2: display( a,b, 2) ;break;/* 2 */case 3: display( a,b, 3) ;break;/* 3 */case 4: display( a,b, 4) ;break;/* 4 */case 5: display( a,b, 5) ;break;/* 5 */case 6: display( a,b, 6) ;break;/* 6 */case 7: display( a,b, 7) ;break;/* 7 */case 8: display( a,b, 8) ;break;/* 8 */case 9: display( a,b, 9) ;break;/* 9 */default: break; /* 键消抖延时函数 */void delay(unsigned int i) int j; for(;i0;i-) for(j=0;j100;j+);/* 键扫描函数 */uchar keyscan(void)uchar scancode,tmpcode;P1 = 0xf0; / 发全0行扫描码if (P1&0xf0)!=0xf0)/ 若有键按下delay(2);/ 延时去抖动if (P1&0xf0)!=0xf0)/ 延时后再判断一次,去除抖动影响scancode = 0xfe; /第一行变低while(scancode&0x10)!=0)/ 逐行扫描P1 = scancode;/ 输出行扫描码if (P1&0xf0)!=0xf0)/ 本行有键按下tmpcode = (P1&0xf0)|0x0f;/* 返回特征字节码,为1的位即对应于行和列 */return(scancode)+(tmpcode);else scancode = (scancode1)|0x01;/ 行扫描码左移一位return(0);/ 无键按下,返回值为0 /* 密码比较函数 */bit pwcmp(void)bit flag;uchar i;for (i=0;i8;i+)if (pwi=pwbufi)flag = 1;elseflag = 0;i=8;return(flag);/* 密码清除函数 */void pwclk(unsigned char k)unsigned char i;for (i=0;i8;i+)wordbufi = 0;/ 数码管显示00000000starbufi = 0;if(k=0) pwbufi = 0;/ 用FFFFFF清除已经输入的密码else pwi = 0;/ 用FFFFFF清除已经输入的密码/* 按键声响函数 */void alarm() unsigned char i; for(i=0;i200;i+) warn=!warn;delay(1); /* 密码报警函数 */void alarm1() unsigned int i; for(i=0;i0) for(a=0;a150;a+) warn=!warn;delay(1); for(b=0;b150;b+) warn=!warn; delay(2); i-; /* 按键处理函数 */void key_conduct(unsigned char a,unsigned char b) switch(a) case 0x11:/ 1行1列,数字0if (count8)if(b=0) starbufcount = *;/ 对应密码位上显示* pwbufcount = 0;else pwcount = 0; wordbufcount = 0; lcd_display(2,count+1,wordbufcount);count+; alarm();break;case 0x21:/ 1行2列,数字1if (count8)if(b=0) starbufcount = *;/ 对应密码位上显示* pwbufcount = 1;else pwcount = 1; wordbufcount = 1; lcd_display(2,count+1,wordbufcount);count+;alarm();break;case 0x41:/ 1行3列,数字2if (count8)if(b=0) starbufcount = *;/ 对应密码位上显示* pwbufcount = 2;else pwcount = 2; wordbufcount = 2; lcd_display(2,count+1,wordbufcount);count+;alarm();break; case 0x81:/ 1行4列,数字3if (count8)if(b=0) starbufcount = *;/ 对应密码位上显示* pwbufcount = 3;else pwcount = 3; wordbufcount = 3; lcd_display(2,count+1,wordbufcount);count+;alarm();break;case 0x12:/ 2行1列,数字4if (count8)if(b=0) starbufcount = *;/ 对应密码位上显示* pwbufcount = 4;else pwcount = 4; wordbufcount = 4; lcd_display(2,count+1,wordbufcount);count+;alarm();break;case 0x22:/ 2行2列,数字5if (count8)if(b=0) starbufcount = *;/ 对应密码位上显示* pwbufcount = 5;else pwcount = 5; wordbufcount = 5; lcd_display(2,count+1,wordbufcount);count+;alarm();break;case 0x42:/ 2行3列,数字6if (count8)if(b=0) starbufcount = *;/ 对应密码位上显示* pwbufcount = 6;else pwcount = 6; wordbufcount = 6; lcd_display(2,count+1,wordbufcount);count+;alarm();break;case 0x82:/ 2行4列,数字7if (count8)if(b=0) starbufcount = *;/ 对应密码位上显示* pwbufcount = 7;else pwcount = 7; wordbufcount = 7; lcd_display(2,count+1,wo

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论