Servo control

This tutorial demonstrates: using the basic timer (TIM7) interrupt to simulate PWM control of the external S1 interface servo on the development board.

The first tutorial in this chapter will be more detailed than the following tutorials. The purpose is to demonstrate from the new project to the complete effect, and guide users how to use STM32CubeIDE to develop

1. Software-Hardware

2. Brief principle

1. Hardware schematic diagram

image-20231014113119959

2. Physical connection diagram

image-20231111153337864

Note: Refer to the color of the servo wire for servo wiring. This experiment requires connecting 5V and GND jumper caps

Steering gearDevelopment board
VCC(red)5V
SIG(yellow)S1/S2/S3/S4
GND(brown)GND

3. Control principle

By changing the duty cycle of the PWM signal, the angle of rotation of the servo is controlled

PWM is the abbreviation of pulse width modulation, which is a technology that controls the level by adjusting the pulse width of the signal.

Period: The duration of a complete PWM waveform;

Duty cycle: the ratio of high level duration to cycle time;

Frequency: The reciprocal of the period is called frequency, which is the number of PWM cycles generated per second;

Set the period of the PWM control signal to 20ms, which is a frequency of 50Hz; the high level time of the pulse determines the angle of rotation of the servo.

High level pulse width corresponding to common angles

Steering gear (180°)High level pulse width (us)
500
45°1000
90°1500
135°2000
180°2500

Using the TIM7 timer interrupt function on the STM32F103ZET6 development board

PWM servo (schematic name)Control pinFunction
S1PB0Analog PWM output control S1 servo
S2PB1Analog PWM output control S2 servo
S3PD14Analog PWM output control S3 servo
S4PD15Analog PWM output control S4 servo

Timing formula

T(s)=(ARR+1)(PSC+1)TIM_CLK(Hz)

Timing time of this project: 10us

T(s)=(ARR+1)(PSC+1)TIM_CLK(Hz)=(9+1)(71+1)72000000(Hz)=0.00001s=10us

3. Engineering experience

Use the project files we provide to directly experience the corresponding functions of the development board.

Later tutorials do not provide this content to avoid duplication of content. You can go to [2. Development environment construction and use: engineering experience and transplantation] to view the operation

1. Open the project

image-20231128164212903

Copy the project file to the directory of English path, use STM32CubeIDE to open the project file, open the project file and select the .project file

image-20231019112028635

4. Project configuration

This tutorial will completely demonstrate the configuration process. Later, the content of new project, chip selection, project settings, pin settings of SYS, RCC configuration, clock configuration and project configuration will be omitted. Any changes will be stated in the tutorial. .

1. New project

image-20231019113256941

image-20230925203837627

2、chip selection

image-20230925204025253

3、Project settings

image-20231019113232378

image-20230925205412520

image-20230925205504890

4、Pin configuration

image-20231019114035388

image-20231019114107414

image-20231019111012841

image-20231019114356700

image-20231019114451469

image-20231019111448715

5、Clock configuration

image-20230926101233831

6、Project configuration

image-20231019121526743

image-20231019121820781

image-20231019121843188

7、Generate code

image-20231011102854602

image-20231019122700707

The above is the peripheral configuration and initialization code generation.

5. Main functions

It mainly introduces the functional code written by the user. For detailed code, you can open the project file provided by us yourself and enter the Bsp folder to view the source code.

1. User function

Function: PwmServo_Handle

Function prototypevoid PwmServo_Handle(void)
Function descriptionCalled in timer interrupt, simulate output PWM signal
Input parametersNone
Output parametersNone
NotesThis function is called by the timer update interrupt callback function HAL_TIM_PeriodElapsedCallback

Function: PwmServo_Angle_To_Pulse

Function prototypeuint16_t PwmServo_Angle_To_Pulse(uint8_t angle)
Function descriptionConvert the specified angle into pulse width value
Input parametersangle: rotation angle
Output parametersPulse width value

Function: PwmServo_Init

Function prototypevoid PwmServo_Init(void)
Function descriptionSet the initial angle of each servo to 90°
Input parametersNone
Output parametersNone

Function: PwmServo_Set_Angle

Function prototypevoid PwmServo_Set_Angle(uint8_t index, uint8_t angle)
Function descriptionSet the initial angle of a single servo
Input parameter 1index: servo serial number
Input parameter 2angle: rotation angle
Output parametersNone

Function: PwmServo_Set_Angle_All

Function prototypevoid PwmServo_Set_Angle_All
(uint8_t angle_s1, uint8_t angle_s2, uint8_t angle_s3, uint8_t angle_s4)
Function descriptionSet all servo rotation angles
Input parameter 1S1 servo rotation angle
Input parameter 2S2 servo rotation angle
Input parameter 3S3 servo rotation angle
Input parameter 4S4 servo rotation angle
Output parametersNone

2. HAL library function analysis

Function: HAL_GPIO_Init

Function prototypevoid HAL_GPIO_Init(GPIO_TypeDef *GPIOx, GPIO_InitTypeDef *GPIO_Init)
Function descriptionInitialize GPIO pin parameters
Input parameter 1GPIOx: Set the GPIO port, x takes the value A, B, C, D, E, F, G
Input parameter 2GPIO_Init: GPIO initialization structure
Output parametersNone

Function: HAL_GPIO_WritePin

Function prototypevoid HAL_GPIO_WritePin(GPIO_TypeDef *GPIOx, uint16_t GPIO_Pin, GPIO_PinState PinState)
Function DescriptionSet/Clear the specified pin
Input parameter 1GPIOx: Set the GPIO port, x takes the value A, B, C, D, E, F, G
Input parameter 2GPIO_Pin: Set the GPIO pin, x value is 0-15
Input parameter 3PinState: Bit_RESET: clear the data port bit (low level); Bit_SET: set the data port bit (high level)
Output parametersNone

Function: HAL_TIM_Base_Init

Function prototypeHAL_StatusTypeDef HAL_TIM_Base_Init(TIM_HandleTypeDef *htim)
Function descriptionInitialize timer time base unit
Input parametershtim: timer handle address
Output parametersHAL status value: HAL_OK, HAL_ERROR, HAL_BUSY, HAL_TIMEOUT
NotesThis function will call the MCU underlying initialization function HAL_TIM_Base_MspInit to complete the settings of pins, clocks and interrupts

Function: HAL_TIM_Base_MspInit

Function prototypevoid HAL_TIM_Base_MspInit(TIM_HandleTypeDef *htim);
Function descriptionInitialize the peripheral clock, GPIO and NVIC of the timer
Input parametershtim: timer handle address
Output parametersNone

Function: HAL_TIM_Base_MspDeInit

Function prototypevoid HAL_TIM_Base_MspDeInit(TIM_HandleTypeDef *htim)
Function descriptionCancel the initialization of timer peripheral clock, GPIO and NVIC
Input parametershtim: timer handle address
Output parametersNone

HAL_TIM_IRQHandler: timer interrupt service function

Function prototypevoid HAL_TIM_IRQHandler(TIM_HandleTypeDef *htim)
Function descriptionTimer interrupt service function
Input parametershtim: timer handle address
Output parametersNone
NotesInternally, this function needs to first determine the interrupt type and clear the corresponding interrupt flag bit, and finally call the callback function

HAL_TIM_PeriodElapsedCallback: Timer update interrupt callback function

Function prototypevoid HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
Function descriptionTimer update interrupt callback function
Input parametershtim: timer handle address
Output parametersNone
NotesThis function is called by HAL_TIM_IRQHandler, and specific processing tasks can be written internally

6. Program download

1. Serial port download

image-20231019151805108

image-20231019151944969

image-20231019152053805

image-20231201121939503

image-20231019153213132

image-20231019154039009

image-20231019174450537

7. Experimental Phenomenon

After downloading the program successfully, press the RESET button of the development board and observe the development board phenomenon!

Phenomenon:

S1 interface servo: cyclic rotation from 0° → 45° → 90° → 135° → 180° → 135° → 90° → 45° → 0°.