Advanced Timer (TIM)

Tutorial demonstrating advanced timer (TIM8) PWM driving motor.

Hardware connection

image-20240825203848090

Since we have configured a special connection line, we only need to install it to the corresponding interface:

PeripheralsDevelopment BoardDescription
Motor 1: M+ (M1A)PC6The PC6 pin of the development board controls the input pin of the motor driver chip, and the output pin of the driver chip controls motor 1: M+
Motor 1: M- (M1B)PC7The PC7 pin of the development board controls the input pin of the motor driver chip, and the output pin of the driver chip controls motor 1: M-
Motor 1: Encoder VCC3.3VConnect the development board 3.3V to power the encoder
Motor 1: Encoder GNDGNDConnect the development board GND to the encoder common ground
Motor 1: Encoder A phase (H1A)PA7Encoder A of motor 1 is connected to pin PA7 of the development board
Motor 1: Encoder B phase (H1B)PA6Encoder B of motor 1 is connected to pin PA6 of the development board
Motor 2: M+ (M2A)PC8The PC8 pin of the development board controls the input pin of the motor driver chip, and the output pin of the driver chip controls motor 2: M+
Motor 2: M- (M2B)PC9The PC9 pin of the development board controls the input pin of the motor driver chip, and the output pin of the driver chip controls motor 2: M-
Motor 2: Encoder VCC3.3VConnect the development board 3.3V to power the encoder
Motor 2: Encoder GNDGNDConnect the development board GNDV to the encoder common ground
Motor 2: Encoder A phase (H2A)PB7Encoder A of motor 2 is connected to pin PB7 of the development board
Motor 2: Encoder B phase (H2B)PB6Encoder B of motor 2 is connected to pin PB6 of the development board

Control principle

Use the PWM output function of TIM8 of the advanced timer on the STM32F103RCT6 development board to change the duty cycle of the PWM signal to control the speed of the motor rotation.

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 the duration of the high level to the period time;

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

Timer typeAdvanced timer
Timer nameTIM1, TIM8
Counter digits16
Counting modeIncremental/decremental/center alignment
Prescaler coefficient1-65536
Generate DMA requestYes
Capture/compare channel4
Complementary outputYes
Clock frequency72MHz (maximum)
Mount busAPB2

Pin definition

Main control chipPinMain function (after reset)Default multiplexing functionRedefine function
STM32F103RCT6PC6PC6I2S2_MCK/TIM8_CH1/SDIO_D6TIM3_CH1
STM32F103RCT6PC7PC7I2S3_MCK/TIM8_CH2/SDIO_D7TIM3_CH2
STM32F103RCT6PC8PC8TIM8_CH3/SDIO_D0TIM3_CH3
STM32F103RCT6PC9PC9TIM8_CH4/SDIO/D1TIM3_CH4

Software code

Since the default function of the pin is the normal IO pin function, we need to use the multiplexing function.

Control function

The tutorial only briefly introduces the code, you can open the project source code to read the details.

Balance_Motor_Init

Balance_PWM_Init

Set_Pwm

myabs

Experimental phenomenon

The Advanced_Timer.hex file generated by the project compilation is located in the OBJ folder of the Advanced_Timer project. Find the Advanced_Timer.hex file corresponding to the project and use the FlyMcu software to download the program to the development board.

After the program is successfully downloaded: the two motors will continue to run according to the rule of moving forward for 2s, stopping for 1s, moving backward for 2s, and stopping for 1s.