请注意,本文内容源自机器翻译,可能存在语法或其它翻译错误,仅供参考。如需获取准确内容,请参阅链接中的英语原文或自行翻译。
器件型号:TM4C123GH6PM 大家好、
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); } }
使用上述方法的超声波读数始终返回0。 您可以帮助 检查是否需要修改任何内容吗? 谢谢。
此致、
切里