//按下哪一个键就在数码管上显示数字,并且传送到串口调试助手上
#include <reg52.h>
#include "uart.h"
typedef unsigned char uint8;
typedef unsigned int uint16;
uint8 number[]={0x0c0,0x0F9,0x0A4,0x0B0,
0x99,0x92,0x82,0xF8,
0x80,0x90,0x88,0x83,
0xC6,0xA1,0x86,0x8E};
uint16 n,num=0;
void UART_init()
{
TMOD = 0x20; //0010 0000 说明在定时器1的工作方式2下工作
TH1 = 0xFD;
TL1 = 0xFD;
TR1 = 1; //定时器1运行控制位=1,说明开始计时
SCON = 0x50; //0101 0000 前两位说明在串口工作方式1 第三位SM2在方式1设置为0 第四位REN=1允许串口接收
}
void delay()
{
n=1000;
while(n--);
}
main()
{
P0 = 0xFF; // led初始化
ENLED1 = 0;
ENLED2 = 1; //3/8译码器使能
ADDR2 = 0;
ADDR1 = 0;
ADDR0 = 0; //选中第一个数码管
UART_init();
while (1)
{
keyOut1 = 0;
keyOut2 = 1;
keyOut3 = 1;
keyOut4 = 1;
if(keyIn1 == 0)
{
delay();
if(keyIn1 == 0)
P0 =number[0];
SBUF=0;
}
if(keyIn2 == 0)
{
delay();
if(keyIn2 == 0)
P0 =number[1];
SBUF=1;
}
if(keyIn3 == 0)
{
delay();
if(keyIn3 == 0)
P0 =number[2];
SBUF=2;
}
if(keyIn4 == 0)
{
delay();
if(keyIn4 == 0)
P0 =number[3];
SBUF=3;
}
while((keyIn1 == 0)||(keyIn2 == 0)||
(keyIn3 == 0)||(keyIn4 == 0));
keyOut1 = 1;
keyOut2 = 0;
keyOut3 = 1;
keyOut4 = 1;
if(keyIn1 == 0)
{
delay();
if(keyIn1 == 0)
P0 =number[4];
SBUF=4;
}
if(keyIn2 == 0)
{
delay();
if(keyIn2 == 0)
P0 =number[5];
SBUF=5;
}
if(keyIn3 == 0)
{
delay();
if(keyIn3 == 0)
P0 =number[6];
SBUF=6;
}
if(keyIn4 == 0)
{
delay();
if(keyIn4 == 0)
P0 =number[7];
SBUF=7;
}
while((keyIn1 == 0)||(keyIn2 == 0)||
(keyIn3 == 0)||(keyIn4 == 0));
keyOut1 = 1;
keyOut2 = 1;
keyOut3 = 0;
keyOut4 = 1;
if(keyIn1 == 0)
{
delay();
if(keyIn1 == 0)
P0 =number[8];
SBUF=8;
}
if(keyIn2 == 0)
{
delay();
if(keyIn2 == 0)
P0 =number[9];
SBUF=9;
}
if(keyIn3 == 0)
{
delay();
if(keyIn3 == 0)
P0 =number[10];
SBUF=10;
}
if(keyIn4 == 0)
{
delay();
if(keyIn4 == 0)
P0 =number[11];
SBUF=11;
}
while((keyIn1 == 0)||(keyIn2 == 0)||
(keyIn3 == 0)||(keyIn4 == 0));
keyOut1 = 1;
keyOut2 = 1;
keyOut3 = 1;
keyOut4 = 0;
if(keyIn1 == 0)
{
delay();
if(keyIn1 == 0)
P0 =number[12];
SBUF=12;
}
if(keyIn2 == 0)
{
delay();
if(keyIn2 == 0)
P0 =number[13];
SBUF=13;
}
if(keyIn3 == 0)
{
delay();
if(keyIn3 == 0)
P0 =number[14];
SBUF=14;
}
if(keyIn4 == 0)
{
delay();
if(keyIn4 == 0)
P0 =number[15];
SBUF=15;
}
while((keyIn1 == 0)||(keyIn2 == 0)||
(keyIn3 == 0)||(keyIn4 == 0));
}
}
我用到的是直接法,大家还可以试试扫描法