GD32F470 用TIMER2每秒输出一个TRGO事件,想用这个TRGO事件触发TIMER1输出100k pwm,用于触发后面的ADC,但是实际上的运行结果是就算不初始化TIMER2,TIMER1也能触发后面的ADC,且cnt寄存器也在计数,求各位大佬们帮我看看是怎么回事,以下分别是定时器1和定时器2的配置过程:
/*ADC设置为周期触发,定时器配置*/
void timer_config(void)
{
timer_oc_parameter_struct timer_ocintpara;
timer_parameter_struct timer_initpara;
timer_deinit(TIMER1);
/* TIMER1 相关配置 */
/* 预分频值设置为199,主频200MHz / (199 + 1) = 1MHz */
timer_initpara.prescaler = 199;
timer_initpara.alignedmode = TIMER_COUNTER_EDGE;
timer_initpara.counterdirection = TIMER_COUNTER_UP;
/* 计数周期设置为9,1MHz / (9 + 1) = 100kHz */
timer_initpara.period = 9;
timer_initpara.clockdivision = TIMER_CKDIV_DIV1;
timer_initpara.repetitioncounter = 0;
timer_init(TIMER1, &timer_initpara);
/* 设置定时器1从模式:由定时器2的TRGO信号触发 */
timer_slave_mode_select(TIMER1, TIMER_SLAVE_MODE_EVENT); // 从模式为触发模式
timer_input_trigger_source_select(TIMER1, TIMER_SMCFG_TRGSEL_ITI2); // 触发源为定时器2的TRGO信号
/* 配置定时器1的输出:PWM模式 */
timer_ocintpara.ocpolarity = TIMER_OC_POLARITY_HIGH;
timer_ocintpara.ocnpolarity = TIMER_OCN_POLARITY_HIGH;
timer_ocintpara.outputnstate = TIMER_CCXN_DISABLE;
timer_ocintpara.outputstate = TIMER_CCX_ENABLE;
timer_ocintpara.ocidlestate = TIMER_OC_IDLE_STATE_LOW;
timer_ocintpara.ocnidlestate = TIMER_OCN_IDLE_STATE_LOW;
timer_channel_output_config(TIMER1, TIMER_CH_3, &timer_ocintpara);
/* 设置PWM占空比 */
timer_channel_output_pulse_value_config(TIMER1, TIMER_CH_3, 4);
timer_channel_output_mode_config(TIMER1, TIMER_CH_3, TIMER_OC_MODE_PWM0);
timer_channel_output_shadow_config(TIMER1, TIMER_CH_3, TIMER_OC_SHADOW_DISABLE);
/* 启用主从模式配置(如果需要同步操作) */
timer_master_slave_mode_config(TIMER1, TIMER_MASTER_SLAVE_MODE_ENABLE);
/* 启用定时器1 */
timer_enable(TIMER1);
}
void timer2_config(void)
{
/* TIMER2 配置,用于输出 1Hz 信号,高电平占比 10% */
timer_oc_parameter_struct timer_ocintpara;
timer_parameter_struct timer_initpara;
/* 启用 TIMER2 时钟 */
rcu_periph_clock_enable(RCU_TIMER2);
/* 清空 TIMER2 配置 */
timer_deinit(TIMER2);
/* TIMER2 配置 */
timer_struct_para_init(&timer_initpara);
timer_initpara.prescaler = 1999; // 200MHz / (19999 + 1) = 10kHz
timer_initpara.alignedmode = TIMER_COUNTER_EDGE;
timer_initpara.counterdirection = TIMER_COUNTER_UP;
timer_initpara.period = 99999; // 10kHz / (9999 + 1) = 1Hz
timer_initpara.clockdivision = TIMER_CKDIV_DIV1;
timer_initpara.repetitioncounter = 0;
timer_init(TIMER2, &timer_initpara);
/* 配置 CH1 为 PWM 模式0 */
timer_channel_output_struct_para_init(&timer_ocintpara);
timer_ocintpara.ocpolarity = TIMER_OC_POLARITY_HIGH;
timer_ocintpara.outputstate = TIMER_CCX_ENABLE;
timer_channel_output_config(TIMER2, TIMER_CH_1, &timer_ocintpara);
/* 设置比较值,占比10%,高电平为总周期的 10% */
timer_channel_output_pulse_value_config(TIMER2, TIMER_CH_1, 9999); // (9999 + 1) * 10% - 1
timer_channel_output_mode_config(TIMER2, TIMER_CH_1, TIMER_OC_MODE_TIMING);
timer_channel_output_shadow_config(TIMER2, TIMER_CH_1, TIMER_OC_SHADOW_DISABLE);
/* 启用自动重载 */
timer_auto_reload_shadow_enable(TIMER2);
/* 配置为主定时器,TRGO 输出 */
timer_master_output_trigger_source_select(TIMER2, TIMER_TRI_OUT_SRC_UPDATE); // 选择定时器更新事件作为TRGO
timer_master_slave_mode_config(TIMER2, TIMER_MASTER_SLAVE_MODE_ENABLE); // 启用主从模式
/* 启用 TIMER2 */
timer_enable(TIMER2);
} |