功能:
扫描读取4*4矩阵键盘的输入,并显示在数码管上,扫描含消抖处理。包含C源码及Proteus仿真文件,包含原理图;
注:仅实现了0-15的显示,6根数码管显示的数相同。
仿真原理图如下(proteus仿真工程文件可到本帖附件中下载)
单片机源程序如下:
- #include<reg52.h>
- #define uint unsigned int
- #define uchar unsigned char
- sbit wela = P2^7; //位选端口
- sbit dula = P2^6; //段选端口
- sbit beep = P2^3; //蜂鸣器端口
- uchar temp, num;
- void keyscan();
- void delay(uchar num);
- void display(uchar num);
- void initialization();
- uchar code Table[] = //数码管段码数组
- {
- 0x3F,0x06,0x5B,0x4F,0x66,0x6D, //共阴极段码 0 1 2 3 4 5
- 0x7D,0x07,0x7F,0x6F,0x77,0x7C, //共阴极段码 6 7 8 9 A b
- 0x39,0x5E,0x79,0x71,0x73,0x3E, //共阴极段码 C d E F P U
- 0x31,0x6E,0x76,0x38,0x00 //共阴极段码 T y H L 灭灯
- };
- void main()
- {
- initialization();
- while (1)
- {
- beep = 0;
- keyscan();
- }
- }
- /************延时函数*************/
- void delay(uchar num) //延时 num ms
- {
- uint x, y;
- for(x = num; x > 0; x--)
- {
- for(y = 110; y > 0; y--);
- }
- }
- /**** 键盘扫描函数 ****
- **** 函数功能 ****
- **** 扫描4*4矩阵键盘的输入 ****/
- void keyscan()
- {
-
- P3 = 0xfe; //检测第一行矩阵键盘(0-3)
- temp = P3;
- temp = temp & 0xf0; //判断是否有按键按下
- while(temp != 0xf0)
- {
- delay(5);
- P3 = 0xfe;
- temp = temp & 0xf0;
- while(temp != 0xf0) //延时消抖 确认有按键按下
- {
- temp = P3;
- switch (temp)
- {
- case 0xee: num = 0; //检测到第一个键按下
- break;
- case 0xde: num = 1; //检测到第二个键按下
- break;
- case 0xbe: num = 2; //检测到第三个键按下
- break;
- case 0x7e: num = 3; //检测到第四个键按下
- break;
- }
- while(temp != 0xf0) //松手检测
- {
- temp = P3;
- temp = temp & 0xf0;
- }
- display(num);
- }
- }
- P3 = 0xfd; //检测第二行矩阵键盘(4-7)
- temp = P3;
- temp = temp & 0xf0; //判断是否有按键按下
- while(temp != 0xf0)
- {
- delay(5);
- P3 = 0xfd;
- temp = temp & 0xf0;
- while(temp != 0xf0) //延时消抖 确认有按键按下
- {
- temp = P3;
- switch (temp)
- {
- case 0xed: num = 4; //检测到第一个键按下
- break;
- case 0xdd: num = 5; //检测到第二个键按下
- break;
- case 0xbd: num = 6; //检测到第三个键按下
- break;
- case 0x7d: num = 7; //检测到第四个键按下
- break;
- }
- while(temp != 0xf0) //松手检测
- {
- temp = P3;
- temp = temp & 0xf0;
- }
- display(num);
- }
- }
- P3 = 0xfb; //检测第三行矩阵键盘(8-11)
- temp = P3;
- temp = temp & 0xf0; //判断是否有按键按下
- while(temp != 0xf0)
- {
- delay(5);
- P3 = 0xfb;
- temp = temp & 0xf0;
- while(temp != 0xf0) //延时消抖 确认有按键按下
- {
- temp = P3;
- switch (temp)
- {
- case 0xeb: num = 8; //检测到第一个键按下
- break;
- case 0xdb: num = 9; //检测到第二个键按下
- break;
- case 0xbb: num = 10; //检测到第三个键按下
- break;
- case 0x7b: num = 11; //检测到第四个键按下
- break;
- }
- while(temp != 0xf0) //松手检测
- {
- temp = P3;
- temp = temp & 0xf0;
- }
- display(num);
- }
- }
-
- P3 = 0xf7; //检测第四行矩阵键盘(12-15)
- temp = P3;
- temp = temp & 0xf0; //判断是否有按键按下
- while(temp != 0xf0)
- {
- delay(5);
- P3 = 0xf7;
- temp = temp & 0xf0;
- while(temp != 0xf0) //延时消抖 确认有按键按下
- {
- temp = P3;
- switch (temp)
- {
- case 0xe7: num = 12; //检测到第一个键按下
- break;
- case 0xd7: num = 13; //检测到第二个键按下
- break;
- case 0xb7: num = 14; //检测到第三个键按下
- break;
- case 0x77: num = 15; //检测到第四个键按下
- break;
- }
- while(temp != 0xf0) //松手检测
- {
- temp = P3;
- temp = temp & 0xf0;
- }
- display(num);
- }
- }
- }
- void display(uchar num) //数码管显示函数
- {
- beep = 1;
- dula = 1;
- P0 = Table[num];
- dula = 0;
- }
- void initialization() //数码管初始化
- {
- dula = 1; //开段码锁存
- P0 = 0x00; //设数码管初值
- dula = 0;
- wela = 1;
- P0 = 0xc0; //选通6根数码管
- wela = 0;
- }
复制代码
所有资料51hei附件下载:
Key_scan.rar
(29.94 KB, 下载次数: 43)
|