独立按键、4x4矩阵按键
一、独立按键
(1)独立按键1.0
代码样例(分文件编程)
config.h
// 初始化灯端口和数码管端口
sbit L0 = P1^0;
sbit L1 = P1^1;
sbit L2 = P1^2;
sbit L3 = P1^3;
sbit L4 = P1^4;
sbit L5 = P1^5;
sbit L6 = P1^6;
sbit L7 = P1^7;
sbit K1 = P3^4;
sbit K2 = P3^5;
sbit K3 = P3^6;
sbit K4 = P3^7;
main.c
#include<reg52.h>
#include "config.h"
void main()
{
// 1111 1111
P1 = 0xff; // 让P1口一开始的灯全部为高电平,也就是不亮
while(1)
{
L0 = K1; //控制第一、二个灯亮
L1 = K1;
L2 = K2; //控制第三、四个灯亮
L3 = K2;
L4 = K3; //控制第五、六个灯亮
L5 = K3;
L6 = K4; //控制第七、八个灯亮
L7 = K4;
}
}
(2)独立按键2.0
消除按键抖动
config.h
// 初始化灯端口和数码管端口
sbit L0 = P1^0;
sbit L1 = P1^1;
sbit L2 = P1^2;
sbit L3 = P1^3;
sbit L4 = P1^4;
sbit L5 = P1^5;
sbit L6 = P1^6;
sbit L7 = P1^7;
sbit K1 = P3^4;
sbit K2 = P3^5;
sbit K3 = P3^6;
sbit K4 = P3^7;
main.c
#include<reg52.h>
#include "config.h"
void delay(unsigned int xms) // 延迟函数
{
unsigned