Because of the Thanksgiving holiday in the U.S., TI E2E™ design support forum responses may be delayed the week of November 23.

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.

CCS/TM4C123GH6PM: Calculate time duration while pin is high

Part Number: TM4C123GH6PM
Other Parts Discussed in Thread: EK-TM4C1294XL, EK-TM4C123GXL

Tool/software: Code Composer Studio

Hello,

Im try to calculate time duration when pin is on high level. My idea is to use interrupt function which will indicate when pin become high and same interrupt function will indicate when pin go back to low level. I will collect information about time in buffer (first element is time when pin become high and second element is time which is time when pin back to low level). For that purpose I create timer which every 1uS increase variable Microseconds. So for some reason when i disconnect wire from pin, program go in interrupt function some times(program go once in interrupt function). When pin is connected to device (HCSR 04) program always go 2 times in interrupt function whitout calling and then work properly. So my question is what can cause that unpredictable behavior?

/*
 * HCSR04.c
 *
 *  Created on: Dec 11, 2019
 *      Author: suba
 */
#include "Standard_Library.h"
#include "TM4C123GH6PM_API.h"
#include "HCSR04.h"

//*************************************** HARDWARE CONNECTION *****************************************************
//
// PE3 - Trigger pin - ORANGE
// PE4 - Echo pin - YELLOW
//
//*****************************************************************************************************************

volatile double DistanceInCentimeters = 0;
volatile uint32_t BufferForTimes[2];
volatile uint32_t TimeHCSR04 = 0; // timer precision is 1uS
volatile uint8_t EchoPinHowManyTimesChangedState = 0;
volatile uint32_t MicroSeconds = 0;

//****************************************************************************************************************
//
// Configure peripherals for HCSR04
//
//****************************************************************************************************************
void HCSR04_Initialize(void)
{
    //
    // Configure TRIGGER and ECHO pins
    //
    SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOE);

    GPIOPinTypeGPIOOutput(GPIO_PORTE_BASE, TRIGGER_PIN);
    GPIOPinTypeGPIOOutput(GPIO_PORTE_BASE, ECHO_PIN);
    GPIOPinTypeGPIOOutput(GPIO_PORTE_BASE, ~TRIGGER_PIN);
    GPIOPinWrite(GPIO_PORTE_BASE, ECHO_PIN, ~ECHO_PIN);

    GPIOPinTypeGPIOInput(GPIO_PORTE_BASE, ECHO_PIN);
    GPIOPadConfigSet(GPIO_PORTE_BASE, ECHO_PIN, GPIO_STRENGTH_2MA, GPIO_PIN_TYPE_STD_WPD);  //  pull down resistor , 2mA max

    IntPrioritySet(INT_GPIOE, 1);
    GPIOIntEnable(GPIO_PORTE_BASE, ECHO_PIN);
    GPIOIntTypeSet(GPIO_PORTE_BASE, ECHO_PIN, GPIO_BOTH_EDGES);
    IntRegister(INT_GPIOE, HCSR04_Read_Distance_Int_Handler);
    IntEnable(INT_GPIOE);
    //
    // Configure timer
    //
    SysCtlPeripheralEnable(SYSCTL_PERIPH_TIMER2);

    TimerConfigure(TIMER2_BASE, TIMER_CFG_PERIODIC);
    TimerLoadSet(TIMER2_BASE, TIMER_A, SysCtlClockGet() / 1000000); // 1uS
    TimerEnable(TIMER2_BASE, TIMER_A);

    IntPrioritySet(INT_TIMER5A, 4);
    TimerIntEnable(TIMER2_BASE, TIMER_TIMA_TIMEOUT);
    TimerIntRegister(TIMER2_BASE, TIMER_A, Microseconds_Int_Handler);
    IntEnable(INT_TIMER2A);
}
//****************************************************************************************************************
//
// Increase variable MicroSeconds for 1 every 1uS
//
//****************************************************************************************************************v
void Microseconds_Int_Handler(void)
{
    uint32_t Timer2InterruptStatus = 0;
    Timer2InterruptStatus = TimerIntStatus(TIMER2_BASE, true);
    TimerIntClear(TIMER2_BASE, Timer2InterruptStatus);

    MicroSeconds++;
}
//****************************************************************************************************************
//
// Trigger HCSR04
// When use this function in burst mode use more then 60mS delay between 2 shoots (Typical delay ~ 75mS )
//
//****************************************************************************************************************
void Trigger_HCSR04(void)
{
    GPIOPinWrite(GPIO_PORTE_BASE, TRIGGER_PIN, TRIGGER_PIN);
    SysCtlDelay(570/3); // provide 10uS high on trigger pin
    GPIOPinWrite(GPIO_PORTE_BASE, TRIGGER_PIN, ~TRIGGER_PIN);
}
//****************************************************************************************************************
//
// This function will be called 2 times for 1 measurement
// Function will be called first time when is detected high on ECHO_PIN
// Function will be called second time when is detected low on ECHO_PIN
// Each time when function is called, collect info about time and move that value in BufferForTimes[]
// Difference between two function calls is time which is used for calculating distance
//
//****************************************************************************************************************
void HCSR04_Read_Distance_Int_Handler(void)
{
    uint32_t FlagStatus = 0;
    FlagStatus = GPIOIntStatus(GPIO_PORTE_BASE, true);
    GPIOIntClear(GPIO_PORTE_BASE, FlagStatus);

    BufferForTimes[EchoPinHowManyTimesChangedState] = MicroSeconds;
    EchoPinHowManyTimesChangedState++;
    //
    // Check is limit reached
    // If this function is called two times then distance can be calculated
    // Reset counter to 0 which count how many times is function called
    //
   if(EchoPinHowManyTimesChangedState == ECHO_PIN_HOW_MANY_TIMES_CHANGE_STATE_LIMIT) 
    {
        EchoPinHowManyTimesChangedState = 0;
        TimeHCSR04 = BufferForTimes[1] - BufferForTimes[0];
        DistanceInCentimeters = (SPEED_OF_SOUND * TimeHCSR04) / 2; // formula for calculating distance
    }
}

int j = 0;
void main(void)
{
    SysCtlClockSet(SYSCTL_USE_PLL | SYSCTL_OSC_MAIN | SYSCTL_XTAL_16MHZ | SYSCTL_SYSDIV_4); // clock set 50MHz
    IntMasterEnable();
    HCSR04_Initialize();
    while(1)
    {
        if(j == 1)
        {
            Trigger_HCSR04();
        }

        SysCtlDelay(1200000); //72mS delay
    }
}

  • GPIOPinTypeGPIOOutput(GPIO_PORTE_BASE, ~TRIGGER_PIN); need to be change in GPIOPinWrite(GPIO_PORTE_BASE, TRIGGER_PIN, ~TRIGGER_PIN);

    This nothing change, still need help

  • Hello,

    I had just copied the exact code issue you've (just) detected.

    May I suggest that your use of, 'Separate pins (and interrupts) - one to detect (only) signal Rise - and a second to detect (only) signal Fall - often provides much needed clarity & insight.    (as it is a 'KISS' technique.)

    Similarly - your use of separate (and dedicated Timers) - mated to each 'pin registering the specific interrupt' - aids your code construction and especially - its analysis.

    I'm boarding a flight shortly - haven't the time now to 'dig deeper' into your code.    

    Most always - by breaking your objective into 'Small, clearly Measurable Parts, and then testing & verifying each 'small part' - and only then combining - you will (faster & easier) achieve your coding objective...

  • Hello Milan,

    This is an example we have put together for the EK-TM4C1294XL. The same principle applies for the EK-TM4C123GXL as well. See the following example code:

    //*****************************************************************************
    //
    // timer_edge_capture.c - Example for how to measure a pulsed signal with the
    //                        timer peripheral.
    //
    // Copyright (c) 2019 Texas Instruments Incorporated.  All rights reserved.
    //
    //*****************************************************************************
    
    #include <stdint.h>
    #include <stdbool.h>
    #include <math.h>
    #include "inc/hw_ints.h"
    #include "inc/hw_memmap.h"
    #include "inc/hw_timer.h"
    #include "inc/hw_types.h"
    #include "driverlib/debug.h"
    #include "driverlib/gpio.h"
    #include "driverlib/interrupt.h"
    #include "driverlib/pin_map.h"
    #include "driverlib/rom.h"
    #include "driverlib/rom_map.h"
    #include "driverlib/sysctl.h"
    #include "driverlib/timer.h"
    #include "driverlib/uart.h"
    #include "utils/uartstdio.h"
    
    //*****************************************************************************
    //
    //! \addtogroup example_list
    //! <h1>Timer Edge Capture Demo (timer_edge_capture)</h1>
    //!
    //! This example demonstrates how to use two timers to determine the duration
    //! of the high period of an input signal.  The same logic can be applied to
    //! also measure the low period.  The example uses Timer 0 in Split Mode and
    //! the 8-bit prescaler to create two 24-bit wide timers.  There are some
    //! limitations with this method to be aware of.  The limitation on the lower
    //! end is if a signal is too fast to sample with the timers due to the time to
    //! execute code.  The limitation on the upper end is if the signal period is
    //! longer than 139.8 milliseconds at which point the 24-bit timer would
    //! overflow.  That issue can be worked around by using two 32-bit timers which
    //! would allow measuring a signal up to 35.79 seconds long.
    //!
    //! To test this example either input a square wave to PL4 and PL5 with a
    //! signal generator or leverage a second LaunchPad running a TivaWare PWM
    //! project such as pwm_interrupt.
    //!
    //! This example uses the following peripherals and I/O signals.
    //! - GPIO Port L peripheral (for TIMER0 pins)
    //! - T0CCP0 - PL4
    //! - T0CCP1 - PL5
    //!
    //! UART0, connected to the Virtual Serial Port and running at 115,200, 8-N-1,
    //! is used to display messages from this application.
    //
    //*****************************************************************************
    
    //****************************************************************************
    //
    // The variable g_ui32SysClock contains the system clock frequency in Hz.
    //
    //****************************************************************************
    uint32_t g_ui32SysClock;
    
    //****************************************************************************
    //
    // Variables to store Timer values from the ISR, must be volatile.
    //
    //****************************************************************************
    volatile uint32_t g_ui32HighStartCount;
    volatile uint32_t g_ui32HighEndCount;
    
    //****************************************************************************
    //
    // Flag to indicate if an interrupt occurred, must be volatile.
    //
    //****************************************************************************
    volatile bool g_bIntFlag = false;
    
    //*****************************************************************************
    //
    // The error routine that is called if the driver library encounters an error.
    //
    //*****************************************************************************
    #ifdef DEBUG
    void
    __error__(char *pcFilename, uint32_t ui32Line)
    {
    }
    #endif
    
    //*****************************************************************************
    //
    // The interrupt handler for the Timer 0A interrupt.
    //
    //*****************************************************************************
    void
    Timer0AIntHandler(void)
    {
        //
        // Clear the timer interrupt.
        //
        MAP_TimerIntClear(TIMER0_BASE, TIMER_CAPA_EVENT);
    
        //
        // Store the end time.  In Edge Time Mode, the prescaler is used for the
        // the most significant bits.  Therefore, it must be shifted by 16 before
        // being added onto the final value.
        //
        g_ui32HighStartCount = (MAP_TimerValueGet(TIMER0_BASE, TIMER_A)) +
                               (MAP_TimerPrescaleGet(TIMER0_BASE, TIMER_A) << 16);
    }
    
    //*****************************************************************************
    //
    // The interrupt handler for the Timer 0B interrupt.
    //
    //*****************************************************************************
    void
    Timer0BIntHandler(void)
    {
        //
        // Clear the timer interrupt.
        //
        MAP_TimerIntClear(TIMER0_BASE, TIMER_CAPB_EVENT);
    
        //
        // Store the end time.  In Edge Time Mode, the prescaler is used for the
        // the most significant bits.  Therefore, it must be shifted by 16 before
        // being added onto the final value.
        //
        g_ui32HighEndCount = (MAP_TimerValueGet(TIMER0_BASE, TIMER_B)) +
                             (MAP_TimerPrescaleGet(TIMER0_BASE, TIMER_B) << 16);
    
        //
        // Set the global flag to indicate that new timer values have been stored.
        // This is only done on Timer B as the measurement is tracking the high
        // period of the signal, so Timer A handles the rising edge at the start
        // of the measurement and Timer B handles the falling edge at the end of
        // the measurement.  Therefore, once Timer B is triggered, the application
        // can now calculate the high period of the signal.
        //
        g_bIntFlag = true;
    }
    
    //*****************************************************************************
    //
    // Configure the UART and its pins.  This must be called before UARTprintf().
    //
    //*****************************************************************************
    void
    ConfigureUART(void)
    {
        //
        // Enable the GPIO Peripheral used by the UART.
        //
        MAP_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOA);
    
        //
        // Enable UART0.
        //
        MAP_SysCtlPeripheralEnable(SYSCTL_PERIPH_UART0);
    
        //
        // Configure GPIO Pins for UART mode.
        //
        MAP_GPIOPinConfigure(GPIO_PA0_U0RX);
        MAP_GPIOPinConfigure(GPIO_PA1_U0TX);
        MAP_GPIOPinTypeUART(GPIO_PORTA_BASE, GPIO_PIN_0 | GPIO_PIN_1);
    
        //
        // Initialize the UART for console I/O.
        //
        UARTStdioConfig(0, 115200, g_ui32SysClock);
    }
    
    //*****************************************************************************
    //
    // Configure the system clock, UART, and setup Timer0 to be a Split Timer
    // which uses the 8-bit prescaler to create two 24-bit timers to measure signal
    // high periods based on rising and falling edge events on the Timer pins PL4
    // and PL5.  The measurement results will be output on UART0 to the Virtual
    // Serial Port.
    //
    //*****************************************************************************
    int
    main(void)
    {
        uint32_t ui32HighPeriod;
    
        //
        // Run from the PLL at 120 MHz.
        //
        g_ui32SysClock = MAP_SysCtlClockFreqSet((SYSCTL_XTAL_25MHZ |
                                                 SYSCTL_OSC_MAIN |
                                                 SYSCTL_USE_PLL |
                                                 SYSCTL_CFG_VCO_480), 120000000);
    
        //
        // Enable the peripherals used by this example.
        //
        MAP_SysCtlPeripheralEnable(SYSCTL_PERIPH_TIMER0);
        MAP_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOL);
    
        //
        // Configure the GPIO to be CCP pins for the Timer peripheral.
        //
        MAP_GPIOPinConfigure(GPIO_PL4_T0CCP0);
        MAP_GPIOPinConfigure(GPIO_PL5_T0CCP1);
    
        //
        // Configure the GPIO for the Timer peripheral.
        //
        MAP_GPIOPinTypeTimer(GPIO_PORTL_BASE, GPIO_PIN_4 | GPIO_PIN_5);
    
        //
        // Initialize the UART and write initial status.
        //
        ConfigureUART();
        UARTprintf("Edge Capture Example for measuring the high period of an"
                   "input signal.\n");
        UARTprintf("Input a square wave onto pins PL4 and PL5 of the "
                   "EK-TM4C1294XL.\n");
    
        //
        // Initialize Timers A and B to both run as periodic up-count edge capture
        // timers.  This will split the 32-bit timer into two 16-bit timers.
        //
        MAP_TimerConfigure(TIMER0_BASE, (TIMER_CFG_SPLIT_PAIR |
                                         TIMER_CFG_A_PERIODIC |
    				     TIMER_CFG_A_CAP_TIME_UP |
                                         TIMER_CFG_B_PERIODIC |
    				     TIMER_CFG_B_CAP_TIME_UP));
    
        //
        // To use the timer in Edge Time mode, it must be preloaded with initial
        // values.  If the prescaler is used, then it must be preloaded as well.
        // Since we want to use all 24-bits for both timers it will be loaded with
        // the maximum of 0xFFFF for the 16-bit wide split timers, and 0xFF to add
        // the additional 8-bits to the split timers with the prescaler.
        //
        MAP_TimerLoadSet(TIMER0_BASE, TIMER_BOTH, 0xFFFF);
        MAP_TimerPrescaleSet(TIMER0_BASE, TIMER_BOTH, 0xFF);
    
        //
        // Configure Timer A to trigger on a Positive Edge and configure Timer B
        // to trigger on a Negative Edge.
        //
        MAP_TimerControlEvent(TIMER0_BASE, TIMER_A, TIMER_EVENT_POS_EDGE);
        MAP_TimerControlEvent(TIMER0_BASE, TIMER_B, TIMER_EVENT_NEG_EDGE);
    
        //
        // Clear the interrupt status flag.  This is done to make sure the
        // interrupt flag is cleared before we enable it.
        //
        MAP_TimerIntClear(TIMER0_BASE, TIMER_CAPA_EVENT | TIMER_CAPB_EVENT);
    
        //
        // Enable the Timer A and B interrupts for Capture Events.
        //
        MAP_TimerIntEnable(TIMER0_BASE, TIMER_CAPA_EVENT | TIMER_CAPB_EVENT);
    
        //
        // Enable the interrupts for Timer A and Timer B on the processor (NVIC).
        //
        MAP_IntEnable(INT_TIMER0A);
        MAP_IntEnable(INT_TIMER0B);
    
        //
        // Enable processor interrupts.
        //
        MAP_IntMasterEnable();
    
        //
        // Enable both Timer A and Timer B to begin the application.
        //
        MAP_TimerEnable(TIMER0_BASE, TIMER_BOTH);
    
        while(1)
        {
            //
            // Wait until the Timer B interrupt has occurred.
            //
            if (g_bIntFlag)
            {
                //
                // Simple check to avoid overflow cases.  The End Count is the
                // second measurement taken and therefore should never be smaller
                // than the Start Count unless the timer has overflowed.  If that
                // occurs, then add 2^24-1 to the End Count before subtraction.
                //
                if (g_ui32HighEndCount > g_ui32HighStartCount)
                {
                    ui32HighPeriod = g_ui32HighEndCount - g_ui32HighStartCount;
                }
                else
                {
                    ui32HighPeriod =
                            (g_ui32HighEndCount + 16777215) - g_ui32HighStartCount;
                }
    
                //
                // Now that the results have been processed, toggle the interrupt
                // flag back to false to wait for the next measurement pair to be
                // done.  This is done before the UART output in case the timers
                // are triggered while the application is still running.
                //
                g_bIntFlag = false;
    
                //
                // The System Clock speed is needed to calculate the period length
                // of the input signal.  Dividing by the system clock gives the
                // period in seconds.  Multiplying by 10^6 then shifts the result
                // to microseconds to simplify the UART output of the result.
                //
                ui32HighPeriod /= (g_ui32SysClock / 1000000);
    
                //
                // Output the result of the measured input signal high period.
                //
                UARTprintf("Input Signal Period = %d microseconds\n",
                           ui32HighPeriod);
            }
        }
    }