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超声波模块

Guru**** 668880 points
请注意,本文内容源自机器翻译,可能存在语法或其它翻译错误,仅供参考。如需获取准确内容,请参阅链接中的英语原文或自行翻译。

https://e2e.ti.com/support/microcontrollers/arm-based-microcontrollers-group/arm-based-microcontrollers/f/arm-based-microcontrollers-forum/1253633/tm4c123gh6pm-hc-sr04-ultrasonic-module

器件型号: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。  您可以帮助 检查是否需要修改任何内容吗?  谢谢。

此致、

切里

  • 请注意,本文内容源自机器翻译,可能存在语法或其它翻译错误,仅供参考。如需获取准确内容,请参阅链接中的英语原文或自行翻译。

    您好、Cherry:

     如果你在计时器 ISR 中放置一个断点、处理器会在这里停止吗?

     另外、在第29行、将时间变量清零、GPIOB4为低电平。 如果时间为零、那么需要计算到零的距离、对吧? 因此可以在示波器上检查引脚输入状态。  

    A 抬头、我在办公室外、直到8/3。