I’m trying to interface 6 ultrasoinc sesnors with Tiva C launch pad using FreeRTOS this code is working for only onw sensor but I have some problems when I add more than one I get wrong readings and sometimes when connecting 2 sensors they return the same value
~~~
include “FreeRTOS.h”
include “task.h”
include “UART.h”
include “Ultrasoinc_2.h”
unsigned long Reading1=0;
TaskHandle
t UltrasonicReadingHandle=NULL;
TaskHandle
t UtrasoincInterruptHandle=NULL;
// Task to send Trigger Every 20 ms
void Ultrasonic
Trigger( void * pvParameters )
{
TickTypet xLastWakeTime;
while(1)
{
// Enable interrupt on PB0 pin connected to ultrasonic Echo
GPIO
PORTBIM_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;
}
taskEXITCRITICAL();
vTaskSuspend(NULL);
}
}
int main(void)
{
xTaskCreate(Ultrasoinc
InterruptB,”Utrasoinc
Interrupt”,150,NULL,1,&UtrasoincInterruptHandle);
xTaskCreate(Ultrasonic
Trigger,”UltrasonicReading”,150,NULL,2,&Ultrasonic
ReadingHandle);
// Enable system clock
SYSCTLRCGCGPIO
R|=(1<<0);
SYSCTLRCGCGPIO
R|=(1<<1);
SYSCTLRCGCGPIO
R|=(1<<4);
// Initialize UART
UART0Init(9600,16000000);
// Make Trigger pin output and Echo pin input and enable interrupts in echo pin
Configure
Echo();
ConfigureTrigger();
// NVIC register
Enable
UltrasioncInterrupts();
vTaskStartScheduler();
while(1)
{
}
}
// External interrupt ISR
void Ultrasonic
PortBInterrupt()
{
// Clear Flag
GPIO
PORTBICR
R|= (1<<0);
// Resume handling Task
BaseTypet check;
check = xTaskResumeFromISR(Utrasoinc
InterruptHandle);
portYIELDFROM_ISR(check);
}
~~~
I don’t know how to read more than one sensor, as the task handling the interrupt is resumed from ISR so each sensor gives an interrupt enters the same task and I can’t determine the echo is comming from which sensor.
Is it better to have separate tasks to send trigger for each sensor?
Thanks