基于51單片機
51hei.png (16.62 KB, 下載次數: 14)
下載附件
2020-1-15 00:55 上傳
單片機源程序如下:
- #include <intrins.h>
- #include <reg52.h>
- #define ulong unsigned long
- #define uint unsigned int
- #define uchar unsigned char
- uchar temp,num,flag,r;
- /******************************/
- //顯示字符串定義
- uchar code table1[]="You are welcome";
- uchar code table3[]="******";
- uchar code table4[]="code is wrong!";
- uchar code table5[]="you are right!";
- uchar code table6[]="the second time!";
- uchar code table7[]="the last time!";
- uchar code table8[]="Input new password:";
- uchar code table9[]="Modify right!!!";
- uchar code table10[]="password:";
- uchar code table12[]=" calling....";
- uchar code table13[]=" ";
- uchar code table14[]="Input again:";
- uchar code table15[]="Modify error!!!";
- /******************************/
- uchar init_password[6]={1,2,3,4,5,6};//定義原始密碼存放的數組
- uchar password[6]={0};//定義輸入密碼存放的數組
- /******************************/
- //1602位定義
- sbit lcdrs=P2^0;// 數據/指令
- sbit lcdrw=P2^1;// 讀/寫
- sbit lcden=P2^2;// 使能端
- /******************************/
- sbit beep=P3^7;
- /******************************/
- void delay(uint z);
- void init();
- void write_com(uchar com);
- void write_data(uchar dat1);
- void keyscan();
- void mima();
- void mima1();
- void delete();
- /******************************/
- //1602初始化設置
- void init()
- {
- lcden=0;//使能
- write_com(0x38);//顯示模式設置
- write_com(0x0e);//顯示開關設置(顯示有效、光標顯示有效、光標閃爍無效)?
- write_com(0x06);//光標移動設置(讀或寫一個字符后地址指針加一,光標加一,寫一個字符串后,整屏顯示不移動)
- write_com(0x01);//顯示清屏
- write_com(0x80);//讀忙
- }
- /******************************/
- //向1602寫指令
- void write_com(uchar com)
- {
- lcdrs=0;//指令
- lcdrw=0;//寫
- P0=com;//將指令代碼送到P0口,通過數據線給1602
- delay(5);
- lcden=1;
- delay(5);
- lcden=0;//下降沿使能
- }
- /******************************/
- //向1602寫數據
- void write_data(uchar dat1)
- {
- lcdrs=1;//數據
- lcdrw=0;//寫
- P0=dat1;
- delay(5);
- lcden=1;
- delay(5);
- lcden=0;//下降沿使能
- }
- /******************************/
- //延時子程序
- void delay(uint z)
- {
- uint x,y;
- for(x=z;x>0;x--)
- for(y=110;y>0;y--);
- }
- /******************************/
- //按鍵掃描子程序
- void keyscan()
- {
- P1=0x7f;
- temp=P1;
- temp=temp&0x0f;
- while(temp!=0x0f)
- {
- delay(5);
- temp=P1;
- temp=temp&0x0f;
- while(temp!=0x0f)
- {
- temp=P1;//按鍵消抖
- switch(temp)
- {
- case 0x7e:num=0;flag=1;//按鍵0被按下,且0為有效的按鍵
- break;
- case 0x7d:num=1;flag=1;//按鍵1被按下,且1為有效的按鍵
- break;
- case 0x7b:num=2;flag=1;//按鍵2被按下,且2為有效的按鍵
- break;
- case 0x77:num=3;flag=1;//按鍵3被按下,且3為有效的按鍵
- break;
- }
- while(temp!=0x0f)
- {
- temp=P1;
- temp=temp&0x0f;
- }
- }
- }
- P1=0xbf;
- temp=P1;
- temp=temp&0x0f;
- while(temp!=0x0f)
- {
- delay(5);
- temp=P1;
- temp=temp&0x0f;
- while(temp!=0x0f)
- {
- temp=P1;//消抖
- switch(temp)
- {
- case 0xbe:num=4;flag=1;//按鍵4被按下,且4為有效的按鍵
- break;
- case 0xbd:num=5;flag=1;//按鍵5被按下,且5為有效的按鍵
- break;
- case 0xbb:num=6;flag=1;//按鍵6被按下,且6為有效的按鍵
- break;
- case 0xb7:num=7;flag=1;//按鍵7被按下,且7為有效的按鍵
- break;
- }
- while(temp!=0x0f)
- {
- temp=P1;
- temp=temp&0x0f;
- }
- }
- }
- P1=0xdf;
- temp=P1;
- temp=temp&0x0f;
- while(temp!=0x0f)
- {
- delay(5);
- temp=P1;
- temp=temp&0x0f;
- while(temp!=0x0f)
- {
- temp=P1;
- switch(temp)
- {
- case 0xde:num=8;flag=1;//按鍵8被按下,且8為有效的按鍵
- break;
- case 0xdd:num=9;flag=1;//按鍵9被按下,且9為有效的按鍵
- break;
- case 0xdb:num=10;//按鍵10被按下,且該鍵無效
- break;
- case 0xd7:num=11;//按鍵11被按下,且該鍵無效
- break;
- }
- while(temp!=0x0f)
- {
- temp=P1;
- temp=temp&0x0f;
- }
- }
- }
- P1=0xef;
- temp=P1;
- temp=temp&0x0f;
- while(temp!=0x0f)
- {
- delay(5);
- temp=P1;
- temp=temp&0x0f;
- while(temp!=0x0f)
- {
- temp=P1;
- switch(temp)
- {
- case 0xee:num=12;//按鍵12被按下,且該鍵無效
- break;
- case 0xed:num=13;flag=1;//按鍵13,即修改密碼鍵被按下,且13為有效的按鍵
- break;
- case 0xeb:num=14;flag=1;//按鍵14,即退格鍵被按下,且14為有效的按鍵
- break;
- case 0xe7:num=15;flag=1;//按鍵15,即輸入密碼鍵被按下,且15為有效的按鍵
- break;
- }
- while(temp!=0x0f)
- {
- temp=P1;
- temp=temp&0x0f;
- }
- }
- }
- }
- /******************************/
- void main()
- {
- uchar m=0,w=0;
- init();//1602初始化
- for(r=0;r<15;r++)
- {
- delay(5);
- write_data(table1[r]);
- }//顯示歡迎界面
- write_com(0x0c);//顯示,關光標
- do
- {
- keyscan();
- }
- while(num!=15);//在輸入密碼鍵按下之前一直進行鍵盤掃描
- mima();//子程序調用
- while((password[m]==init_password[m])&m<6)
- {
- m++;
- }
- if(m>=6)
- {
-
- mima1();//子程序調用
- }
- else
- {
- m=0;
- write_com(0x80+0x40);//顯示從第二行開始
- for(r=0;r<14;r++)
- {
- delay(20);
- write_data(table4[r]);
- }//顯示密碼錯誤
- delay(2000);//延時
- init();//1602初始化
- for(r=0;r<16;r++)
- {
- delay(20);
- write_data(table6[r]);
- }//顯示第二次輸入
- delay(2000);
- mima();
- while((password[m]==init_password[m])&m<6 )
- {
- m++;
- }
- if(m>=6)
- {
- mima1();
- }
- else
-
- {
- m=0;
- write_com(0x80+0x40);
- for(r=0;r<14;r++)
- {
- delay(20);
- write_data(table4[r]);
- }//顯示密碼錯誤
- delay(2000);
- init();//1602初始化
- for(r=0;r<14;r++)
- {
- delay(20);
- write_data(table7[r]);
- }//最后一次機會
- delay(2000);
- mima();
-
- while((password[m]==init_password[m])&m<6)
- {
- m++;
- }
- if(m>=6)
- {
-
- mima1();
- }
- else
- {
- init();
- write_com(0x80+0x40);
- for(r=0;r<14;r++)
- {
- delay(20);
- write_data(table12[r]);
- }//顯示報警
- write_com(0x0c);
- while(1)
- {
- delay(1);
- beep=~beep;
- }//喇叭發出警報音
- }
-
-
- }
-
- }
-
- }
- void mima()
- {
- uchar w=0;
- init();
- for(r=0;r<9;r++)
- {
- delay(20);
- write_data(table10[r]);
- }//顯示請輸入密碼
- do
- {
- flag=0;
- keyscan();
- write_com(0x80+9+w);//接著password:后第w位處顯示
- if(flag==1&&num<=9)//輸入的是0—9的情況下
- {
- password[w]=num;//將輸入的數存入數組的相應位
- write_data(table3[w]);//1602顯示*
- w++;//位數加一
- if(w==6)
- write_com(0x0c);//六位都輸入后,顯示從屏幕左方開始
- }
- if(flag==1&&num==14)//如果退格鍵被按下
- {
- w--;//位數減一
- if(w==255)
- w=w+1;//如果位數變成-1,即255,則加一,變成0
- write_com(0x80+9+w);//從字符串后第w位處開始顯示
- write_data(table13[w+1]);//顯示*
- if(flag==1&&num<=9)
- {
- password[w]=num;
- write_data(table3[w]);
- }
- }
- } while(w<=5);
- }
- void mima1()
- {
- uchar f=0,n=0,d=0;
- write_com(0x80+0x40); //從第二行開始顯示
- for(r=0;r<14;r++)
- {
- delay(20);
- write_data(table5[r]);//顯示密碼正確
- }
- delay(2000);
- do
- {
- num=16;
- keyscan();
- }while(num!=13);//在改密碼鍵按下前一直掃描鍵盤
- if(flag==1&&num==13)
- {
- init();//顯示初始化
- for(r=0;r<15;r++)
- {
- delay(20);
- write_data(table8[r]);//顯示輸入新密碼
- }
- write_com(0x0c);//顯示,關光標
- write_com(0x80+0x40+n);//從第二行開始顯示
- write_com(0x0e);//顯示開關初始化
- }
- do{
- num=16;
- keyscan();
- write_com(0x80+0x40+n);
- if(flag==1&&num<=9)
- {
- init_password[n]=num;//將輸入值存入新的密碼數組的相應位
- write_data(table3[n]);//顯示*
- n++;
- if(n==6)
- write_com(0x0c);
- }
- if(flag==1&&num==14)
- {
- n--;//如果退格鍵按下,左移一位
- if(n==255)
- n=n+1;//如果n減為負值則從0開始
- write_com(0x80+0x40+n);//從相應位置開始顯示
- write_data(table13[n]);//顯示空字符
- if(flag==1&&num<=9)
- {
- password[n]=num;
- write_data(table3[n]);
- }
- }
- }
- while(n<=5);
- init();
- for(r=0;r<12;r++)
- {
- delay(20);
- write_data(table14[r]);
- }//顯示重新輸入
- do
- {
- flag=0;
- keyscan();
- ……………………
- …………限于本文篇幅 余下代碼請從51黑下載附件…………
復制代碼
所有資料51hei提供下載:
LCD1602 16點鍵盤.zip
(44.44 KB, 下載次數: 31)
2020-1-14 20:56 上傳
點擊文件名下載附件
下載積分: 黑幣 -5
|