第一步:配置GPIO,开启定时器时钟,选择定时器的时钟:
- 配置定时器:TIM_TimeBaseInit(): -> 这一步决定了PWM的频率,占空比,分辨率
/**
* @brief Initializes the TIMx Time Base Unit peripheral according to
* the specified parameters in the TIM_TimeBaseInitStruct.
* @param TIMx: where x can be 1 to 17 to select the TIM peripheral.
* @param TIM_TimeBaseInitStruct: pointer to a TIM_TimeBaseInitTypeDef
* structure that contains the configuration information for the
* specified TIM peripheral.
* @retval None
*/
void TIM_TimeBaseInit(TIM_TypeDef* TIMx, TIM_TimeBaseInitTypeDef* TIM_TimeBaseInitStruct)
{
.........
}
// TIM_TimeBaseInitStruct.TIM_ClockDivision 配置分频器
// TIM_TimeBaseInitStruct.TIM_CounterMode 配置计数模式
// TIM_TimeBaseInitStruct.TIM_Period 配置ARR自动重装器 (0~65535)
// TIM_TimeBaseInitStruct.TIM_Prescaler 配置PSC预分频器 (0~65535)
// TIM_TimeBaseInitStruct.TIM_RepetitionCounter 配置重复寄存器
注意:TIM_TimeBaseInitStruct.TIM_RepetitionCounter 是高级定时器特有参数,如不使用高级定时器
可将这参数置为0
PWM频率:
Freq = CK_CNT/(ARR+1) = CK_PSC/(PCS+1)/(ARR+1)
PWM占空比
Dult = CCR/(ARR+1)
PWM分辨率
Reso = 1/(ARR+1)
解释:
CCR : 比较寄存器
CK_CNT :计数器 ARR : 自动重装器
CK_PSC :时钟频率(一般STM32为72MHZ) PCS : 预分频器
例如:频率为1KHZ,占空比50%,分辨率为1%的PWM波形
则: 72000000/(PCS+1)/(ARR+1)=1000
CCR/(ARR+1) = 50% = 0.5
1/(ARR+1) = 1% = 0.01
就可选: ARR = 100 CRR = 50 PCS = 720 (实际是100-1 , 720-1)
频率为17KHZ,占空比10%,分辨率为1%的PWM波形
则: 72000000/(PCS+1)/(ARR+1)=17000
CCR/(ARR+1) = 50% = 0.1
1/(ARR+1) = 1% = 0.01
就可选: ARR = 100 CRR = 10 PCS = 42 (实际是100-1 , 42-1)
频率为20KHZ,占空比15%,分辨率为0.1%的PWM波形
则: 72000000/(PCS+1)/(ARR+1)=20000
CCR/(ARR+1) = 15% = 0.15
1/(ARR+1) = 0.1% = 0.001
就可选: ARR = 1000 CRR = 150 PCS = 36 (实际是1000-1 , 36-1)
- 配置输出比较:TIM_OC1Init()、TIM_OC2Init()、TIM_OC3Init()、TIM_OC4Init()
/**
* @brief Initializes the TIMx Channel1 according to the specified
* parameters in the TIM_OCInitStruct.
* @param TIMx: where x can be 1 to 17 except 6 and 7 to select the TIM peripheral.
* @param TIM_OCInitStruct: pointer to a TIM_OCInitTypeDef structure
* that contains the configuration information for the specified TIM peripheral.
* @retval None
*/
void TIM_OC1Init(TIM_TypeDef* TIMx, TIM_OCInitTypeDef* TIM_OCInitStruct)
{
....................
}
结构体讲解:
TIM_OCInitStruct.TIM_OCMode 输出比较的模式
TIM_OCInitStruct.TIM_OCPolarity 输出比较的极性
TIM_OCInitStruct.TIM_OutputState 输出模式的状态
TIM_OCInitStruct.TIM_Pulse 配置输出比较寄存器CCR的值(0~65535)
还有一些结构体成员高级定时器才需要配置,这里可以用TIM_OCStructInit(&TIM_OCInitStruct);初始化
- 最后定时器使能:TIM_Cmd();
/**
* @brief Enables or disables the specified TIM peripheral.
* @param TIMx: where x can be 1 to 17 to select the TIMx peripheral.
* @param NewState: new state of the TIMx peripheral.
* This parameter can be: ENABLE or DISABLE.
* @retval None
*/
void TIM_Cmd(TIM_TypeDef* TIMx, FunctionalState NewState)
{
}
- 那么如何更改占空比呢:
TIM_SetCompare1()、TIM_SetCompare2()、TIM_SetCompare3()、TIM_SetCompare4()
/**
* @brief Sets the TIMx Capture Compare1 Register value
* @param TIMx: where x can be 1 to 17 except 6 and 7 to select the TIM peripheral.
* @param Compare1: specifies the Capture Compare1 register new value.
* @retval None
*/
void TIM_SetCompare1(TIM_TypeDef* TIMx, uint16_t Compare1)
{
................
}
值得注意的是:一个定时器虽然可以同时输出4路PWM,但这4路PWN的频率必须是相同的,但是占空比和分辨率是可各自调整的
- 那么如何更改频率呢:void TIM_PrescalerConfig();
/**
* @brief Configures the TIMx Prescaler.
* @param TIMx: where x can be 1 to 17 to select the TIM peripheral.
* @param Prescaler: specifies the Prescaler Register value
* @param TIM_PSCReloadMode: specifies the TIM Prescaler Reload mode
* This parameter can be one of the following values:
* @arg TIM_PSCReloadMode_Update: The Prescaler is loaded at the update event.
* @arg TIM_PSCReloadMode_Immediate: The Prescaler is loaded immediately.
* @retval None
*/
void TIM_PrescalerConfig(TIM_TypeDef* TIMx, uint16_t Prescaler, uint16_t TIM_PSCReloadMode)
{
........
}
更改预分频器PSC的值
- 其他功能函数
- 强制推挽输出高电平或低电平:
TIM_ForcedOC1Config()、TIM_ForcedOC2Config()、TIM_ForcedOC3Config()TIM_ForcedOC4Config()、
/**
* @brief Forces the TIMx output 1 waveform to active or inactive level.
* @param TIMx: where x can be 1 to 17 except 6 and 7 to select the TIM peripheral.
* @param TIM_ForcedAction: specifies the forced Action to be set to the output waveform.
* This parameter can be one of the following values:
* @arg TIM_ForcedAction_Active: Force active level on OC1REF
* @arg TIM_ForcedAction_InActive: Force inactive level on OC1REF.
* @retval None
*/
void TIM_ForcedOC1Config(TIM_TypeDef* TIMx, uint16_t TIM_ForcedAction)
{
.......
}
- 启用或禁用CCR1上的TIMx外设的影子寄存器:
TIM_OC1PreloadConfig()、TIM_OC2PreloadConfig()、TIM_OC3PreloadConfig()、TIM_OC4PreloadConfig()
/**
* @brief Enables or disables the TIMx peripheral Preload register on CCR1.
* @param TIMx: where x can be 1 to 17 except 6 and 7 to select the TIM peripheral.
* @param TIM_OCPreload: new state of the TIMx peripheral Preload register
* This parameter can be one of the following values:
* @arg TIM_OCPreload_Enable
* @arg TIM_OCPreload_Disable
* @retval None
*/
void TIM_OC1PreloadConfig(TIM_TypeDef* TIMx, uint16_t TIM_OCPreload)
{
...........
}
- 外部事件清除REF信号:
TIM_ClearOC1Ref()、TIM_ClearOC2Ref()、TIM_ClearOC3Ref()、TIM_ClearOC4Ref()、
/**
* @brief Clears or safeguards the OCREF1 signal on an external event
* @param TIMx: where x can be 1, 2, 3, 4, 5 or 8 to select the TIM peripheral.
* @param TIM_OCClear: new state of the Output Compare Clear Enable Bit.
* This parameter can be one of the following values:
* @arg TIM_OCClear_Enable: TIM Output clear enable
* @arg TIM_OCClear_Disable: TIM Output clear disable
* @retval None
*/
void TIM_ClearOC1Ref(TIM_TypeDef* TIMx, uint16_t TIM_OCClear)
{
。。。。。。
}
- 设置输出比较的极性:
void TIM_OC1PolarityConfig();
void TIM_OC1NPolarityConfig();(高级定时器互补通道)
void TIM_OC2PolarityConfig();
void TIM_OC2NPolarityConfig();(高级定时器互补通道)
void TIM_OC3PolarityConfig();
void TIM_OC3NPolarityConfig();(高级定时器互补通道)
void TIM_OC4PolarityConfig();(OC4没有互补通道)
- 设置是否使能
void TIM_CCxCmd();
void TIM_CCxNCmd();(高级定时器互补通道)
- 设置比较模式
void TIM_SelectOCxM();
- 启用或禁用TIM外设主输出:
/**
* @brief Enables or disables the TIM peripheral Main Outputs.
* @param TIMx: where x can be 1, 8, 15, 16 or 17 to select the TIMx peripheral.
* @param NewState: new state of the TIM peripheral Main Outputs.
* This parameter can be: ENABLE or DISABLE.
* @retval None
*/
void TIM_CtrlPWMOutputs(TIM_TypeDef* TIMx, FunctionalState NewState)
{
.....
}
仅高级定时器输出PWM使用,若不配置,高级定时器将无法输出PWM