本代碼中P1為交通燈輸出,出現(xiàn)的問題是進入中斷后,退出中斷后數(shù)據(jù)和延時時間有點不對勁,不知道什么原因,希望大神幫幫忙。
#include<reg52.h>
#define uchar unsigned char
#define uint unsigned int
uchar code a[10]={0xf3,0xf7,0xf5,0xde,0xfe,0xee,0xf6};
uchar i;
/*------------------------------------------------
定時器1子程序
------------------------------------------------*/
void sec(uchar i)
{
while(i--)
{
uchar k=10;
while(k--)
{
TH1=0X3C;
TL1=0XB0;
TR1=1;
while(!TF1);
TF1=0;
}
}
}
/*------------------------------------------------
中斷初始化子程序
------------------------------------------------*/
void Init_contr(void)
{
TMOD |= 0x10;
sec(1);
TCON=0;
PX0=1;
IE=0x85;
}
/*------------------------------------------------
主程序
------------------------------------------------*/
void main (void)
{
Init_contr();
while (1)
{
P1=a[0];
sec(110);
for(i=0;i<3;i++)
{
P1=a[1];
sec(1);
P1=a[0];
sec(1);
}
P1=a[2];
sec(4);
P1=a[3];
sec(50);
for(i=0;i<3;i++)
{
P1=a[4];
sec(1);
P1=a[3];
sec(1);
}
P1=a[5];
sec(4);
}
}
/*------------------------------------------------
外部中斷0程序(緊急情況)
------------------------------------------------*/
void sus(void) interrupt 0
{
char temp;
temp=P1;
P1=a[6];
sec(40);
P1=temp;
}
/*------------------------------------------------
外部中斷1程序(一路有車一路無車)
------------------------------------------------*/
void res(void) interrupt 2
{
char temp;
EA=0; /*關(guān)中斷,以保持現(xiàn)場*/
temp=P1;
EA=1;
if((P3|0xef)==0xff)
{
P1=a[0];
sec(10);
EA=0;
P1=temp;
EA=1;
}
else if((P3|0xdf)==0xff)
{
P1=a[3];
sec(10);
EA=0;
P1=temp;
EA=1;
}
else
{
return;
}
}
|