51和l298n控制的小车程序这样写为啥占空比拉满感觉比别人用pwm写.不用ena和enb的慢,还有请问这个程序的x和y最大值是100吗
#include "headfile.h"
#include "motor.h"
sbit IN1=P0^0;
sbit IN2=P0^1;
sbit IN3=P1^2;
sbit IN4=P1^3;
sbit ENA=P2^2;
sbit ENB=P2^3;
#define R_go IN1=0;IN2=1
#define R_back IN1=1;IN2=0
#define R_stop IN1=0;IN2=0
#define L_back IN3=0;IN4=1
#define L_go IN3=1;IN4=0
#define L_stop IN3=0;IN4=0
void Speedr (uint16 a)
{ ENA=1;
pca_delay_ms(a);
ENA=0;
pca_delay_ms( 100-a);
}
void Speedl (uint16 b)
{ ENB=1;
pca_delay_ms( b);
ENB=0;
pca_delay_ms(100-b);
}
void MOTOR_Control(int16 x, int16 y)
{if(x>0)
{L_go;
Speedl(x);
}
if(y>0)
{R_go;
Speedr(y);
}
if(x<0)
{L_back;
Speedl(-x);
}
if (y<0)
{R_back;
Speedr(-y);
}
if(y=0)
{R_stop ;}
if(x=0)
{L_stop;}
}
void MOTOR_init(void)
{MOTOR_Control(0, 0);
}
#include "headfile.h"
#include "motor.h"
void main()
{DisableGlobalIRQ();
board_init();
EnableGlobalIRQ();
pca_dealy_init();
MOTOR_init();
while(1){
MOTOR_Control(100,100);
一般都是用PWM控制IN1,IN2的,你用来控制EN,虽然不常见但也能转
但你现在问题是控制流程,你这样控制的话两个电机都会有50%的时间空闲,转速上不去的
void MOTOR_Control(int16 x, int16 y)
{if(x>0)
{L_go;
Speedl(x);//执行这个时R电机失去控制
}
if(y>0)
{R_go;
Speedr(y);//执行这个时L电机失去控制
}
if(x<0)
{L_back;
Speedl(-x);
}
if (y<0)
{R_back;
Speedr(-y);
}
if(y=0)
{R_stop ;}
if(x=0)
{L_stop;}
}