Welcome to RDK X3 Robot repository
9. Timer interrupt control PWM servo
9. Timer interrupt controls PWM steering gear
9. Timer interrupt controls PWM steering gear9.1. Experimental purpose9.2. Configure pin information9.3. Analysis of experimental flow chart9.4. Core code interpretation9.5. Hardware connection9.6. Experimental effect
9.1. Experimental purpose
Using STM32 basic timer interrupt function, analog output PWM signal, control PWM steering gear.
9.2. Configure pin information
- Import ioc file from Beep project and name it PwmServo.
According to the schematic diagram, the steering gear S1, S2, S3, S4 is respectively connected to the PC3, PC2, PC1, PC0 pins of the STM32.

Set PC0 PC1 PC2 PC3 pin to output mode, specific parameters are shown as follows:

- Next, we need to configure timer 7. The specific configuration parameters are shown in the following figure.

The timer global interrupt setting is enabled.

9.3. Analysis of experimental flow chart

9.4. Core code interpretation
- Create bsp_pwmServo.h and bsp_pwmServo.c, and add the following contents to pwmServo.

SERVO_X_HIGH() indicates the output high level, and SERVO_X_LOW() indicates the output low level.
- Create the following content in the bsp_pwmServo.c file:
The PwmServo_Init() function initializes the position of the PWM at 90 degrees.

The PwmServo_Angle_To_Pulse() function converts the Angle into the duty cycle value of the PWM.

- The PwmServo_Set_Angle() function sets the pwm steering gear angle, index=0~3, angle= 0-180.

- The PwmServo_Set_Angle_All() function sets the angles of all pwm steering engines. angle_s1 corresponds to the angles of S1, ranging from 1 to 180, and the other three parameters correspond to the angles of S2 S3 S4 respectively.

- The PwmServo_Handle() function needs to be called during the interrupt of the timer to simulate the output PWM signal and control the steering gear according to the Angle value set above.

- Create a new HAL_TIM_PeriodElapsedCallback() function. This function cannot change its name, otherwise it will be found. The PwmServo_Handle() function is interrupted on timer 7 to generate a PWM signal.

- Add the following content to the Bsp_Loop() function to control PWM steering gear.

9.5. Hardware connection
Since the PWM actuator has different voltage driving values, the voltage switching function is added to the expansion board. According to the jumper cap on the expansion board, the PWM output voltage can be modified to 5V or 6.8V. To use PWM steering gear, the corresponding voltage must be selected with jumper cap to avoid burning out the steering gear. If the jumper cap is not inserted, the PWM steering engine cannot be controlled. The pins of PWM steering gear are: yellow -> signal, red -> positive power supply, black -> negative power supply.

Because the power of the PWM actuator is relatively large, do not use the USB5V power supply directly on the expansion board, and use the battery power supply.
9.6. Experimental effect
After burning the program, the LED light flashes every 200 milliseconds. By pressing the key several times, the PWM steering machine will go back and forth between 50 degrees and 150 degrees.