9. Timer interrupt to control PWM servo 9.1. Purpose of the experiment 9.2, configuration pin information 9.3. Analysis of the experimental flow chart 9.4. core code explanation 9.5. Hardware connection 9.6. Experimental effect
Use the basic timer interrupt function of STM32 to simulate the output PWM signal and control the PWM servo.
According to the schematic diagram, the servos S1 S2 S3 S4 are connected to the PC3 PC2 PC1 PC0 pins of the STM32 respectively.
Set the PC0 PC1 PC2 PC3 pins as output mode, the specific parameters are shown in the following figure:
Turn on the timer global interrupt setting.
Among them, SERVO_X_HIGH() means output high level, SERVO_X_LOW() means output low level.
The 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.
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.
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.
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.