4.TIMvoid GD_TIM_Init(uint32_t timer_periph, uint16_t arr, uint16_t psc)
{
timer_parameter_struct timer_initpara;
timer_ic_parameter_struct timer_icinitpara;
if(timer_periph == TIMER1)//中断模式
{
timer_initpara.period = arr;
timer_initpara.prescaler = psc;
timer_initpara.alignedmode = TIMER_COUNTER_EDGE;
timer_initpara.counterdirection = TIMER_COUNTER_UP;
timer_initpara.clockdivision = TIMER_CKDIV_DIV1;
timer_initpara.repetitioncounter = 0;
GD_TIM_Base_MspInit(TIMER1);
timer_init(TIMER1, &timer_initpara);
timer_update_event_enable(TIMER1);
timer_interrupt_enable(TIMER1, TIMER_INT_UP);
timer_flag_clear(TIMER1, TIMER_FLAG_UP);
timer_update_source_config(TIMER1, TIMER_UPDATE_SRC_GLOBAL);
timer_enable(TIMER1);
}
else if(timer_periph == TIMER3)//编码器
{
timer_initpara.period = arr;
timer_initpara.prescaler = psc;
timer_initpara.alignedmode = TIMER_COUNTER_EDGE;
timer_initpara.counterdirection = TIMER_COUNTER_UP;
timer_initpara.clockdivision = TIMER_CKDIV_DIV1;
timer_initpara.repetitioncounter = 0;
GD_TIM_Encoder_MspInit(TIMER3);
timer_init(TIMER3, &timer_initpara);
/* TIMER3 CH0 input capture configuration */
timer_icinitpara.icpolarity = TIMER_IC_POLARITY_RISING;
timer_icinitpara.icselection = TIMER_IC_SELECTION_DIRECTTI;
timer_icinitpara.icprescaler = TIMER_IC_PSC_DIV1;
timer_icinitpara.icfilter = 0x05;
timer_input_capture_config(TIMER3,TIMER_CH_0,&timer_icinitpara);
timer_input_capture_config(TIMER3,TIMER_CH_1,&timer_icinitpara);
timer_quadrature_decoder_mode_config(TIMER3,TIMER_ENCODER_MODE1,TIMER_IC_POLARITY_RISING,TIMER_IC_POLARITY_RISING);
timer_interrupt_flag_clear(TIMER3, TIMER_INT_FLAG_CH0);
timer_interrupt_flag_clear(TIMER3, TIMER_INT_FLAG_CH1);
timer_interrupt_enable(TIMER3, TIMER_INT_UP);
timer_interrupt_enable(TIMER3, TIMER_INT_CH0 |TIMER_INT_CH1);
/* auto-reload preload enable */
// timer_auto_reload_shadow_enable(TIMER3);
timer_enable(TIMER3);
}
}
void GD_TIM_Base_MspInit(uint32_t timer_periph)
{
if(timer_periph == TIMER1)
{
/* TIM1 clock enable */
rcu_periph_clock_enable(RCU_TIMER1);
/* TIM1 interrupt Init */
nvic_irq_enable(TIMER1_IRQn, 5, 0);
timer_deinit(TIMER1);
}
}
void GD_TIM_Base_MspDeInit(uint32_t timer_periph)
{
if(timer_periph == TIMER1)
{
/* TIM1 clock disable */
rcu_periph_clock_disable(RCU_TIMER1);
/* TIM1 interrupt Deinit */
nvic_irq_disable(TIMER1_IRQn);
}
}
//编码器
void GD_TIM_Encoder_MspInit(uint32_t timer_periph)
{
if(timer_periph == TIMER3)
{
/* TIM3 clock enable */
rcu_periph_clock_enable(RCU_TIMER3);
/**TIM3 GPIO Configuration
PD12 ------> TIM3_CH1
PD13 ------> TIM3_CH2
*/
gpio_mode_set(GPIOD, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO_PIN_12|GPIO_PIN_13);
gpio_output_options_set(GPIOD, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_12|GPIO_PIN_13);
gpio_af_set(GPIOD, GPIO_AF_2, GPIO_PIN_12|GPIO_PIN_13);
timer_deinit(TIMER3);
}
}
void GD_TIM_Encoder_MspInit(uint32_t timer_periph)
{
if(timer_periph == TIMER3)
{
/* TIM3 clock enable */
rcu_periph_clock_enable(RCU_TIMER3);
/**TIM3 GPIO Configuration
PD12 ------> TIM3_CH1
PD13 ------> TIM3_CH2
*/
gpio_mode_set(GPIOD, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO_PIN_12|GPIO_PIN_13);
gpio_output_options_set(GPIOD, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_12|GPIO_PIN_13);
gpio_af_set(GPIOD, GPIO_AF_2, GPIO_PIN_12|GPIO_PIN_13);
timer_deinit(TIMER3);
}
}
void GD_TIM_Encoder_MspDeInit(uint32_t timer_periph)
{
if(timer_periph == TIMER3)
{
/* TIM3 clock disable */
rcu_periph_clock_disable(RCU_TIMER3);
/**TIM3 GPIO Configuration
PD12 ------> TIM3_CH1
PD13 ------> TIM3_CH2
*/
gpio_mode_set(GPIOD, GPIO_MODE_INPUT, GPIO_PUPD_NONE, GPIO_PIN_12|GPIO_PIN_13);
gpio_output_options_set(GPIOD, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_12|GPIO_PIN_13);
}
}
void TIMER1_IRQHandler( void )
{
if( timer_interrupt_flag_get(TIMER1, TIMER_INT_FLAG_UP ) != RESET )
{
timer_interrupt_flag_clear(TIMER1, TIMER_INT_FLAG_UP);
}
}
//timer_counter_value_config(TIMER3, 500); //编码器设置值
//timer_counter_read(TIMER3) //编码器读取值
|
|