当给INT0高电平时,竟然触发了中断
#include "reg52.h"
#define unchar unsigned char
#define unint unsigned int
#define _10s 56320
#define _15s 51712
#define _20s 47014
#define _30s 37888
unchar flag;
unint cnt;
unchar sbuff[5];
unint time;
sbit R_EN=P2^4;
void delay();
void shot_delay();
void init();
void timer0_confi(unint x);
void main()
{
unchar k;
init();
while (1)
{
if(flag==1)
{
flag=0;
for(k=0;k<5;k++)
{
SBUF=sbuff[k];
while(!TI);
TI=0;
}
if (sbuff[0]==0x01)
{
switch(sbuff[1])
{
case 0x01: time=_10s;break;
case 0x02: time=_15s;break;
case 0x03: time=_20s;break;
case 0x04: time=_30s;break;
default: time=_15s;break;
}
timer0_confi(time);
switch(sbuff[2])
{
case 0x02: P1=0x0A; break;
case 0x01: P1=0x10;cnt=0; break;
default:P1=0x10;break;
}
ET0=1;
TR0=1;
}
}
}
}
void init()
{
unchar i;
P1=0x10;
SCON=0x50;
TMOD=0x21;//定时器0工作在方式1,定时器1工作在方式2
TH1=0xFD;//波特率为9600
TL1=0xFD;
ET1=0; //禁止定时器1中断
TR1=1; //打开定时器1
TR0=0; //定时器0
ET0=1; //开放定时器0中断
R_EN=0; //485接收使能
shot_delay();
EA=1;//开总中断
ES=1;//开串口中断
REN=1; //串口允许接收
EX0=0;//关闭外部中断0
shot_delay();
EX0=1;//打开外部中断
IE0=0;//关闭中断请求
IT0=1;//选择下降沿触发外部中断0
for (i=0;i<5;i++)//初始化接收缓存
sbuff[i]=0x00;
time=_30s;
}
void int0() interrupt 0
{
EX0=0;
P1=0x0A;
ET0=1;
TR0=1;
IE0=0;
} |