题目:在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();
}
}