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.

TM4C123GH6PM: HC-SR04超声波模块

Part Number: TM4C123GH6PM

volatile uint32_t time = 0;
volatile uint32_t Distance = 0;

void HC_SR04_Init(void)
{
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOB);
//启用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;
//读取定时器中断状态
uint32_t status=TimerIntStatus( TIMER0_BASE, true);
//清除中断标志位
TimerIntClear( TIMER0_BASE, status);
time ++;
}
uint32_t sonar_mm(void )
{

GPIOPinWrite(GPIO_PORTB_BASE, GPIO_PIN_5, GPIO_PIN_5);//Trig高电平
delay_us(18);
GPIOPinWrite(GPIO_PORTB_BASE, GPIO_PIN_5, 0);
while(GPIOPinRead(GPIO_PORTB_BASE ,GPIO_PIN_4) == 0); //等待低电平结束
time=0;
while(GPIOPinRead(GPIO_PORTB_BASE ,GPIO_PIN_4) == 1)
{
;
}

Distance=(time*346)/2; //计算距离,25°C空气中的音速为346m/s

return Distance ;
}
void Timer_Init(void) {
//使能定时器TIMER0,16/32bit
SysCtlPeripheralEnable( SYSCTL_PERIPH_TIMER0);
//配置定时器,将定时器拆分,并配置拆分后的定时器A为周期性计数
TimerConfigure( TIMER0_BASE, TIMER_CFG_SPLIT_PAIR|TIMER_CFG_A_PERIODIC_UP);

TimerLoadSet( TIMER0_BASE, TIMER_A,
SysCtlClockGet()/100000-1);//1us
//为定时器A注册中断函数
TimerIntRegister( TIMER0_BASE, TIMER_A,
Timer0A_Handler);

TimerIntEnable( TIMER0_BASE, TIMER_TIMA_TIMEOUT);
//设置中断优先级
IntPrioritySet( INT_TIMER0A, 0);
//使能中断
IntEnable( INT_TIMER0A);
IntMasterEnable();
//使能定时器
TimerEnable( TIMER0_BASE, TIMER_A);

}

int main(void)
{
HardWave_Init();//串口,系统时钟初始化
HC_SR04_Init();
Timer_Init();
printf("初始化完成\n");

while(1)//主循环
{

printf("%d\n",sonar_mm() );
delay_ms(10);

}
}

这样读取到的超声波返回值为什么一直都是零