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.

The Problem when measure distance with HC SR05

I'm making a project using HC SR05 sensor to measure distance.I'm have checked several time but the result is 0.This is my program.

#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    "uartstdio.h"
#include <string.h>
//Echo put in PD2
//Trigger put in PD3
//**************************************************************
//  Main()
//**************************************************************

float temp = 1.0/80.0;
uint32_t pulse=0;
uint32_t distance=0;
int  Echo_status=0;
void GPIO_ISR(void);
void UART_ISR(void);

void UART_config(void);
void config_timer(void);
void config_Trig(void);
void config_Echo(void);

void UART_ISR(void)
{
    uint32_t ui32Status;
    char        statemove;
    ui32Status = UARTIntStatus(UART0_BASE, true); //get interrupt status
    UARTIntClear(UART0_BASE, ui32Status); //clear the asserted interrupts
    statemove = UARTCharGetNonBlocking(UART0_BASE);
    UARTprintf("%c",statemove);


}


void GPIO_ISR(void)
{
    GPIOIntClear(GPIO_PORTD_BASE, GPIO_PIN_2 ); //clear interrupt flag
    if ( GPIOPinRead(GPIO_PORTD_BASE,  GPIO_PIN_2 ) ==  1 )
    {
      HWREG(TIMER0_BASE + TIMER_O_TAV) = 0; //Loads value 0 into the timer.
      TimerEnable(TIMER0_BASE,TIMER_A); //start timer
      Echo_status=1;
    }
    else
    {

      TimerDisable(TIMER0_BASE,TIMER_A); //stop timer
     pulse = TimerValueGet(TIMER0_BASE,TIMER_A); //record value
    

     Echo_status=0;
    }


}





void main(void)
{
    SysCtlClockSet(SYSCTL_SYSDIV_2_5| SYSCTL_USE_PLL | SYSCTL_OSC_MAIN | SYSCTL_XTAL_16MHZ);

    UART_config();
    config_timer();
    config_Trig();
   config_Echo();

IntMasterEnable();


   while(1)
    {
          if(Echo_status != 1)
          {
            GPIOPinWrite(GPIO_PORTD_BASE, GPIO_PIN_3 , GPIO_PIN_3 );
            SysCtlDelay(300);
            GPIOPinWrite(GPIO_PORTD_BASE, GPIO_PIN_3 ,~GPIO_PIN_3 );
            while(Echo_status != 0);
            pulse =(uint32_t)(temp * pulse);
           distance= pulse / 58;

            UARTprintf("distance = %2dcm \n" , distance);
          }

            SysCtlDelay(400000);

    }
 }
void UART_config(void)
{
    SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOA);
    SysCtlPeripheralEnable(SYSCTL_PERIPH_UART0);
    GPIOPinConfigure(GPIO_PA1_U0TX);
    GPIOPinConfigure(GPIO_PA0_U0RX);
    GPIOPinTypeUART(GPIO_PORTA_BASE, GPIO_PIN_0 | GPIO_PIN_1);
    UARTClockSourceSet(UART0_BASE, UART_CLOCK_PIOSC);
    UARTStdioConfig(0, 115200, 16000000);
    IntEnable(INT_UART0); //enable the UART interrupt
    UARTIntEnable(UART0_BASE, UART_INT_RX | UART_INT_RT); //only enable RX and TX interrupts

}
void config_timer(void)
{
    SysCtlPeripheralEnable(SYSCTL_PERIPH_TIMER0);
    SysCtlDelay(3);
    TimerConfigure(TIMER0_BASE, TIMER_CFG_PERIODIC_UP);
    TimerEnable(TIMER0_BASE, TIMER_A);
}


void config_Trig(void)
{
    SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOD);
    SysCtlDelay(3);
    GPIOPinTypeGPIOOutput(GPIO_PORTD_BASE, GPIO_PIN_3);

}
void config_Echo(void)
{
    SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOD);
     SysCtlDelay(3);
     GPIOPinTypeGPIOInput(GPIO_PORTD_BASE, GPIO_PIN_2 );
  GPIOIntEnable(GPIO_PORTD_BASE, GPIO_PIN_2 );
  GPIOIntTypeSet(GPIO_PORTD_BASE, GPIO_PIN_2 ,GPIO_BOTH_EDGES);
  GPIOIntRegister(GPIO_PORTD_BASE,GPIO_ISR);

}

Thank for reading,

  • Hello Nam

    The use of periodic mode is not correct. You should not load a running timer with TAV as 0. Instead use the free running timer to time stamp the rising and falling edge. Even better would be to use the timer in edge time mount to timestamp the rising and falling edge and read it out.
  • Thank your support.I've checked function timer.It's operate good because i getvalue timer in interrupt UART.The problem now is my program can't jump in GPIO_ISR .
    Please give me the solution.
  • We are well accostumed to using PWM output sensors here. The strategy is:

    1- Set a continuous running timer with any value that higher than the expected input period (usually the maximum period that the timer can take)

    2- Configure the pin to generate a DMA transfer on either rising or falling border. This transfer copies two pieces if information to a memory location for every crossing: the timer count and the GPIO level of that pin. Configure that DMA to execute transfers for enough transitions (depends on how often you want to process the data array to extract the sensor information).

    3- After the DMA is finished, generate an interrupt. This interrupt simply sets a flag to start a function that will process the information.

    Inside the evaluation function, we determine if the border was rising or falling, measuring the distance between the borders, and hence can obtain not only the frequency of the signal but the duty cycle as well. And then set the DMA again for the next cycle of readings.

    Before any screaming, this is not a code sample reply, it's just a strategy suggestion...

  • Thank you so much.
  • TI Family and Friends,


    Did any sample code for this particular implementation method ever surface - specific to Tiva TM4C129x?

    TY,
    CY

  • Chris Yorkey said:
    TI Family and Friends,

    Chris - "Props" for inviting,  "input from outsiders."     (far outside - in certain cases ... ... and (some) cb1 "family" - deny kinship.)

    My firm advised a client using a bit different version/model of this sensor - noted that the code "came together" (much) faster/easier  via the use of TWO Timers.    (one logging rising edge - 2nd the falling edge)

    This was achieved w/the "Edge Time Mode for each Timer" - and it should be noted that "restrictions" are placed upon the Timer in this mode.   (client - & many here "miss" that detail...)

    I will see if our client will release the key aspects of their (successful) code - and then provide it for the benefit of others.     It is suspected that the Timer portion of the code will succeed w/both TM4C123 & 129...