GD32,正交编码器反转时,计数脉冲丢失,正转时计数正常,反转读计数没了值。
int16_t Encoder_Get(void)
{
int16_t Temp;
Temp = timer_counter_read(TIMER0);
//timer_counter_value_config(TIMER0, 0x7FFF);
return Temp;
}
这个函数反转时读的值是0,下面是编码器初始化函数
void timer0_config(void)
{
timer_parameter_struct baseConfig;
timer_oc_parameter_struct occonfig;
timer_ic_parameter_struct ICConfig;
/* Enable TMR0 Periph Clock */
rcu_periph_clock_enable(RCU_TIMER0);
/* Config TMR0 */
baseConfig.clockdivision = TIMER_CKDIV_DIV1;
baseConfig.counterdirection = TIMER_COUNTER_UP;
baseConfig.prescaler = 0;
baseConfig.period = 4800;
baseConfig.repetitioncounter = 0;
timer_init(TIMER0, &baseConfig);
timer_counter_value_config(TIMER0, 0);
timer_slave_mode_select(TIMER0, TIMER_QUAD_DECODER_MODE2);
/* Encoder mode */
timer_quadrature_decoder_mode_config(TIMER0, 3, TIMER_IC_POLARITY_RISING, TIMER_IC_POLARITY_RISING);
//timer_flag_clear(TIMER0, TIMER_FLAG_UP);
/* Enable TMR1,TMR3 Interrupt */
//timer_interrupt_enable(TIMER2, TIMER_INT_CH1);
timer_interrupt_enable(TIMER0, TIMER_INT_UP);
//timer_interrupt_enable(TIMER0, TIMER_INT_CH1);
nvic_priority_group_set(NVIC_PRIGROUP_PRE1_SUB3);
//nvic_irq_enable(TIMER2_IRQn, 1,1);
nvic_irq_enable(TIMER0_UP_IRQn, 1,1);
/* Enable TMR1,TMR3 */
//timer_enable(TIMER2);
timer_enable(TIMER0);
}
|