蓝桥杯模块训练4

数码管显示键值

设计一个4*4矩阵键盘(S4-S19),双数码管显示,其键值对应为“00~15”。按下按键后,数码管显示相应的键值。

#include
#define uint unsigned int
#define uchar unsigned char
uchar code KEY_TABLE[]={0x7e,0xbe,0xde,0xee,0x7d,0xbd,0xdd,0xed,0x7b,0xbb,0xdb,0xeb,0x77,0xb7,0xd7,0xe7};
uchar code TABLE[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x88,0x83,0xc6,0xa1,0x86,0x8e};
uchar temp,num=0,i,j,shi,ge;
void delay1()
{
	uint n=1000;
	while(n--);
}//短延时,作为去除抖动功能
void delay2(uint i)
{
	for(;i>0;i--)
	for(j=110;j>0;j--);
}
void main()
{
	uchar key,m,num,key1,key2;
	while(1){	
	P30=0;P31=0;P32=0;P33=0;P34=1;P35=1;P42=1;P44=1;  //将行置为0;列置为1
	if(P34==0) key1=0xe0;
	if(P35==0) key1=0xd0;
	if(P42==0) key1=0xb0;
	if(P44==0) key1=0x70;
	if((P34==1)&&(P35==1)&&(P42==1)&&(P44==1)) key1=0xf0;
	P30=1;P31=1;P32=1;P33=1;P34=0;P35=0;P42=0;P44=0;   //将列置为0,行置为1
	if(P30==0) key2=0x0e;
	if(P31==0) key2=0x0d;
	if(P32==0) key2=0x0b;
	if(P33==0) key2=0x07;
	if((P30==1)&&(P31==1)&&(P32==1)&&(P33==1)) key2=0x0f;
	  key=key1|key2;
	  if(key!=0xff)
		{delay1();     //延时去抖
		if(key!=0xff)
		{
     for(m=0;m<16;m++)
		if(key==KEY_TABLE[m])
		{num=m;break;}
		 shi=num/10;     //取十位
	   ge=num%10;     //取个位
	 }
	}
		P2=0xc0;P0=0x01;P2=0x00;
	  P2=0xe0;P0=TABLE[shi];P2=0x00;   //显示十位
	  delay2(20);
	  P2=0xc0;P0=0x02;P2=0x00;
	  P2=0xe0;P0=TABLE[ge];P2=0x00;   //显示个位
	  delay2(20);
 }	
}

你可能感兴趣的:(蓝桥杯)