Motor(Motor)

Macro definition

Left motor PWM
#define L_PWMA TIM8->CCR1
#define L_PWMB TIM8->CCR2

Right motor PWM
#define R_PWMA TIM8->CCR3
#define R_PWMB TIM8->CCR4

method

Balance_PWM_Init(u16 arr,u16 psc)

Initialize PWM parameters

parametertypeAnnotation
arru16Automatic reinstallation value
pscu16Pre -scoring coefficient
By setting the premature frequency coefficient and automatic reinstallation value, the output frequency of PWM can be controlled
In the case of balanced car, the default value is ARR = 2880, PSC = 0

Balance_Motor_Init(void)

GPIO mouth clock that enables the car motor

Set_Pwm(int motor_left,int motor_right)

Send a PWM signal to the left and right motors of the car

parametertypeAnnotation
motor_leftu16Pwm of the left motor
motor_rightu16Pwm of the right motor
PWM is a positive rotation (the car forward the direction of the car).
The effective PWM range is related to the set PWM automatic loading value set. When the default ARR = 2880, the effective range of PWM is 1500 ~ 2880

Example

1. Be sure to execute the BALANCE_MOTOR_INIT () first, then execute the balance_pwm_init ()

2. When driving the motor, remember to turn on the vehicle power switch

3. If you want to use the encoder ENCODER, make sure that the function of the initialized motor is to initialize the function of the coder timerBeforeCall !!


#include <Motor.h>
#include <delay.h>
Balance_Motor_Init();
Balance_PWM_Init(2880, 0);
while(1) {
    Set_Pwm(2200, 0);
    delay_ms(2000);
    Set_Pwm(0, 0);
    delay_ms(2000);
    Set_Pwm(0, 2200);
    delay_ms(2000);
    Set_Pwm(0, 0);
    delay_ms(2000);
}