红外线避障测试程序

/***********************************************************
功能:红外线蔽障测试程序
说明: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();
   }
}

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值