Welcome to RDK X3 Robot repository
12. Control motor forward and reverse
12. Control motor forward reversal
12. Control motor forward reversal12.1. Experimental purpose12.2. Configure pin information12.3. Analysis of experimental flow chart12.4. Core code interpretation12.5. Hardware connection12.6. Experimental effect
12.1. Experimental purpose
Using the timer function of STM32, the motor drive chip AM2861 is driven to control the motor forward, reverse and stop.
12.2. Configure pin information
- Import ioc file from Beep project and name it Motor.
According to the schematic diagram, there are four AM2861 motor drive modules in total, one motor drive module controls one motor, and the pin configuration is shown in the figure below.

- Set timer 1, clock source internal clock, and set four channels to output PWM signal CH1 CH2N CH3N CH4 corresponding to pin PA8 PB0 PB1 PA11.

Other parameters are shown in the figure below:

- Then set timer 8, select the internal clock as the clock source, and set four channels to output PWM signals. CH1 CH2 CH3 CH4 corresponds to pins PC6 PC7 PC8 PC9.

Other parameters are the same as those of timer 1.

The final chip configuration pin is shown in the figure below:

12.3. Analysis of experimental flow chart

12.4. Core code interpretation
- Create bsp_motor.h and bsp_motor.c and add the following content to bsp_motor.h:

M1 corresponds to the motor in the upper left corner of the body, M2 corresponds to the motor in the lower left corner, M3 corresponds to the motor in the upper right corner, and M4 corresponds to the motor in the lower right corner.
- Create the following content in the bsp_motor.c file:
Motor timer PWM output starts initialization.

- Motor stop function, brake=1 for brake stop, brake=0 for free stop.

- Because the motor has a certain control dead zone, the dead zone can be filtered. If you choose not to filter, set the MOTOR_IGNORE_PULSE parameter to 0.

- Next is to set the motor speed, where id is the motor ID, speed speed value range: ± (3600-MOTOR_IGNORE_PULSE), 0 is stop.

- Add motor initialization to the Bsp_Init() function.

- Add a button to control the motor function in the Bsp_Loop() function, press the first forward, the second free stop, the third backward, and the fourth brake stop.

12.5. Hardware connection
The motor connection line needs to be connected to the corresponding motor as shown in the figure below, otherwise it may cause the problem that the program does not match the phenomenon. Motor 1 corresponds to the Motor in the upper left corner of the body, Motor 2 corresponds to the Motor in the lower left corner, motor 3 corresponds to the motor in the upper right corner, and Motor 4 corresponds to the motor in the lower right corner.

Because the power of the motor is relatively large, do not directly use USB 5V power supply for the expansion board, and must use battery power supply.
Motor interface sequence diagram:

12.6. Experimental effect
Since the motor will turn when started, please set up the car before the experiment, and the motor wheels are suspended to avoid rampaging.
After burning the program, the LED light flashes every 200 milliseconds. Press the first forward, the second free stop, the third backward, the fourth brake stop.