红外避障小车的代码编写

前两篇博客中我对红外避障小车进行了组装,本次我为其编写了代码。

#include <REG52.H>
sbit ENA=P0^0;
sbit IN1=P0^1;
sbit IN2=P0^2;
sbit ENB=P2^0;
sbit IN3=P2^1;
sbit IN4=P2^2;
sbit A1=P1^0;
sbit B2=P1^1;
void delay(int z) {
    
    
	int x,y;
	for(x=z;x>0;x--)
		for(y=110;y>0;y--);
}
void go() {
    
    
	ENA=1;
	IN1=1;
	IN2=0;
	ENB=1;
	IN3=1;
	IN4=0;
}
void back() {
    
    
	ENA=1;
	IN1=0;
	IN2=1;
	ENB=1;
	IN3=0;
	IN4=1;
}
void right() {
    
    
	ENA=1;
	IN1=0;
	IN2=1;
	ENB=1;
	IN3=1;
	IN4=0;
}
void left()
{
    
    
	ENA=1;
	IN1=0;
	IN2=1;
	ENB=1;
	IN3=1;
	IN4=0;
}
void main() {
    
    
	while(1) {
    
    
		go();
		if(B2==0) {
    
    
			back();
			delay(500);
			left();
			delay(700);
		}
		if(A1==0) {
    
    
			back();
			delay(500);
			right();
			delay(700);
		}
	}
}

其中分别定义了延时,左转,右转,前进,后退的函数,后面在主函数中方便直接调用。
经测试小车装上充电宝后可以正常运作,在下一篇博客中我会将电池盒接入装置中,方便操作的进行。
如果有什么问题或发现错误,欢迎与我交流。

猜你喜欢

转载自blog.csdn.net/qq_52556690/article/details/109960798