这是通过51单片机做的一个电子锁设计分享给大家
里面用了矩阵按键和LCD12864显示屏,显示屏函数和矩阵按键函数我单独做库,可以直接调用,但是显示其他字需要自己取模,取模软件我也放在文件夹里面。
仿真原理图如下(proteus仿真工程文件可到本帖附件中下载)
下面是设计的主函数:
- #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;//蜂鸣器循环延时变量
- 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++;
- }
- }
- }
- }
- /************************************/
- //函数名称:void touch_data_manage(u8 k_value)
- //函数功能:对触屏操作返回的值进行处理
- //注:case后面的数字是宫格键盘的位数
- //从左到右,从上到下,依次是:0,1,...,15
- /************************************/
- void key_data_manage(uchar k_value) //触屏数据处理
- {
- 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, 下载次数: 240)
|