嘗試寫交通燈的程序,下面是兩邊數(shù)碼管的顯示,每一邊數(shù)碼管單獨運行都沒有問題,一起運行數(shù)碼管就會跑飛,不明白原因何在,求指導(dǎo)
#include <reg52.h>
#define uchar unsigned char
typedef unsigned char uint8;
typedef unsigned int uint16;
uint8 second=20;
uint16 mstcnt=0;
sbit P2_0 = P2^0;
sbit P2_1 = P2^1;
sbit P2_2 = P2^2;
sbit P2_3 = P2^3;
uchar second,minute,hour;
unsigned int tcount;
unsigned char m;
uchar code table[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,
0xf8,0x80,0x90}; //共陽極
void delay(unsigned int i);
void time_pro( void)
{
if(second==0)//秒鐘設(shè)為59進制
{ second=20;
}
}
void display()
{
P1=table[second/10];
P2_2 = 0;
delay(5);
P2_2= 1;
P1=(table[second%10]& 0x7f);
P2_3= 0;
delay(5);
P2_3= 1;
P0=table[second/10];
P2_0 = 0;
delay(5);
P2_0= 1;
P0=(table[second%10]& 0x7f);
P2_1= 0;
delay(5);
P2_1= 1;
}
/*演示主程序*/
void timer0(void) interrupt 1 using 0//定時器0方式1,250us中斷一次
{
TH0=0xff;
TL0=0x19;
TMOD = 0x01;
mstcnt++;
if(mstcnt==4000)
{
second--;
mstcnt=0; //注意對計數(shù)單元的清零
}
}
void timer1(void) interrupt 1 using 0//定時器0方式1,250us中斷一次
{
TH0=0xff;
TL0=0x19;
TMOD = 0x01;
mstcnt++;
if(mstcnt==4000)
{
second--;
mstcnt=0; //注意對計數(shù)單元的清零
}
}
void main(void)
{
P0=0xff;
P1=0xff; //初始化p0口,全設(shè)為1使數(shù)碼管
P2=0xff; //初始化p2口,全設(shè)為1使數(shù)碼管
TMOD = 0x01; //time0為定時器,方式1
TH0=0xff; //預(yù)置計數(shù)初值
TL0=0x19;
EA=1;
ET0=1;
TR0=1;
while (1)
{
time_pro( ); //時間處理
display( ); //顯示時間
}
}
/*演示主程序*/
void delay(unsigned int i)
{
uchar j,k;
for(j=i;j>0;j--)
for(k=125;k>0;k--);
}
|