TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable;
TIM_OCInitStructure.TIM_Pulse = Pluse;
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_OCNIdleState_Set;
TIM_OC1Init(BLDC_TIMER_NUM, &TIM_OCInitStructure);
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2;
Pluse = MotorA.TimerPeriod - Pluse;
TIM_OCInitStructure.TIM_Pulse = Pluse;
TIM_OC2Init(BLDC_TIMER_NUM, &TIM_OCInitStructure);
#define Fsys 72000000ul // system freq 72MHz
#define Fpwm 20000 // PWM freq 20K
void ConfigTimer(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB1PeriphClockCmd( RCC_APB1Periph_TIM4,ENABLE);
RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOB,ENABLE);
TIM_DeInit(TIM4);
TIM_TimeBaseStructure.TIM_Prescaler = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_CenterAligned1;
TIM_TimeBaseStructure.TIM_Period = (Fsys/2) / Fpwm;
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;
TIM_TimeBaseInit(TIM4,&TIM_TimeBaseStructure);
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_OutputNState = TIM_OutputState_Disable;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_Low;
TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Reset;
TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCIdleState_Reset;
TIM_OCInitStructure.TIM_Pulse = (((Fsys/2) / Fpwm) * 20) / 100;
TIM_OC3Init(TIM4,&TIM_OCInitStructure);
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2;
TIM_OCInitStructure.TIM_Pulse = (((Fsys/2) / Fpwm) * (100-20)) / 100;
TIM_OC4Init(TIM4,&TIM_OCInitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_9;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
TIM_CtrlPWMOutputs(TIM4, ENABLE);
TIM_Cmd(TIM4,ENABLE);
}
基本设置如下:
1)配置定时器的计数器为中间对齐计数,即先向上计数再向下计数。
2)在该定时器上选择2个通道,并分别配置为输出比较模式,并配置在比较成功时翻转对应的引脚输出。
3)配置自动重装载寄存器TIMx_ARR为要求输出频率的一半。
4)假定CC1为第一个输出信号的通道,再假定第一个信号的正脉冲宽度对应为W1,则配置TIMx_CCR1为TIMx_ARR-W1/2。
5) 同4),假定CC2为第二个输出信号的通道,正脉冲宽度对应为W2,配置TIMx_CCR2为W2/2。
----------------------------------------------
下面以一个例子说明:
假设要求输出的信号频率为10kHz,占空比为1:3。
再假设定时器的输入时钟为72MHz。
输出信号的频率10kHz,换算为计数器的数值为7200。
按照上述3),设置TIMx_ARR=3600
输出信号1的高电平时间W1,换算为计数器的数值为W1=7200/4=1800
按照上述4),设置TIMx_CC1=3600 - W1/2=2700
输出信号2的高电平时间W2,换算为计数器的数值为W2=7200/4=1800
按照上述5),设置TIMx_CC2=2/2=450
参照下图,图中红线表示计数器的数值变化:
①当计数器的数值从0向上计数,达到TIMx_CC1时,CC1匹配成功,CC1的输出电平翻转;
②计数器继续向上计数,达到TIMx_ARR时开始调头向下计数;当计数器的数值下降到TIMx_CC1时,CC1再次匹配成功,CC1的输出电平再次翻转;
③计数器继续向下计数,达到到TIMx_CC2时,CC2匹配成功,CC2的输出电平翻转;
④计数器继续向下计数,减到0时开始调头向上计数;当计数器的数值上升到TIMx_CC2时,CC2再次匹配成功,CC2的输出电平再次翻转;
如此循环,得到连续的相位互为180度的两路输出波形。
注意:上述描述是一个原理性的说明,但能够输出要求的波形并且占空比可调,实际编程计算中需要可能需要对某些数值加1或者减1,以达到准确地输出。
上一篇:STM32 PWM模式相位与设置
下一篇:STM32的IO的配置和模式
推荐阅读最新更新时间:2024-03-16 14:03