#技术资源#概述 在调试PWM波时,有时候会切换系统时钟,导致PWM的频率也变化; 在配置定时器时,通过获取定时器的时钟来配置定时器,可避免PWM频率随着系统时钟的变化而变化。
测试代码如下: int main(void) { GPIO_Config_T GPIO_ConfigStruct; TMR_BaseConfig_T TMR_TimeBaseStruct; TMR_OCConfig_T OCcongigStruct;
//RCM_Reset(); uint32_tpclk1_value=0,pclk2_value; RCM_ReadPCLKFreq(&pclk1_value,&pclk2_value);
RCM_EnableAPB2PeriphClock((RCM_APB2_PERIPH_T)(RCM_APB2_PERIPH_GPIOA |RCM_APB2_PERIPH_TMR1 | RCM_APB2_PERIPH_GPIOB));
GPIO_ConfigStruct.pin = GPIO_PIN_13; GPIO_ConfigStruct.mode= GPIO_MODE_AF_PP; GPIO_ConfigStruct.speed = GPIO_SPEED_50MHz; GPIO_Config(GPIOB, &GPIO_ConfigStruct);
GPIO_ConfigStruct.pin = GPIO_PIN_8; GPIO_Config(GPIOA, &GPIO_ConfigStruct);
TMR_TimeBaseStruct.clockDivision = TMR_CLOCK_DIV_1; TMR_TimeBaseStruct.countMode = TMR_COUNTER_MODE_UP; TMR_TimeBaseStruct.division = (pclk2_value/100000-1); //TMR_TimeBaseStruct.division =720-1; TMR_TimeBaseStruct.period = 10-1; TMR_ConfigTimeBase(TMR1, &TMR_TimeBaseStruct);
OCcongigStruct.idleState = TMR_OC_IDLE_STATE_RESET; OCcongigStruct.mode = TMR_OC_MODE_PWM1; OCcongigStruct.nIdleState = TMR_OC_NIDLE_STATE_RESET; OCcongigStruct.nPolarity = TMR_OC_NPOLARITY_HIGH; OCcongigStruct.outputNState = TMR_OC_NSTATE_ENABLE; OCcongigStruct.outputState = TMR_OC_STATE_ENABLE; OCcongigStruct.polarity = TMR_OC_POLARITY_HIGH; OCcongigStruct.pulse = 3; TMR_ConfigOC1(TMR1,&OCcongigStruct);
TMR_ConfigOC1Preload(TMR1, TMR_OC_PRELOAD_ENABLE); TMR_EnableAutoReload(TMR1);
TMR_Enable(TMR1); TMR_EnablePWMOutputs(TMR1);
while(1) { }
}
具体内容,请查看附件。
|