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的定时器测高电平持续时间

#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"



unsigned int interval=0;

void Timer0IntHandler(void)
{

    UARTprintf("Entre interrupts");
    TimerIntClear(TIMER0_BASE,TIMER_CAPA_EVENT);
    interval=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 Timer0
    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_POS_EDGE);//上升沿触发
   //TimerIntRegister(TIMER0_BASE,TIMER_A,Timer0IntHandler);
    IntMasterEnable();
    TimerIntEnable(TIMER0_BASE, TIMER_CAPA_EVENT);
    IntEnable(INT_TIMER0A);


   TimerEnable(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);
        }
/*
 *PB6 for timer0
 * trigPWM PF1
 * echo PB6
 * UART0 PA0 & PA1
 * */
    int main(void)
   {
        SysCtlClockSet(SYSCTL_SYSDIV_5|SYSCTL_USE_OSC|SYSCTL_OSC_MAIN|SYSCTL_XTAL_16MHZ);

        InitUART();
        UARTprintf("Hello ,measurement starts->\n");

        InitPWM();
        InitTimer();
     //   UARTprintf("pass inittimer->\n");
       



while(1)
{




        UARTprintf("The time is %d:\n",interval);
   




}
 }


超声波测距传感器为hc-sr04。目前程序先获得高电平时间。思路:PF1产生高电平为20us的pwm触发传感器发射超声波,此时echo端将会产生高电平,定时器设置为上升沿触发,设置下降沿触发中断,进入中断后,使用TimerValueGet();获取当前timer的值,即高电平持续时间,使用uart发回电脑使用串口助手查看。我的思路是不是正确?为什么根本就进入不了中断。定时器配置出问题了吗。串口助手可以正常查看部分提示内容,说明uart配置正确。pwm从PF1输出,开发板上的红灯亮,说明pwm也没问题。被TM4的定时器折磨不浅。求解脱