网上关于使用GD32单片机的ETI引脚作为计数器输入引脚的资料少之又少,此文希望有需要的朋友避免弯路。
我们把ETI引脚作为方波输入引脚,其中ETR输入引脚为PA0。通过查资料得知:
因此将先将引脚进行映射,代码如下:
void gpio_config(void)
{
rcu_periph_clock_enable(RCU_GPIOA);
rcu_periph_clock_enable(RCU_AF);
/*configure PA0(TIMER1 CH0) as alternate function*/
gpio_init(GPIOA, GPIO_MODE_AF_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_0);
gpio_pin_remap_config(GPIO_TIMER1_PARTIAL_REMAP2,ENABLE);
}
然后再将时钟和中断开启即可进行输入计数了。如下代码:
void timer_config(void)
{
/* ----------------------------------------------------------------------------
TIMER1 Configuration:
TIMER1 count with external clock, the prescaler is 0, the period is 1000.
---------------------------------------------------------------------------- */
timer_parameter_struct timer_initpara;
timer_ic_parameter_struct timer_icinitpara;
rcu_periph_clock_enable(RCU_TIMER1);
timer_deinit(TIMER1);
/* initialize TIMER init parameter struct */
timer_struct_para_init(&timer_initpara);
/* TIMER1 configuration */
timer_initpara.prescaler = 0;
timer_initpara.alignedmode = TIMER_COUNTER_EDGE;
timer_initpara.counterdirection = TIMER_COUNTER_UP;
timer_initpara.period = 999;
timer_initpara.clockdivision = TIMER_CKDIV_DIV1;
timer_init(TIMER1, &timer_initpara);
/* TIMER1 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 = 0x01;
timer_input_capture_config(TIMER1, TIMER_CH_0, &timer_icinitpara);
/* slave mode selection : TIMER1 */
/* TIMER1 input trigger : external trigger connected to CI0 */
timer_input_trigger_source_select(TIMER1,TIMER_SMCFG_TRGSEL_CI0FE0);
timer_slave_mode_select(TIMER1,TIMER_SLAVE_MODE_EXTERNAL0);
/* enable the TIMER interrupt */
timer_interrupt_flag_clear(TIMER1, TIMER_INT_FLAG_UP);
timer_interrupt_enable(TIMER1, TIMER_INT_UP);
timer_enable(TIMER1);
}
开启中断:
void nvic_config(void)
{
nvic_priority_group_set(NVIC_PRIGROUP_PRE1_SUB3);
nvic_irq_enable(TIMER1_IRQn, 1, 1);
}
具体的例子如下,可进行下载:
https://download.csdn.net/download/xiaoyaofriend/89514393