STM32F0 TIM1 LL库生成PWM波

tech2022-08-11  129

一.功能说明

使用STM32F030F4,通过Time1的CH2(PA9)产生周期为1s,占空比50%的PWM波,高电平在前,低电平在后。处理器时钟选用HSI(8MHz),不启用PLL,定时器外设时钟8Mhz,定时器时钟8000分频,计一个数1ms。

二.TIM1概述

Timer1作为高级定时器,比其他定时器多了两个功能,一个重复计数器,一个刹车和死区。 重复计数器:可以让定时器多个周期才产生一次更新中断,如果需要设定产生多少个PWM波周期,这个功能比较实用。 刹车和死区:主要用于电机控制。 由于这两个功能导致Timer1用于产生普通PWM波会比其它定时器多一些配置。

三.CubeMX配置

Clock配置:

Timer1配置:

LL库选择:

四.Cube生成的TIM1初始化代码

/* TIM1 init function */ void MX_TIM1_Init(void) { LL_TIM_InitTypeDef TIM_InitStruct = {0}; LL_TIM_OC_InitTypeDef TIM_OC_InitStruct = {0}; LL_TIM_BDTR_InitTypeDef TIM_BDTRInitStruct = {0}; LL_GPIO_InitTypeDef GPIO_InitStruct = {0}; /* Peripheral clock enable */ LL_APB1_GRP2_EnableClock(LL_APB1_GRP2_PERIPH_TIM1); /* TIM1 interrupt Init */ NVIC_SetPriority(TIM1_BRK_UP_TRG_COM_IRQn, 0); NVIC_EnableIRQ(TIM1_BRK_UP_TRG_COM_IRQn); TIM_InitStruct.Prescaler = 7999; TIM_InitStruct.CounterMode = LL_TIM_COUNTERMODE_UP; TIM_InitStruct.Autoreload = 1000 - 1; TIM_InitStruct.ClockDivision = LL_TIM_CLOCKDIVISION_DIV1; TIM_InitStruct.RepetitionCounter = 0; LL_TIM_Init(TIM1, &TIM_InitStruct); LL_TIM_EnableARRPreload(TIM1); LL_TIM_SetClockSource(TIM1, LL_TIM_CLOCKSOURCE_INTERNAL); LL_TIM_OC_EnablePreload(TIM1, LL_TIM_CHANNEL_CH2); TIM_OC_InitStruct.OCMode = LL_TIM_OCMODE_PWM1; TIM_OC_InitStruct.OCState = LL_TIM_OCSTATE_DISABLE; TIM_OC_InitStruct.OCNState = LL_TIM_OCSTATE_DISABLE; TIM_OC_InitStruct.CompareValue = 500; TIM_OC_InitStruct.OCPolarity = LL_TIM_OCPOLARITY_HIGH; TIM_OC_InitStruct.OCNPolarity = LL_TIM_OCPOLARITY_HIGH; TIM_OC_InitStruct.OCIdleState = LL_TIM_OCIDLESTATE_LOW; TIM_OC_InitStruct.OCNIdleState = LL_TIM_OCIDLESTATE_LOW; LL_TIM_OC_Init(TIM1, LL_TIM_CHANNEL_CH2, &TIM_OC_InitStruct); LL_TIM_OC_DisableFast(TIM1, LL_TIM_CHANNEL_CH2); LL_TIM_SetTriggerOutput(TIM1, LL_TIM_TRGO_RESET); LL_TIM_DisableMasterSlaveMode(TIM1); TIM_BDTRInitStruct.OSSRState = LL_TIM_OSSR_DISABLE; TIM_BDTRInitStruct.OSSIState = LL_TIM_OSSI_DISABLE; TIM_BDTRInitStruct.LockLevel = LL_TIM_LOCKLEVEL_OFF; TIM_BDTRInitStruct.DeadTime = 0; TIM_BDTRInitStruct.BreakState = LL_TIM_BREAK_DISABLE; TIM_BDTRInitStruct.BreakPolarity = LL_TIM_BREAK_POLARITY_HIGH; TIM_BDTRInitStruct.AutomaticOutput = LL_TIM_AUTOMATICOUTPUT_DISABLE; LL_TIM_BDTR_Init(TIM1, &TIM_BDTRInitStruct); LL_AHB1_GRP1_EnableClock(LL_AHB1_GRP1_PERIPH_GPIOA); /**TIM1 GPIO Configuration PA9 ------> TIM1_CH2 */ GPIO_InitStruct.Pin = LL_GPIO_PIN_9; GPIO_InitStruct.Mode = LL_GPIO_MODE_ALTERNATE; GPIO_InitStruct.Speed = LL_GPIO_SPEED_FREQ_LOW; GPIO_InitStruct.OutputType = LL_GPIO_OUTPUT_PUSHPULL; GPIO_InitStruct.Pull = LL_GPIO_PULL_NO; GPIO_InitStruct.Alternate = LL_GPIO_AF_2; LL_GPIO_Init(GPIOA, &GPIO_InitStruct); }

此初始化代码完成了基本的参数配置,没有启动定时器,所以只调用MX_TIM1_Init()函数,无法产生PWM波。

五.增加启动代码

/** * @brief None * @param None * @retval None */ void Timer1Ch2Start( void ) { MX_TIM1_Init(); LL_TIM_CC_EnableChannel( TIM1 , LL_TIM_CHANNEL_CH2 ); LL_TIM_EnableAllOutputs( TIM1 ); //只有Timer1等高级定时器需要,必须放置在LL_TIM_BDTR_Init(TIM1, &TIM_BDTRInitStruct);之后。 LL_TIM_EnableCounter( TIM1 ); LL_TIM_GenerateEvent_UPDATE( TIM1 ); } );之后。 LL_TIM_EnableCounter( TIM1 ); LL_TIM_GenerateEvent_UPDATE( TIM1 ); }

火柴棍科技工作室:www.huochaigun.top

技术交流群

STM32MP1:861926625

ESP8266:476685983

无线(NB-IOT, Cat.1, 4G)通信模组:666129332

最新回复(0)