数码管是共阳的,现在我估计是给数码管的高低电平错了,该怎么改?
#include<reg51.h>
sbit kh=P1^0;
sbit km=P1^1;
sbit ks=P1^2;
sbit st=P1^3;
sbit b=P3^7;
unsigned char table[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x88, 0x83,0xc6,0xa1,0x86,0x8e};
unsigned char i=0,hour=0,minute=0,second=0;
void delayms(unsigned int x)
{
unsigned char a=160;
while(x--)
{
while(a--);
a=160;
}
}
main()
{
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
TMOD=1;
TR0=1;
ET0=1;
EA=1;
while(1)
{
P0=table[hour/10];
P2=0x20;
delayms(1);
P2=0;
P0=table[hour%10];
P2=0x10;
delayms(1);
P2=0;
P0=table[minute/10];
P2=0x08;
delayms(1);
P2=0;
P0=table[minute%10];
P2=0x04;
delayms(1);
P2=0;
P0=table[second/10];
P2=0x02;
delayms(1);
P2=0;
P0=table[second%10];
P2=0x01;
delayms(1);
P2=0;
if(!kh)
{
delayms(200);
hour++;
if(hour>23)
hour=23;
}
else if(!km)
{
delayms(200);
minute++;
if(minute>59)
minute=59;
}
else if(!ks)
{
delayms(200);
second++;
if(second>59)
second=59;
}
if(!st)
{
delayms(200);
TR0=1;
}
if(TR0==1&&hour==0&&minute==0&&second<3)
b=!b;
delayms(1);
}
}
void t0() interrupt 1
{
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
i++;
if(i>=20)
{
i=0;
second--;
if(second>59)
{
second=59;
minute--;
if(minute>59)
{
minute=59;
hour--;
if(hour>23)
{
hour=0;
minute=0;
second=0;
TR0=0;
}
}
}
}
} |