|
个人更倾向于使用PWM1,PWM2的方式,因为Toggle(翻转)模式需要考虑Channel的初始状态
需要在Toggle开始前强制设定两个Channel的相位,曾经产品上就出现过这样的问题(相同的HEX,运行时产生不同的结果)
根据版主的提示写的一段能满足楼主要求的代码
/**
软件: IAR 4.42 32K 限制版
库: 2.0
硬件: 万利 EKSTM3210B-LK1
说明: 使用TIM3的Channel3和Channel4产生楼主所述波形
*/
#define Fsys 72000000ul // system freq 72MHz
#define Fpwm 20000 // PWM freq 20K
#define PWM_DUTY 20 // PWM duty 20%
void ConfigTimer(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB1PeriphClockCmd( RCC_APB1Periph_TIM3,ENABLE);
RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOB,ENABLE);
TIM_DeInit(TIM3);
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(TIM3,&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) * PWM_DUTY) / 100;
TIM_OC3Init(TIM3,&TIM_OCInitStructure);
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2;
TIM_OCInitStructure.TIM_Pulse = (((Fsys/2) / Fpwm) * (100-PWM_DUTY)) / 100;
TIM_OC4Init(TIM3,&TIM_OCInitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
TIM_CtrlPWMOutputs(TIM3, ENABLE);
TIM_Cmd(TIM3,ENABLE);
}
无图无真相。图中CH3即 Timer3 Channel3(GPIOB.0), CH4即 Timer3 Channel4(GPIOB.1)
|
|