将多个HC-SR04传感器与Tiva C接口

时间:2019-04-05 17:06:46

标签: c freertos

我正在使用FreeRTOS将6个超声波传感器HC-SR04与Tiva c评估板连接起来。该代码适用于一个传感器,但我无法为6个传感器进行修改。

我尝试连接3个传感器并等待20毫秒,然后发送每个触发器,代码才起作用,但我不知道如何将FreeRTOS与多个传感器一起使用

#include "FreeRTOS.h"
#include "task.h"
#include "UART.h"
#include "Ultrasoinc_2.h"
unsigned long Reading1=0;

TaskHandle_t  Ultrasonic_ReadingHandle=NULL;
TaskHandle_t  Utrasoinc_InterruptHandle=NULL;

// Task to send Trigger Every 20 ms
void Ultrasonic_Trigger( void * pvParameters )
{
    TickType_t xLastWakeTime;
    while(1)
    {
        // Enable interrupt on PB0 pin connected to ultrasonic Echo
        GPIO_PORTB_IM_R|=(1<<0);

        // Makes trigger pin high for 10 us then low to send trigger to ultrasonic
        Send_Trigger(1);
        xLastWakeTime = xTaskGetTickCount();
        vTaskDelayUntil(&xLastWakeTime,20);
    }
}

// Task Resumed from External interrupt at Port B
/* At first Echo Flag is low , get the time of rising edge on PB0 , the next time to enter the task echo flag is high ,
 * get time of falling edge and calculate the difference to get the distance measured
 */
void Ultrasoinc_Interrupt_B( void * pvParameters )
{
TickType_t Start;
for( ;; )
{
 taskENTER_CRITICAL();
 if (!Echo)
  {
  // Get rising Edge time
  Start=xTaskGetTickCount();
  Echo=1;
  }
  else
  {
  Echo=0;
  // Difference between rising and falling edges
  pulse = (unsigned long)((xTaskGetTickCount()-Start)*170/100);
  Delay_us(100);
  Reading1=pulse;
  }
 taskEXIT_CRITICAL();
 vTaskSuspend(NULL);
 }
}



int main(void)
   {

   xTaskCreate(Ultrasoinc_Interrupt_B,"Utrasoinc_Interrupt",150,NULL,1,&Utrasoinc_InterruptHandle);
   xTaskCreate(Ultrasonic_Trigger,"Ultrasonic_Reading",150,NULL,2,&Ultrasonic_ReadingHandle);
   // Enable system clock
           SYSCTL_RCGCGPIO_R|=(1<<0);
           SYSCTL_RCGCGPIO_R|=(1<<1);
           SYSCTL_RCGCGPIO_R|=(1<<4);
   // Initialize UART
           UART0_Init(9600,16000000);

   // Make Trigger pin output and Echo pin input and enable interrupts in echo pin
           Configure_Echo();
           Configure_Trigger();
           // NVIC register
           Enable_Ultrasionc_Interrupts();
           vTaskStartScheduler();

    while(1)
    {

    }

}
// External interrupt ISR
void Ultrasonic_PortB_Interrupt()
  {
    // Clear Flag
    GPIO_PORTB_ICR_R|= (1<<0);
    // Resume handling Task
    BaseType_t check;
    check = xTaskResumeFromISR(Utrasoinc_InterruptHandle);
    portYIELD_FROM_ISR(check);
  }

0 个答案:

没有答案