第1页共31页编号:时间:2021年x月x日书山有路勤为径,学海无涯苦作舟页码:第1页共31页基于AT89C51单片机电子密码锁#include"main.h"/********************LCDPARTSTART*******************************///5ms延时voidDelay5Ms(void){unsignedintTempCyc=5552;while(TempCyc--);}//读状态unsignedcharReadStatusLCM(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);}第2页共31页第1页共31页编号:时间:2021年x月x日书山有路勤为径,学海无涯苦作舟页码:第2页共31页//写数据voidWriteDataLCM(unsignedcharWDLCM){ReadStatusLCM();//检测忙LCM_Data=WDLCM;LCM_RS=1;LCM_RW=0;LCM_E=0;//若晶振速度太高可以在这后加小的延时LCM_E=0;//延时LCM_E=1;}//写指令voidWriteCommandLCM(unsignedcharWCLCM,BuysC)//BuysC为0时忽略忙检测{if(BuysC)ReadStatusLCM();//根据需要检测忙LCM_Data=WCLCM;LCM_RS=0;LCM_RW=0;LCM_E=0;LCM_E=0;LCM_E=1;}//读数据unsignedcharReadDataLCM(void)第3页共31页第2页共31页编号:时间:2021年x月x日书山有路勤为径,学海无涯苦作舟页码:第3页共31页{LCM_RS=1;LCM_RW=1;LCM_E=0;LCM_E=0;LCM_E=1;return(LCM_Data);}voidLCMInit(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);//显示开及光标设置}第4页共31页第3页共31页编号:时间:2021年x月x日书山有路勤为径,学海无涯苦作舟页码:第4页共31页//按指定位置显示一个字符,x表示列,Y表示行voidLCD_write_char(unsignedcharX,unsignedcharY,unsignedcharDData){Y&=0x1;X&=0xF;//限制X不能大于15,Y不能大于1if(Y)X|=0x40;//当要显示第二行时地址码+0x40;X|=0x80;//算出指令码WriteCommandLCM(X,0);//这里不检测忙信号,发送地址码WriteDataLCM(DData);}//按指定位置显示一串字符voidLCD_write_string(unsignedcharX,unsignedcharY,unsignedcharcode*DData){unsignedcharListLength;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++;第5页共31页第4页共31页编号:时间:2021年x月x日书山有路勤为径,学海无涯苦作舟页码:第5页共31页X++;}}}/************/#include
#include#defineucharunsignedchar#defineuintunsignedint/********************LCDPARTSTART*******************************/voiddelay(uintz)//延时{uintx,y;for(x=z;x>0;x--)for(y=124;y>0;y--);}sbite=P2^5;sbitrw=P2^6;sbitsr=P2^7;ucharcodename[]="zxs";ucharcodename1[]="zcf";ucharcodename2[]="zx";第6页共31页第5页共31页编号:时间:2021年x月x日书山有路勤为径,学海无涯苦作舟页码:第6页共31页voidwrite_com(ucharcom){intrs;rs=0;P0=com;delay(5);e=1;delay(5);e=0;}voidwrite_data(uchardate){intrs;rs=1;P0=date;delay(5);e=1;delay(5);e=0;}voidinit(){e=0;rw=0;write_com(0x38);第7页共31页第6页共31页编号:时间:2021年x月x日书山有路勤为径,学海无涯苦作舟页码:第7页共31页write_com(0x0c);write_com(0x06);write_com(0x01);write_com(0x80);}voiddisplay(){uchari;for(i=0;i<3;i++){write_data(name[i]);}write_com(0x80+0x40);for(i=0;i<3;i++){write_data(name1[i]);}}voidmain(){init();display();}第8页共31页第7页共31页编号:时间:2021年x月x日书山有路勤为径,学海无涯苦作舟页码:第8页共31页#include#defineucharunsignedcharucharstarbuf[10];ucharwordbuf[8];ucharpw[8]={1,2,3,4,5,6,7,8};ucharpwbuf[8];ucharc...