能实现密码锁功能,输入错误报警等 #include #include #define LCM_Data P0 #define uchar unsigned char #define uint unsigned int #define w 6 //?????? ***it lcd1602_rs=P2^7; ***it lcd1602_rw=P2^6; ***it lcd1602_en=P2^5; ***it Scl=P3^4; //24C02???? ***it Sda=P3^5; //24C02???? ***it ALAM = P2^1; //?? ***it KEY = P3^6; //?? ***it open_led=P2^2; //????? bit operation=0; //????? bit pass=0; //?????? bit ReInputEn=0; //???????? bit s3_keydown=0; //3?????? bit key_disable=0; //?????? unsigned char countt0,second; //t0?????,???? void Delay5Ms(void); unsigned char code a[]={0xFE,0xFD,0xFB,0xF7}; //??????? unsigned char code start_line[] = {"password: "}; unsigned char code name[] = {"===Coded Lock==="}; //???? unsigned char code Correct[] = {" correct "}; //???? unsigned char code Error[] = {" error "}; //???? unsigned char code codepass[] = {" pass "}; unsigned char code LockOpen[] = {" open "}; //OPEN unsigned char code SetNew[] = {"SetNewWordEnable"}; unsigned char code Input[] = {"input: "}; //INPUT unsigned char code ResetOK[] = {"ResetPasswordOK "}; unsigned char code initword[] = {"Init password..."}; unsigned char code Er_try[] = {"error,try again!"}; unsigned char code again[] = {"input again "}; unsigned char InputData[6]; //??????? unsigned char CurrentPassword[6]={1,3,1,4,2,0}; //????? unsigned char TempPassword[6]; unsigned char N=0; //???????? unsigned char ErrorCont; //?????? unsigned char CorrectCont; //?????? unsigned char ReInputCont; //?????? unsigned char code initpassword[6]={0,0,0,0,0,0}; //=====================5ms??============================== void Delay5Ms(void) { unsigned int TempCyc = 5552; while(TempCyc--); } //===================400ms??============================== void Delay400Ms(void) { unsigned char TempCycA = 5; unsigned int TempCycB; while(TempCycA--) { TempCycB=7269; while(TempCycB--); } } //================================24C02========================================= void mDelay(uint t) //?? { uchar i; while(t--) { for(i=0;i<125;i++) {;} } } void Nop(void) //??? { _nop_(); _nop_(); _nop_(); _nop_(); } /*????*/ void Start(void) { Sda=1; Scl=1; Nop(); Sda=0; Nop(); } /*????*/ void Stop(void) { Sda=0; Scl=1; Nop(); Sda=1; Nop(); } /*???*/ void Ack(void) { Sda=0; Nop(); Scl=1; Nop(); Scl=0; } /*?????*/ void NoAck(void) { Sda=1; Nop(); Scl=1; Nop(); Scl=0; } /*???????,Data????????*/ void Send(uchar Data) { uchar BitCounter=8; uchar temp; do { temp=Data; Scl=0; Nop(); if((temp&0x80)==0x80) Sda=1; else Sda=0; Scl=1; temp=Data<<1; Data=temp; BitCounter--; } while(BitCounter); Scl=0; } /*???????,???????*/ uchar Read(void) { uchar temp=0; uchar temp1=0; uchar BitCounter=8; Sda=1; do{ Scl=0; Nop(); Scl=1; Nop(); if(Sda) temp=temp|0x01; else temp=temp&0xfe; if(BitCounter-1) { temp1=temp<<1; temp=temp1; } BitCounter--; } while(BitCounter); return(temp); } void WrToROM(uchar Data[],uchar Address,uchar Num) { uchar i; uchar *PData; PData=Data; for(i=0;i Start(); Send(0xa0); Ack(); Send(Address+i); Ack(); Send(*(PData+i)); Ack(); Stop(); mDelay(20); } } void RdFromROM(uchar Data[],uchar Address,uchar Num) { uchar i; uchar *PData; PData=Data; for(i=0;i Start(); Send(0xa0); Ack(); Send(Address+i); Ack(); Start(); Send(0xa1); Ack(); *(PData+i)=Read(); Scl=0; NoAck(); Stop(); } } //=======================================LCD1602=============================== #define yi 0x80 //LCD????????,??LCD1602??????D7???1(100000000=80) #define er 0x80+0x40 //LCD???????(???????????????0x40) //----------------????,??????---------------------- void delay(uint xms)//????,???? { uint x,y; for(x=xms;x>0;x--) for(y=110;y>0;y--); } //--------------------------???--------------------------- write_1602com(uchar com)//****????????**** { lcd1602_rs=0;//??/???????? lcd1602_rw=0; //??????? P0=com;//???? delay(1); lcd1602_en=1;//?????,???????????? delay(1); lcd1602_en=0;//en????,?????,?????? } //-------------------------???----------------------------- write_1602dat(uchar dat)//***????????**** { lcd1602_rs=1;//??/???????? lcd1602_rw=0; //??????? P0=dat;//???? delay(1); lcd1602_en=1; //en????,????????? delay(1); lcd1602_en=0; //en????,?????,?????? } //-------------------------???------------------------- void lcd_init(void) { write_1602com(0x38);//????????,??:16*2???,5*7??,8??? write_1602com(0x0c);//???????? write_1602com(0x06);//?????,?????? write_1602com(0x01);//??? } //==============?????????========================= unsigned char coding(unsigned char m) { unsigned char k; switch(m) { case (0x11): k=1;break; case (0x21): k=2;break; case (0x41): k=3;break; case (0x81): k='A';break; case (0x12): k=4;break; case (0x22): k=5;break; case (0x42): k=6;break; case (0x82): k='B';break; case (0x14): k=7;break; case (0x24): k=8;break; case (0x44): k=9;break; case (0x84): k='C';break; case (0x18): k='*';break; case (0x28): k=0;break; case (0x48): k='#';break; case (0x88): k='D';break; } return(k); } //=====================??????????=============================== unsigned char keynum(void) { unsigned char row,col,i; P1=0xf0; if((P1&0xf0)!=0xf0) { Delay5Ms(); Delay5Ms(); if((P1&0xf0)!=0xf0) { row=P1^0xf0; //???? i=0; P1=a; //???? while(i<4) { if((P1&0xf0)!=0xf0) { col=~(P1&0xff); //???? break; //???????? } else { i++; P1=a; } } } else { return 0; } while((P1&0xf0)!=0xf0); return (row|col); //?????????? } else return 0; //???????0 } //=======================?????,??????======================== void OneAlam(void) { ALAM=0; Delay5Ms(); ALAM=1; } //========================?????,??????======================== void TwoAlam(void) { ALAM=0; Delay5Ms(); ALAM=1; Delay5Ms(); ALAM=0; Delay5Ms(); ALAM=1; } //========================?????,????======================== void ThreeAlam(void) { ALAM=0; Delay5Ms(); ALAM=1; Delay5Ms(); ALAM=0; Delay5Ms(); ALAM=1; Delay5Ms(); ALAM=0; Delay5Ms(); ALAM=1; } //===================?????N???,?H??????============================ void DisplayOne(void) { // DisplayOneChar(9+N,1,'*'); write_1602com(yi+5+N); write_1602dat('*'); } //=======================??????========================= void DisplayChar(void) { unsigned char i; if(pass==1) { //DisplayListChar(0,1,LockOpen); write_1602com(er); for(i=0;i<16;i++) { write_1602dat(LockOpen); } } else { if(N==0) { //DisplayListChar(0,1,Error); write_1602com(er); for(i=0;i<16;i++) { write_1602dat(Error); } } else { //DisplayListChar(0,1,start_line); write_1602com(er); for(i=0;i<16;i++) { write_1602dat(start_line); } } } } void DisplayInput(void) { unsigned char i; if(CorrectCont==1) { //DisplayListChar(0,0,Input); write_1602com(er); for(i=0;i<16;i++) { write_1602dat(Input); } } } //========================????================================// void ResetPassword(void) { unsigned char i; unsigned char j; if(pass==0) { pass=0; DisplayChar(); ThreeAlam(); } else { if(ReInputEn==1) { if(N==6) { ReInputCont++; if(ReInputCont==2) { for(i=0;i<6;) { if(TempPassword==InputData) //???????????? i++; else { //DisplayListChar(0,1,Error); write_1602com(er); for(j=0;j<16;j++) { write_1602dat(Error[j]); } ThreeAlam(); //???? pass=0; ReInputEn=0; //??????, ReInputCont=0; DisplayChar(); break; } } if(i==6) { //DisplayListChar(0,1,ResetOK); write_1602com(er); for(j=0;j<16;j++) { write_1602dat(ResetOK[j]); } TwoAlam(); //?????? WrToROM(TempPassword,0,6); //??????24C02?? ReInputEn=0; } ReInputCont=0; CorrectCont=0; } else { OneAlam(); //DisplayListChar(0, 1, again); //???????? write_1602com(er); for(j=0;j<16;j++) { write_1602dat(again[j]); } for(i=0;i<6;i++) { TempPassword=InputData; //????????????? } } N=0; //??????????? } } } } //=======================??????????,???????====================== void Alam_KeyUnable(void) { P1=0x00; { ALAM=~ALAM; Delay5Ms(); } } //=======================??????============================================ void Cancel(void) { unsigned char i; unsigned char j; //DisplayListChar(0, 1, start_line); write_1602com(er); for(j=0;j<16;j++) { write_1602dat(start_line[j]); } TwoAlam(); //??? for(i=0;i<6;i++) { InputData=0; } KEY=1; //??? ALAM=1; //??? operation=0; //??????? pass=0; //???????? ReInputEn=0; //?????????? ErrorCont=0; //?????????? CorrectCont=0; //?????????? ReInputCont=0; //?????????? open_led=1; s3_keydown=0; key_disable=0; N=0; //????????? } //==========================???,?????????????? void Ensure(void) { unsigned char i,j; RdFromROM(CurrentPassword,0,6); //?24C02??????? if(N==6) { if(ReInputEn==0) //????????? { for(i=0;i<6;) { if(CurrentPassword==InputData) { i++; } else { ErrorCont++; if(ErrorCont==3) //??????????,??????? { write_1602com(er); for(i=0;i<16;i++) { write_1602dat(Error); } do Alam_KeyUnable(); while(1); } else { TR0=1; //???? key_disable=1; //???? pass=0; break; } } } if(i==6) { CorrectCont++; if(CorrectCont==1) //??????,??????????,??, { //DisplayListChar(0,1,LockOpen); write_1602com(er); for(j=0;j<16;j++) { write_1602dat(LockOpen[j]); } TwoAlam(); //??????? KEY=0; //?? pass=1; //?????? TR0=1; //???? open_led=0; //?????? for(j=0;j<6;j++) //????? { InputData=0; } } else //????????,???????? { //DisplayListChar(0,1,SetNew); write_1602com(er); for(j=0;j<16;j++) { write_1602dat(SetNew[j]); } TwoAlam(); //?????? ReInputEn=1; //???????? CorrectCont=0; //??????? } } else //=========================???????????????131420???????============ { if((InputData[0]==1)&&(InputData[1]==3)&&(InputData[2]==1)&&(InputData[3]==4)&&(InputData[4]==2)&&(InputData[5]==0)) { WrToROM(initpassword,0,6); //?????????24C02?? //DisplayListChar(0,1,initword); //??????? write_1602com(er); for(j=0;j<16;j++) { write_1602dat(initword[j]); } TwoAlam(); Delay400Ms(); TwoAlam(); N=0; } else { //DisplayListChar(0,1,Error); write_1602com(er); for(j=0;j<16;j++) { write_1602dat(Error[j]); } ThreeAlam(); //????? pass=0; } } } else //????????????,??????, { //DisplayListChar(0,1,Er_try); write_1602com(er); for(j=0;j<16;j++) { write_1602dat(Er_try[j]); } ThreeAlam(); } } else { //DisplayListChar(0,1,Error); write_1602com(er); for(j=0;j<16;j++) { write_1602dat(Error[j]); } ThreeAlam(); //????? pass=0; } N=0; //??????????,????????? operation=1; } //==============================???=============================== void main(void) { unsigned char KEY,NUM; unsigned char i,j; P1=0xFF; TMOD=0x11; TL0=0xB0; TH0=0x3C; EA=1; ET0=1; TR0=0; Delay400Ms(); //????,?LCM?????? lcd_init(); //LCD??? write_1602com(yi);//?????????????0????????? for(i=0;i<16;i++) { write_1602dat(name);//???????????????? } write_1602com(er);//????????????,??2???????? for(i=0;i<16;i++) { write_1602dat(start_line);//?????????,???? } write_1602com(er+9); //?????? write_1602com(0x0f); //??????? Delay5Ms(); //????(???) N=0; //????????? while(1) { if(key_disable==1) Alam_KeyUnable(); else ALAM=1; //??? KEY=keynum(); if(KEY!=0) { if(key_disable==1) { second=0; } else { NUM=coding(KEY); { switch(NUM) { case ('A'): ; break; case ('B'): ; break; case ('C'): ; break; case ('D'): ResetPassword(); break; //?????? case ('*'): Cancel(); break; //?????? case ('#'): Ensure(); break; //???, default: { //DisplayListChar(0,1,Input); write_1602com(er); for(i=0;i<16;i++) { write_1602dat(Input); } operation=0; if(N<6) //????????6??,???????,??6?????? { OneAlam(); //????? //DisplayOneChar(6+N,1,'*'); for(j=0;j<=N;j++) { write_1602com(er+6+j); write_1602dat('*'); } InputData[N]=NUM; N++; } else //????????6?,???? { N=6; break; } } } } } } } } //*********************************??????************************************** void time0_int(void) interrupt 1 { TL0=0xB0; TH0=0x3C; //TR0=1; countt0++; if(countt0==20) { countt0=0; second++; if(pass==1) { if(second==1) { open_led=1; //???? TR0=0; //???? TL0=0xB0; TH0=0x3C; second=0; } } else { if(second==3) { TR0=0; second=0; key_disable=0; s3_keydown=0; TL0=0xB0; TH0=0x3C; } else TR0=1; } } } |
更多回帖