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.

I'm trying to read 6 ultrasonic sensors with Tiva TM4C. Can someone help me with efficiency please?

I'm currently using a code I found online by Luis Rafael on  where he uses a timer to measure the echo. I am trying to read 6 different ultrasonic sensors and tried to use 6 different timers. Basically all of the timers on the board. I realize this is an incredibly inefficient idea and was wondering what other things I might be able to try. 

I was thinking about letting a single timer run and just take the time from falling edge and substract the time from rising edge to it. All the while making sure that the timer for falling edge is always a higher number than the one for rising edge. To do this I was thinking of making an if statement which would add the highest value of the timer to the falling edge time if it was smaller than the rising edge time. Is there anything else I should account for?

Thank you,

Luis Alberto Vergara-Rodriguez

  while(1)
  {

    //Checks if a pulse read is in progress
    if((echowait_A2 & echowait_A5 & echowait_A6 & echowait_A7 & echowait_D0 & echowait_D3) != 1){
      //Does the required pulse of 10uS
      GPIOPinWrite(GPIO_PORTA_BASE, GPIO_PIN_3, GPIO_PIN_3);
      SysCtlDelay(266);
      GPIOPinWrite(GPIO_PORTA_BASE, GPIO_PIN_3, ~GPIO_PIN_3);

      //Converts the counter value to cm.
      pulse_A2 =(uint32_t)(temp * pulse_A2);
      pulse_A5 =(uint32_t)(temp * pulse_A5);
      pulse_A6 =(uint32_t)(temp * pulse_A6);
      pulse_A7 =(uint32_t)(temp * pulse_A7);
      pulse_D0 =(uint32_t)(temp * pulse_D0);
      pulse_D3 =(uint32_t)(temp * pulse_D3);
      pulse_A2 = pulse_A2 / 58;
      pulse_A5 = pulse_A5 / 58;
      pulse_A6 = pulse_A6 / 58;
      pulse_A7 = pulse_A7 / 58;
      pulse_D0 = pulse_D0 / 58;
      pulse_D3 = pulse_D3 / 58;

      //Prints out the distance measured.
      UARTprintf("A2 = %2dcm  A5 = %2dcm  A6 = %2dcm  A7 = %2dcm  D0 = %2dcm  D3 = %2dcm  \n" , pulse_A2,pulse_A5,pulse_A6,pulse_A7,pulse_D3,pulse_D0);
    }
      //wait about 10ms until the next reading.
      SysCtlDelay(400000);


  }
}
void inputInt(){
    uint32_t status_A=0;
    uint32_t status_D=0;

    status_A = GPIOIntStatus(GPIO_PORTA_BASE,true);
    status_D = GPIOIntStatus(GPIO_PORTD_BASE,true);
    GPIOIntClear(GPIO_PORTA_BASE,status_A);
    GPIOIntClear(GPIO_PORTD_BASE,status_D);

    if( (status_A & GPIO_INT_PIN_2) == GPIO_INT_PIN_2){
      //Then there was a port A pin2 interrupt
          /*
            If it's a rising edge then set he timer to 0
            It's in periodic mode so it was in some random value
          */
          if ( GPIOPinRead(GPIO_PORTA_BASE, GPIO_PIN_2) == GPIO_PIN_2){
            HWREG(TIMER2_BASE + TIMER_O_TAV) = 0; //Loads value 0 into the timer.
            TimerEnable(TIMER2_BASE,TIMER_A);
            echowait_A2=1;
          }
          /*
            If it's a falling edge that was detected, then get the value of the counter
          */
          else{
            pulse_A2 = TimerValueGet(TIMER2_BASE,TIMER_A); //record value
            TimerDisable(TIMER2_BASE,TIMER_A);
            echowait_A2=0;
          }

    }

    if( (status_A & GPIO_INT_PIN_5) == GPIO_INT_PIN_5){
      //Then there was a port A pin5 interrupt
          /*
            If it's a rising edge then set he timer to 0
            It's in periodic mode so it was in some random value
          */
          if ( GPIOPinRead(GPIO_PORTA_BASE, GPIO_PIN_5) == GPIO_PIN_5){
            HWREG(TIMER3_BASE + TIMER_O_TAV) = 0; //Loads value 0 into the timer.
            TimerEnable(TIMER3_BASE,TIMER_A);
            echowait_A5=1;
          }
          /*
            If it's a falling edge that was detected, then get the value of the counter
          */
          else{
            pulse_A5 = TimerValueGet(TIMER3_BASE,TIMER_A); //record value
            TimerDisable(TIMER3_BASE,TIMER_A);
            echowait_A5=0;
          }

    }
    if( (status_A & GPIO_INT_PIN_6) == GPIO_INT_PIN_6){
      //Then there was a port A pin6 interrupt
          /*
            If it's a rising edge then set he timer to 0
            It's in periodic mode so it was in some random value
          */
          if ( GPIOPinRead(GPIO_PORTA_BASE, GPIO_PIN_6) == GPIO_PIN_6){
            HWREG(TIMER4_BASE + TIMER_O_TAV) = 0; //Loads value 0 into the timer.
            TimerEnable(TIMER4_BASE,TIMER_A);
            echowait_A6=1;
          }
          /*
            If it's a falling edge that was detected, then get the value of the counter
          */
          else{
            pulse_A6 = TimerValueGet(TIMER4_BASE,TIMER_A); //record value
            TimerDisable(TIMER4_BASE,TIMER_A);
            echowait_A6=0;
          }

    }

    if( (status_A & GPIO_INT_PIN_7) == GPIO_INT_PIN_7){
      //Then there was a port A pin7 interrupt
          /*
            If it's a rising edge then set he timer to 0
            It's in periodic mode so it was in some random value
          */
          if ( GPIOPinRead(GPIO_PORTA_BASE, GPIO_PIN_7) == GPIO_PIN_7){
            HWREG(TIMER5_BASE + TIMER_O_TAV) = 0; //Loads value 0 into the timer.
            TimerEnable(TIMER5_BASE,TIMER_A);
            echowait_A7=1;
          }
          /*
            If it's a falling edge that was detected, then get the value of the counter
          */
          else{
            pulse_A7 = TimerValueGet(TIMER5_BASE,TIMER_A); //record value
            TimerDisable(TIMER5_BASE,TIMER_A);
            echowait_A7=0;
          }

    }
    if( (status_D & GPIO_INT_PIN_0) == GPIO_INT_PIN_0){
      //Then there was a port D pin0 interrupt
          /*
            If it's a rising edge then set he timer to 0
            It's in periodic mode so it was in some random value
          */
          if ( GPIOPinRead(GPIO_PORTD_BASE, GPIO_PIN_0) == GPIO_PIN_0){
            HWREG(TIMER0_BASE + TIMER_O_TAV) = 0; //Loads value 0 into the timer.
            TimerEnable(TIMER0_BASE,TIMER_A);
            echowait_D0=1;
          }
          /*
            If it's a falling edge that was detected, then get the value of the counter
          */
          else{
            pulse_D0 = TimerValueGet(TIMER0_BASE,TIMER_A); //record value
            TimerDisable(TIMER0_BASE,TIMER_A);
            echowait_D0=0;
          }

    }

    if( (status_D & GPIO_INT_PIN_3) == GPIO_INT_PIN_3){
      //Then there was a port D pin3 interrupt
          /*
            If it's a rising edge then set he timer to 0
            It's in periodic mode so it was in some random value
          */
          if ( GPIOPinRead(GPIO_PORTA_BASE, GPIO_PIN_3) == GPIO_PIN_3){
            HWREG(TIMER1_BASE + TIMER_O_TAV) = 0; //Loads value 0 into the timer.
            TimerEnable(TIMER1_BASE,TIMER_A);
            echowait_D3=1;
          }
          /*
            If it's a falling edge that was detected, then get the value of the counter
          */
          else{
            pulse_D3 = TimerValueGet(TIMER1_BASE,TIMER_A); //record value
            TimerDisable(TIMER1_BASE,TIMER_A);
            echowait_D3=0;
          }

    }

}
void Captureinit(){
  /*
    Set the timer to be periodic.
  */
  SysCtlPeripheralEnable(SYSCTL_PERIPH_TIMER0);
  SysCtlDelay(3);
  TimerConfigure(TIMER0_BASE, TIMER_CFG_PERIODIC_UP);
  TimerEnable(TIMER0_BASE,TIMER_A);
  SysCtlPeripheralEnable(SYSCTL_PERIPH_TIMER1);
  SysCtlDelay(3);
  TimerConfigure(TIMER1_BASE, TIMER_CFG_PERIODIC_UP);
  TimerEnable(TIMER1_BASE,TIMER_A);
  SysCtlPeripheralEnable(SYSCTL_PERIPH_TIMER2);
  SysCtlDelay(3);
  TimerConfigure(TIMER2_BASE, TIMER_CFG_PERIODIC_UP);
  TimerEnable(TIMER2_BASE,TIMER_A);
  SysCtlPeripheralEnable(SYSCTL_PERIPH_TIMER3);
  SysCtlDelay(3);
  TimerConfigure(TIMER3_BASE, TIMER_CFG_PERIODIC_UP);
  TimerEnable(TIMER3_BASE,TIMER_A);
  SysCtlPeripheralEnable(SYSCTL_PERIPH_TIMER4);
  SysCtlDelay(3);
  TimerConfigure(TIMER4_BASE, TIMER_CFG_PERIODIC_UP);
  TimerEnable(TIMER4_BASE,TIMER_A);
  SysCtlPeripheralEnable(SYSCTL_PERIPH_TIMER5);
  SysCtlDelay(3);
  TimerConfigure(TIMER5_BASE, TIMER_CFG_PERIODIC_UP);
  TimerEnable(TIMER5_BASE,TIMER_A);
}

  • Hi Luis,

    That was just a really simple example. I assume you are using a sonar kinda like SRF04.

    If you are referring to this code:

    Then it should be very simple.
    First 1 question:
    Do you want to read all the sonars at the same time or one after the other? (note that firing multiple sonars at the same time can cause really bad readings because of echos from one to another).

    If it's 1 after another you could simply use 1 GPIO output for each Trigger pin and then just 1 GPIO input for all the 6 Echo pins. Why? Because if you can control which sonar is firing and sending data to you so you know which sonar is sending you info to the input.
    Other option is use 6 GPIO pins from the same GPIO peripheral (still only works 1 at a time). The interrupts from the all the GPIO pins in the same GPIO peripheral call the same interrupt.

    If you need to read all at the same time it's a bit more complicated.


    Edit:
    Which board are you using? You should be able to use the 32/16bit timers in split mode so you have 12 16bit timers. Though if you have the Tiva running at high speeds errors could give out the maximum distance (equivalent to a 18ms pulse) which would cause a overflow. 16bits should be enough for most pratical values (too long distances starts to give inaccurate values anyway).
    If you also take into account the wide timers you have + 12 32bit timers

  • Luis Vergara said:
    I was thinking about letting a single timer run and just take the time from falling edge and substract the time from rising edge to it. All the while making sure that the timer for falling edge is always a higher number than the one for rising edge. To do this I was thinking of making an if statement which would add the highest value of the timer to the falling edge time if it was smaller than the rising edge time. Is there anything else I should account for?



    Now analyzing what you are trying here now that I have time,

    If I understand correctly you want to have a timer always running and register the value on it every-time a falling edge and a rising edge happens. Then see the difference in pairs.
    Well your suggestion should work since it should not be possible a second timeout.
    Anyway, take into account that if you fire multiple sonars at the same time you can get multiple interrupts firing at the same time from the various echo signals, which could cause inaccurate values, something like the DMA would cause less (not reduce completely) inaccuracy if multiple echos rise or fall at the same time, though there's still a bit of delay since there were gonna be multiple accesses to the timer value.

  • Thank you for the answer, I tried this and it seems to be working fine except for a single detail.

    UARTprintf("A2 = %2dcm A5 = %2dcm A6 = %2dcm A7 = %2dcm D0 = %2dcm D3 = %dcm %d" , pulse_A2,pulse_A5,pulse_A6,pulse_A7,pulse_D0,pulse_D3);

    This line is displaying everything good except for D3 which is giving me an incrementing number. The issue arises with the pulse_D3 being at the end of the UARTprintf line. If I changed the order of the expression, it will cause that specific number to be random. In other words, if I change pulse_D3 to the front and instead put pulse_A2 at the end then pulse_A2 will display random incrementing numbers.

    UARTprintf("A2 = %2dcm A5 = %2dcm A6 = %2dcm A7 = %2dcm D0 = %2dcm D3 = %dcm %d" , pulse_D3,pulse_A5,pulse_A6,pulse_A7,pulse_D0,pulse_A2);

    For example this line will show pulse_D3 correctly but now pulse_A2 shows a completely random number like "3234430" that increments with each pass. (Sometimes even negative)

    Other than that this code is giving me good results. I do realize that this is what you suggested to be reading all sensors at the same time and as far as the interference of the other sensors, well they will be far enough apart to avoid the problem.

    I'm I doing something that is obviously incorrect in this code?

    Thanks.


    #include <stdint.h>
    #include <stdbool.h>
    #include "stdlib.h"
    #include "inc/hw_ints.h"
    #include "inc/hw_memmap.h"
    #include "inc/hw_timer.h"
    #include "inc/hw_uart.h"
    #include "inc/hw_gpio.h"
    #include "inc/hw_pwm.h"
    #include "inc/hw_types.h"
    #include "driverlib/pin_map.h"

    #include "driverlib/timer.h"
    #include "driverlib/gpio.h"
    #include "driverlib/interrupt.h"
    #include "driverlib/rom.h"
    #include "driverlib/rom_map.h"
    #include "driverlib/sysctl.h"
    #include "driverlib/uart.h"
    #include "driverlib/udma.h"
    #include "driverlib/pwm.h"
    #include "driverlib/ssi.h"
    #include "driverlib/systick.h"


    #include "utils/uartstdio.c"
    #include <string.h>


    void inputInt();
    void Captureinit();
    void InitConsole(void);

    //This is to avoid doing the math everytime you do a reading
    const double temp = 1.0/80.0;

    //Stores the pulse length
    volatile uint32_t pulse_A2=0;
    volatile uint32_t pulse_A5=0;
    volatile uint32_t pulse_A6=0;
    volatile uint32_t pulse_A7=0;
    volatile uint32_t pulse_D0=0;
    volatile uint32_t pulse_D3=0;

    //Stores the rising edge time
    volatile uint32_t Port_A_Pin_2_Rising = 0;
    volatile uint32_t Port_A_Pin_5_Rising = 0;
    volatile uint32_t Port_A_Pin_6_Rising = 0;
    volatile uint32_t Port_A_Pin_7_Rising = 0;
    volatile uint32_t Port_D_Pin_0_Rising = 0;
    volatile uint32_t Port_D_Pin_3_Rising = 0;


    //Tells the main code if the a pulse is being read at the moment
    volatile uint8_t echowait_A2=0;
    volatile uint8_t echowait_A5=0;
    volatile uint8_t echowait_A6=0;
    volatile uint8_t echowait_A7=0;
    volatile uint8_t echowait_D0=0;
    volatile uint8_t echowait_D3=0;

    int main()
    {
    //Set system clock to 80Mhz
    SysCtlClockSet(SYSCTL_SYSDIV_2_5|SYSCTL_USE_PLL|SYSCTL_OSC_MAIN|SYSCTL_XTAL_16MHZ);

    //Configures the UART
    InitConsole();

    //Configures the timer
    Captureinit();

    //Configure Trigger pin
    SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOA);
    SysCtlDelay(3);
    GPIOPinTypeGPIOOutput(GPIO_PORTA_BASE, GPIO_PIN_3);

    //Configure Echo pins
    SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOA);
    SysCtlDelay(3);
    GPIOPinTypeGPIOInput(GPIO_PORTA_BASE, GPIO_PIN_2 | GPIO_PIN_5 | GPIO_PIN_6 | GPIO_PIN_7);
    GPIOIntEnable(GPIO_PORTA_BASE, GPIO_PIN_2 | GPIO_PIN_5 | GPIO_PIN_6 | GPIO_PIN_7);
    GPIOIntTypeSet(GPIO_PORTA_BASE, GPIO_PIN_2 | GPIO_PIN_5 | GPIO_PIN_6 | GPIO_PIN_7,GPIO_BOTH_EDGES);
    GPIOIntRegister(GPIO_PORTA_BASE,inputInt);
    SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOD);
    SysCtlDelay(3);
    GPIOPinTypeGPIOInput(GPIO_PORTD_BASE, GPIO_PIN_0 | GPIO_PIN_3);
    GPIOIntEnable(GPIO_PORTD_BASE, GPIO_PIN_0 | GPIO_PIN_3);
    GPIOIntTypeSet(GPIO_PORTD_BASE, GPIO_PIN_0 | GPIO_PIN_3, GPIO_BOTH_EDGES);
    GPIOIntRegister(GPIO_PORTD_BASE,inputInt);


    while(1)
    {

    //Checks if a pulse read is in progress
    if((echowait_A2 & echowait_A5 & echowait_A6 & echowait_A7 & echowait_D0 & echowait_D3) != 1){
    //Does the required pulse of 10uS
    GPIOPinWrite(GPIO_PORTA_BASE, GPIO_PIN_3, GPIO_PIN_3);
    SysCtlDelay(266);
    GPIOPinWrite(GPIO_PORTA_BASE, GPIO_PIN_3, ~GPIO_PIN_3);

    //Converts the counter value to cm.
    pulse_A2 =(uint32_t)(temp * pulse_A2);
    pulse_A5 =(uint32_t)(temp * pulse_A5);
    pulse_A6 =(uint32_t)(temp * pulse_A6);
    pulse_A7 =(uint32_t)(temp * pulse_A7);
    pulse_D0 =(uint32_t)(temp * pulse_D0);
    pulse_D3 =(uint32_t)(temp * pulse_D3);
    pulse_A2 = pulse_A2 / 58;
    pulse_A5 = pulse_A5 / 58;
    pulse_A6 = pulse_A6 / 58;
    pulse_A7 = pulse_A7 / 58;
    pulse_D0 = pulse_D0 / 58;
    pulse_D3 = pulse_D3 / 58;



    //Prints out the distance measured.
    UARTprintf("A2 = %2dcm A5 = %2dcm A6 = %2dcm A7 = %2dcm D0 = %2dcm D3 = %dcm %d" , pulse_A2,pulse_A5,pulse_A6,pulse_A7,pulse_D0,pulse_D3);
    UARTprintf("\n");
    }
    //wait about 10ms until the next reading.
    SysCtlDelay(400000);


    }
    }
    void inputInt(){
    uint32_t status_A=0;
    uint32_t status_D=0;

    status_A = GPIOIntStatus(GPIO_PORTA_BASE,true);
    status_D = GPIOIntStatus(GPIO_PORTD_BASE,true);
    GPIOIntClear(GPIO_PORTA_BASE,status_A);
    GPIOIntClear(GPIO_PORTD_BASE,status_D);

    if( (status_A & GPIO_INT_PIN_2) == GPIO_INT_PIN_2){
    //Then there was a port A pin2 interrupt
    /*
    If it's a rising edge then set he timer to 0
    It's in periodic mode so it was in some random value
    */
    if ( GPIOPinRead(GPIO_PORTA_BASE, GPIO_PIN_2) == GPIO_PIN_2){
    Port_A_Pin_2_Rising = TimerValueGet(TIMER2_BASE,TIMER_A);
    echowait_A2=1;
    }
    /*
    If it's a falling edge that was detected, then get the value of the counter
    */
    else{
    pulse_A2 = TimerValueGet(TIMER2_BASE,TIMER_A); //record value
    echowait_A2=0;
    if ( pulse_A2 < Port_A_Pin_2_Rising )
    pulse_A2 = pulse_A2 + (0xffffffff -1);
    pulse_A2 = pulse_A2 - Port_A_Pin_2_Rising;
    }


    }

    if( (status_A & GPIO_INT_PIN_5) == GPIO_INT_PIN_5){
    //Then there was a port A pin5 interrupt
    /*
    If it's a rising edge then set he timer to 0
    It's in periodic mode so it was in some random value
    */
    if ( GPIOPinRead(GPIO_PORTA_BASE, GPIO_PIN_5) == GPIO_PIN_5){
    Port_A_Pin_5_Rising = TimerValueGet(TIMER2_BASE,TIMER_A);
    echowait_A5=1;
    }
    /*
    If it's a falling edge that was detected, then get the value of the counter
    */
    else{
    pulse_A5 = TimerValueGet(TIMER2_BASE,TIMER_A); //record value
    echowait_A5=0;
    if ( pulse_A5 < Port_A_Pin_2_Rising )
    pulse_A5 = pulse_A5 + (0xffffffff -1);
    pulse_A5 = pulse_A5 - Port_A_Pin_5_Rising;
    }

    }
    if( (status_A & GPIO_INT_PIN_6) == GPIO_INT_PIN_6){
    //Then there was a port A pin6 interrupt
    /*
    If it's a rising edge then set he timer to 0
    It's in periodic mode so it was in some random value
    */
    if ( GPIOPinRead(GPIO_PORTA_BASE, GPIO_PIN_6) == GPIO_PIN_6){
    Port_A_Pin_6_Rising = TimerValueGet(TIMER2_BASE,TIMER_A);
    echowait_A6=1;
    }
    /*
    If it's a falling edge that was detected, then get the value of the counter
    */
    else{
    pulse_A6 = TimerValueGet(TIMER2_BASE,TIMER_A); //record value
    echowait_A6=0;
    if ( pulse_A6 < Port_A_Pin_6_Rising )
    pulse_A6 = pulse_A6 + (0xffffffff -1);
    pulse_A6 = pulse_A6 - Port_A_Pin_6_Rising;
    }

    }

    if( (status_A & GPIO_INT_PIN_7) == GPIO_INT_PIN_7){
    //Then there was a port A pin7 interrupt
    /*
    If it's a rising edge then set he timer to 0
    It's in periodic mode so it was in some random value
    */
    if ( GPIOPinRead(GPIO_PORTA_BASE, GPIO_PIN_7) == GPIO_PIN_7){
    Port_A_Pin_7_Rising = TimerValueGet(TIMER2_BASE,TIMER_A);
    echowait_A7=1;
    }
    /*
    If it's a falling edge that was detected, then get the value of the counter
    */
    else{
    pulse_A7 = TimerValueGet(TIMER2_BASE,TIMER_A); //record value
    echowait_A7=0;
    if ( pulse_A7 < Port_A_Pin_7_Rising )
    pulse_A7 = pulse_A7 + (0xffffffff -1);
    pulse_A7 = pulse_A7 - Port_A_Pin_7_Rising;
    }

    }
    if( (status_D & GPIO_INT_PIN_0) == GPIO_INT_PIN_0){
    //Then there was a port D pin0 interrupt
    /*
    If it's a rising edge then set he timer to 0
    It's in periodic mode so it was in some random value
    */
    if ( GPIOPinRead(GPIO_PORTD_BASE, GPIO_PIN_0) == GPIO_PIN_0){
    Port_D_Pin_0_Rising = TimerValueGet(TIMER2_BASE,TIMER_A);
    echowait_D0=1;
    }
    /*
    If it's a falling edge that was detected, then get the value of the counter
    */
    else{
    pulse_D0 = TimerValueGet(TIMER2_BASE,TIMER_A); //record value
    echowait_D0=0;
    if ( pulse_D0 < Port_D_Pin_0_Rising )
    pulse_D0 = pulse_D0 + (0xffffffff -1);
    pulse_D0 = pulse_D0 - Port_D_Pin_0_Rising;
    }

    }

    if( (status_D & GPIO_INT_PIN_3) == GPIO_INT_PIN_3){
    //Then there was a port D pin3 interrupt
    /*
    If it's a rising edge then set he timer to 0
    It's in periodic mode so it was in some random value
    */
    if ( GPIOPinRead(GPIO_PORTA_BASE, GPIO_PIN_3) == GPIO_PIN_3){
    Port_D_Pin_3_Rising = TimerValueGet(TIMER2_BASE,TIMER_A);
    echowait_D3=1;
    }
    /*
    If it's a falling edge that was detected, then get the value of the counter
    */
    else{
    pulse_D3 = TimerValueGet(TIMER2_BASE,TIMER_A); //record value
    echowait_D3=0;
    if ( pulse_D3 < Port_D_Pin_3_Rising )
    pulse_D3 = pulse_D3 + (0xffffffff -1);
    pulse_D3 = pulse_D3 - Port_D_Pin_3_Rising;
    }

    }

    }
    void Captureinit(){
    /*
    Set the timer to be periodic.
    */
    SysCtlPeripheralEnable(SYSCTL_PERIPH_TIMER2);
    SysCtlDelay(3);
    TimerConfigure(TIMER2_BASE, TIMER_CFG_PERIODIC_UP);
    TimerEnable(TIMER2_BASE,TIMER_A);
    }

    void InitConsole(void)
    {
    //
    // Enable GPIO port A which is used for UART0 pins.
    // TODO: change this to whichever GPIO port you are using.
    //
    SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOA);
    SysCtlDelay(3);

    //
    // Configure the pin muxing for UART0 functions on port A0 and A1.
    // This step is not necessary if your part does not support pin muxing.
    // TODO: change this to select the port/pin you are using.
    //
    GPIOPinConfigure(GPIO_PA0_U0RX);
    GPIOPinConfigure(GPIO_PA1_U0TX);

    //
    // Enable UART0 so that we can configure the clock.
    //
    SysCtlPeripheralEnable(SYSCTL_PERIPH_UART0);

    //
    // Use the internal 16MHz oscillator as the UART clock source.
    //
    UARTClockSourceSet(UART0_BASE, UART_CLOCK_PIOSC);

    //
    // Select the alternate (UART) function for these pins.
    // TODO: change this to select the port/pin you are using.
    //
    GPIOPinTypeUART(GPIO_PORTA_BASE, GPIO_PIN_0 | GPIO_PIN_1);

    //
    // Initialize the UART for console I/O.
    //
    UARTStdioConfig(0, 115200, 16000000);

    }
  • Err... My eyes might be mistaking me, but you have 7 '%d' and only 6 parameters (6 variables) after your string.
    UARTprintf("A2 = %2dcm A5 = %2dcm A6 = %2dcm A7 = %2dcm D0 = %2dcm D3 = %dcm %d" , pulse_D3,pulse_A5,pulse_A6,pulse_A7,pulse_D0,pulse_A2);
  • Oh that is right. See what I was doing was trying to make sure it wasn't a problem with whatever was at the end of the statement so I made int no = 0; and then I used the print statement with 'no' as it's last parameter and it seems I must've forgotten to remove it.

    Among other things that I have tried was doing a printf for each parameter, the result was bad. The first 3 parameter printed out okay, meaning that the distances made sense, however, the last three parameters were completely random numbers. I also tried to only use 2 different printf statements the first printf contained the first 3 parameters ( A2,A5,A6) while the second printf contained the last three parameters (A7,D0,D3). This caused parameter A6 and parameter D3 to be randomly incrementing numbers. I was not able to find out anything useful from these different methods I tried.

    Any ideas on what might be wrong?

    Thank You,

    Luis Vergara

  • Hello Luis,

    What I do not like about the code is the fact that a single GPIO Interrupt is being used to read an the width. If the GPIO input toggle skew then one if condition that was false may have to wait all the time to re-enter the loop. The original project works good for a single sensor, but when multiple sensors are required, event-time capture with timed start is a better approach.

    Regards
    Amit
  • That was a really simple example. I suggested adapting it if only 1 sensor would be read at a time.

    The ideal is as you said a timer with time capture. It would possibly also allow for a timeout which can be very useful.
    I still have yet to use that timer mode. Seems like a good opportunity to try it out next week (1 week easter break yay)
  • Hello Luis A,

    My post was not meant to offend you (it was the adaption of 1 sensor to 6 sensor(s) that did not have the forethought).

    Regards
    Amit
  • That definitely seems like a good idea. I'll look into that and see how it works. Thanks.
  • Hello Luis V,

    Do make sure that the timers are started when the pulse is to be sent. There is a synchronization feature to synchronize all the timers at the same time.

    Regards
    Amit