51单片机中断相关问题
各位高手,本人是刚接触单片机编程,关于51单片机的中断这块还不是很懂,下面有个小程序,望哪位高手能帮忙分析下,谢谢!
#include<reg51.h>
#define uchar unsigned char
#define uint unsigned int
sbit dula=P2^6;
sbit wela=P2^7;
sbit beep=P2^3;
unsigned int pp;
unsigned char code table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,
0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};
uchar j,k,l=255;
uchar a1,a0;
uchar shijian,deng=2;
void delay(unsigned int i)
{
for(j=i;j>0;j--)
for(k=125;k>0;k--);
}
void display(unsigned char sh_c,unsigned char g_c) //此语句是数码管显示用的,可以不理它
{
dula=0;
P0=table[sh_c];
dula=1;
dula=0;
wela=0;
P0=0xfe;
wela=1;
wela=0;
delay(5);
P0=table[g_c];
dula=1;
dula=0;
P0=0xfd;
wela=1;
wela=0;
delay(5);
}
void main()
{
TMOD=0x01; //定时/计数器工作方式1
TR0=1; //TR0置1时,T0开始工作
TH0=(65536-46080)/256;// 由于晶振为11.0592,故所记次数应为46080,计时器每隔50000微秒发起一次中断。
TL0=(65536-46080)%256;//46080的来历,为50000*11.0592/12
ET0=1; //定时/计数器T0中断允许位
EA=1; //CPU开中断(即EA=1) //此处开中断,程序是先执行void time0() interrupt 1 还是执行下面while里面的内容?
while(1)
{
if(shijian==0) //此处shijian变量没有赋初值啊?怎么判断等不等于0?
{
switch(deng)
{
case 0: shijian=50; //红灯
deng=1;
break;
case 1: shijian=15; //黄灯
deng=2;
break;
case 2: shijian=50; //绿灯
deng=3;
break;
case 3: shijian=15; //黄灯
deng=0;
break;
}
}
if(pp==20)
{ pp=0;
shijian--;
}
a0=shijian%10;
a1=shijian/10;
display(a1,a0);
switch(deng)
{
case 1: P1=0X00; //红灯
break;
case 2: //黄灯
if(shijian%2==0)
P1=0x00;
if(shijian%2==1)
P1=0xff;
break;
case 3: //绿灯
if(shijian%2==0)
P1=0x55;
if(shijian%2==1)
P1=0xaa;
break;
case 0: if(shijian%2==0) //黄灯
P1=0x00;
if(shijian%2==1)
P1=0xff;
break;
}
}
}
void time0() interrupt 1 //定时/计数器工作方式1
{ TH0=(65536-46080)/256; //初值
TL0=(65536-46080)%256;
pp++;
}