红外壁障模块分析
原理和寻线是一样的,寻线红外观朝下,跟随朝前
跟随小车的原理
左边跟随模块能返回红外,输出低电平,右边不能返回,输出高电平,说明物体在左边,需要左转
右边跟随模块能返回红外,输出低电平,左边不能返回,输出高电平,说明物体在右边,需要右转
编程实现
分文件方式编程,函数声明的.h文件就不发出来啦。
//main.c
#include "motor.h"
#include "delay.h"
#include "reg52.h"
sbit leftSensor = P2^5;
sbit rightSensor = P2^4;
void main()
{
while(1){
if(leftSensor == 0 && rightSensor == 0){
goForward();
}
if(leftSensor == 1 && rightSensor == 0){
goRight();
}
if(leftSensor == 0 && rightSensor == 1){
goLeft();
}
if(leftSensor == 1 && rightSensor == 1){
//停
stop();
}
}
}
//motor.c
#include "reg52.h"
sbit RightCon1A = P3^2;
sbit RightCon1B = P3^3;
sbit LeftCon1A = P3^4;
sbit LeftCon1B = P3^5;
void goForward()
{
LeftCon1A = 0;
LeftCon1B = 1;
RightCon1A = 0;
RightCon1B = 1;
}
void goRight()
{
LeftCon1A = 0;
LeftCon1B = 1;
RightCon1A = 0;
RightCon1B = 0;
}
void goLeft()
{
LeftCon1A = 0;
LeftCon1B = 0;
RightCon1A = 0;
RightCon1B = 1;
}
void goBack()
{
LeftCon1A = 1;
LeftCon1B = 0;
RightCon1A = 1;
RightCon1B = 0;
}
void stop()
{
LeftCon1A = 0;
LeftCon1B = 0;
RightCon1A = 0;
RightCon1B = 0;
}
//delay.c
#include "intrins.h"
void Delay1000ms() //@11.0592MHz
{
unsigned char i, j, k;
_nop_();
i = 8;
j = 1;
k = 243;
do
{
do
{
while (--k);
} while (--j);
} while (--i);
}