附上代码:
#include<reg52.h>
#define uint unsigned int
#define uchar unsigned char
uchar code LedChar[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};
sbit P2_0 = P2^0;
sbit P2_1 = P2^1;
sbit P2_2 = P2^2;
sbit P2_3 = P2^3;
uint cnt=0;
uchar flag1s=0;
void delay(uchar i)
{
uchar j,k;
for(j=i;j>0;j--)
for(k=125;k>0;k--);
}
void display()
{
uint sec=0;
while(1)
{
if(flag1s==1)
{
flag1s=0;
sec++;
}
}
P2_0 = 1;
P0=LedChar[sec%10];
P2_0 = 0;
delay(5);
P2_1 = 1;
P0=LedChar[sec/10%10];
P2_1 = 0;
delay(5);
P2_2 = 1;
P0=LedChar[sec/100%10];
P2_2 = 0;
delay(5);
P2_3 = 1;
P0=LedChar[sec/1000%10];
P2_3 = 0;
delay(5);
}
void main()
{
EA=1;
TMOD=0x01;
TH0=0xFC;
TL0=0x67;
TR0=1;
ET0=1;
while(1)
{
uint a;
for(a=100;a>0;a--)
{
display();
}
}
}
void InterruptTimer0() interrupt 1
{
TH0=0xFC;
TL0=0x67;
cnt++;
if(cnt>=1000)
{
cnt=0;
flag1s=1;
}
}
用于计时的sec变量运行是正常的,但是P0输出总是0xFF,数码管始终是不亮的,问题也不知道出在哪。