详细没有测验,先保藏,以后去测验一下
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,以到达精确地输出。