我有一个带有 TB6612FNG 和 Atmega328P 的定制板,带有 arduino uno 引导加载程序。当我对其进行编程时,电机控制器可以很好地与 PWM 控制配合使用。但是,当我将电源从 USB 电源切换到电池电源时,电机不再工作,它有点抖动并且没有电机运动。但是,如果我将速度更新为 255 或 -255,它将全速运行。
综上所述,无论 USB 电源或电池电源如何,电机控制器都能在全速下正常工作。但是,如果我将速度降低到除 255 或 -255 之外的任何值,它就无法使用电池电源,而只能使用 USB 电源。当我同时连接电池和 USB 电源时,即使这样,它也无法在较低的速度下工作。
电池 (VM) 连接到为电路板和电机控制器供电的稳压器 (1117 - 5V - VCC) 和直接电源 - VM 连接到 TB6612FNG VM 引脚。这是否与电池电源如何为芯片和电机控制器供电有关,或者哪个先到达,VM 或 VCC,或类似的东西?
请找到下面的代码以及定制板的示意图。任何帮助表示赞赏。
#define PWMA 5
#define AIN1 10
#define AIN2 4
#define BIN1 7
#define BIN2 8
#define PWMB 6
#define motor_A 0
#define motor_B 1
#define FORWARD 1
#define REVERSE 0
#define RIGHT 1
#define LEFT 0
void setup()
{
pinMode(PWMA, OUTPUT);
pinMode(AIN1, OUTPUT);
pinMode(AIN2, OUTPUT);
pinMode(PWMB, OUTPUT);
pinMode(BIN1, OUTPUT);
pinMode(BIN2, OUTPUT);
delay(5000);
}
void loop()
{
motor_brake();
delay(1000);
motor_drive(FORWARD, 150); // if this replaced with 255, it works on battery also, but at full speed
delay(1000);
motor_brake();
delay(1000);
motor_drive(REVERSE, 150); // if this replaced with 255, it works on battery also, but at full speed
delay(1000);
motor_brake();
delay(1000);
motor_turn(RIGHT, 150, 150); // if this replaced with 255, it works on battery also, but at full speed
delay(1000);
motor_brake();
delay(1000);
}
void motor_brake()
{
digitalWrite(AIN1, 1);
digitalWrite(AIN2, 1);
digitalWrite(PWMA, LOW);
digitalWrite(BIN1, 1);
digitalWrite(BIN2, 1);
digitalWrite(PWMB, LOW);
}
void motor_drive(char direction, unsigned char speed)
{
if (direction == FORWARD)
{
motor_control(motor_A, FORWARD, speed);
motor_control(motor_B, FORWARD, speed);
}
else
{
motor_control(motor_A, REVERSE, speed);
motor_control(motor_B, REVERSE, speed);
}
}
void motor_turn(char direction, unsigned char speed_A, unsigned char speed_B )
{
if (direction == RIGHT)
{
motor_control(motor_A, REVERSE, speed_A);
motor_control(motor_B, FORWARD, speed_B);
}
else
{
motor_control(motor_A, FORWARD, speed_A);
motor_control(motor_B, REVERSE, speed_B);
}
}
void motor_control(char motor, char direction, unsigned char speed)
{
if (motor == motor_A)
{
if (direction == FORWARD)
{
digitalWrite(AIN1, HIGH);
digitalWrite(AIN2, LOW);
}
else
{
digitalWrite(AIN1, LOW);
digitalWrite(AIN2, HIGH);
}
analogWrite(PWMA, speed);
}
else
{
if (direction == FORWARD)
{
digitalWrite(BIN1, LOW);
digitalWrite(BIN2, HIGH);
}
else
{
digitalWrite(BIN1, HIGH);
digitalWrite(BIN2, LOW);
}
analogWrite(PWMB, speed);
}
}