本帖最后由 jsrdczy 于 2021-10-29 16:37 编辑
1.配置timer1 为正交译码接收,用来计数AB相脉冲个数。
2.首先将另外两个io口接到tim的ch0和ch1上,手动拉高拉低进行计数测试,按照表中的四种情况进行手动测试1)当ch0低电平,ch1上升下降沿无计数
2)当ch0高电平,ch1上升下降沿计数+1 -1
3)ch1低电平,ch0沿变化+1 -1
4)ch1高电平,ch0沿变化+1 -1
总结:ch0 为低电平时,ch1上升沿下降沿均未计数。其余三种情况计数值正确计数。
初始化代码如下,我试过tim1和tim2,均是此现象
rcu_periph_clock_enable(encoder->per_clk);
rcu_periph_clock_enable(encoder->ch0_clk);
rcu_periph_clock_enable(encoder->ch1_clk);
/*configture ch0 ch1*/
gpio_init(encoder->ch0_port, GPIO_MODE_IN_FLOATING, GPIO_OSPEED_50MHZ, encoder->ch0_pin);
gpio_init(encoder->ch1_port, GPIO_MODE_IN_FLOATING, GPIO_OSPEED_50MHZ, encoder->ch1_pin);
/*set irqn and enable*/
nvic_irq_enable(encoder->encoder_irqn,4U,0U);
timer_parameter_struct tim_initpara;
tim_initpara.prescaler = 0U;
tim_initpara.alignedmode = TIMER_COUNTER_EDGE;
tim_initpara.counterdirection = TIMER_COUNTER_UP;
tim_initpara.period = 0x7FFF;
tim_initpara.clockdivision = TIMER_CKDIV_DIV1;
tim_initpara.repetitioncounter = 0U;
timer_init(gd32_device->tim_periph,&tim_initpara);
/*time encoder config*/
timer_quadrature_decoder_mode_config(gd32_device->tim_periph,TIMER_ENCODER_MODE2,TIMER_IC_POLARITY_RISING,TIMER_IC_POLARITY_RISING);
/*ic config*/
ic_initpara.icselection = TIMER_IC_SELECTION_DIRECTTI;
ic_initpara.icpolarity = TIMER_IC_POLARITY_RISING;
ic_initpara.icprescaler = TIMER_IC_PSC_DIV1;
ic_initpara.icfilter = 0;
timer_input_capture_config(gd32_device->tim_periph,TIMER_CH_0,&ic_initpara);
ic_initpara.icselection = TIMER_IC_SELECTION_DIRECTTI;
ic_initpara.icpolarity = TIMER_IC_POLARITY_RISING;
ic_initpara.icprescaler = TIMER_IC_PSC_DIV1;
ic_initpara.icfilter = 0;
timer_input_capture_config(gd32_device->tim_periph,TIMER_CH_1,&ic_initpara);
/*clear update flag*/
timer_interrupt_flag_clear(gd32_device->tim_periph,TIMER_INT_FLAG_UP);
/*update source only cnt overflow*/
timer_update_source_config(gd32_device->tim_periph,TIMER_UPDATE_SRC_REGULAR);
/*update interrupt*/
timer_interrupt_enable(gd32_device->tim_periph,TIMER_INT_UP);
/*start cnt enable*/
timer_enable(gd32_device->tim_periph);
希望有人能一起探探讨下,很迷惑
|