NucleoF303K8板, 使用8M外部时钟(HSE),用TIM1和TIM3提供PWM,引脚分别是PB0(3CH3),PA8(1CH1),PA11(1CH4),PB5(3CH2).目标是用宏PWM_RATE控制PWM的周期,然后PWM的脉宽可调1000us-2000us可调.PWM_RATE设置过400和490,但始终不能得到准确的PWM带宽,实际得到的脉宽比要求值搞几百us.
希望有高手指教:
1)是不是STM32的PWM脉宽输出都是不能准确控制的?
2)要得到更准确的PWM脉宽控制,应用什么样的思路?
PWM初始化代码:
void pwm_out_init(){
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1,ENABLE);
pwm_out[PWM1].GPIOx = GPIOB;
pwm_out[PWM1].Pin = GPIO_Pin_0;
pwm_out[PWM1].GPIO_PinSource = GPIO_PinSource0;
pwm_out[PWM1].GPIO_AF = GPIO_AF_2;
pwm_out[PWM1].TIMx = TIM3;
pwm_out[PWM1].Channel = 3;
pwm_out[PWM2].GPIOx = GPIOA;
pwm_out[PWM2].Pin = GPIO_Pin_8;
pwm_out[PWM2].GPIO_PinSource = GPIO_PinSource8;
pwm_out[PWM2].GPIO_AF = GPIO_AF_6;
pwm_out[PWM2].TIMx = TIM1;
pwm_out[PWM2].Channel = 1;
pwm_out[PWM3].GPIOx = GPIOA;
pwm_out[PWM3].Pin = GPIO_Pin_11;
pwm_out[PWM3].GPIO_PinSource = GPIO_PinSource11;
pwm_out[PWM3].GPIO_AF = GPIO_AF_11;
pwm_out[PWM3].TIMx = TIM1;
pwm_out[PWM3].Channel = 4;
pwm_out[PWM4].GPIOx = GPIOB;
pwm_out[PWM4].Pin = GPIO_Pin_5;
pwm_out[PWM4].GPIO_PinSource = GPIO_PinSource5;
pwm_out[PWM4].GPIO_AF = GPIO_AF_2;
pwm_out[PWM4].TIMx = TIM3;
pwm_out[PWM4].Channel = 2;
for(uint8_t i=0;i<PWM_NUM;i++){
pwm_out_channel_init(&pwm_out);
pwm_channel_out((pwm_channel_e)i,2000);
}
}
void pwm_out_channel_init(pwm_out_channel_t *pwm_channel){
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
pwm_channel->tim_clk = 72000000;
pwm_channel->prescale = (uint16_t)(pwm_channel->tim_clk / PWM_RATE / 0xFFFF)+1; //<----------------------- 但PWM周期的时钟计数不能超过16位整数的最大值, 以此选定分频系统, 这里应该得到3
pwm_channel->period = (uint32_t)( pwm_channel->tim_clk / PWM_RATE /pwm_channel->prescale - 1);//<------------------------ 计算分频后的时钟在一个PWM周期里的计数值
GPIO_StructInit(&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = pwm_channel->Pin;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(pwm_channel->GPIOx,&GPIO_InitStructure);
GPIO_PinAFConfig(pwm_channel->GPIOx,pwm_channel->GPIO_PinSource,pwm_channel->GPIO_AF);
TIM_TimeBaseStructInit(&TIM_TimeBaseStructure);
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //<------------------------ 向上计数模式
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseStructure.TIM_Prescaler = pwm_channel->prescale;
TIM_TimeBaseStructure.TIM_Period = pwm_channel->period;
TIM_TimeBaseInit(pwm_channel->TIMx,&TIM_TimeBaseStructure);
TIM_ARRPreloadConfig(pwm_channel->TIMx, ENABLE);
TIM_OCStructInit(&TIM_OCInitStructure);
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; //<------------------------- 低于设定值时输出有效电平
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; //<---------------------------- 有效电平为高电平
TIM_OCInitStructure.TIM_Pulse=0;
switch(pwm_channel->Channel){
case 1:
TIM_OC1Init(pwm_channel->TIMx,&TIM_OCInitStructure);
TIM_OC1PreloadConfig(pwm_channel->TIMx, TIM_OCPreload_Enable);
pwm_channel->outFunc = TIM_SetCompare1;
break;
case 2:
TIM_OC2Init(pwm_channel->TIMx,&TIM_OCInitStructure);
TIM_OC2PreloadConfig(pwm_channel->TIMx, TIM_OCPreload_Enable);
pwm_channel->outFunc = TIM_SetCompare2;
break;
case 3:
TIM_OC3Init(pwm_channel->TIMx,&TIM_OCInitStructure);
TIM_OC3PreloadConfig(pwm_channel->TIMx, TIM_OCPreload_Enable);
pwm_channel->outFunc = TIM_SetCompare3;
break;
case 4:
TIM_OC4Init(pwm_channel->TIMx,&TIM_OCInitStructure);
TIM_OC4PreloadConfig(pwm_channel->TIMx, TIM_OCPreload_Enable);
pwm_channel->outFunc = TIM_SetCompare4;
break;
default:break;
}
TIM_Cmd(pwm_channel->TIMx,ENABLE);
}
PWM脉宽控制代码如下:
void pwm_channel_out(pwm_channel_e ch, uint16_t us){
uint32_t duty = (uint32_t)(pwm_out[ch].tim_clk/1000000/pwm_out[ch].prescale*((uint32_t)us));
pwm_out[ch].outFunc(pwm_out[ch].TIMx,duty);
}
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
一周热门 更多>