STM32 uses advanced timers to output complementary pwm waves

foreword

A recent project used stm32. I checked a lot of information on the Internet and stepped on a lot of pitfalls. Here are the configuration steps and instructions.

hardware and software

  1. The hardware uses stm32h750vbt6;
  2. The software uses stm32cubemx and keil5;

cubemx new project

insert image description here

Open Debug mode

Open Debug mode for easy debugging, can be ignored, does not affect the code running

insert image description here

Configure clock source

When using the cube to configure the clock source, there are the following three options:
Disable (disable)
BYPASS Clock Source (bypass clock source)
Crystal/Ceramic Resonator (crystal/ceramic crystal oscillator)

The role of the six clocks

insert image description here

Select Crystal/Ceramic Resonator, that is, use an external crystal oscillator as the clock source of HSE.

insert image description here

configure clock

Just configure according to the figure below, the highest clock frequency is 240Mhz
insert image description here

Configure advanced timer TIM8 and general timer TIM3

insert image description here
insert image description here

insert image description here
insert image description here

Here is a brief explanation of several parameters used to configure the pwm output

Prescaler frequency division factor
Counter Mode counting mode (divided into up and down)
Count Period counting period
Internal Clock Division internal clock frequency division
auto-reload preload automatic reload
TRGO Event Selection trigger event

Pulse is simply understood as the duty cycle, which can be set to half of the count period of the Count Period to output a square wave with a duty cycle of 50%.

Among them, the frequency division coefficient and counting period are more commonly used, and can be used to set the timer overflow time. Generally speaking, overflow also represents the generation of timer interrupt (if interrupt is configured). The overflow time calculation formula is as follows:
insert image description here

In the above formula, arr is the setting value of Count Period, and psc is the setting value of Prescaler.
Tclk is the frequency of the clock line attached to the timer. For STM32H750, the maximum is 240Mhz (according to your own settings), I don’t remember that you can look at the clock tree you configured.
It can be seen from the figure below that TIM3 is hung on APB1, and TIM8 is hung on APB2. The TO APB1 Timer clocksClocks and TO APB2 Timer clocksClocks of the clock we just configured are both 240Mhz.

insert image description here

If I want to generate a 100HZ square wave, I need to configure the overflow time to be 10ms (the period of 100HZ is 10ms).

You can choose to set psc to 2399 and arr to 999,
then the overflow time formula is:
2400*1000/240(us)=10ms
Configure according to the above calculation results, other parameters do not need to be changed

Or another algorithm:
(240*10^6 Hz) / (2399+1) / (999+1) = 100Hz

What we set up is:

TIM3:(240 10^3 KHz) / 20 / 100 = 120KHz
TIM8:(240
10^6 Hz)/ (24000) / (10000) = 1Hz

Advanced Control and General Purpose Timer Channel Pinouts

insert image description here

Configure project path

Choose MDK-ARM that is keil5

insert image description here
insert image description here

generate project

insert image description here

Main function call case

open main.c
insert image description here

Add the following code in the main function

  /* USER CODE BEGIN 2 */
	
	/* 定时器3通道1输出PWM */
	HAL_TIM_PWM_Start_IT(&htim3,TIM_CHANNEL_1);
	//占空比百分之50
	__HAL_TIM_SET_COMPARE(&htim3,TIM_CHANNEL_1,12000);
	
		/* 定时器8通道1输出PWM */
	HAL_TIM_PWM_Start_IT(&htim8,TIM_CHANNEL_1);
	/* 定时器8通道1互补输出PWM */
	HAL_TIMEx_PWMN_Start(&htim8, TIM_CHANNEL_1);
	//占空比百分之50
	__HAL_TIM_SET_COMPARE(&htim8,TIM_CHANNEL_1,50);


  /* USER CODE END 2 */

insert image description here

Configuration downloader burning verification

insert image description here
insert image description here

output result

insert image description here
insert image description here
consistent with our expected results

Guess you like

Origin blog.csdn.net/Running_free/article/details/131808441