各位高手,小弟正在基于STC51单片机开发程序,主程序的while循环中加入了一个if判断,如果start_xunji_flag全局变量等于ON,则进入当前分支,否则不进入分支,继续循环。在外部中断0服务子程序中,添加对全局变量start_xunji_flag设置为ON。测试程序,进入外部中断0,全局变量start_xunji_flag被设置为ON,但是主程序的while循环中未进入if分支。请教其中的原因,谢谢!代码如下:
/************************************************************
函数功能:主函数
*************************************************************/
void main()
{
LCD_init(); //调用LCD初始化函数
LCD_write_string(0,0,dis1);/*从第0列第0行起显示"smart
car"*/
LCD_write_string(0,1,dis2);/*从第0列第1行起显示智能小车状态,初始值为停止*/
Pwm_Init();/*脉宽调制初始化*/
/*设置红外线中断为最高优先级*/
IPH|=PX0H;
PX0
= 1;
/*设置定时器1中断为最低优先级*/
IPH &= ~PT1H;
PT1 = 0;
EA=1; //开启总中断
EX0=1; //开外中断0 ,接收红外模块信号中断
ET0=1; //定时器T0中断允许
IT0=1; //外部中断0的下降沿触发
TMOD=0x11; //使用定时器T0的模式1,使用16位定时器,设置定时器T1,使用16位定时器
TR0=0; //定时器T0关闭,停止定时器
/*初始化占空比*/
CCAP0H=CCAP0L=0x2C;
CCAP1H=CCAP1L=0x2C;
/*定时器1调速占空比设置*/
count1 = 50;
count2
= 50;
/*设置P0.7~P0.2为准双向口,P0.1为高阻输入,P0.0为高阻输入,引脚的输出阻抗大于输入阻抗即为高阻输入*/
P0M1 = 0x03;
P0M0
= 0x00;
T1_Init();/*定时器1初始化*/
start_xunji_flag = OFF;/*默认寻迹开关初始化为关闭*/
/*等待红外信号产生的中断,寻迹主循环*/
while(1) //while循环
{
//LCD_write_string(0,1," ");
//LCD_write_string(0,1,"while");
if
(start_xunji_flag == ON)
{
//LCD_write_string(0,1," ");
//LCD_write_string(0,1,"enter");
if(SJ1==0&&SJ2==1) //右边传感器脱离黑线
{
left(); //小车左转
}
if(SJ1==1&&SJ2==0) //左边传感器脱离黑线
{
right(); //小车右转
}
if(SJ1==1&&SJ2==1) //两个传感器都在黑线两边,小车匀速前进
{
forward(); //小车前进
}
if(SJ1==0&&SJ2==0) //两个传感器都在黑线上时,小车匀速前进
{
forward(); //小车前进
}
}
};
}
/************************************************************
函数功能:红外线触发的外中断处理函数
*************************************************************/
void Int0(void) interrupt 0
{
TR1 = 0; /*关闭定时器1*/
EX0=0; //关闭外中断0,不再接收二次红外信号的中断,只解码当前红外信号
TH0=0; //定时器T0的高8位清0
TL0=0; //定时器T0的低8位清0
TR0=1; //开启定时器T0
while(IR==0); //如果是低电平就等待,给引导码低电平计时
TR0=0; //关闭定时器T0
LowTime=TH0*256+TL0; //保存低电平时间
TH0=0; //定时器T0的高8位清0
TL0=0; //定时器T0的低8位清0
TR0=1; //开启定时器T0
while(IR==1); //如果是高电平就等待,给引导码高电平计时
TR0=0; //关闭定时器T0
HighTime=TH0*256+TL0; //保存引导码的高电平长度
if((LowTime>7800)&&(LowTime<8800)&&(HighTime>3600)&&(HighTime<4700))
{
//LCD_write_string(0,1," ");
//LCD_write_string(0,1,"speed
changed");
//根据电平长度确定是否为引导码,如果是引导码,就开始解码,否则放弃,引导码的低电平计时
//次数=9000us/1.085=8294, 判断区间:8300-500=7800,8300+500=8800.
if(DeCode()==1) // 执行遥控解码功能
{
switch(IRcode[2])
{
case
0x46:
/*恢复原始速度*/
CCAP0H=CCAP0L=0x2C;
CCAP1H=CCAP1L=0x2C;
forward();
LCD_write_string(7,1," ");
LCD_write_string(7,1,"forward");
break;
case
0x15:
//back();
//LCD_write_string(0,1," ");
//LCD_write_string(0,1,"speed
changed");
start_xunji_flag = ON;/*启动寻迹功能*/
//flag = ON;
//xunji();
LCD_write_string(7,1," ");
LCD_write_string(7,1,"back");
break;
case
0x44:
left();
LCD_write_string(7,1," ");
LCD_write_string(7,1,"left");
break;
case
0x43:
right();
LCD_write_string(7,1," ");
LCD_write_string(7,1,"right");
break;
case
0x40:
start_xunji_flag
= OFF;/*停止寻迹功能*/
stop();
LCD_write_string(7,1," ");
LCD_write_string(7,1,"stop");
break;
default:
/*调整PWM占空比*/
CCAP0H=CCAP0L=0x4C;
CCAP1H=CCAP1L=0x2C;
forward();/*调速后前进*/
//stop();
LCD_write_string(0,1," ");
LCD_write_string(0,1,"speed
changed");
break;
}
}
}
EX0=1; //开启外中断EX0
TR1
= 1; /*打开定时器1*/
}
|