I'm currently using a code I found online by Luis Rafael on where he uses a timer to measure the echo. I am trying to read 6 different ultrasonic sensors and tried to use 6 different timers. Basically all of the timers on the board. I realize this is an incredibly inefficient idea and was wondering what other things I might be able to try.
I was thinking about letting a single timer run and just take the time from falling edge and substract the time from rising edge to it. All the while making sure that the timer for falling edge is always a higher number than the one for rising edge. To do this I was thinking of making an if statement which would add the highest value of the timer to the falling edge time if it was smaller than the rising edge time. Is there anything else I should account for?
Thank you,
Luis Alberto Vergara-Rodriguez
while(1)
{
//Checks if a pulse read is in progress
if((echowait_A2 & echowait_A5 & echowait_A6 & echowait_A7 & echowait_D0 & echowait_D3) != 1){
//Does the required pulse of 10uS
GPIOPinWrite(GPIO_PORTA_BASE, GPIO_PIN_3, GPIO_PIN_3);
SysCtlDelay(266);
GPIOPinWrite(GPIO_PORTA_BASE, GPIO_PIN_3, ~GPIO_PIN_3);
//Converts the counter value to cm.
pulse_A2 =(uint32_t)(temp * pulse_A2);
pulse_A5 =(uint32_t)(temp * pulse_A5);
pulse_A6 =(uint32_t)(temp * pulse_A6);
pulse_A7 =(uint32_t)(temp * pulse_A7);
pulse_D0 =(uint32_t)(temp * pulse_D0);
pulse_D3 =(uint32_t)(temp * pulse_D3);
pulse_A2 = pulse_A2 / 58;
pulse_A5 = pulse_A5 / 58;
pulse_A6 = pulse_A6 / 58;
pulse_A7 = pulse_A7 / 58;
pulse_D0 = pulse_D0 / 58;
pulse_D3 = pulse_D3 / 58;
//Prints out the distance measured.
UARTprintf("A2 = %2dcm A5 = %2dcm A6 = %2dcm A7 = %2dcm D0 = %2dcm D3 = %2dcm \n" , pulse_A2,pulse_A5,pulse_A6,pulse_A7,pulse_D3,pulse_D0);
}
//wait about 10ms until the next reading.
SysCtlDelay(400000);
}
}
void inputInt(){
uint32_t status_A=0;
uint32_t status_D=0;
status_A = GPIOIntStatus(GPIO_PORTA_BASE,true);
status_D = GPIOIntStatus(GPIO_PORTD_BASE,true);
GPIOIntClear(GPIO_PORTA_BASE,status_A);
GPIOIntClear(GPIO_PORTD_BASE,status_D);
if( (status_A & GPIO_INT_PIN_2) == GPIO_INT_PIN_2){
//Then there was a port A pin2 interrupt
/*
If it's a rising edge then set he timer to 0
It's in periodic mode so it was in some random value
*/
if ( GPIOPinRead(GPIO_PORTA_BASE, GPIO_PIN_2) == GPIO_PIN_2){
HWREG(TIMER2_BASE + TIMER_O_TAV) = 0; //Loads value 0 into the timer.
TimerEnable(TIMER2_BASE,TIMER_A);
echowait_A2=1;
}
/*
If it's a falling edge that was detected, then get the value of the counter
*/
else{
pulse_A2 = TimerValueGet(TIMER2_BASE,TIMER_A); //record value
TimerDisable(TIMER2_BASE,TIMER_A);
echowait_A2=0;
}
}
if( (status_A & GPIO_INT_PIN_5) == GPIO_INT_PIN_5){
//Then there was a port A pin5 interrupt
/*
If it's a rising edge then set he timer to 0
It's in periodic mode so it was in some random value
*/
if ( GPIOPinRead(GPIO_PORTA_BASE, GPIO_PIN_5) == GPIO_PIN_5){
HWREG(TIMER3_BASE + TIMER_O_TAV) = 0; //Loads value 0 into the timer.
TimerEnable(TIMER3_BASE,TIMER_A);
echowait_A5=1;
}
/*
If it's a falling edge that was detected, then get the value of the counter
*/
else{
pulse_A5 = TimerValueGet(TIMER3_BASE,TIMER_A); //record value
TimerDisable(TIMER3_BASE,TIMER_A);
echowait_A5=0;
}
}
if( (status_A & GPIO_INT_PIN_6) == GPIO_INT_PIN_6){
//Then there was a port A pin6 interrupt
/*
If it's a rising edge then set he timer to 0
It's in periodic mode so it was in some random value
*/
if ( GPIOPinRead(GPIO_PORTA_BASE, GPIO_PIN_6) == GPIO_PIN_6){
HWREG(TIMER4_BASE + TIMER_O_TAV) = 0; //Loads value 0 into the timer.
TimerEnable(TIMER4_BASE,TIMER_A);
echowait_A6=1;
}
/*
If it's a falling edge that was detected, then get the value of the counter
*/
else{
pulse_A6 = TimerValueGet(TIMER4_BASE,TIMER_A); //record value
TimerDisable(TIMER4_BASE,TIMER_A);
echowait_A6=0;
}
}
if( (status_A & GPIO_INT_PIN_7) == GPIO_INT_PIN_7){
//Then there was a port A pin7 interrupt
/*
If it's a rising edge then set he timer to 0
It's in periodic mode so it was in some random value
*/
if ( GPIOPinRead(GPIO_PORTA_BASE, GPIO_PIN_7) == GPIO_PIN_7){
HWREG(TIMER5_BASE + TIMER_O_TAV) = 0; //Loads value 0 into the timer.
TimerEnable(TIMER5_BASE,TIMER_A);
echowait_A7=1;
}
/*
If it's a falling edge that was detected, then get the value of the counter
*/
else{
pulse_A7 = TimerValueGet(TIMER5_BASE,TIMER_A); //record value
TimerDisable(TIMER5_BASE,TIMER_A);
echowait_A7=0;
}
}
if( (status_D & GPIO_INT_PIN_0) == GPIO_INT_PIN_0){
//Then there was a port D pin0 interrupt
/*
If it's a rising edge then set he timer to 0
It's in periodic mode so it was in some random value
*/
if ( GPIOPinRead(GPIO_PORTD_BASE, GPIO_PIN_0) == GPIO_PIN_0){
HWREG(TIMER0_BASE + TIMER_O_TAV) = 0; //Loads value 0 into the timer.
TimerEnable(TIMER0_BASE,TIMER_A);
echowait_D0=1;
}
/*
If it's a falling edge that was detected, then get the value of the counter
*/
else{
pulse_D0 = TimerValueGet(TIMER0_BASE,TIMER_A); //record value
TimerDisable(TIMER0_BASE,TIMER_A);
echowait_D0=0;
}
}
if( (status_D & GPIO_INT_PIN_3) == GPIO_INT_PIN_3){
//Then there was a port D pin3 interrupt
/*
If it's a rising edge then set he timer to 0
It's in periodic mode so it was in some random value
*/
if ( GPIOPinRead(GPIO_PORTA_BASE, GPIO_PIN_3) == GPIO_PIN_3){
HWREG(TIMER1_BASE + TIMER_O_TAV) = 0; //Loads value 0 into the timer.
TimerEnable(TIMER1_BASE,TIMER_A);
echowait_D3=1;
}
/*
If it's a falling edge that was detected, then get the value of the counter
*/
else{
pulse_D3 = TimerValueGet(TIMER1_BASE,TIMER_A); //record value
TimerDisable(TIMER1_BASE,TIMER_A);
echowait_D3=0;
}
}
}
void Captureinit(){
/*
Set the timer to be periodic.
*/
SysCtlPeripheralEnable(SYSCTL_PERIPH_TIMER0);
SysCtlDelay(3);
TimerConfigure(TIMER0_BASE, TIMER_CFG_PERIODIC_UP);
TimerEnable(TIMER0_BASE,TIMER_A);
SysCtlPeripheralEnable(SYSCTL_PERIPH_TIMER1);
SysCtlDelay(3);
TimerConfigure(TIMER1_BASE, TIMER_CFG_PERIODIC_UP);
TimerEnable(TIMER1_BASE,TIMER_A);
SysCtlPeripheralEnable(SYSCTL_PERIPH_TIMER2);
SysCtlDelay(3);
TimerConfigure(TIMER2_BASE, TIMER_CFG_PERIODIC_UP);
TimerEnable(TIMER2_BASE,TIMER_A);
SysCtlPeripheralEnable(SYSCTL_PERIPH_TIMER3);
SysCtlDelay(3);
TimerConfigure(TIMER3_BASE, TIMER_CFG_PERIODIC_UP);
TimerEnable(TIMER3_BASE,TIMER_A);
SysCtlPeripheralEnable(SYSCTL_PERIPH_TIMER4);
SysCtlDelay(3);
TimerConfigure(TIMER4_BASE, TIMER_CFG_PERIODIC_UP);
TimerEnable(TIMER4_BASE,TIMER_A);
SysCtlPeripheralEnable(SYSCTL_PERIPH_TIMER5);
SysCtlDelay(3);
TimerConfigure(TIMER5_BASE, TIMER_CFG_PERIODIC_UP);
TimerEnable(TIMER5_BASE,TIMER_A);
}