9. Timer interrupt to control PWM servo

9.1. Purpose of the experiment

Use the basic timer interrupt function of STM32 to simulate the output PWM signal and control the PWM servo.

9.2, configuration pin information

  1. Import the ioc file from the Beep project and name it PwmServo.

According to the schematic diagram, the servos S1 S2 S3 S4 are connected to the PC3 PC2 PC1 PC0 pins of the STM32 respectively.

image-20220311092423330

Set the PC0 PC1 PC2 PC3 pins as output mode, the specific parameters are shown in the following figure:

image-20220311093207893

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

image-20220311093320238

Turn on the timer global interrupt setting.

image-20220311093358063

9.3. Analysis of the experimental flow chart

image-20220519102718568

9.4. core code explanation

  1. Create new bsp_pwmServo.h and bsp_pwmServo.c, and add the following content in pwmServo.h:

image-20220311094400800

Among them, SERVO_X_HIGH() means output high level, SERVO_X_LOW() means output low level.

  1. Create the following content in the bsp_pwmServo.c file:

The PwmServo_Init() function initializes the PWM position to 90 degrees.

image-20220311094947794

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

image-20220311095419820

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

image-20220311095059758

  1. The 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 and S4 respectively.

image-20220311095205303

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

image-20220311095845165

  1. Create a new HAL_TIM_PeriodElapsedCallback() function. The name of this function cannot be changed, otherwise the function will be found. The PwmServo_Handle() function is called by the timer 7 interrupt to generate the PWM signal.

image-20220311100301329

  1. Add the following content to the Bsp_Loop() function and press the button to control the PWM servo.

image-20220311102019612

9.5. Hardware connection

Since the PWM servos have different voltage drive values, the expansion board has added a voltage switching function. According to the jumper cap on the expansion board, the PWM output voltage can be modified to 5V or 6.8V. To use the PWM servo, the corresponding voltage must be selected with a jumper cap to avoid burning the servo. The PWM servos cannot be controlled without the jumper caps inserted. The pins of the PWM servo are: yellow->signal, red->power positive, black->power negative.

image-20220311101252607

Since the power of the PWM servo is relatively large, the expansion board should not be powered by USB 5V directly, but must be powered by DC 12V.

9.6. Experimental effect

After the program is programmed, the LED light flashes every 200 milliseconds. Press the button multiple times, the PWM servo will go back and forth between 50 degrees and 150 degrees.