1.前言
当我们熟悉了数码管的位选与段选,并了解的矩阵键盘的扫描之后就可以编写程序了。
1.1实验现象
按下矩阵键盘S1并松开,数码管第一位(LED8)显示0;按下矩阵键盘S2并松开,数码管第一位显示1;...按下矩阵键盘S16并松开,数码管第一位显示F;
1.2工作原理
矩阵键盘扫描(输入扫描)
原理:读取第1行(列)→读取第2行(列) →读取第3行(列) → ……,然后快速循环这个过程,最终实现所有按键同时检测的效果
https://blog.csdn.net/YLG_lin/article/details/126429384?utm_source=app&app_version=5.3.0&code=app_1562916241&uLinkId=usr1mkqgl919blen
数码管的显示(位选,段选)
https://blog.csdn.net/YLG_lin/article/details/126406076?utm_source=app&app_version=5.3.0&code=app_1562916241&uLinkId=usr1mkqgl919blen
2.源码
#include<regx52.h>
//数码管段码表,123456789AbcdEF;
unsigned char Table[ ]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};
void Delay(unsigned int xms)//延时函数
{
unsigned char i, j;
while(xms--)
{
i = 2;
j = 239;
do
{
while (--j);
} while (--i);
}
}
unsigned char MatrixKey()//按键扫描(逐列扫描)
{
unsigned char KeyNumber=0;
P1=0xFF;
P1_3=0;
if(P1_7==0){Delay(20);while(P1_7==0);Delay(20);KeyNumber=1;}
if(P1_6==0){Delay(20);while(P1_6==0);Delay(20);KeyNumber=5;}
if(P1_5==0){Delay(20);while(P1_5==0);Delay(20);KeyNumber=9;}
if(P1_4==0){Delay(20);while(P1_4==0);Delay(20);KeyNumber=13;}
P1=0xFF;
P1_2=0;
if(P1_7==0){Delay(20);while(P1_7==0);Delay(20);KeyNumber=2;}
if(P1_6==0){Delay(20);while(P1_6==0);Delay(20);KeyNumber=6;}
if(P1_5==0){Delay(20);while(P1_5==0);Delay(20);KeyNumber=10;}
if(P1_4==0){Delay(20);while(P1_4==0);Delay(20);KeyNumber=14;}
P1=0xFF;
P1_1=0;
if(P1_7==0){Delay(20);while(P1_7==0);Delay(20);KeyNumber=3;}
if(P1_6==0){Delay(20);while(P1_6==0);Delay(20);KeyNumber=7;}
if(P1_5==0){Delay(20);while(P1_5==0);Delay(20);KeyNumber=11;}
if(P1_4==0){Delay(20);while(P1_4==0);Delay(20);KeyNumber=15;}
P1=0xFF;
P1_0=0;
if(P1_7==0){Delay(20);while(P1_7==0);Delay(20);KeyNumber=4;}
if(P1_6==0){Delay(20);while(P1_6==0);Delay(20);KeyNumber=8;}
if(P1_5==0){Delay(20);while(P1_5==0);Delay(20);KeyNumber=12;}
if(P1_4==0){Delay(20);while(P1_4==0);Delay(20);KeyNumber=16;}
return KeyNumber;
}
void Xian_Shi(KeyNum)
{
P2_4=1;P2_3=1;P2_2=1;//选中第一位(LED8)
P0=Table[KeyNum-1]; //数字的首元素为Table[0];
Delay(1);
}
void main()
{
while(1)
{
unsigned char KeyNum=MatrixKey();//不断循环扫描
if(KeyNum) //判断是否有按键按下,如果有就显示
{
Xian_Shi(KeyNum);
}
}
}
文章来源地址https://uudwc.com/A/eP4M4
文章来源:https://uudwc.com/A/eP4M4