Welcome to ROSMASTER R2 repository
9. Timer interrupt control PWM servos
9. Timer interrupt control PWM servos
9. Timer interrupt control PWM servos9.1. Purpose of experiment9.2 Configuring Pin Information9.3. Experimental flow chart analysis9.4. Core Code Explanation9.5. Hardware Connection9.6. Experimental Effect
9.1. Purpose of experiment
Use the basic timer interrupt function of STM32 to simulate the output of PWM signal to control the PWM servo.
9.2 Configuring Pin Information
- Import the ioc file from Beep's project and name it PwmServo.
According to the schematic diagram, it can be seen that servo S1 S2 S3 S4 are connected to the PC3 PC2 PC1 PC0 pins of STM32 respectively.

Set PC0 PC1 PC2 PC3 pins to output mode with the parameters shown below:

- Next we need to configure Timer 7, the specific configuration parameters are shown below.

Turns on the timer global interrupt setting.

9.3. Experimental flow chart analysis

9.4. Core Code Explanation
- Create a new bsp_pwmServo.h and bsp_pwmServo.c, and add the following to pwmServo.h:

Among them, SERVO_X_HIGH() means output high level, SERVO_X_LOW() means output low level.
- In the bsp_pwmServo.c file, create the following new content:
PwmServo_Init() function initializes the PWM position to 90 degrees.

The PwmServo_Angle_To_Pulse() function converts the angle into a PWM duty cycle value.

3.PwmServo_Set_Angle() function sets the pwm servo angle, index=0~3, angle is 0-180.

4.PwmServo_Set_Angle_All() function sets the angle of all pwm servos, angle_s1 corresponds to the angle of S1, the range is 1-180, and the other three parameters correspond to the angle values of S2 S3 S4 respectively.

- PwmServo_Handle() function needs to be called in the interrupt of the timer to simulate the output PWM signal to control the servo according to the servo angle value set above.

- New HAL_TIM_PeriodElapsedCallback() function, this function can not change the name, otherwise this function will be found. Call PwmServo_Handle() function at timer 7 interrupt to generate PWM signal.

- Add the following to the Bsp_Loop() function button to control the PWM servo.

9.5. Hardware Connection
Because PWM servo has different voltage drive value, so the expansion board added the function of voltage switching, according to the jumper cap on the expansion board, you can modify the PWM output voltage to 5V or 6.8 V. To use PWM servo, you must use the jumper cap to select the corresponding voltage, to avoid burning the servo. If you don't insert the jumper cap, you can't control the PWM servo.The pins of PWM servo are: yellow->signal, red->power positive, black->power negative.

Due to the relatively large power of the PWM servo, the expansion board should not use USB5V power supply directly, but need to use DC 12V power supply.
9.6. Experimental Effect
After burning the program, the LED flashes every 200 milliseconds. Pressing the key several times, the PWM servo will go back and forth between 50 degrees and 150 degrees.