Hi Team,
volatile uint32_t time = 0; volatile uint32_t Distance = 0; void HC_SR04_Init(void) { SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOB); //enable GPIO_B GPIOPinTypeGPIOInput(GPIO_PORTB_BASE, GPIO_PIN_4); GPIOPinTypeGPIOOutput(GPIO_PORTB_BASE, GPIO_PIN_5); GPIOPinWrite(GPIO_PORTB_BASE ,GPIO_PIN_5 ,0); } void Timer0A_Handler(void) { static uint32_t time_count=0; //Read Timer Interrupt Status uint32_t status=TimerIntStatus( TIMER0_BASE, true); //Clear interrupt flag bit TimerIntClear( TIMER0_BASE, status); time ++; } uint32_t sonar_mm(void ) { GPIOPinWrite(GPIO_PORTB_BASE, GPIO_PIN_5, GPIO_PIN_5);//Trig high delay_us(18); GPIOPinWrite(GPIO_PORTB_BASE, GPIO_PIN_5, 0); while(GPIOPinRead(GPIO_PORTB_BASE ,GPIO_PIN_4) == 0); //Wait for low end time=0; while(GPIOPinRead(GPIO_PORTB_BASE ,GPIO_PIN_4) == 1) { ; } Distance=(time*346)/2; //Calculate the distance, and sound velocity in 25°C air is 346 m/s. return Distance ; } void Timer_Init(void) { //enable TIMER0,16/32bit SysCtlPeripheralEnable( SYSCTL_PERIPH_TIMER0); //Configure the timer, split the timer, and configure Timer a after the timer to be periodic TimerConfigure( TIMER0_BASE, TIMER_CFG_SPLIT_PAIR|TIMER_CFG_A_PERIODIC_UP); TimerLoadSet( TIMER0_BASE, TIMER_A, SysCtlClockGet()/100000-1);//1us //Register the interrupt function for Timer A. TimerIntRegister( TIMER0_BASE, TIMER_A, Timer0A_Handler); TimerIntEnable( TIMER0_BASE, TIMER_TIMA_TIMEOUT); //Set interrupt priority IntPrioritySet( INT_TIMER0A, 0); //Enable interrupts IntEnable( INT_TIMER0A); IntMasterEnable(); //Enable the timer TimerEnable( TIMER0_BASE, TIMER_A); } int main(void) { HardWave_Init();//Serial interface, system clock initialization HC_SR04_Init(); Timer_Init(); printf("Initialization complete \n"); while(1)//The main loop { printf("%d\n",sonar_mm() ); delay_ms(10); } }
The ultrasonic readout using the above method always returns 0. Could you help check is there anything need to be modified? Thanks.
Best Regards,
Cherry