STM32F4定时器时钟频率和时钟源

stm32cubemx配置PWM_STM32
(1)高级定时器timer1, timer8以及通用定时器timer9, timer10, timer11的时钟来源是APB2总线
(2)通用定时器timer2timer5,通用定时器timer12timer14以及基本定时器timer6,timer7的时钟来源是APB1总线

cubemx配置

以下转自:https://blog.csdn.net/qq_42967008/article/details/89267010

一、打开Cube,建立工程

图片:stm32cubemx配置PWM_STM32开发_02
点击ACCESS project from MCU
然后选择芯片类型(我这里选的是F103C8T6)
图片:stm32cubemx配置PWM_STM32_03

二、系统配置

在 Pinout&Configuration—System Core中:
设置时钟RCCHSE(外部高速时钟)为晶振模式:
Crystal/ceramic Resonatorstm32cubemx配置PWM_STM32开发_04
设置系统SYSDebugSerial Wire(SWD调试)stm32cubemx配置PWM_STM32开发_05

三、配置PWM

在 Pinout&Configuration—Timers中:
打开TIM3,在TIM3 Mode and Configuration中设置CH1,CH2,CH3,CH4的模式为PWM输出模式:stm32cubemx配置PWM_STM32_06
(我这里是TIM3输出四路PWM)
配置预分频系数
在Configuration中设置预分频系数为72-1,自动重装值为500-1,则PWM的输出频率为72000000/72/500=2000Hz。根据需要配置PWM有效极性,我这里设置为High:stm32cubemx配置PWM_STM32开发_07

四、在 Clock Configuration中:

配置时钟为72 Mhz。
stm32cubemx配置PWM_STM32开发_08

五、工程输出配置

stm32cubemx配置PWM_STM32开发_09
Tips:最好把Linker Settings中的Minimum Heap Size设置为0x600。
stm32cubemx配置PWM_STM32开发_10
最后点击GENERATE CODE代码就生成了:
stm32cubemx配置PWM_STM32_11
至此,一个工程就创建完了。

六、代码部分处理

HAL库的PWM控制函数

HAL_TIM_PWM_Start//PWM启动函数
HAL_TIM_PWM_Stop//PWM停止函数
_HAL_TIM_SET_COMPARE//占空比
_HAL_TIM_SET_AUTORELOAD//周期

注意

TIM3->CCR2 = dutyCycle

__HAL_TIM_SET_COMPARE(&htim3, TIM_CHANNEL_2, dutyCycle);
作用相同

注意!!!一定在主函数中加入PWM开启函数
stm32cubemx配置PWM_STM32_12

到此,输出正常stm32cubemx配置PWM_STM32开发_13