大家好,遇到问题了,我在定时器中断函数中用线反转法采集键盘的按键,返回值readcode为0到15,送给数码管段选disp_duan[],因为readcode是不断的得到,每20ms返回一个readcode。请问我依次按下0-5键,如何才能让6位数码管依次显示6次按键采集的键值“0 1 2 3 4 5”?谢谢各位!
#include <STDIO.H>
#include <MATH.H>
#include <STRING.H>
#include <absacc.h>
#include <intrins.h>
#include <reg52.H>//修改
typedef unsigned int uint;
typedef unsigned char uchar;
sfr T2MOD = 0XC9;
sbit beep=P3^4;
uchar readcode;
uchar readnum;
uchar readtemp1,readtemp2,readtemp3,readtemp4,readtemp5,readtemp6;
uchar code disp_duan[]={0x3f,0x06,0x5B,0x4f,0x66,0x6D,0x7d,0x07,0x7f,0x6F,0x77,0x7c,0x39,0x5e,0x79,0x71};//0-9ZGHENG
//uchar code disp_duan[]= {0xf6,0x06,0xe5,0xf1,0x33,0xd3,0xd7,0x70,0xf7,0x73};//0-9
//0x77,0x7c,0x39,0x5e,0x79,0x71};
uchar code disp_wei[]={0x01,0x02,0x04,0x08,0x10,0x20};
char DISP_DATA[]; //显示的数字
uchar DISP_NUM; //显示的位数
bit scan_key(uchar);
void disp_led(void);
uchar num[4][4]={0,1,2,3,
4,5,6,7,
8,9,10,11,
12,13,14,15};
#define KEYPORT P0//定义键盘端口
void sysinit(void)
{
P0=0xff;
P1=0xff;
P2=0xff;
P3=0xff;
beep=0;
readnum=0;
}
void delayms(uint xms)
{
uchar m,n;
for(m=xms;m>0;m--)
for(n=110;n>0;n--);
}
uchar scan_key(void)
{
uchar i,j,tmp1,tmp2;
P0=0x0f;//低四位行线置高,高四位列线置低
if(P0!=0x0f)//如果有键按下
{
delayms(10);
if(P0!=0x0f)//确实有键按下
{
tmp1=P0;
switch(tmp1)
{
case 0x07: i=3; break;
case 0x0b: i=2; break;
case 0x0d: i=1; break;
case 0x0e: i=0; break;
}
P0=0xf0;//低四位行线置低,高四位列线置高
delayms(10);
tmp2=P0;
switch(tmp2)
{
case 0x70: j=3; break;
case 0xb0: j=2; break;
case 0xd0: j=1; break;
case 0xe0: j=0; break;
}
}
readcode=num[i][j];
return readcode;
}
}
uchar get_num(uchar one,uchar two,uchar three,uchar four,uchar five,uchar six)
{
readnum++;
readnum=readnum%6;
}
void Timer0_intinit(void)
{
TMOD=0X01;
TH0=-2000/256;
TL0=-2000%256;
ET0=1;
TR0=1;
EA=1;
}
void Timer0() interrupt 1
{
uchar WaitTime;
WaitTime++;
if(WaitTime==10)
{
scan_key();//判断接收到的数据
WaitTime=0;
}
}
void disp_led(uchar one,uchar two,uchar three,uchar four,uchar five,uchar six)
{
P2=disp_duan[one];
P1=disp_wei[0];
delayms(10);
P2=disp_duan[two];
P1=disp_wei[1];
delayms(10);
P2=disp_duan[three];
P1=disp_wei[2];
delayms(10);
P2=disp_duan[four];
P1=disp_wei[3];
delayms(10);
P2=disp_duan[five];
P1=disp_wei[4];
delayms(10);
P2=disp_duan[six];
P1=disp_wei[5];
delayms(10);
}
void main()
{
sysinit();
delayms(100);
while(1)
{
scan_key();
disp_led();
}
} |