這是通過51單片機做的一個電子鎖設計分享給大家
里面用了矩陣按鍵和LCD12864顯示屏,顯示屏函數(shù)和矩陣按鍵函數(shù)我單獨做庫,可以直接調用,但是顯示其他字需要自己取模,取模軟件我也放在文件夾里面。
仿真原理圖如下(proteus仿真工程文件可到本帖附件中下載)
仿真工作圖.png (94.22 KB, 下載次數(shù): 50)
下載附件
2020-6-1 21:21 上傳
下面是設計的主函數(shù):
- #include "reg52.h"
- #include "LCD12864.h"
- #include "key.h"
- #include "chinese.h"
- void main(void)
- {
- init_lcd();
- screen_protect();
- while(1)
- {
- KeyDown();
- key_data_manage(KeyValue);
- }
- }
復制代碼
- #include "key.h"
- uchar KeyValue; //用來存放讀取到的鍵值
- uint u1,u2;//蜂鳴器循環(huán)延時變量
- unsigned long myl=202020;
- void delay(uint i)
- {
- while(i--);
- }
- void delay500ms(void) //誤差 0us
- {
- unsigned char a,b,c;
- for(c=23;c>0;c--)
- for(b=152;b>0;b--)
- for(a=70;a>0;a--);
- }
- void KeyDown(void)
- {
- char a=0;
- GPIO_KEY=0x0f;
- if(GPIO_KEY!=0x0f)//讀取按鍵是否按下
- {
- delay(1000);//延時10ms進行消抖
- if(GPIO_KEY!=0x0f)//再次檢測鍵盤是否按下
- {
- //測試列
- GPIO_KEY=0X0F;
- switch(GPIO_KEY)
- {
- case(0X07): KeyValue=0;break;
- case(0X0b): KeyValue=1;break;
- case(0X0d): KeyValue=2;break;
- case(0X0e): KeyValue=3;break;
- }
- //測試行
- GPIO_KEY=0XF0;
- switch(GPIO_KEY)
- {
- case(0X70): KeyValue=KeyValue;break;
- case(0Xb0): KeyValue=KeyValue+4;break;
- case(0Xd0): KeyValue=KeyValue+8;break;
- case(0Xe0): KeyValue=KeyValue+12;break;
- }
- while((a<5)&&(GPIO_KEY!=0xf0)) //檢測按鍵松手檢測
- {
- for(u1=0;u1<500;u1++)
- {
- for(u2=0;u2<80;u2++); //延時
- BEEP=~BEEP; //取反輸出到喇叭的信號
- }
- a++;
- }
- }
- }
- }
- /************************************/
- //函數(shù)名稱:void touch_data_manage(u8 k_value)
- //函數(shù)功能:對觸屏操作返回的值進行處理
- //注:case后面的數(shù)字是宮格鍵盤的位數(shù)
- //從左到右,從上到下,依次是:0,1,...,15
- /************************************/
- void key_data_manage(uchar k_value) //觸屏數(shù)據(jù)處理
- {
- uchar j;
- static uchar number,i;
- static uchar mode=20;
- static unsigned long num = 0; //預輸入值
- switch(k_value)
- {
- case 8:number = 0;i++;num=num*10+0;break;
- case 15:number = 1;i++;num=num*10+1;break;
- case 11:number = 2;i++;num=num*10+2;break;
- case 7:number = 3;i++;num=num*10+3;break;
- case 14:number = 4;i++;num=num*10+4;break;
- case 10:number = 5;i++;num=num*10+5;break;
- case 6:number = 6;i++;num=num*10+6;break;
- case 13:number = 7;i++;num=num*10+7;break;
- case 9:number = 8;i++;num=num*10+8;break;
- case 5:number = 9;i++;num=num*10+9;break;
- case 2:mode=2;break;
- case 1:Clr_hang(3,5);i=0;num=0;break;
- case 0:mode=4;break;
- case 4:;break;
- case 12:;break;
- case 3:mode=0;break;
- }
- KeyValue=20;
- if(mode == 0)
- {
- Clr_Scr();
- screen_outkey();
- num=0;
- mode=1;
- i=0;
- }
- if((mode==1)&&(i>=1))
- {
- ShowOneChar(3,8+8*i,Hz_num[10]);
- }
- if((mode==2)&&(i>=1))
- {
- ShowOneChar(3,8+8*i,Hz_num[number]);
- }
- if((mode==4)&&(num>99999))
- {
- if(num == myl)
- {
- Clr_Scr();
- for(j=0;j<7;j++)
- ShowOneChin(3,16*j,Hz_right[j]);
- P36=0;
- delay500ms();
- P36=1;
- }
- if((mode==4)&&(myl!=num)&&(num>99999))
- {
- ……………………
- …………限于本文篇幅 余下代碼請從51黑下載附件…………
復制代碼
全部資料51hei下載地址:
電子鎖proteus.zip
(240.43 KB, 下載次數(shù): 240)
2020-6-1 21:22 上傳
點擊文件名下載附件
下載積分: 黑幣 -5
|