CT107D:7、矩阵键盘的基本操作

题目:在CT107D单片机综合训练平台上,首先将J5处的跳帽接到1~2引脚,使按键S4~S19按键组成4x4的矩阵键盘。在扫描按键的过程中,发现有按键触发信号后(不做去抖动),待按键松开后,在数码管的第一位显示相应的数字:从左至右,从上到下,依次显示0~F。

原理图

#include "reg52.h"
sbit HC138_A=P2^5;
sbit HC138_B=P2^6;
sbit HC138_C=P2^7;

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

sbit C4=P3^4;
sbit C3=P3^5;
sbit C2=P3^6;
sbit C1=P3^7;

unsigned char code duanma[18]=
{0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,
0x90,0x88,0x80,0xc6,0xc0,0x86,0x8e,0xbf,0x7f};

//关闭全部外设
void guan()
{
	HC138_A=1;
	HC138_B=0;
	HC138_C=1;
	P0=0x00;//蜂鸣器
	HC138_A=0;
	HC138_B=0;
	HC138_C=1;
	P0=0xff;//关闭led
	HC138_A=0;
	HC138_B=1;
	HC138_C=1;
	P0=0X00;//关闭com端	
}

void com()//打开com控制端
{
	HC138_A=0;
	HC138_B=1;
	HC138_C=1;
}

void shumaguan()//打开数码管控制端
{
	HC138_A=1;
	HC138_B=1;
	HC138_C=1;
}

void delay(unsigned char t)
{
	while(t--);
}
void display(unsigned char value)//显示
{
	com();
	P0=0x01;//com1
	shumaguan();
	P0=value;
	delay(60000);
}

void scankey()//扫描
{
	R1=0;//扫描第一行
	R2=R3=R4=1;
	C1=C2=C3=C4=1;
	
	if(C1==0)
	{
		while(C1==0);
		display(duanma[0]);
	}
	else if(C2==0)
	{
		while(C2 == 0);
		display(duanma[1]);
	}
	else if(C3==0)
	{
		 
		 while(C3 == 0);
		 display(duanma[2]);
	}
	else if(C4==0)
	{
		while(C4 == 0);
		display(duanma[3]);	
	}
	
	R2=0;//扫描第二行
	R1=R3=R4=1;
	C1=C2=C3=C4=1;
	
	if(C1==0)
	{
		while(C1 == 0);
		display(duanma[4]);
	}
	else if(C2==0)
	{
		while(C2 == 0);
		display(duanma[5]);
	}
	else if(C3==0)
	{
		 while(C3 == 0);
		 display(duanma[6]);
	}
	else if(C4==0)
	{
		while(C4 == 0);
		display(duanma[7]);	
	}

	R3=0;//扫描第三行
	R2=R1=R4=1;
	C1=C2=C3=C4=1;
	
	if(C1==0)
	{
		while(C1 == 0);
		display(duanma[8]);
	}
	else if(C2==0)
	{
		while(C2 == 0);
		display(duanma[9]);
	}
	else if(C3==0)
	{
		 while(C3 == 0);
		 display(duanma[10]);
	}
	else if(C4==0)
	{
		while(C4 == 0);
		display(duanma[11]);	
	}

	R4=0;//扫描第四行
	R2=R3=R1=1;
	C1=C2=C3=C4=1;
	
	if(C1==0)
	{
		while(C1 == 0);
		display(duanma[12]);
	}
	else if(C2==0)
	{
		while(C2 == 0);
		display(duanma[13]);
	}
	else if(C3==0)
	{
		 while(C3 == 0);
		 display(duanma[14]);
	}
	else if(C4==0)
	{
		while(C4 == 0);
		display(duanma[15]);	
	}
		
}

void main()
{
	guan();
	while(1)
	{
		scankey();
		

	}
}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值