|
各位大神好
以下是51单片机(STC89C52)蓝牙遥控+超声波跟随+红外循迹主程序,现在经过测试功能均能实现,能否帮忙解释下主程序里面超声波+红外切换那块还有问题么?其他子程序是否还有问题,有没有什么优化代码的可能,感谢解惑!!!(完整程序放置在附件)
#include <REGX52.H>
#include <INTRINS.H>
#include <Timer2.H>
#include <Timer0.H>
#include "UART.H"
#include "Delay.H"
#include "MotorDrive.H"
#include "HC_SR04.H"
#include <IR_Tracking.H>
bit gb_ultrasonic=0;
bit gb_IR=0;
bit gbRI=0;
sbit L298N_ENA=P1^4;
sbit L298N_ENB=P1^5;
sbit L298N_ENA1=P1^1;
sbit L298N_ENB1=P0^2;
unsigned char Compare,Compare1,Compare2,Compare3;
unsigned char Counter,Counter1,Counter2,Counter3;
unsigned char Data;
void main()
{
UART_Init(); //串口初始化
Timer0_Init(); //T0初始化
Timer2_Init(); //T2初始化
Compare=Compare1=Compare2=Compare3=20;
while(1)
{
if(gb_IR)
IR_Tracking();//红外循迹
if(gb_ultrasonic)
Ultrasound_Dis();//超声波跟随
if(gbRI)//有新的串口数据 在串口接收中断中加入gbRI=1;
{
gbRI=0;//新的串口数据已处理
gb_IR=0;//除循迹指令外,其它指令不循迹
gb_ultrasonic=0;//除超声波跟随指令外,其它指令不跟随
if(Data == 8)
gb_ultrasonic=1;//收到超声波跟随指令
if(Data == 7)
gb_IR=1;//收到循迹指令
else if(Data == 0)
StopIt();
else if(Data == 1)
{
Compare=Compare1=Compare2=Compare3=100;
Forward();
}
else if(Data == 2)
{
Compare=Compare1=Compare2=Compare3=100;
BackOff();
}
else if(Data==3)
{
Compare=Compare1=Compare2=Compare3=40;
TurnLeft();
}
else if(Data==4)
{
Compare=Compare1=Compare2=Compare3=40;
TurnRight();
}
else if(Data==5)
{
Compare=Compare1=Compare2=Compare3=30;
TurnLeftCircle();
}
else if(Data==6)
{
Compare=Compare1=Compare2=Compare3=30;
TurnRightCircle();
}
}
}
}
void Timer0_Routine() interrupt 1
{
TL0=0xA4; //设置定时初值
TH0=0xFF; //设置定时初值
Counter++;
Counter%=100; //计数值变化范围限制在0~99
Counter1++;
Counter1%=100; //计数值变化范围限制在0~99
Counter2++;
Counter2%=100; //计数值变化范围限制在0~99
Counter3++;
Counter3%=100; //计数值变化范围限制在0~99
if(Counter<Compare)
{
L298N_ENA=1;
}
else
{
L298N_ENA=0;
}
if(Counter1<Compare1)
{
L298N_ENB=1;
}
else
{
L298N_ENB=0;
}
if(Counter2<Compare2)
{
L298N_ENA1=1;
}
else
{
L298N_ENA1=0;
}
if(Counter3<Compare3)
{
L298N_ENB1=1;
}
else
{
L298N_ENB1=0;
}
}
void UART_Routine() interrupt 4 //串口中断
{
gbRI=1;
if(RI==1) //允许单片机接收数据
{
Data=SBUF; //读取数据
UART_SendByte(SBUF); //将接收到的数据发回串口
RI=0; //接收标志位清0 软件复位
}
}
|
|