#include<reg52.h> #include<intrins.h> #define uint unsigned int #define uchar unsigned char #define ulong unsigned long //這三個(gè)引腳參考資料 sbit E=P2^7; //1602使能引腳 sbit RW=P2^6; //1602讀寫(xiě)引腳 sbit RS=P2^5; //1602數(shù)據(jù)/命令選擇引腳 void Delay_1ms(uint i) { uint x,j; for(j=0;j<i;j++) for(x=0;x<=148;x++); } void delay() { _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); } bit Busy(void)//狀態(tài)判斷 { bit busy_flag = 0; RS = 0; RW = 1; E = 1; delay(); busy_flag = (bit)(P0 & 0x80); E = 0; return busy_flag; } void wcmd(uchar del) // 功能 : 1602命令函數(shù) { while(Busy()); RS = 0; RW = 0; E = 0; delay(); P0 = del; delay(); E = 1; delay(); E = 0; } void wdata(uchar del) //功能 : 1602寫(xiě)數(shù)據(jù)函數(shù) { while(Busy()); RS = 1; RW = 0; E = 0; delay(); P0 = del; delay(); E = 1; delay(); E = 0; } void L1602_init(void) // 功能 : 1602初始化 { wcmd(0x38); Delay_1ms(5); wcmd(0x38); Delay_1ms(5); wcmd(0x38); Delay_1ms(5); wcmd(0x38); wcmd(0x08); wcmd(0x0c); wcmd(0x04); wcmd(0x01); } void L1602_char(uchar hang,uchar lie,char sign) { uchar a; if(hang == 1) a = 0x80; if(hang == 2) a = 0xc0; a = a + lie - 1; wcmd(a); wdata(sign); } void L1602_string(uchar hang,uchar lie,uchar *p) { uchar a,b=0; if(hang == 1) a = 0x80; if(hang == 2) a = 0xc0; a = a + lie - 1; while(1) { wcmd(a++); b++; if((*p == '\0')||(b==16)) break; wdata(*p); p++; } } uchar Keyscan() { uchar num,temp; while(1) { P3=0xfe; temp=P3; temp=temp&0xf0; while(temp!=0xf0) { delay(); temp=P3; temp=temp&0xf0; while(temp!=0xf0) { temp=P3; switch(temp) { case 0xee:num=0; break; case 0xde:num=1; break; case 0xbe:num=2; break; case 0x7e:num=3; break; } while(temp!=0xf0) { temp=P3; temp=temp&0xf0; } return num; } } P3=0xfd; temp=P3; temp=temp&0xf0; while(temp!=0xf0) { delay(); temp=P3; temp=temp&0xf0; while(temp!=0xf0) { temp=P3; switch(temp) { case 0xed:num=4; break; case 0xdd:num=5; break; case 0xbd:num=6; break; case 0x7d:num=7; break; } while(temp!=0xf0) { temp=P3; temp=temp&0xf0; } return num; } } P3=0xfb; temp=P3; temp=temp&0xf0; while(temp!=0xf0) { delay(); temp=P3; temp=temp&0xf0; while(temp!=0xf0) { temp=P3; switch(temp) { case 0xeb:num=8; break; case 0xdb:num=9; break; case 0xbb:num=10; break; case 0x7b:num=11; break; } while(temp!=0xf0) { temp=P3; temp=temp&0xf0; } return num; } } P3=0xf7; temp=P3; temp=temp&0xf0; while(temp!=0xf0) { delay(); temp=P3; temp=temp&0xf0; while(temp!=0xf0) { temp=P3; switch(temp) { case 0xe7:num=12; break; case 0xd7:num=13; break; case 0xb7:num=14; break; case 0x77:num=15; break; } while(temp!=0xf0) { temp=P3; temp=temp&0xf0; } return num; } } } } void Main(void) { uchar i=6,j=0; uchar SLED[6]={0}; //讀出的鍵值 ulong Key_Value; ulong code6wei = 0x123456; //16進(jìn)制6位數(shù) ulong codebijiao = 0; L1602_init(); L1602_string(1,1," The code is:"); while(1) { Key_Value=10; Key_Value = Keyscan(); if(Key_Value < 10) L1602_char(2,i,Key_Value + 48); else L1602_char(2,i,Key_Value + 87); codebijiao = codebijiao | (Key_Value <<((5-j)*4)); i++; j++; Delay_1ms(300); if(j==6) { if(codebijiao == code6wei) { wcmd(0x01); L1602_string(1,1,"right!!!"); L1602_string(2,1,"welcome back!!!"); while(1); } else { wcmd(0x01); L1602_string(1,1,"wrong!!!"); L1602_string(2,1,"Input afresh!!!"); Delay_1ms(2000); j = 0; i = 6; wcmd(0x01); L1602_string(1,1," The code is:"); } } } }