蓝桥杯 矩阵扫描

模块:矩阵键盘 数码管 (CD107D)键盘

 功能:从左至右,从上至下。依次代表 0~F 按下相应的按键,数码管显示对应的数字

 矩阵扫描新方法(简单)

 

 

#include <STC15F2K60S2.H>
#define uchar unsigned char
#define uint unsigned int
sbit R1 = P3^0;
sbit R2 = P3^1;
sbit R3 = P3^2;
sbit R4 = P3^3;

sbit C1 = P4^4;
sbit C2 = P4^2;
sbit C3 = P3^5;
sbit C4 = P3^4;
uchar num;
uint code SMG_duanma[] =  //数码管段选表 0~F
{0xc0 ,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x88,0x83,0xc6,0xa1,0x86,0x8e};
void HC138init (uchar 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;
   }
}
void init ()	 // 关闭蜂鸣器继电器LED灯
{
  HC138init (4);
   P0 = 0xff;
  HC138init (5);
  P0 = 0x00;
}
void displaybit(uchar dat)	// 数码管静态显示一个字节
{
	HC138init (6);
	P0 = 0x01;
	HC138init (7);
	P0 = dat;
}
void matrixscan ()
{
  R1 = 0; R3 = R2 = R4 = 1;	 // 让R1 = 0 判断是那个按键
  C1 = C2 = C3 = C4 = 1;
  if(C1 == 0)
  {
     while(C1 == 0);
	 num = 0;
	 displaybit(SMG_duanma[num]);
  }
  else if(C2 == 0)
  {
     while(C2 == 0);
	 num = 1;
	 displaybit(SMG_duanma[num]);
  }
  else  if(C3 == 0)
  {
     while(C3 == 0);
	 num = 2;
	 displaybit(SMG_duanma[num]);
  }
  else  if(C4 == 0)
  {
     while(C2 == 0);
	 num = 3;
	 displaybit(SMG_duanma[num]);
  }

  R2 = 0;					   // 让R2 = 0 判断是那个按键
  R3 = R1 = R4 = 1;
  C1 = C2 = C3 = C4 = 1;
  if(C1 == 0)
  {
     while(C1 == 0);
	 num = 4;
	 displaybit(SMG_duanma[num]);
  }
  else  if(C2 == 0)
  {
     while(C2 == 0);
	 num = 5;
	 displaybit(SMG_duanma[num]);
  }
  else  if(C3 == 0)
  {
     while(C3 == 0);
	 num = 6;
	 displaybit(SMG_duanma[num]);
  }
  else  if(C4 == 0)
  {
     while(C2 == 0);
	 num = 7;
	 displaybit(SMG_duanma[num]);
  }

  R3 = 0;					  // 让R3 = 0 判断是那个按键
  R2 = R1 = R4 = 1;
  C1 = C2 = C3 = C4 = 1;
  if(C1 == 0)
  {
     while(C1 == 0);
	 num = 8;
	 displaybit(SMG_duanma[num]);
  }
  else  if(C2 == 0)
  {
     while(C2 == 0);
	 num = 9;
	 displaybit(SMG_duanma[num]);
  }
  else  if(C3 == 0)
  {
     while(C3 == 0);
	 num = 10;
	 displaybit(SMG_duanma[num]);
  }
  else  if(C4 == 0)
  {
     while(C2 == 0);
	 num = 11;
	 displaybit(SMG_duanma[num]);
  }

  R4 = 0;						 // 让R4 = 0 判断是那个按键
  R2 = R1 = R3 = 1;
  C1 = C2 = C3 = C4 = 1;
  if(C1 == 0)
  {
     while(C1 == 0);
	 num = 12;
	 displaybit(SMG_duanma[num]);
  }
  else  if(C2 == 0)
  {
     while(C2 == 0);
	 num = 13;
	 displaybit(SMG_duanma[num]);
  }
  else  if(C3 == 0)
  {
     while(C3 == 0);
	 num = 14;
	 displaybit(SMG_duanma[num]);
  }
  else  if(C4 == 0)
  {
     while(C2 == 0);
	 num = 15;
	 displaybit(SMG_duanma[num]);
  }
}
void main ()
{
    init ();
  while(1)
  {
  	 matrixscan ();
  }
}

 

 

©️2020 CSDN 皮肤主题: 深蓝海洋 设计师: CSDN官方博客 返回首页
实付0元
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值