/***********************************************************
功能:红外线蔽障测试程序
说明:P1_0接左电机,P1_1接右电机(四个方向的打包程序)
LeftIR P1_2 //左边红外接收连接到P1_2
RightIR P3_5 //右边红外接收连接到P3_5
LeftLaunch P1_3 //左边红外发射连接到P1_3
RightLaunch P3_6 //右边红外发射连接到P3_6
时间:2012.12.15
作者:荆轩道
************************************************************/
#include<BoeBot.h>
#include<uart.h>
#include<intrins.h>
#define LeftIR P1_2 //左边红外接收连接到P1_2
#define RightIR P3_5 //右边红外接收连接到P3_5
#define LeftLaunch P1_3 //左边红外发射连接到P1_3
#define RightLaunch P3_6 //右边红外发射连接到P3_6
void IRLaunch(unsigned char IR)
{
int counter;
if(IR=='L')
for(counter=0;counter<38;counter++)
{
LeftLaunch=1;
_nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_();
_nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_();
LeftLaunch=0;
_nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_();
_nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_();
}
if(IR=='R')
for(counter=0;counter<38;counter++)//右边发射
{
RightLaunch=1;
_nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_();
_nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_();
RightLaunch=0;
_nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_();
_nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_();
}
}
void Forward(void)//向前行走子程序
{
P1_1=1;
delay_nus(1700);
P1_1=0;
P1_0=1;
delay_nus(1300);
P1_0=0;
delay_nms(20);
}
void Left_Turn(void)//左转子程序
{
int i;
for( i=1;i<=26;i++)
{
P1_1=1;
delay_nus(1300);
P1_1=0;
P1_0=1;
delay_nus(1300);
P1_0=0;
delay_nms(20);
}
}
void Right_Turn(void)//右转子程序
{
int i;
for( i=1;i<=26;i++)
{
P1_1=1;
delay_nus(1700);
P1_1=0;
P1_0=1;
delay_nus(1700);
P1_0=0;
delay_nms(20);
}
}
void Backward(void)//向后行走子程序
{
int i;
for( i=1;i<=65;i++)
{
P1_1=1;
delay_nus(1300);
P1_1=0;
P1_0=1;
delay_nus(1700);
P1_0=0;
delay_nms(20);
}
}
int main(void)
{
int irDetectLeft,irDetectRight;
uart_Init();
printf("Program Running!\n");
while(1)
{
IRLaunch('R'); //右边发射
irDetectRight = RightIR;//右边接收
IRLaunch('L'); //左边发射
irDetectLeft = LeftIR;//左边接收
if((irDetectLeft==0)&&(irDetectRight==0))//两边同时接收到红外线
{
Backward();
Left_Turn();
Left_Turn();
}
else if(irDetectLeft==0)//只有左边接收到红外线
{
Backward();
Right_Turn();
}
else if(irDetectRight==0)//只有右边接收到红外线
{
Backward();
Left_Turn();
}
else
Forward();
}
}