打印

GD32F407VET6定时2输入捕获频率咨询

[复制链接]
3229|0
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
akingyouyou|  楼主 | 2022-7-18 16:28 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
MCU料号:GD32F407VET6
用PC7捕获输入频率调试正常,改为pc8之后无法捕获,且每6s左右进入定时中断;
PC7:timer2 CH1
PC8:timer2 CH2
有相关修改的地方,我都做了备注,请各位大佬指导一下,问题出在哪里
//************************************************************************************
#include "TimerPhy.h"      // 定时器相关头文件

uint32_t TimCounter;  // 计数器
uint8_t  LedFlag;     // LED 翻转标志
__IO uint16_t readvalue1 = 0, readvalue2 = 0;
__IO uint16_t ccnumber = 0;
__IO uint32_t count = 0;
__IO float fre = 0;
__IO char Getfre = 0;

/************************ 函数定义 ************************/
void gpio_configuration(void)
{   
    #if 0
    rcu_periph_clock_enable(RCU_GPIOC);
    /*configure PC7 (TIMER2 CH1) as alternate function*/
    gpio_mode_set(GPIOC, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO_PIN_7);
    gpio_output_options_set(GPIOC, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ,GPIO_PIN_7);
    gpio_af_set(GPIOC, GPIO_AF_2, GPIO_PIN_7);
    #endif
    #if 1
    rcu_periph_clock_enable(RCU_GPIOC);
    /*configure PC8 (TIMER2 CH2) as alternate function*/
    gpio_mode_set(GPIOC, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO_PIN_8);
    gpio_output_options_set(GPIOC, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ,GPIO_PIN_8);
    gpio_af_set(GPIOC, GPIO_AF_2, GPIO_PIN_8);   
    #endif
}

/**
    \brief      configure the nested vectored interrupt controller
    \param[in]  none
    \param[out] none
    \retval     none
  */
void nvic_configuration(void)
{
    nvic_priority_group_set(NVIC_PRIGROUP_PRE1_SUB3);
    nvic_irq_enable(TIMER2_IRQn, 1, 1);
}

/**
    \brief      configure the TIMER peripheral
    \param[in]  none
    \param[out] none
    \retval     none
  */
void timer2_configuration(void)
{
/* TIMER2 configuration: PWM input mode ------------------------
     the external signal is connected to TIMER2 CH0 pin(PB4)
     the rising edge is used as active edge
     the TIMER2 CH0CV is used to compute the frequency value
     the TIMER2 CH1CV is used to compute the duty cycle value//ch1v没用到
    我的板子主频200M;200M/10000=20k
  ------------------------------------------------------------ */
    timer_ic_parameter_struct timer_icinitpara;
    timer_parameter_struct timer_initpara;

    rcu_periph_clock_enable(RCU_TIMER2);
    rcu_timer_clock_prescaler_config(RCU_TIMER_PSC_MUL4);

    timer_deinit(TIMER2);

    /* TIMER2 configuration */
    timer_initpara.prescaler         = 9999;//10000分频(与捕获分频要相同)20k
    timer_initpara.alignedmode       = TIMER_COUNTER_EDGE;//TIMER_COUNTER_EDGE;
    timer_initpara.counterdirection  = TIMER_COUNTER_UP;//向上计数模式
    timer_initpara.period            = 65535;//65535;//定时周期,自动从装载寄存器ARR的值;0-65536
    timer_initpara.clockdivision     = TIMER_CKDIV_DIV1;//分频因子
    timer_initpara.repetitioncounter = 0;
    timer_init(TIMER2,&timer_initpara);

    /* TIMER2 configuration */
    /* TIMER2 CH0 PWM input capture configuration */
    timer_icinitpara.icpolarity  = TIMER_IC_POLARITY_FALLING;//TIMER_IC_POLARITY_RISING;//上升沿捕获
    timer_icinitpara.icselection = TIMER_IC_SELECTION_DIRECTTI;
    timer_icinitpara.icprescaler = TIMER_IC_PSC_DIV1;//捕获定时器分频prescaler预分频,这里分频
    timer_icinitpara.icfilter    = 0x0;//滤波
    //timer_input_pwm_capture_config(TIMER2,TIMER_CH_1,&timer_icinitpara);
    timer_input_pwm_capture_config(TIMER2,TIMER_CH_2,&timer_icinitpara);

    /* auto-reload preload enable */
    timer_auto_reload_shadow_enable(TIMER2);/* 自动加载定时器参数 */
    /* clear channel 0 interrupt bit */
    //timer_interrupt_flag_clear(TIMER2,TIMER_INT_CH1); //清除通道1中断标志
    timer_interrupt_flag_clear(TIMER2,TIMER_INT_CH2); //清除通道1中断标志      
    /* channel 0 interrupt enable */
    //timer_interrupt_enable(TIMER2,TIMER_INT_CH1);  //使能通道1中断
    timer_interrupt_enable(TIMER2,TIMER_INT_CH2);  //使能通道1中断  

    /* TIMER2 counter enable */
    timer_enable(TIMER2);//使能定时器2
}
void TIMER_Config(void)
{
    gpio_configuration();
    nvic_configuration();
    timer2_configuration();
}

void TIMER2_IRQHandler(void)
{
    //if(SET == timer_interrupt_flag_get(TIMER2,TIMER_INT_CH1))
    if(SET == timer_interrupt_flag_get(TIMER2,TIMER_INT_CH2))
    {
        /* clear channel 0 interrupt bit */
        //timer_interrupt_flag_clear(TIMER2,TIMER_INT_CH1);//清除中断标志
        timer_interrupt_flag_clear(TIMER2,TIMER_INT_CH2);//清除中断标志
        if(0 == ccnumber)
        {
            /* read channel 0 capture value */
            //readvalue1 = timer_channel_capture_value_register_read(TIMER2,TIMER_CH_1);//获取捕获数值1
            readvalue1 = timer_channel_capture_value_register_read(TIMER2,TIMER_CH_2);//获取捕获数值1
            ccnumber = 1;
        }
        else if(1 == ccnumber)
        {
            /* read channel 0 capture value */
            //readvalue2 = timer_channel_capture_value_register_read(TIMER2,TIMER_CH_1);//获取捕获数值2
            readvalue2 = timer_channel_capture_value_register_read(TIMER2,TIMER_CH_2);//获取捕获数值2
            if(readvalue2 > readvalue1)
            {//计算捕获值差
                count = (readvalue2 - readvalue1);
            }
            else
            {
                count = ((0xFFFF - readvalue1) + readvalue2);
            }
            fre = (float)20000 / count;//计算频率
            ccnumber = 0;
            Getfre=1;
        }
    }
}

void GetFrefun(void)//放在main里面测试的主函数
{
    if(Getfre==1)
    {
        Getfre=0;
        printf("the value1 is %d,the value2 is %d\n",readvalue1,readvalue2);
        printf("the count is %d\n",count);
        printf("the frequence is %f \n\n",fre);
    }   
}
/****************************End*****************************/




使用特权

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

本版积分规则

1

主题

11

帖子

1

粉丝