打印
[研电赛技术支持]

GD32F10X timer 问题

[复制链接]
941|8
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
chris420|  楼主 | 2022-3-24 09:44 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
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

使用特权

评论回复
沙发
sonicll| | 2022-3-24 11:17 | 只看该作者
F10x的TIMER的输入捕获是不支持双边沿的,不知道你用的哪个版本的固件库,TIMER_IC_POLARITY_BOTH_EDGE这个宏在F10x里是不存在的

使用特权

评论回复
板凳
小凡凡丶| | 2022-3-24 14:11 | 只看该作者
如果是上升沿触发,8是没有问题的,输入捕获8个脉冲,两次的差值就是8,(1*108)/108=1us,125KHZ的频率,一个脉冲就是8us,没问题啊,就是你第二次得考虑数据有没有跨国=过整个65536个计数周期。

使用特权

评论回复
地板
chris420|  楼主 | 2022-3-24 14:29 | 只看该作者
sonicll 发表于 2022-3-24 11:17
F10x的TIMER的输入捕获是不支持双边沿的,不知道你用的哪个版本的固件库,TIMER_IC_POLARITY_BOTH_EDGE这个 ...

/*!
    \file    gd32f10x_timer.h
    \brief   definitions for the TIMER

    \version 2014-12-26, V1.0.0, firmware for GD32F10x
    \version 2017-06-20, V2.0.0, firmware for GD32F10x
    \version 2018-07-31, V2.1.0, firmware for GD32F10x
*/
我是这个版本,里面有这个定义

使用特权

评论回复
5
chris420|  楼主 | 2022-3-24 14:29 | 只看该作者
小凡凡丶 发表于 2022-3-24 14:11
如果是上升沿触发,8是没有问题的,输入捕获8个脉冲,两次的差值就是8,(1*108)/108=1us,125KHZ的频率, ...

我想要的是 双边缘触发, 能够获取到4us

使用特权

评论回复
6
lifeforrent| | 2022-3-24 17:22 | 只看该作者
库代码是不对的,F10x用户手册里的确没有双边沿的功能。

使用特权

评论回复
7
chris420|  楼主 | 2022-3-24 21:39 | 只看该作者
lifeforrent 发表于 2022-3-24 17:22
库代码是不对的,F10x用户手册里的确没有双边沿的功能。

是的,我刚查了手册,有说通过边沿检测器,
可以选择检测上升沿或者下降沿。并没有说任意沿。
另外中断 EXTI有三种触发类型:上升沿触发、下降沿触发和任意沿触发。我现在改成用中断实现这个功能了。 谢谢

使用特权

评论回复
8
sonicll| | 2022-3-25 08:58 | 只看该作者
chris420 发表于 2022-3-24 14:29
/*!
    \file    gd32f10x_timer.h
    \brief   definitions for the TIMER

你固件版本太老了,现在最新版应该是2021年的

使用特权

评论回复
9
chris420|  楼主 | 2022-3-25 10:55 | 只看该作者
sonicll 发表于 2022-3-25 08:58
你固件版本太老了,现在最新版应该是2021年的

好的,我更新下看看,谢谢!

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

1

主题

5

帖子

0

粉丝