GD你好,
我在使用GD32F10X timer2 采样一个125KHZ信号时,使用的timer2的通道0 EDGE触发, 在每次通道0中断的时候读取timer2的计数, 但是每次读到的都是8us,如果改成上升沿触发,每次中断时读取timer2的计数,也是8us。
125KHZ的方波信号从示波器上看是正常的。请问下如果正确设置timer2的 通道0触发为 EDGE触发,此时中断读取timer计数起应该是4us才对?
timer初始化和中断代码如下?请帮忙看看
void capture_config(void)
{
timer_ic_parameter_struct timer_icintpara;
timer_parameter_struct timer_initpara;
//gpio_init(RFID_PWM_GPIO_PORT, GPIO_MODE_AIN, GPIO_OSPEED_50MHZ, GPIO_PIN_6); GPIO_MODE_IN_FLOATING
gpio_init(GPIOA, GPIO_MODE_IN_FLOATING, GPIO_OSPEED_50MHZ, GPIO_PIN_6);
nvic_priority_group_set(NVIC_PRIGROUP_PRE1_SUB3);//NVIC_PRIGROUP_PRE2_SUB2 NVIC_PRIGROUP_PRE1_SUB3
nvic_irq_enable(TIMER2_IRQn, 1, 1);
rcu_periph_clock_enable(RCU_TIMER2);
timer_deinit(TIMER2);
/* TIMER0 configuration */
timer_initpara.prescaler = 107;//10799 10K
timer_initpara.alignedmode = TIMER_COUNTER_EDGE;//TIMER_COUNTER_EDGE TIMER_COUNTER_CENTER_BOTH;
timer_initpara.counterdirection = TIMER_COUNTER_UP; //TIMER_COUNTER_UP TIMER_COUNTER_DOWN
timer_initpara.period = 65535;//65535
timer_initpara.clockdivision = TIMER_CKDIV_DIV1;
timer_initpara.repetitioncounter = 0;
timer_init(TIMER2,&timer_initpara);
/* CH1 configuration in PWM mode1 */
timer_icintpara.icpolarity = TIMER_IC_POLARITY_BOTH_EDGE; //TIMER_IC_POLARITY_BOTH_EDGE TIMER_IC_POLARITY_RISING TIMER_IC_POLARITY_FALLING
timer_icintpara.icselection = TIMER_IC_SELECTION_DIRECTTI;// TIMER_IC_SELECTION_DIRECTTI TIMER_IC_SELECTION_INDIRECTTI TIMER_IC_SELECTION_ITS
timer_icintpara.icprescaler = TIMER_IC_PSC_DIV1;
timer_icintpara.icfilter = 0x1;
timer_input_capture_config(TIMER2, TIMER_CH_0, &timer_icintpara);
timer_auto_reload_shadow_enable(TIMER2);
timer_interrupt_flag_clear(TIMER2, TIMER_INT_FLAG_CH0);
timer_interrupt_enable(TIMER2, TIMER_INT_CH0);
timer_enable(TIMER2);
printf("capture_config success !\n");
}
中断处理如下:
void TIMER2_IRQHandler(void)
{
__IO uint16_t fre = 0;
__IO uint16_t MASK = 0;
__IO uint8_t io = 0;
int i = 0;
if(SET == timer_interrupt_flag_get(TIMER2, TIMER_INT_FLAG_CH0))//TIMER_INT_FLAG_CH0 TIMER_INT_CH0
{
timer_interrupt_flag_clear(TIMER2, TIMER_INT_FLAG_CH0);
if(0 == ccnumber){
/* read channel 0 capture value */
readvalue1 = timer_channel_capture_value_register_read(TIMER2,TIMER_CH_0);
ccnumber = 1;
}else if(1 == ccnumber){
/* read channel 0 capture value */
readvalue2 = timer_channel_capture_value_register_read(TIMER2,TIMER_CH_0);
count = (readvalue2 - readvalue1);
//fre = (float)1000000 / count;
// printf("the value1 is %d,the value2 is %d\n",readvalue1,readvalue2);
printf("the count is %d\r\n",count);
// printf("the frequence is %f\n",fre);
ccnumber = 0;
}
}
return;
}
输出打印一直为:
the count is 8
the count is 8
the count is 8
the count is 8
the count is 8
the count is 8
the count is 8
the count is 8
the count is 8
the count is 8
the count is 8
the count is 8
the count is 8
the count is 8
the count is 8
the count is 8
the count is 8
the count is 8
the count is 8
the count is 8
the count is 8
the count is 8
the count is 8
the count is 8
the count is 8
the count is 8
the count is 8
the count is 8
|