矩阵键盘的使用

本文介绍了在51单片机中如何使用矩阵键盘,包括引脚配置、译码器选择以及数码管显示函数的编写。作者分享了从定义引脚到实现键盘输入和数码管显示的具体步骤,同时提醒读者注意接线问题。
摘要由CSDN通过智能技术生成

在51单片机中,矩阵键盘的使用量是非常频繁,于是掌握矩阵键盘的使用是非常重要的,接下来是我的学习心得。

这是矩阵键盘最基础的操作了,首先还是最老生常谈的定义引脚和写出译码器的选择,这里就不通过代码体现了,要完成这个训练,我们还要弄一个0到F的字符显示数组

unsigned char  code SMG[18] =	{0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90,0x88,0x80,0xc6,0xc0,0x86,0x8e,0xbf,

我们这次需要用到数码管,我们先把数码管显示函数写出来

void DisplayLED(unsigned char i)
{
	Slect573(6);
	P0 = 0x01;
	Slect573(7);
	P0 = i;
}

接下来我们先让第一排键盘起效果,如代码所示

R1 = 0;
	R4 = R2 = R3 = 1;
	B1 = B2 = B3 = B4 =1;
	if(B1 == 0)
	{
		while(B1 == 0);
		Key_num = 0;
		DisplayLED(SMG[Key_num]);
	}
	
	else if(B2 == 0)
	{
		while(B2 == 0);
		Key_num = 1;
		DisplayLED(SMG[Key_num]);
	}
	
	else if(B3 == 0)
	{
		while(B3 == 0);
		Key_num = 2;
		DisplayLED(SMG[Key_num]);
	}	
	
	else if(B4 == 0)
	{
		while(B4 == 0);
		Key_num = 3;
		DisplayLED(SMG[Key_num]);
	}

其实吧,接下来也就是非常无聊的重复写代码了,但是在写代码的过程中,我曾出现了一个致命的问题,调试了半天,还是不行,最后发现,我靠,引脚接错了!各位应以为戒啊!蓝桥杯的板子是J5需要接到KBD的.

最终代码就是这样啦!

#include "reg52.h"

unsigned char  code SMG[18] =	{0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90,0x88,0x80,0xc6,0xc0,0x86,0x8e,0xbf,0x7f};

sfr P4 = 0xc0;

sbit R1 = P3^0;
sbit R2 = P3^1;
sbit R3 = P3^2;
sbit R4 = P3^3;

sbit B1 = P4^4;
sbit B2 = P4^2;
sbit B3 = P3^5;
sbit B4 = P3^4;


void Slect573(unsigned char n)
{
	switch(n)
	{
		case 4:
			P2 = (P2 & 0x1f) | 0x80;
			break;
		case 5:
			P2 = (P2 & 0x1f) | 0xa0;
			break;
		case 6:
			P2 = (P2 & 0x1f) | 0xc0;
			break;
		case 7:
			P2 = (P2 & 0x1f) | 0xe0;	
			break;
	}
}

unsigned char Key_num = 0;

void DisplayLED(unsigned char i)
{
	Slect573(6);
	P0 = 0x01;
	Slect573(7);
	P0 = i;
}

void ScanKey()
{
	R1 = 0;
	R4 = R2 = R3 = 1;
	B1 = B2 = B3 = B4 =1;
	if(B1 == 0)
	{
		while(B1 == 0);
		Key_num = 0;
		DisplayLED(SMG[Key_num]);
	}
	
	else if(B2 == 0)
	{
		while(B2 == 0);
		Key_num = 1;
		DisplayLED(SMG[Key_num]);
	}
	
	else if(B3 == 0)
	{
		while(B3 == 0);
		Key_num = 2;
		DisplayLED(SMG[Key_num]);
	}	
	
	else if(B4 == 0)
	{
		while(B4 == 0);
		Key_num = 3;
		DisplayLED(SMG[Key_num]);
	}
	
	R2 = 0;
	R1 = R4 = R3 = 1;
	B1 = B2 = B3 = B4 =1;
	if(B1 == 0)
	{
		while(B1 == 0);
		Key_num = 4;
		DisplayLED(SMG[Key_num]);
	}
	
	else if(B2 == 0)
	{
		while(B2 == 0);
		Key_num = 5;
		DisplayLED(SMG[Key_num]);
	}
	
	else if(B3 == 0)
	{
		while(B3 == 0);
		Key_num = 6;
		DisplayLED(SMG[Key_num]);
	}	
	
	else if(B4 == 0)
	{
		while(B4 == 0);
		Key_num = 7;
		DisplayLED(SMG[Key_num]);
	}

	R3 = 0;
	R1 = R2 = R4 = 1;
	B1 = B2 = B3 = B4 =1;	
	if(B1 == 0)
	{
		while(B1 == 0);
		Key_num = 8;
		DisplayLED(SMG[Key_num]);
	}
	
	else if(B2 == 0)
	{
		while(B2 == 0);
		Key_num = 9;
		DisplayLED(SMG[Key_num]);
	}
	
	else if(B3 == 0)
	{
		while(B3 == 0);
		Key_num = 10;
		DisplayLED(SMG[Key_num]);
	}	
	
	else if(B4 == 0)
	{
		while(B4 == 0);
		Key_num = 11;
		DisplayLED(SMG[Key_num]);
	}	

	R4 = 0;
	R1 = R2 = R3 = 1;
	B1 = B2 = B3 = B4 =1;	
	if(B1 == 0)
	{
		while(B1 == 0);
		Key_num = 12;
		DisplayLED(SMG[Key_num]);
	}
	
	else if(B2 == 0)
	{
		while(B2 == 0);
		Key_num = 13;
		DisplayLED(SMG[Key_num]);
	}
	
	else if(B3 == 0)
	{
		while(B3 == 0);
		Key_num = 14;
		DisplayLED(SMG[Key_num]);
	}	
	
	else if(B4 == 0)
	{
		while(B4 == 0);
		Key_num = 15;
		DisplayLED(SMG[Key_num]);
	}	
}

void main()
{
	while(1)
	{
		ScanKey();
	}
}	

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值