#ifndef __QXA51_H__ #define __QXA51_H__ sbit IN1=P1^2; //为1 左电机反转 sbit IN2=P1^3; //为1 左电机正转 sbit IN3=P1^6; //为1 右电机正转 sbit IN4=P1^7; //为1 右电机反转 sbit EN1=P1^4; //为1 左电机使能 sbit EN2=P1^5; //为1 右电机使能 #define left_motor_en EN1=1 //左电机使能 #define left_motor_stops EN1=0 //左电机停止 #define right_motor_en EN2=1 //右电机使能 #define right_motor_stops EN2=0 //右电机停止 #define left_motor_go IN1=0,IN2=1 //左电机正转 #define left_motor_back IN1=1,IN2=0 //左电机反转 #define right_motor_go IN3=1,IN4=0 //右电机正转 #define right_motor_back IN3=0,IN4=1 //右电机反转 #endif
以下为控制小车运动的main函数,用delay函数将小车的运动区分开,如果小车想右转则左轮正转,如果小车想左转则右轮正转
#include#include void delay(unsigned int z)//毫秒级延时 { unsigned int x,y; for(x = z; x > 0; x--) for(y = 114; y > 0 ; y--); } void forward() { left_motor_en;//左电机使能 right_motor_en;//右电机使能 left_motor_go;//左电机正转 right_motor_go;//右电机正转 } void backward() { left_motor_en;//左电机使能 right_motor_en;//右电机使能 left_motor_back;//左电机反转 right_motor_back;//右电机反转 } void left() { left_motor_stops;//左电机停止 right_motor_en;//右电机使能 right_motor_go;//右电机正转 } void right() { right_motor_stops;//右电机停止 left_motor_en;//左电机使能 left_motor_go;//左电机正转 } void stop() { left_motor_stops;//左电机停止 right_motor_stops;//右电机停止 } void left_rapidly() { left_motor_en;//左电机使能 right_motor_en;//右电机使能 right_motor_go;//右电机正转 left_motor_back;//左电机反转 } void right_rapidly() { left_motor_en;//左电机使能 right_motor_en;//右电机使能 left_motor_go;//左电机正转 right_motor_back;//右电机反转 } void main() { forward();//小车前进 delay(3000);//毫秒级延迟 stop();//小车停止 delay(3000);//毫秒级延迟 backward();//小车后退 delay(3000);//毫秒级延迟 left();//小车左转 delay(3000);//毫秒级延迟 stop();//小车停止 delay(3000);//毫秒级延迟 right();//小车右转 delay(3000);//毫秒级延迟 left_rapidly();//小车左旋转 delay(3000);//毫秒级延迟 right_rapidly();//小车右旋转 delay(3000);//毫秒级延迟 stop();//小车停止 while(1); }



