发表于:2014/7/9 8:26:17
#0楼
各位高手,小弟正在基于STC51单片机开发程序,主程序的while循环中加入了一个if判断,如果start_xunji_flag全局变量等于ON,则进入当前分支,否则不进入分支,继续循环。在外部中断0服务子程序中,添加对全局变量start_xunji_flag设置为ON。测试程序,进入外部中断0,全局变量start_xunji_flag被设置为ON,但是主程序的while循环中未进入if分支。请教其中的原因,谢谢!代码如下:
/************************************************************
函数功能:主函数
*************************************************************/
void main()
{
start_xunji_flag = OFF;/*默认寻迹开关初始化为关闭*/
/*等待红外信号产生的中断,寻迹主循环*/
while(1) //while循环
{
if (start_xunji_flag == ON)
{
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
{
if((LowTime>7800)&&(LowTime<8800)&&(HighTime>3600)&&(HighTime<4700))
{
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*/
}
/************************************************************
函数功能:主函数
*************************************************************/
void main()
{
start_xunji_flag = OFF;/*默认寻迹开关初始化为关闭*/
/*等待红外信号产生的中断,寻迹主循环*/
while(1) //while循环
{
if (start_xunji_flag == ON)
{
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
{
if((LowTime>7800)&&(LowTime<8800)&&(HighTime>3600)&&(HighTime<4700))
{
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*/
}
10分悬赏