近日由于各种缺货涨价等等原因,准备换GD32F303,可是正交编码器模式总是调试不通,以前STM32F103很好配置的。没办法到处找资料、例程,最后找到厂家给的一个例程还是不行,好像例程是利用中断处理的,难道不应该是硬件自动计数?不知大家有没有遇到这种问题。
附上例程代码如下:
void timer2_init(void)
{
timer_parameter_struct timer_initpara;
timer_ic_parameter_struct timer_icinitpara;
timer_oc_parameter_struct timer_ocintpara;
rcu_periph_clock_enable(RCU_TIMER2);
timer_deinit(TIMER2);
timer_initpara.prescaler = 0;
timer_initpara.alignedmode = TIMER_COUNTER_EDGE;
timer_initpara.counterdirection = TIMER_COUNTER_UP;
timer_initpara.period = 3999;
timer_initpara.clockdivision = 0;
timer_initpara.repetitioncounter = 0;
timer_init(TIMER2, &timer_initpara);
timer_icinitpara.icpolarity = TIMER_IC_POLARITY_RISING;
timer_icinitpara.icselection = TIMER_IC_SELECTION_DIRECTTI;
timer_icinitpara.icprescaler = TIMER_IC_PSC_DIV1;
timer_icinitpara.icfilter = 0x5;
timer_input_capture_config(TIMER2, TIMER_CH_0, &timer_icinitpara);
timer_icinitpara.icfilter = 0x5;
timer_input_capture_config(TIMER2, TIMER_CH_2, &timer_icinitpara);
timer_quadrature_decoder_mode_config(TIMER2,TIMER_ENCODER_MODE2,TIMER_IC_POLARITY_RISING,TIMER_IC_POLARITY_RISING);
timer_interrupt_flag_clear(TIMER2, TIMER_INT_FLAG_CH0);
timer_interrupt_flag_clear(TIMER2, TIMER_INT_FLAG_CH2);
timer_interrupt_enable(TIMER2, TIMER_INT_CH0 |TIMER_INT_CH2);
timer_enable(TIMER2);
}
int main(void)
{
timer2_init();
while(1);
}
void TIMER2_IRQHandler(void)
{
if(timer_interrupt_flag_get(TIMER2, TIMER_INT_FLAG_CH0)){
timer_interrupt_flag_clear(TIMER2, TIMER_INT_FLAG_CH0);
//处理正交编码信号
}
if(timer_interrupt_flag_get(TIMER2, TIMER_INT_FLAG_CH2)){
timer_interrupt_flag_clear(TIMER2, TIMER_INT_FLAG_CH2);
//处理过零点信号
}
}
|