资源预览内容
第1页 / 共31页
第2页 / 共31页
第3页 / 共31页
第4页 / 共31页
第5页 / 共31页
第6页 / 共31页
第7页 / 共31页
第8页 / 共31页
第9页 / 共31页
第10页 / 共31页
亲,该文档总共31页,到这儿已超出免费预览范围,如果喜欢就下载吧!
资源描述
精品资料推荐基于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)
网站客服QQ:2055934822
金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号