MCU:GD32F103RBT6
IDE:Keil5
问题:使用PA1引脚复用功能TIMER4_CH1定时器输出PWM无效,查阅数据手册发现复用功能有TIMER1_CH1和TIMER4_CH1。于是以相同代码换成TIMER1_CH定时器可以正常输出PWM。代码如下所示:
timer_parameter_struct TIM4_Parameter_Struct;
timer_oc_parameter_struct TIM4_OC_Struct;
rcu_periph_clock_enable(RCU_GPIOA);
rcu_periph_clock_enable(RCU_TIMER4);
gpio_init(GPIOA, GPIO_MODE_AF_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_1);
timer_deinit(TIMER4);
TIM4_Parameter_Struct.alignedmode = TIMER_COUNTER_EDGE; /* 对齐模式 */
TIM4_Parameter_Struct.clockdivision = TIMER_CKDIV_DIV1; /* 时钟分频因子 */
TIM4_Parameter_Struct.counterdirection = TIMER_COUNTER_CENTER_UP; /* 计数方向 */
TIM4_Parameter_Struct.period = 999; /* 周期 */
TIM4_Parameter_Struct.prescaler = 107; /* 预分频值 */
TIM4_Parameter_Struct.repetitioncounter = 0; /* 重复计数值 */
timer_init(TIMER4, &TIM4_Parameter_Struct); /* 初始化定时器 */
TIM4_OC_Struct.ocidlestate = TIMER_OC_IDLE_STATE_LOW; /* 空闲状态下通道输出 */
TIM4_OC_Struct.ocnidlestate = TIMER_OCN_IDLE_STATE_LOW; /* 空闲状态下互补通道输出 */
TIM4_OC_Struct.ocnpolarity = TIMER_OCN_POLARITY_HIGH; /* 互补通道输出极性 */
TIM4_OC_Struct.ocpolarity = TIMER_OC_POLARITY_HIGH; /* 通道输出极性 */
TIM4_OC_Struct.outputnstate = TIMER_CCXN_DISABLE; /* 互补通道输出状态 */
TIM4_OC_Struct.outputstate = TIMER_CCX_ENABLE; /* 通道输出状态 */
timer_channel_output_config(TIMER4, TIMER_CH_1, &TIM4_OC_Struct);
/* 占空比配置 */
timer_channel_output_pulse_value_config(TIMER4, TIMER_CH_1, 500);
timer_channel_output_mode_config(TIMER4, TIMER_CH_1, TIMER_OC_MODE_PWM0);
timer_channel_output_shadow_config(TIMER4, TIMER_CH_1, TIMER_OC_SHADOW_DISABLE);
timer_auto_reload_shadow_enable(TIMER4);
timer_enable(TIMER4);
|