1一.PWM波的翻转模式:
翻转模式,当计数寄存器达到设定的比较值时,在该输出引脚上翻转电平,从而产生PWM波。
二.翻转模式的用途:
1.实现频率可调:PWM的正常输出模式可以可以实现PWM波占空比的调节,频率调节可以通过设定的ARR调节。在翻转模式下也可以实现频率可调,当达到比较设定值时,电平翻转,在中断服务函数里将比较值变为原来的2倍,如下代码:
void TIM3_IRQHandler(void)
{
u16 capture = 0;
if (TIM_GetITStatus(TIM3, TIM_IT_CC1) != RESET)
{
TIM_ClearITPendingBit(TIM3, TIM_IT_CC1 );
capture = TIM_GetCapture1(TIM3);
TIM_SetCompare1(TIM3, capture + CCR1_Val );
}
}
故:此PWM波频率为时钟频率除以两倍的比较值,通过改变比较值从而调节PWM的频率。
2.实现异相方波:
异相方波,是两路方波(占空比为50%)的基础上才能进行的说法,频率一致,其中一路波滞后后超前于另外一路波。具体配置如下:
CCR2_Val = CCR1_Val + 200;//滞后值
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_Toggle;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable;
TIM_OCInitStructure.TIM_Pulse = CCR1_Val;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_High;
TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set;
TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCIdleState_Reset;
TIM_OC1Init(TIM3, &TIM_OCInitStructure);
TIM_OCInitStructure.TIM_Pulse = CCR2_Val;
TIM_OC2Init(TIM3, &TIM_OCInitStructure);
在中断服务函数里面重新设定比较:
void TIM3_IRQHandler(void)
{
u16 capture = 0;
if (TIM_GetITStatus(TIM3, TIM_IT_CC1) != RESET)
{
TIM_ClearITPendingBit(TIM3, TIM_IT_CC1 );
capture = TIM_GetCapture1(TIM3);
TIM_SetCompare1(TIM3, capture + CCR1_Val );
}
if (TIM_GetITStatus(TIM3, TIM_IT_CC2) != RESET)
{
TIM_ClearITPendingBit(TIM3, TIM_IT_CC2);
capture = TIM_GetCapture2(TIM3);
TIM_SetCompare2(TIM3, capture + CCR1_Val);
}
}
改变滞后值则可改变两列波的相位差。