本帖最后由 shen19920816 于 2022-8-26 16:21 编辑
用定时器的通道0 通道1做输入捕获,进不去中断。同样的初始化函数,tim3就可以。有大佬遇到过吗
void Hall0_config(uint16_t pcs,uint16_t arr)
{
timer_ic_parameter_struct timer_icinitpara;
timer_parameter_struct timer_initpara;
rcu_periph_clock_enable(RCU_GPIOB);
rcu_periph_clock_enable(RCU_TIMER2);
rcu_timer_clock_prescaler_config(RCU_TIMER_PSC_MUL4);
timer_deinit(TIMER2);
gpio_mode_set(GPIOB, GPIO_MODE_INPUT, GPIO_PUPD_NONE, GPIO_PIN_4| GPIO_PIN_5);
gpio_af_set(GPIOB, GPIO_AF_2, GPIO_PIN_4| GPIO_PIN_5);
// nvic_priority_group_set(NVIC_PRIGROUP_PRE1_SUB3);
////////////********Timer2*******************////////////////////////
timer_initpara.prescaler = pcs-1;
timer_initpara.alignedmode = TIMER_COUNTER_EDGE;
timer_initpara.counterdirection = TIMER_COUNTER_UP;
timer_initpara.period = arr - 1;
timer_initpara.clockdivision = TIMER_CKDIV_DIV1;
timer_initpara.repetitioncounter = 0;
timer_init(TIMER2, &timer_initpara);
timer_channel_input_struct_para_init(&timer_icinitpara);
timer_icinitpara.icpolarity = TIMER_IC_POLARITY_RISING; //上升沿触发
timer_icinitpara.icselection = TIMER_IC_SELECTION_DIRECTTI;
timer_icinitpara.icprescaler = TIMER_IC_PSC_DIV1;
timer_icinitpara.icfilter = 0x4;//滤波
timer_input_capture_config(TIMER2,TIMER_CH_0,&timer_icinitpara);
timer_input_capture_config(TIMER2,TIMER_CH_1,&timer_icinitpara);
timer_auto_reload_shadow_enable(TIMER2);
timer_interrupt_flag_clear(TIMER2,TIMER_INT_FLAG_UP);
nvic_irq_enable(TIMER2_IRQn, 1, 1);
timer_interrupt_flag_clear(TIMER2,TIMER_INT_FLAG_CH0);
timer_interrupt_enable(TIMER2,TIMER_INT_CH0);
timer_interrupt_flag_clear(TIMER2,TIMER_INT_FLAG_CH1);
timer_interrupt_enable(TIMER2,TIMER_INT_CH1);
timer_enable(TIMER2);
}
|