stm32103vc 输出pwm频率与实际相差10倍,我用keil仿真是正常的,有没有遇到过同样问题的void TIM2_PWM_Init(u16 arr,u16 psc){
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE); //ê1Äü¶¨ê±Æ÷2ê±Öó
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA , ENABLE); //ê1ÄüGPIOíaéèoíAFIO¸′óÃ1|ÄüÄ£¿éê±Öó
//éèÖøÃòy½ÅÎa¸′óÃêä3ö1|Äü,êä3öTIM2 CH1μÄWMÂö3å2¨DÎ GPIOA.0
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0; //TIM_CH1
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //¸′óÃíÆíìêä3ö
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);//3õê¼»ˉGPIO
//3õê¼»ˉTIM2
TIM_TimeBaseStructure.TIM_Period = arr; //éèÖÃÔúÏÂò»¸ö¸üDÂê¼t×°èë»î¶ˉμÄ×Ô¶ˉÖØ×°ÔؼÄ′æÆ÷ÖüÆúμÄÖμ
TIM_TimeBaseStructure.TIM_Prescaler =psc; //éèÖÃóÃà′×÷ÎaTIMxê±ÖóÆμÂê3yêyμÄÔ¤·ÖÆμÖμ
TIM_TimeBaseStructure.TIM_ClockDivision = 0; //éèÖÃê±Öó·Ö¸î:TDTS = Tck_tim
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIMÏòéϼÆêyÄ£ê½
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure); //¸ù¾YTIM_TimeBaseInitStructÖDÖ¸¶¨μÄ2Îêy3õê¼»ˉTIMxμÄê±¼ä»ùêyμ¥λ
//3õê¼»ˉTIM2 Channel2 PWMÄ£ê½
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2; //Ñ¡Ôñ¶¨ê±Æ÷Ä£ê½:TIMÂö3å¿í¶èμ÷ÖÆÄ£ê½2
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //±è½Ïêä3öê1Äü
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; //êä3ö¼«DÔ:TIMêä3ö±è½Ï¼«DÔ¸ß
TIM_OC1Init(TIM2, &TIM_OCInitStructure); //¸ù¾YTÖ¸¶¨μÄ2Îêy3õê¼»ˉíaéèTIM2 OC1
TIM_OC1PreloadConfig(TIM2, TIM_OCPreload_Enable); //ê1ÄüTIM2ÔúCCR1éÏμÄԤװÔؼÄ′æÆ÷
TIM_Cmd(TIM2, ENABLE); //ê1ÄüTIM2
}
主函数中调用
TIM2_PWM_Init(899,0); //
TIM_SetCompare1(TIM2,500);
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
一周热门 更多>