This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

TM4C123G定时器输入捕获读取计数值不正确

我想测一个信号的高电平时间,用了定时器的输入捕获,代码如下

现在问题是在中断里读出的值远远超出了0-65535这个范围,很纳闷,麻烦大家看一下配置哪里有没有什么问题


  • void pwm_in_init(void)
    {
    // 启用Timer2模块
    SysCtlPeripheralEnable(SYSCTL_PERIPH_TIMER2);
    TimerClockSourceSet(TIMER2_BASE,TIMER_CLOCK_PIOSC); //设置定时器时钟为内部精准时钟 16M

    // 启用GPIO_B作为脉冲捕捉脚
    SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOB);
    // 配置GPIO脚为使用Timer2捕捉模式
    GPIOPinConfigure(GPIO_PB0_T2CCP0);
    GPIOPinTypeTimer(GPIO_PORTB_BASE, GPIO_PIN_0);
    // 为管脚配置弱上拉模式
    GPIOPadConfigSet(GPIO_PORTB_BASE, GPIO_PIN_0, GPIO_STRENGTH_2MA, GPIO_PIN_TYPE_STD_WPU);
    // 配置使用Timer2的TimerA模块为沿触发加计时模式
    TimerConfigure(TIMER2_BASE, TIMER_CFG_SPLIT_PAIR | TIMER_CFG_A_CAP_TIME_UP);
    // 使用上升沿下降沿触发
    TimerControlEvent(TIMER2_BASE, TIMER_A, TIMER_EVENT_BOTH_EDGES);
    // 设置计数范围为0~0xFFFF
    TimerLoadSet(TIMER2_BASE, TIMER_A, 0xffff);
    // 注册中断处理函数以响应触发事件
    TimerIntRegister(TIMER2_BASE, TIMER_A, Timer2AIntHandler);

    // 系统总中断开
    IntMasterEnable();

    // 时钟中断允许,中断事件为Capture模式中边沿触发
    TimerIntEnable(TIMER2_BASE, TIMER_CAPA_EVENT);
    // NVIC中允许定时器A模块中断
    IntEnable(INT_TIMER2A);
    // 启动捕捉模块
    TimerEnable(TIMER2_BASE, TIMER_A);

    }

    void Timer2AIntHandler(void)
    {
    unsigned long ulstatus;
    static unsigned short _tmp;
    // 读取中断标志位
    ulstatus = TimerIntStatus(TIMER2_BASE, TIMER_CAPA_EVENT);
    // 清除中断标志位
    TimerIntClear(TIMER2_BASE, ulstatus);
    UARTprintf("%d\r\n",TimerValueGet(TIMER2_BASE, TIMER_A));

    }

  • 建议您参考一下下面的程序

    #include <stdint.h>
    #include <stdbool.h>
    #include <stdio.h>
    #include "inc/hw_memmap.h"
    #include "inc/hw_types.h"
    #include "inc/hw_gpio.h"
    #include "inc/hw_nvic.h"
    #include "inc/hw_ints.h"
    #include "driverlib/debug.h"
    #include "driverlib/gpio.h"
    #include "driverlib/pin_map.h"
    #include "driverlib/sysctl.h"
    #include "driverlib/interrupt.h"
    #include "driverlib/timer.h"
    #include "utils/uartstdio.h"
     
    void Timer0B_Int_Handler();
    int main(void)
    {
            //system config
    	SysCtlClockSet(SYSCTL_SYSDIV_4|SYSCTL_USE_PLL
    	                  |SYSCTL_OSC_MAIN|SYSCTL_XTAL_16MHZ);
     
    	SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOF);
    	SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOB);
     	SysCtlPeripheralEnable(SYSCTL_PERIPH_TIMER0);
    	SysCtlDelay(200);//配置完系统寄存器后要延时一段时间
     
            //GPIO config
    	GPIOPinConfigure(GPIO_PB7_T0CCP1);
    	GPIOPinTypeTimer(GPIO_PORTB_BASE, GPIO_PIN_7);
     
            //Timer config
    	TimerConfigure(TIMER0_BASE, TIMER_CFG_SPLIT_PAIR | TIMER_CFG_B_CAP_TIME_UP);
    	TimerControlEvent(TIMER0_BASE, TIMER_B, TIMER_EVENT_POS_EDGE);
        
            //Int config
    	IntEnable(INT_TIMER0B);
    	TimerIntEnable(TIMER0_BASE, TIMER_CAPB_EVENT);
    	IntMasterEnable();
    	TimerEnable(TIMER0_BASE, TIMER_B);
     
            while(1);
    }
    void Timer0B_Int_Handler()
    {
    	TimerIntClear(TIMER0_BASE, TIMER_CAPB_EVENT);
    }

    附上链接: https://blog.csdn.net/a826319028/article/details/27370905
  • 无论使用该程序还是我的程序都可以进入中断,但是两个程序都无法通过TimerValueGet()函数读取到正确的值,得不到正确的值,我就无法算出我所检测的高电平的时间

  • 还有一种可能,串口发送的时间大于了定时器的时间,出现了问题。