本帖最后由 WuKaiLi 于 2022-11-24 16:49 编辑
由于MCU的定时器频率限制,建议你使用最大频率,并且不要进行分频。你的代码有一些问题:
1.计数值设置太小,为2,不利于精度;
2.翻转电平错误,计数值你设置的2,理论计数为0,1,2;你设置的翻转值为1,则电平持续时间的计数值为0,翻转电平时间持续的的计数值为1,2;所以你占空比不对;
我写了一个demo,供你参考,注意:系统频率为72M;
int main(void)
{
GPIO_Config_T GPIO_ConfigStruct;
TMR_BaseConfig_T TMR_TimeBaseStruct;
TMR_OCConfig_T OCcongigStruct;
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 = 0;
TMR_TimeBaseStruct.period = 23;
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 = 12;
TMR_ConfigOC1(TMR1, &OCcongigStruct);
TMR_ConfigOC1Preload(TMR1, TMR_OC_PRELOAD_ENABLE);
TMR_EnableAutoReload(TMR1);
TMR_Enable(TMR1);
TMR_EnablePWMOutputs(TMR1);
while(1)
{
}
}
示波器测试截图如下,不知道是否满足你的需求:
|