各位专家,以前用STM32的ETR很容易就实现了,想用GD32F303的TIMER0 ETI实现外部脉冲计数,怎么配置?好几天了,怎么配置都不行,谢谢大家!
--------------------------------------------------------------------
以前STM32是这样的:
static void tim1_counter_gpio_init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_OType = GPIO_OType_OD;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* Connect PXx to ETR */
GPIO_PinAFConfig(GPIOA, GPIO_PinSource12, GPIO_AF_2);
}
/* Public functions ----------------------------------------------------------*/
/***************************************************
Func: TIM1 for 外部计数器
***************************************************/
void TIM1_Counter_Init(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;
tim1_counter_gpio_init();
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1,ENABLE); //使能TIM时钟
TIM_DeInit(TIM1);
TIM_TimeBaseInitStructure.TIM_Period = 0xFFFFFFFF;//计数到此值后, 再计1个才产生中断同时计数寄存器值清零
TIM_TimeBaseInitStructure.TIM_Prescaler = 0; //定时器分频
TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up;//向上计数模式
TIM_TimeBaseInitStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseInit(TIM1,&TIM_TimeBaseInitStructure);//初始化TIM
TIM_ETRClockMode2Config(TIM1, TIM_ExtTRGPSC_OFF,TIM_ExtTRGPolarity_NonInverted, 0);//0次采样滤波 外部时钟模式2
TIM_ClearITPendingBit(TIM1, TIM_IT_Update); //清除中断标志位,避免第一次自动进入中断一次
TIM_ITConfig(TIM1, TIM_IT_Update, ENABLE);
TIM_SetCounter(TIM1, 0);//设置计数初值
TIM_Cmd(TIM1, ENABLE); //使能定时器
}
--------------------------------------------------------------------
移植到GD32F303是这样的,但是不行
static void tim1_counter_gpio_init(void)
{
rcu_periph_clock_enable(RCU_GPIOA);
rcu_periph_clock_enable(RCU_AF);
gpio_init(GPIOA,GPIO_MODE_AF_OD,GPIO_OSPEED_50MHZ,GPIO_PIN_12);
}
/* Public functions ----------------------------------------------------------*/
/***************************************************
Func: TIM1 for 外部计数器
***************************************************/
void TIM1_Counter_Init(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;
tim1_counter_gpio_init();
rcu_periph_clock_enable(RCU_TIMER0);
timer_deinit(TIMER0);
TIM_TimeBaseInitStructure.prescaler = 0;
TIM_TimeBaseInitStructure.alignedmode = TIMER_COUNTER_EDGE;
TIM_TimeBaseInitStructure.counterdirection = TIMER_COUNTER_UP;
TIM_TimeBaseInitStructure.period = 3599;
TIM_TimeBaseInitStructure.clockdivision = TIMER_CKDIV_DIV1;
TIM_TimeBaseInitStructure.repetitioncounter = 0; //计数到此值后, 再计1个才产生中断同时计数寄存器值清零
timer_init(TIMER0,&TIM_TimeBaseInitStructure);
timer_external_clock_mode1_config(TIMER0, TIMER_EXT_TRI_PSC_OFF, TIMER_ETP_RISING, 0);
timer_counter_value_config(TIMER0, 0);
timer_enable(TIMER0);
} |