單片機源程序如下:
- #include<reg52.h>
- #define uint unsigned int
- #define uchar unsigned char
- #define KEY P3 //鍵盤輸入端
- #define No_key 20 //無按鍵時返回值
- #define lcddata P2 //1602的數據輸入端口
- sbit lcden=P2^7;
- sbit lcdrs=P2^6; //0輸入指令,1輸入數據
- sbit lcdrw=P2^5; //0向LCD寫入數據或指令,1從LCD讀取信息
- sbit light_red=P3^4;
- sbit buzzer=P3^5;
- uchar j; //用來統計輸入個數的全局變量
- uchar aa; //用來在定時器中計數的全局變量
- uchar code table[]="WELCOME! ";
- uchar code table1[]="LOCK SUCCESS!";
- uchar code table3[]="LOCK FAILURE!";
- uchar code table2[]="ENTER PASSWORD:";
- uchar code key_table[]={1,2,3,10,
- 4,5,6,11,
- 7,8,9,12,
- 0,13,14,15};
- uchar password[]={2,0,1,7,1,0}; //設定初始密碼
- uchar save[6]; //保存輸入數據
- uchar conflag;
- uchar startflag;
- uchar lockflag;
- void delay(uint z);
- void wright_com(uchar com); //寫命令函數
- void wright_data(uchar date); //寫數據函數
- void init();
- void display_OK(); //顯示OK
- void delete();
- uchar keyscan();
- void enter_code(uchar t); //輸入密碼,并把輸入的數據存入數組中
- void confirm(); //輸入密碼逐一對比
- void succeed_an();
- void fail_an();
- void lockkey();
- void reset();
- void display_enter();
- void display_wrong();
- void main(void)
- {
- uchar temp;
- init();
- while(1)
- {
- if(lockflag) //輸入錯誤,才會鎖存
- {
- temp=keyscan();
- if(temp != No_key)
- {
- aa=0; //重新計時
- if(temp==10)
- {
- lcdrw=0; //清屏
- lcden=0;
- wright_com(0x01);
- reset();
- light_red=1;
- startflag=1; //開始標志置位
- }
- }
- }
- else
- {
- temp=keyscan();
- if(temp != No_key)
- {
- if(temp==10)
- {
- lcdrw=0; //清屏
- lcden=0;
- wright_com(0x01);
- reset();
- startflag=1; //開始標志置位
- }
- if(startflag)
- {
- enter_code(temp);
- if(temp==13)
- {
- confirm();
- if(conflag)
- {
- succeed_an();
- }
- else
- {
- fail_an();
- }
- }
- if(temp==14)
- {
- delete();
- }
- }
- }
- }
- }
- }
- /*顯示enter*/
- void display_enter()
- {
- uchar num;
- wright_com(0x80);
- for(num=0;num<13;num++)
- {
- wright_data(table2[num]);
- }
- }
- /*顯示OK*/
- void display_ok()
- {
- uchar num;
- wright_com(0x80);
- for(num=0;num<13;num++)
- {
- wright_data(table1[num]);
- }
- }
- /*顯示錯誤*/
- void display_wrong()
- {
- uchar num;
- wright_com(0x80);
- for(num=0;num<14;num++)
- {
- wright_data(table3[num]);
- }
- }
- void delete()
- {
- wright_com(0x80+0x40+j);
- wright_data(' ');
- save[--j]=0;
- wright_com(0x80+0x40+j);
- }
- /*復位各種變量*/
- void reset()
- {
- uchar num;
- display_enter();
- wright_com(0x80+0x40);
- for(num;num<6;num++)
- {
- save[num]=0;
- wright_data(' ');
- }
- wright_com(0x80+0x40);
- lockflag=0;
- conflag=0;
- j=0;
- }
- void succeed_an()
- {
- buzzer=0;
- display_ok();
- delay(1000);
- buzzer=1;
- }
- void fail_an()
- {
- display_wrong();
- lockkey();
- }
- void lockkey()
- {
- lockflag=1;
- }
- void enter_code(uchar t)
- {
- if(t>=0&&t<10)
- {
- if(j==0)
- {
- wright_com(0x80+0x40);
- wright_data('*');
- }
- else
- {
- wright_data('*');
- }
- save[j++]=t;
- }
- }
- void confirm()
- {
- uchar k;
- for(k=0;k<6;k++)
- { if(password[k]!=save[k])
- {
- break;
- }
- }
- if(k==6)
- {
- conflag=1;
- }
- }
- void timer0() interrupt 1
- {
- TH0=(65536-50000)/256;
- TL0=(65536-50000)%256;
- if(lockflag)
- {
- aa++;
- light_red=0;
- if(aa>=60)
- {
- aa=0;
- light_red=1;
- lockflag=0;
- }
- }
- }
- void init()
- {
- uchar num;
- TMOD=1;
- ET0=1;
- EA=1;
- TR0=1;
- lcdrw=0;
- lcden=0;
- wright_com(0x38);
- wright_com(0x0c);
- wright_com(0x01);
- wright_com(0x80);
- for(num=0;num<9;num++)
- {
- wright_data(table[num]);
- delay(1);
- }
- }
- void wright_com(uchar com)
- {
- lcdrs=0;
- lcddata=com;
- delay(1);
- lcden=1;
- delay(1);
- lcden=0;
- }
- void wright_data(uchar date)
- {
- lcdrs=1;
- lcddata=date;
- delay(1);
- lcden=1;
- delay(1);
- lcden=0;
- }
- void delay(uint z)
- {
- uint x,y;
- for(x=z;x>0;x--)
- for(y=110;y>0;y--);
- }
- /*鍵盤掃描 逐行掃描*/
- uchar keyscan()
- {
- uchar temp;
- uchar num=No_key;
- KEY=0xfe;
- temp=KEY;
- temp=temp&0xf0;
- while(temp !=0xf0)
- {
- delay(5);
- temp=KEY;
- temp=temp&0xf0;
- while(temp!=0xf0)
- {
- temp=KEY;
- switch(temp)
- {
- case 0xee:num=1;
- break;
- case 0xde:num=2;
- break;
- case 0xbe:num=3;
- break;
- case 0x7e:num=10;
- break;
- }
- while(temp!=0xf0)
- {
- temp=KEY;
- temp=temp&0xf0;
- }
- }
- }
- KEY=0xfd;
- temp=KEY;
- temp=temp&0xf0;
- while(temp!=0xf0)
- {
- delay(5);
- temp=KEY;
- temp=temp&0xf0;
- while(temp!=0xf0)
- {
- temp=KEY;
- switch(temp)
- {
- case 0xed:num=4;
- break;
- case 0xdd:num=5;
- break;
- case 0xbd:num=6;
- break;
- case 0x7d:num=11;
- break;
- }
- while(temp!=0xf0)
- {
- temp=KEY;
- temp=temp&0xf0;
- }
- }
- }
- KEY=0xfb;
- temp=KEY;
- temp=temp&0xf0;
- while(temp!=0xf0)
- {
- delay(5);
- temp=KEY;
- temp=temp&0xf0;
- while(temp!=0xf0)
- {
- temp=KEY;
- switch(temp)
- {
- case 0xeb:num=7;
- break;
- case 0xdb:num=8;
- break;
- case 0xbb:num=9;
- break;
- case 0x7b:num=12;
- break;
- }
- while(temp!=0xf0)
- {
- temp=KEY;
- temp=temp&0xf0;
- }
- }
- }
- KEY=0xf7;
- temp=KEY;
- temp=temp&0xf0;
- while(temp!=0xf0)
- {
- delay(5);
- temp=KEY;
- temp=temp&0xf0;
- while(temp!=0xf0)
- {
- temp=KEY;
- switch(temp)
- {
- case 0xe7:num=0;
- break;
- case 0xd7:num=13;
- break;
- case 0xb7:num=14;
- break;
- case 0x77:num=15;
- break;
- }
- while(temp!=0xf0)
- {
- temp=KEY;
- temp=temp&0xf0;
- }
- }
- }
- return num;
- }
復制代碼
|