1 矩阵按键
1.1 需求描述
本案例实现以下功能:按下矩阵按键SW5到SW20,数码管会显示对应的按键编号。
1.2 硬件设计
1.2.1 硬件原理图
1.2.2 矩阵按键原理
1.3软件设计
1)Int_MatrixKeyboard.h
在项目的Int目录下创建Int_MatrixKeyboard.h,写入以下内容。
#ifndef __INT_MATRIXKEYBOARD_H__
#define __INT_MATRIXKEYBOARD_H__
#include "Util.h"/**
?* @brief 查看是否有矩阵按键被按下
?*
?* @return u8 返回被按下的按键编号,没有按键被按下返回0
?*/
u8 Int_MatrixKeyboard_CheckKey();#endif
2)Int_MatrixKeyboard.c
在项目的Int目录下创建Int_MatrixKeyboard.c,写入以下内容。
#include "Int_MatrixKeyboard.h"
#include <STC89C5xRC.H>#define KEYIN P2u8 Int_MatrixKeyboard_CheckKey()
{// 将P20 置为 0 ;则P27 - P20分别为1111 1110 0xFEKEYIN = 0xFE;// SW5 按下 ;则P27 - P20分别为1110 1110 0xEEif (KEYIN == 0xEE) {// 消前抖Com_Util_Delay1ms(10);if (KEYIN == 0xEE) {// SW5 已经被按下等待抬起while (1) {if (KEYIN == 0xFE) {// 消后抖Com_Util_Delay1ms(10);if (KEYIN == 0xFE) {return 5;}}}}}// SW6 按下 ;则P27 - P20分别为1101 1110 0xDEif (KEYIN == 0xDE) {// 消前抖Com_Util_Delay1ms(10);if (KEYIN == 0xDE) {// SW5 已经被按下等待抬起while (1) {if (KEYIN == 0xFE) {// 消后抖Com_Util_Delay1ms(10);if (KEYIN == 0xFE) {return 6;}}}}}// SW7 按下 ;则P27 - P20分别为1011 1110 0xBEif (KEYIN == 0xBE) {// 消前抖Com_Util_Delay1ms(10);if (KEYIN == 0xBE) {// SW5 已经被按下等待抬起while (1) {if (KEYIN == 0xFE) {// 消后抖Com_Util_Delay1ms(10);if (KEYIN == 0xFE) {return 7;}}}}}// SW8 按下 ;则P27 - P20分别为0111 1110 0x7Eif (KEYIN == 0x7E) {// 消前抖Com_Util_Delay1ms(10);if (KEYIN == 0x7E) {// SW5 已经被按下等待抬起while (1) {if (KEYIN == 0xFE) {// 消后抖Com_Util_Delay1ms(10);if (KEYIN == 0xFE) {return 8;}}}}}// 将P21 置为 0 ;则P27 - P20分别为1111 1101 0xFDKEYIN = 0xFD;// SW9 按下 ;则P27 - P20分别为1110 1101 0xEDif (KEYIN == 0xED) {// 消前抖Com_Util_Delay1ms(10);if (KEYIN == 0xED) {// SW5 已经被按下等待抬起while (1) {if (KEYIN == 0xFD) {// 消后抖Com_Util_Delay1ms(10);if (KEYIN == 0xFD) {return 9;}}}}}// SW10 按下 ;则P27 - P20分别为1101 1101 0xDDif (KEYIN == 0xDD) {// 消前抖Com_Util_Delay1ms(10);if (KEYIN == 0xDD) {// SW5 已经被按下等待抬起while (1) {if (KEYIN == 0xFD) {// 消后抖Com_Util_Delay1ms(10);if (KEYIN == 0xFD) {return 10;}}}}}// SW11 按下 ;则P27 - P20分别为1011 1101 0xBDif (KEYIN == 0xBD) {// 消前抖Com_Util_Delay1ms(10);if (KEYIN == 0xBD) {// SW5 已经被按下等待抬起while (1) {if (KEYIN == 0xFD) {// 消后抖Com_Util_Delay1ms(10);if (KEYIN == 0xFD) {return 11;}}}}}// SW12 按下 ;则P27 - P20分别为0111 1101 0x7Dif (KEYIN == 0x7D) {// 消前抖Com_Util_Delay1ms(10);if (KEYIN == 0x7D) {// SW12 已经被按下等待抬起while (1) {if (KEYIN == 0xFD) {// 消后抖Com_Util_Delay1ms(10);if (KEYIN == 0xFD) {return 12;}}}}}// 将P22 置为 0 ;则P27 - P20分别为1111 1011 0xFBKEYIN = 0xFB;// SW13 按下 ;则P27 - P20分别为1110 1011 0xEBif (KEYIN == 0xEB) {// 消前抖Com_Util_Delay1ms(10);if (KEYIN == 0xEB) {// SW5 已经被按下等待抬起while (1) {if (KEYIN == 0xFB) {// 消后抖Com_Util_Delay1ms(10);if (KEYIN == 0xFB) {return 13;}}}}}// SW14 按下 ;则P27 - P20分别为1101 1011 0xDBif (KEYIN == 0xDB) {// 消前抖Com_Util_Delay1ms(10);if (KEYIN == 0xDB) {// SW5 已经被按下等待抬起while (1) {if (KEYIN == 0xFB) {// 消后抖Com_Util_Delay1ms(10);if (KEYIN == 0xFB) {return 14;}}}}}// SW15 按下 ;则P27 - P20分别为1011 1011 0xBBif (KEYIN == 0xBB) {// 消前抖Com_Util_Delay1ms(10);if (KEYIN == 0xBB) {// SW5 已经被按下等待抬起while (1) {if (KEYIN == 0xFB) {// 消后抖Com_Util_Delay1ms(10);if (KEYIN == 0xFB) {return 15;}}}}}// SW16 按下 ;则P27 - P20分别为0111 1011 0x7Bif (KEYIN == 0x7B) {// 消前抖Com_Util_Delay1ms(10);if (KEYIN == 0x7B) {// SW5 已经被按下等待抬起while (1) {if (KEYIN == 0xFB) {// 消后抖Com_Util_Delay1ms(10);if (KEYIN == 0xFB) {return 16;}}}}}// 将P23 置为 0 ;则P27 - P20分别为1111 0111 0xF7KEYIN = 0xF7;// SW17 按下 ;则P27 - P20分别为1110 0111 0xE7if (KEYIN == 0xE7) {// 消前抖Com_Util_Delay1ms(10);if (KEYIN == 0xE7) {// SW5 已经被按下等待抬起while (1) {if (KEYIN == 0xF7) {// 消后抖Com_Util_Delay1ms(10);if (KEYIN == 0xF7) {return 17;}}}}}// SW18 按下 ;则P27 - P20分别为1101 0111 0xD7if (KEYIN == 0xD7) {// 消前抖Com_Util_Delay1ms(10);if (KEYIN == 0xD7) {// SW5 已经被按下等待抬起while (1) {if (KEYIN == 0xF7) {// 消后抖Com_Util_Delay1ms(10);if (KEYIN == 0xF7) {return 18;}}}}}// SW19 按下 ;则P27 - P20分别为1011 0111 0xB7if (KEYIN == 0xB7) {// 消前抖Com_Util_Delay1ms(10);if (KEYIN == 0xB7) {// SW5 已经被按下等待抬起while (1) {if (KEYIN == 0xF7) {// 消后抖Com_Util_Delay1ms(10);if (KEYIN == 0xF7) {return 19;}}}}}// SW20 按下 ;则P27 - P20分别为0111 0111 0x77if (KEYIN == 0x77) {// 消前抖Com_Util_Delay1ms(10);if (KEYIN == 0x77) {// SW5 已经被按下等待抬起while (1) {if (KEYIN == 0xF7) {// 消后抖Com_Util_Delay1ms(10);if (KEYIN == 0xF7) {return 20;}}}}}return 0;
}
3)main.c
#include <STC89C5XRC.H>
#include "Int_DigitalTube.h"
#include "Int_MatrixKeyboard.h"void main()
{u8 key_pressed;Int_DigitalTube_DisplayNum(0);while (1) {key_pressed = Int_MatrixKeyboard_CheckKey();if(key_pressed) {Int_DigitalTube_DisplayNum(key_pressed);}Int_DigitalTube_Refresh();}
}
5XRC.H>
#include “Int_DigitalTube.h”
#include “Int_MatrixKeyboard.h”
void main()
{
u8 key_pressed;
Int_DigitalTube_DisplayNum(0);
while (1) {
key_pressed = Int_MatrixKeyboard_CheckKey();
if(key_pressed) {
Int_DigitalTube_DisplayNum(key_pressed);
}
Int_DigitalTube_Refresh();
}
}