|
基于51單片機(jī)
51hei.png (16.62 KB, 下載次數(shù): 14)
下載附件
2020-1-15 00:55 上傳
單片機(jī)源程序如下:
- #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};//定義原始密碼存放的數(shù)組
- uchar password[6]={0};//定義輸入密碼存放的數(shù)組
- /******************************/
- //1602位定義
- sbit lcdrs=P2^0;// 數(shù)據(jù)/指令
- sbit lcdrw=P2^1;// 讀/寫(xiě)
- 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初始化設(shè)置
- void init()
- {
- lcden=0;//使能
- write_com(0x38);//顯示模式設(shè)置
- write_com(0x0e);//顯示開(kāi)關(guān)設(shè)置(顯示有效、光標(biāo)顯示有效、光標(biāo)閃爍無(wú)效)?
- write_com(0x06);//光標(biāo)移動(dòng)設(shè)置(讀或?qū)懸粋(gè)字符后地址指針加一,光標(biāo)加一,寫(xiě)一個(gè)字符串后,整屏顯示不移動(dòng))
- write_com(0x01);//顯示清屏
- write_com(0x80);//讀忙
- }
- /******************************/
- //向1602寫(xiě)指令
- void write_com(uchar com)
- {
- lcdrs=0;//指令
- lcdrw=0;//寫(xiě)
- P0=com;//將指令代碼送到P0口,通過(guò)數(shù)據(jù)線(xiàn)給1602
- delay(5);
- lcden=1;
- delay(5);
- lcden=0;//下降沿使能
- }
- /******************************/
- //向1602寫(xiě)數(shù)據(jù)
- void write_data(uchar dat1)
- {
- lcdrs=1;//數(shù)據(jù)
- lcdrw=0;//寫(xiě)
- P0=dat1;
- delay(5);
- lcden=1;
- delay(5);
- lcden=0;//下降沿使能
- }
- /******************************/
- //延時(shí)子程序
- 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被按下,且該鍵無(wú)效
- break;
- case 0xd7:num=11;//按鍵11被按下,且該鍵無(wú)效
- 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被按下,且該鍵無(wú)效
- 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);//顯示,關(guān)光標(biāo)
- do
- {
- keyscan();
- }
- while(num!=15);//在輸入密碼鍵按下之前一直進(jìn)行鍵盤(pán)掃描
- mima();//子程序調(diào)用
- while((password[m]==init_password[m])&m<6)
- {
- m++;
- }
- if(m>=6)
- {
-
- mima1();//子程序調(diào)用
- }
- else
- {
- m=0;
- write_com(0x80+0x40);//顯示從第二行開(kāi)始
- for(r=0;r<14;r++)
- {
- delay(20);
- write_data(table4[r]);
- }//顯示密碼錯(cuò)誤
- delay(2000);//延時(shí)
- 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]);
- }//顯示密碼錯(cuò)誤
- delay(2000);
- init();//1602初始化
- for(r=0;r<14;r++)
- {
- delay(20);
- write_data(table7[r]);
- }//最后一次機(jī)會(huì)
- 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]);
- }//顯示報(bào)警
- write_com(0x0c);
- while(1)
- {
- delay(1);
- beep=~beep;
- }//喇叭發(fā)出警報(bào)音
- }
-
-
- }
-
- }
-
- }
- void mima()
- {
- uchar w=0;
- init();
- for(r=0;r<9;r++)
- {
- delay(20);
- write_data(table10[r]);
- }//顯示請(qǐng)輸入密碼
- do
- {
- flag=0;
- keyscan();
- write_com(0x80+9+w);//接著password:后第w位處顯示
- if(flag==1&&num<=9)//輸入的是0—9的情況下
- {
- password[w]=num;//將輸入的數(shù)存入數(shù)組的相應(yīng)位
- write_data(table3[w]);//1602顯示*
- w++;//位數(shù)加一
- if(w==6)
- write_com(0x0c);//六位都輸入后,顯示從屏幕左方開(kāi)始
- }
- if(flag==1&&num==14)//如果退格鍵被按下
- {
- w--;//位數(shù)減一
- if(w==255)
- w=w+1;//如果位數(shù)變成-1,即255,則加一,變成0
- write_com(0x80+9+w);//從字符串后第w位處開(kāi)始顯示
- 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); //從第二行開(kāi)始顯示
- for(r=0;r<14;r++)
- {
- delay(20);
- write_data(table5[r]);//顯示密碼正確
- }
- delay(2000);
- do
- {
- num=16;
- keyscan();
- }while(num!=13);//在改密碼鍵按下前一直掃描鍵盤(pán)
- if(flag==1&&num==13)
- {
- init();//顯示初始化
- for(r=0;r<15;r++)
- {
- delay(20);
- write_data(table8[r]);//顯示輸入新密碼
- }
- write_com(0x0c);//顯示,關(guān)光標(biāo)
- write_com(0x80+0x40+n);//從第二行開(kāi)始顯示
- write_com(0x0e);//顯示開(kāi)關(guān)初始化
- }
- do{
- num=16;
- keyscan();
- write_com(0x80+0x40+n);
- if(flag==1&&num<=9)
- {
- init_password[n]=num;//將輸入值存入新的密碼數(shù)組的相應(yīng)位
- write_data(table3[n]);//顯示*
- n++;
- if(n==6)
- write_com(0x0c);
- }
- if(flag==1&&num==14)
- {
- n--;//如果退格鍵按下,左移一位
- if(n==255)
- n=n+1;//如果n減為負(fù)值則從0開(kāi)始
- write_com(0x80+0x40+n);//從相應(yīng)位置開(kāi)始顯示
- 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();
- ……………………
- …………限于本文篇幅 余下代碼請(qǐng)從51黑下載附件…………
復(fù)制代碼
所有資料51hei提供下載:
LCD1602 16點(diǎn)鍵盤(pán).zip
(44.44 KB, 下載次數(shù): 31)
2020-1-14 20:56 上傳
點(diǎn)擊文件名下載附件
下載積分: 黑幣 -5
|
|