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.

TM4C123GH6Pm测量一段高电平持续时间



void Timer0IntHandler(void)
{


    TimerIntClear(TIMER0_BASE,TIMER_CAPA_EVENT);
    TimeLater=TimerValueGet(TIMER0_BASE,TIMER_A) ;

 

}

void InitTimer()
{
    SysCtlPeripheralEnable(SYSCTL_PERIPH_TIMER0); // Timer0

    SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOB);//PORTB for timer0

    GPIOPinConfigure(GPIO_PB6_T0CCP0 ); // configure PB6 for Timer0A
      GPIOPinTypeGPIOInput(GPIO_PORTB_BASE, GPIO_PIN_6);
     GPIOPinTypeTimer(GPIO_PORTB_BASE, GPIO_PIN_6);//PB6 for timer0
    TimerConfigure(TIMER0_BASE,TIMER_CFG_A_CAP_TIME_UP|TIMER_CFG_SPLIT_PAIR);
   TimerControlEvent(TIMER0_BASE,TIMER_A,TIMER_EVENT_BOTH_EDGES);
   if(GPIOPinRead(GPIO_PORTB_BASE, GPIO_PIN_6)==24)
     { 

         TimeBeginning=TimerValueGet(TIMER0_BASE,TIMER_A) ;

     }

  TimerLoadSet(TIMER0_BASE, TIMER_A, 1000);
     TimerIntRegister(TIMER0_BASE,TIMER_A,Timer0IntHandler);
    IntMasterEnable();
    TimerIntEnable(TIMER0_BASE, TIMER_CAPA_EVENT);
    TimerControlEvent(TIMER0_BASE,TIMER_A,TIMER_EVENT_NEG_EDGE);
    IntEnable(INT_TIMER0A);

   TimerEnable(TIMER0_BASE , TIMER_A);

    }

 

这样配置定时器:

使用Timer0 的PB6引脚作为方波输入引脚。

配置定时器向上计数。设置初值1000

定时器设置为上升沿触发,检测到上升沿时,获取定时器的值(使用TimeValueGet()),作为方波高电平起点值。

中断注册为下降沿触发。中断函数里获取定时器的值,作为方波高电平终点值。两者作差得到高电平持续时间。

但是,我在运行程序时view expressions,发现只有高电平起点的定时器值一直为零。附图如下,这是为什么

  • 工程师大牛快进来看看啊

  • 被定时器困扰了好久了

  • if(GPIOPinRead(GPIO_PORTB_BASE, GPIO_PIN_6)==24)//24 0X18 PIN4 AND PIN5 
       {
     
           TimeBeginning=TimerValueGet(TIMER0_BASE,TIMER_A) ;
     
       }
    这个函数永远进不去,TimeBeginning的值永远是初始化值。
  • #include <stdint.h>
    #include <stdbool.h>
    #include "inc/hw_memmap.h"
    #include "inc/hw_types.h"
    #include "inc/hw_ints.h"
    #include "driverlib/interrupt.h"
    #include "driverlib/sysctl.h"
    #include "driverlib/uart.h"
    #include "driverlib/gpio.h"
    #include "driverlib/pin_map.h"
    #include "driverlib/timer.h"
    #include "driverlib/pwm.h"
    #include "utils/uartstdio.h"
    
    void Delay_Ns(uint32_t n)
     {
         uint32_t i = 0;
         for(i = n;i > 0;i--)
             SysCtlDelay(SysCtlClockGet() / 3);
     }
    
    unsigned int TimeBeginning,TimeLater,interval;
    int PF1;
    
    void Timer0IntHandler(void)
    {
    
    
        TimerIntClear(TIMER0_BASE,TIMER_CAPA_EVENT);
        TimeLater=TimerValueGet(TIMER0_BASE,TIMER_A) ;
    
    
    
    
    }
     void InitUART()
     {
         SysCtlPeripheralEnable(SYSCTL_PERIPH_UART0);//Enable UART0 module
    
         SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOA);//for UART
         GPIOPinTypeUART(GPIO_PORTA_BASE, GPIO_PIN_0 | GPIO_PIN_1);//PA0&PA1 for UART0
    
         UARTClockSourceSet(UART0_BASE, UART_CLOCK_PIOSC);
       //configure the baud clock source the precision internal oscillator(UART_CLOCK_PIOSC)
         UARTStdioConfig(0, 4800, 16000000);
    
     }
    void InitTimer()
    {
        SysCtlPeripheralEnable(SYSCTL_PERIPH_TIMER0); // Timer0
    
        SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOB);//PORTB for timer0
    
        GPIOPinConfigure(GPIO_PB6_T0CCP0 ); // configure PB6 for Timer0A
     
       GPIOPinTypeGPIOInput(GPIO_PORTB_BASE, GPIO_PIN_6);
     
        GPIOPinTypeTimer(GPIO_PORTB_BASE, GPIO_PIN_6);//PB6 for timer0
    
       TimerConfigure(TIMER0_BASE,TIMER_CFG_A_CAP_TIME_UP|TIMER_CFG_SPLIT_PAIR);
      
      TimerControlEvent(TIMER0_BASE,TIMER_A,TIMER_EVENT_BOTH_EDGES);//both edge to start timing
    
    
    
      TimerLoadSet(TIMER0_BASE, TIMER_A, SysCtlClockGet()/1000);//16000
     
     
    
       TimerIntRegister(TIMER0_BASE,TIMER_A,Timer0IntHandler);
        IntMasterEnable();
        TimerIntEnable(TIMER0_BASE, TIMER_CAPA_EVENT);
        TimerControlEvent(TIMER0_BASE,TIMER_A,TIMER_EVENT_NEG_EDGE);
        IntEnable(INT_TIMER0A);
    
       TimerEnable(TIMER0_BASE , TIMER_A);
       if(GPIOPinRead(GPIO_PORTB_BASE, GPIO_PIN_6)==0x40)
            {
           UARTprintf("find pos edge");//to inspect whether this program ever entre  if{}
                TimeBeginning=TimerValueGet(TIMER0_BASE,TIMER_A) ;
    
            }
    
        }
    
    
    void InitPWM()
    {
            SysCtlPWMClockSet(SYSCTL_PWMDIV_1);
            SysCtlPeripheralEnable(SYSCTL_PERIPH_PWM1);//使能pwm1外设
            SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOF);//使能PF
            GPIOPinConfigure(GPIO_PF1_M1PWM5 );//设置端口复用,
            GPIOPinTypePWM(GPIO_PORTF_BASE, GPIO_PIN_1);//设置端口的pwm输出功能
    
            PWMGenConfigure(PWM1_BASE, PWM_GEN_2, PWM_GEN_MODE_DOWN | PWM_GEN_MODE_NO_SYNC);//下降计数,非同步
            PWMGenPeriodSet(PWM1_BASE, PWM_GEN_2, 640);//0.1MHz
    
            PWMPulseWidthSet(PWM1_BASE, PWM_OUT_5, 320);//占空比50%,脉冲宽度320
            PWMOutputState(PWM1_BASE, PWM_OUT_5_BIT, true);
            PWMGenEnable(PWM1_BASE, PWM_GEN_2);
            PF1=GPIOPinRead(GPIO_PORTF_BASE,GPIO_PIN_1);
            }
    /*
     *PB6 for timer0
     * trigPWM PF1
     * echo PB6
     * UART0 PA0 & PA1
     * */
        int main(void)
       {
          uint32_t distance;
            SysCtlClockSet(SYSCTL_SYSDIV_1|SYSCTL_USE_OSC|SYSCTL_OSC_MAIN|SYSCTL_XTAL_16MHZ);
    
            InitPWM();
            InitUART();
            UARTprintf("Hello ,measurement starts->\n");
    
    
    
    
    
    
    
    while(1)
    {
    
                InitTimer();
    
           distance=0;
    
           interval=TimeLater-TimeBeginning;
            UARTprintf("The time is %d:\n",interval);
           distance=1.0/interval*17000;
    
            UARTprintf("The distance is %d\n:",distance);
            Delay_Ns(3);
    
    
    
    
    
    }
     }
    
    

    TimeBeginning还是0,没有发生任何变化。UART,PWM配置正确。求解问题到底出在哪里