设计一个单片机键盘接口电路,实现4*4矩阵键盘,用数码管显示相应按键内容,当指定某一个按键连续被按三次时,显示“口”字符号,按键有去抖动功能,数码管可以通过清零键清零。
仿真原理图如下(proteus仿真工程文件可到本帖附件中下载)
单片机源程序如下:
- //0x3F, 0x06, 0x5B, 0x4F, 0x66, 0x6D, 0x7D, 0x07, 0x7F, 0x6F, 0x77, 0x7C, 0x39, 0x5E, 0x79, 0x71 共阴
- //0xC0, 0xF9, 0xA4, 0xB0, 0x99, 0x92, 0x82, 0xF8, 0x80, 0x90, 0x88, 0x83, 0xC6, 0xA1, 0x86, 0x8E 共杨
- #include <reg52.h>
- #define GPIO_DIG P0 //段选数码管
- #define GPIO_KEY P2 //矩阵按键
- sbit led = P1^0;
- typedef unsigned char uchar;
- typedef unsigned int uint;
- uint keynum = 0;
- uint count=0;
- uchar code table[]={0x3F, 0x06, 0x5B, 0x4F, 0x66, 0x6D, 0x7D, 0x07, 0x7F, 0x6F, 0x77, 0x7C, 0x39, 0x5E, 0x79, 0x71};
- uchar code table0[]={0x5c};
- void delay( uchar i ){
- while( i-- );
- }
- void key44scan(void)//键盘扫描函数
- {
- // uint count=0;
- uint t;
- P2=0xfe;
- t=P2;
- t=t&0xf0;
- if(t!=0xf0){
- delay(100);
- if(t!=0xf0)
- {
-
- t=P2;
- count++;
- switch(t)
- {
- case 0xee:keynum=1; break;
- case 0xde:keynum=5; break;
- case 0xbe:keynum=9; break;
- case 0x7e:keynum=13; break;
- }
- P0=table[keynum];
- while(t!=0xf0)
- {
- t=P2;
- t=t&0xf0;
- }
- }
- }
-
- P2=0xfd;
- t=P2;
- t=t&0xf0;
- if(t!=0xf0){
- delay(100);
- if(t!=0xf0)
- {
-
- t=P2;
- count++;
- switch(t)
- {
- case 0xed:keynum=2; break;
- case 0xdd:keynum=6; break;
- case 0xbd:keynum=10; break;
- case 0x7d:keynum=14; break;
- }
- P0=table[keynum];
- while(t!=0xf0)
- {
- t=P2;
- t=t&0xf0;
- }
- }
- }
- P2=0xfb;
- t=P2;
- t=t&0xf0;
- if(t!=0xf0){
- delay(100);
- if(t!=0xf0)
- {
-
- t=P2;
- count++;
- switch(t)
- {
- case 0xeb:keynum=3; break;
- case 0xdb:keynum=7; break;
- case 0xbb:keynum=11; break;
- case 0x7b:keynum=15; break;
- }
- P0=table[keynum];
- while(t!=0xf0)
- {
- t=P2;
- t=t&0xf0;
- }
- }
- }
- P2=0xf7;
- t=P2;
- t=t&0xf0;
- if(t!=0xf0){
- delay(100);
- if(t!=0xf0)
- {
-
- t=P2;
- count++;
- switch(t)
- {
- case 0xe7:keynum=4; break;
- case 0xd7:keynum=8; break;
- case 0xb7:keynum=12; break;
- case 0x77:keynum=0; break;
- }
- P0=table[keynum];
- while(t!=0xf0)
- {
- t=P2;
- t=t&0xf0;
- }
- }
- }
- // if(count==3){
- // count = 0;
- // led =~ led;
- // P0=table0[0];
- // delay(1000);
- // }
-
- }
- void main (){
- led = 0;
- while(1) {
- key44scan();
-
- if(count==3){
- //count = 0;
- led =~ led;
- P0=table0[0];
- // delay(1000);
- }
- if(count==4){
- count = 0;
- led =~ led;
- P0=table[0];
- // delay(1000);
- }
- }
- }
复制代码
Keil代码与Proteus仿真下载:
实验2.7z
(65.26 KB, 下载次数: 48)
|