小测试,做一个数字秒表,有问题求解决!
#include<reg51.h>
sbit KEY1=P1^0;
sbit KEY2=P1^1;
sbit KEY3=P1^2;
sbit KEY4=P1^3;
/////P2第三位接138输入口,P0接数码管,P1接键盘扫描
unsigned char LED[16]={0x3f,0x06,0x5B,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};
unsigned char LEDBuff[6]={0xff,0xff,0xff,0xff,0xff,0xff};
bit stopwatchRefresh=1;
bit stopwatchRunning=0;
unsigned char T0RH=0;
unsigned char T0RL=0;
unsigned char Decimalpart=0;
unsigned int Integerpart=0;
unsigned char KeySta[4]={1,1,1,1};
void ConfigTimer0(unsigned char ms) //输入的时间应当小于工作模式的时间65ms
{
unsigned long tmp;
tmp=65536-12000000*ms/12/1000;
tmp=tmp+0; //此处设置补偿,使计算更精准
T0RH=(unsigned char )(tmp>>8);
T0RL=(unsigned char )tmp;
TMOD&=0xf0;
TMOD|=0x01;
TH0=T0RH;
TL0=T0RL;
ET0=1;
TR0=1;
}
void stopwatchDisplay()
{ unsigned char i;
unsigned buf[4];
LEDBuff[0]=LED[Decimalpart%10];
LEDBuff[1]=LED[Decimalpart/10&10];
buf[0]=Integerpart%10;
buf[1]=Integerpart/10%10;
buf[2]=Integerpart/100%10;
buf[3]=Integerpart/1000%10;
for (i=3;i>=1;i--)
{
if (buf[i]==0)
LEDBuff[i+2] = 0xff;
else
break;
}
for (;i>=0;i--)
{LEDBuff[i+2]=LED[buf[i]];}
LEDBuff[2]|=0x7f;
}
void LEDScan()
{
static unsigned char i=0;
P0=0xff;
P2=(P2&0xf8) | i;
P0=LEDBuff[i];
if(i<5)
i++;
else
i=0;
}
void KeyScan()
{
unsigned char i;
static unsigned char keybuf[4]=
{0xff,0xff,0xff,0xff};
keybuf[0] = (keybuf[0]<<1) |KEY1;
keybuf[1] = (keybuf[1]<<1) |KEY2;
keybuf[2] = (keybuf[2]<<1) |KEY3;
keybuf[3] = (keybuf[3]<<1) |KEY4;
for (i=0;i<4;i++)
{
if (keybuf[i]==0x00)
{
KeySta[i]=0;
} else if (keybuf[i]==0xff)
{
KeySta[i]=1;
}
}
}
void StopwatchReset()
{
stopwatchRunning = 0;
Decimalpart=0;
Integerpart=0;
stopwatchRefresh=1;
}
void StopwatchAction()
{
if(stopwatchRunning)
stopwatchRunning=0;
else
stopwatchRunning=1;
}
void KeyDriver()
{
unsigned char i ;
unsigned char backup[4]={1,1,1,1};
for (i=0;i<4;i++)
{
if (backup[i]!=KeySta[i])
{
if (backup[i]!=0)
{
if(i==1)
StopwatchReset();
else if(i==2)
StopwatchAction();
}
backup[i]=KeySta[i];
}
}
}
void stopwatchCount()
{
if(stopwatchRunning==1)
{
Decimalpart++;
if(Decimalpart>=100)
{
Decimalpart=0;
Integerpart++;
if(Integerpart>=10000)
{
Integerpart=0;
}
}
} stopwatchRefresh=1;
}
void main()
{
EA=1;
ConfigTimer0(2);
P1=0xff;
while(1)
{
if(stopwatchRefresh==1)
{
stopwatchRefresh=0;
stopwatchDisplay();
}
KeyDriver();
}
}
void interrupttimer0() interrupt 1
{ unsigned char time10ms=0;
TH0=T0RH;
TL0=T0RL;
LEDScan();
KeyScan();
time10ms++;
if (time10ms>=5)
{
time10ms=0;
stopwatchCount();
}
}
你这个最好分别调试看看 按键是用的中断的 那就看看进入中断没有 有没有查询按键值 数码管显示 你就把别的程序屏蔽了 只显示数码管 几个数字看看 对不对
排除了!花了4~6个小时,2处错误,中断缺个静态,还有就是stopwatchDisplay()函数里面for语句不能检测0,如果检测0程序就死,原因不明。修改后
void stopwatchDisplay()
{ unsigned char i;
unsigned buf[4];
LEDBuff[0]=LED[Decimalpart%10];
LEDBuff[1]=LED[Decimalpart/10&10];
buf[0]=Integerpart%10;
buf[1]=Integerpart/10%10;
buf[2]=Integerpart/100%10;
buf[3]=Integerpart/1000%10;
for (i=4;i>=2;i--)
{
if(buf[i-1]==0)
LEDBuff[i+1]=0x00;
else
break;
}
for (;i>=1;i--)
LEDBuff[i+1]=LED[buf[i-1]];
LEDBuff[2]|=0x80;
}
