我用f407ZGT6的板子,
想问大家为什么生成pwm波的时候频率不对,比如我想生成1KHZ的波,出来的却是640HZ?
程序如下
TIM1_PWM_Init(1000-1,84-1); //84M/84=1Mhz 1M/1000=1Khz.
void TIM1_PWM_Init(u32 arr,u32 psc)
{
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
/* Õa¸öêÇ3õê¼»ˉPE9 PE11 PE13 PE14·Ö±eÎa í¨μà1 ¡¢2¡¢3¡¢4 */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1,ENABLE); //TIM1ê±Öóê1Äü
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOE,ENABLE); //ê1ÄüPORTEê±Öó
GPIO_PinAFConfig(GPIOE,GPIO_PinSource9,GPIO_AF_TIM1); //GPIOE9¸′óÃÎa¶¨ê±Æ÷1
GPIO_PinAFConfig(GPIOE,GPIO_PinSource11,GPIO_AF_TIM1); //GPIOE11¸′óÃÎa¶¨ê±Æ÷1
GPIO_PinAFConfig(GPIOE,GPIO_PinSource13,GPIO_AF_TIM1); //GPIOE13¸′óÃÎa¶¨ê±Æ÷1
GPIO_PinAFConfig(GPIOE,GPIO_PinSource14,GPIO_AF_TIM1); //GPIOE14¸′óÃÎa¶¨ê±Æ÷1
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9|GPIO_Pin_11|GPIO_Pin_13|GPIO_Pin_14; //GPIOE9£¬GPIOE11£¬GPIOE13£¬GPIOE14
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; //¸′óÃ1|Äü
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; //íÆíì¸′óÃêä3ö
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; //2»éÏÏÂà-
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz; //Ëù¶è100MHz
GPIO_Init(GPIOE,&GPIO_InitStructure); //3õê¼»ˉPE9,PE11,PE13,PE14
TIM_TimeBaseStructure.TIM_Prescaler=psc; //¶¨ê±Æ÷·ÖÆμ
TIM_TimeBaseStructure.TIM_CounterMode=TIM_CounterMode_Up; //ÏòéϼÆêyÄ£ê½
TIM_TimeBaseStructure.TIM_Period=arr; //×Ô¶ˉÖØ×°ÔØÖμ
TIM_TimeBaseStructure.TIM_ClockDivision=TIM_CKD_DIV1;
TIM_TimeBaseStructure.TIM_RepetitionCounter=0;
TIM_TimeBaseInit(TIM1,&TIM_TimeBaseStructure);//3õê¼»ˉ¶¨ê±Æ÷14
//3õê¼»ˉTIM1 Channel1 2 3 4 PWMÄ£ê½
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_Toggle; //Ñ¡Ôñ¶¨ê±Æ÷Ä£ê½:TIMÂö3å¿í¶èμ÷ÖÆÄ£ê½2
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //±è½Ïêä3öê1Äü
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low; //êä3ö¼«DÔ:TIMêä3ö±è½Ï¼«DÔμí
TIM_OCInitStructure.TIM_Pulse = 0;
TIM_OC1Init(TIM1, &TIM_OCInitStructure); //¸ù¾YTÖ¸¶¨μÄ2Îêy3õê¼»ˉíaéèTIM14 OC1
TIM_OC2Init(TIM1, &TIM_OCInitStructure); //¸ù¾YTÖ¸¶¨μÄ2Îêy3õê¼»ˉíaéèTIM14 OC1
TIM_OC3Init(TIM1, &TIM_OCInitStructure); //¸ù¾YTÖ¸¶¨μÄ2Îêy3õê¼»ˉíaéèTIM14 OC1
TIM_OC4Init(TIM1, &TIM_OCInitStructure); //¸ù¾YTÖ¸¶¨μÄ2Îêy3õê¼»ˉíaéèTIM14 OC1
TIM_OC1PreloadConfig(TIM1, TIM_OCPreload_Disable); //ê1ÄüTIM14ÔúCCR1éÏμÄԤװÔؼÄ′æÆ÷
TIM_OC2PreloadConfig(TIM1, TIM_OCPreload_Disable); //ê1ÄüTIM14ÔúCCR1éÏμÄԤװÔؼÄ′æÆ÷
TIM_OC3PreloadConfig(TIM1, TIM_OCPreload_Disable); //ê1ÄüTIM14ÔúCCR1éÏμÄԤװÔؼÄ′æÆ÷
TIM_OC4PreloadConfig(TIM1, TIM_OCPreload_Disable); //ê1ÄüTIM14ÔúCCR1éÏμÄԤװÔؼÄ′æÆ÷
NVIC_InitStructure.NVIC_IRQChannel = TIM1_CC_IRQn; //WWDGÖD¶Ï
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2; //ÇàÕ¼2£¬×óóÅÏ輶3£¬×é2
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3; //ÇàÕ¼2£¬×óóÅÏ輶3£¬×é2
NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;
NVIC_Init(&NVIC_InitStructure);//NVIC3õê¼»ˉ
TIM_ClearFlag(TIM1, TIM_IT_CC1|TIM_IT_CC2|TIM_IT_CC3|TIM_IT_CC4);
TIM_ITConfig(TIM1,TIM_IT_CC1|TIM_IT_CC2|TIM_IT_CC3|TIM_IT_CC4,ENABLE);
//TIM_ARRPreloadConfig(TIM1,ENABLE);//ARPEê1Äü
TIM_Cmd(TIM1, ENABLE); //ê1ÄüTIM14
TIM_CtrlPWMOutputs(TIM1,ENABLE);
}
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
一周热门 更多>