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,