Measuring Events Duration and Time Between Events

This lesson is a continuation of the previous lesson – Counting events . In that lesson we saw how we can the occurrence of events by capturing the  edges of an input pin. We said the microcontroller can detect the occurrence of an event due to the signal changes that happens at the input pin of microcontroller.

To find the duration of an event, all we have to save  the timestamp of the rising-edge capture in  one variable and save the timestamp of falling-edge capture in another variable, and then subtract the timestamp of the rising-edge from the time stamp of the falling-edge.

Finally, to convert  the results to our desired time units, we have to multiply it by an appropriate value based on the running frequency of our CPU.

In our Timer configuration we have select capture both-edges in order to be able to perform this task.

Following the same principle, to measure the time between two events, we have save the timestamp of  the rising-edge capture of event1 i.e input1 and then the timestamp of the rising-edge capture of event2, find the difference between the two timestamps then convert the difference to the appropriate time unit.

In this example we took the timestamp of when  the input pin got HIGH and then the timestamp of when it got back to LOW,converted the difference to an appropriate time unit for our application and finally converted the value to centimeters.  This is a use case example showing how to calculate the distance of an obstacle in front of an ultrasonic sensor.

Interfacing HC-SR04 Ultrasonic sensor with Tiva C ARM Cortex-M4

Two pieces of  program source code is presented below. The first piece of code  gives a highly precise result however requires the enabling of two General Purpose Timers; one for creating microseconds delay and the other for capturing the rising and falling edges of the Echo pin

The second piece of code is more straightforward in understanding, however, less precise compared to the first. It requires enabling just one General Purpose Timer- for creating microseconds delays.

Program 1

#include "TM4C123.h"                    // Device header
#include <stdint.h>
 
void delay_Microsecond(uint32_t time);
void Timer0_init(void);
uint32_t measureD(void);
 
const double _16MHz_1clock = 62.5e-9; /*Value of 1clock cycle in nanoseconds*/
const uint32_t MULTIPLIER  = 5882;  /*Derived from speed of sound*/
 
#define ECHO (1U<<6) //PB6
#define TRIGGER(1U<<4) //PA4(OUTPUT)
#define BLUE_LED (1U<<2)//PF3 onboard Blue LED 
uint32_t highEdge,lowEdge;
uint32_t ddistance; /*Distance in centimeters*/
uint32_t counter =0; 
uint32_t measureD(void) { 
GPIOA->DATA &=~TRIGGER;
    delay_Microsecond(12);
    GPIOA->DATA |= TRIGGER;
    delay_Microsecond(12);
    GPIOA->DATA &=~TRIGGER;
    /*Capture firstEgde i.e. rising edge*/
    TIMER0->ICR =4;
    while((TIMER0->RIS & 4)==0){}; //Wait till captured
        highEdge =  TIMER0->TAR;
 
        /*Capture secondEdge i.e. falling edge */
        TIMER0->ICR =4; //clear timer capture flag
        while((TIMER0->RIS & 4)  ==0){};
            lowEdge = TIMER0-&gt;TAR;
              ddistance = lowEdge -highEdge;
              ddistance = _16MHz_1clock *(double) MULTIPLIER *(double)ddistance;
 
            return ddistance;
 
}
int main(void){
    SYSCTL->RCGCGPIO |=(1U<<0); //Enable clock for PORTA 
        SYSCTL->RCGCGPIO |=(1U<<5); //Enable clock for PORTF 
        GPIOA->DIR =TRIGGER;  
    GPIOF->DIR =BLUE_LED;
    GPIOA->DEN |=(ECHO)|(TRIGGER);
    GPIOF->DEN |= BLUE_LED;
 
    while(1){
        Timer0_init();
         measureD();
    if(measureD() < 15) 
   GPIOF->DATA |=BLUE_LED;
   else
   GPIOF->DATA &=~BLUE_LED;
   delay_Microsecond(100);
 
  }
}
void delay_Microsecond(uint32_t time)
{
    int i;
    SYSCTL->RCGCTIMER |=(1U<<1); 
        TIMER1->CTL=0;
    TIMER1->CFG=0x04;
    TIMER1->TAMR=0x02;
    TIMER1->TAILR= 16-1;
    TIMER1->ICR =0x1;
    TIMER1->CTL |=0x01;
 
    for(i=0;i<time;i++){ 
       while((TIMER1->RIS & 0x1)==0);
        TIMER1->ICR = 0x1;
    }
 
}
void Timer0_init(void)
{
    SYSCTL->RCGCTIMER |=(1U<<0); 
        SYSCTL->RCGCGPIO |=(1U<<1); 
        GPIOB->DIR &=~ECHO;
    GPIOB->DEN |=ECHO;
    GPIOB->AFSEL |=ECHO;
    GPIOB->PCTL &=~0x0F000000;
    GPIOB->PCTL |= 0x07000000;
 
    TIMER0->CTL &=~1;
    TIMER0->CFG =4;
    TIMER0->TAMR = 0x17;
    TIMER0->CTL |=0x0C;
    TIMER0->CTL |=1;
}

Program 2

//Program: Interfacing HC-SR04 with Tiva C ARM Cortex M4
//Remarks: Less precise version
#include &quot;TM4C123.h&quot;                    // Device header
#include &lt;stdint.h&gt;
#define MAX_TIME 7500
float measureD(void);
uint32_t counter =0;
float distance=0;
#define ECHO (1U&lt;&lt;2) //PA2(INput)
#define TRIGGER (1U&lt;&lt;4) //PA4(OUTPUT)
#define BLUE_LED (1U&lt;&lt;2)//PF3 onboard Blue LED 
void delay_Microsecond(uint32_t time); 
float measureD(void){ 
        GPIOA-&gt;DATA &amp;=~TRIGGER;
    delay_Microsecond(10);
    GPIOA-&gt;DATA |= TRIGGER;
    delay_Microsecond(10);
    GPIOA-&gt;DATA &amp;=~TRIGGER;
    counter =0;
    while((GPIOA-&gt;DATA &amp;ECHO)==0)    {}
    while(((GPIOA-&gt;DATA &amp;ECHO )!=0) &amp;(counter &lt; MAX_TIME)) 
 { 
  counter++; 
  delay_Microsecond(1);
  } 
 distance = (float)counter*(float)0.0170000;
 return distance; } 
int main(void) 
    { 
   SYSCTL-&gt;RCGCGPIO |=(1U&lt;&lt;0); //Enable clock for PORTA 
   SYSCTL-&gt;RCGCGPIO |=(1U&lt;&lt;5); //Enable clock for PORTF 
   GPIOA-&gt;DIR =TRIGGER;
   GPIOF-&gt;DIR =BLUE_LED;
   GPIOA-&gt;DEN |=(ECHO)|(TRIGGER);
   GPIOF-&gt;DEN |= BLUE_LED;
while(1){
 
  measureD();   
    if(measureD() &lt; 10.0){ 
     GPIOF-&gt;DATA |=BLUE_LED;
        }
    else{
    GPIOF-&gt;DATA &amp;=~BLUE_LED;
        }
    delay_Microsecond(10);
  }
}
 
void delay_Microsecond(uint32_t time)
{
    int i;
    SYSCTL-&gt;RCGCTIMER |=(1U&lt;&lt;1); 
        TIMER1-&gt;CTL=0;
    TIMER1-&gt;CFG=0x04;
    TIMER1-&gt;TAMR=0x02;
    TIMER1-&gt;TAILR= 16-1;
    TIMER1-&gt;ICR =0x1;
    TIMER1-&gt;CTL |=0x01;
 
    for(i=0;i&lt;time;i++){ 
        while((TIMER1-&gt;RIS &amp; 0x1)==0);
        TIMER1-&gt;ICR = 0x1;
    }
 
}

Add Comment

Your email address will not be published. Required fields are marked *

Phone: +447491265055
W12 7JN, London, United Kingdom
34A Frithville Gardens
Style switcher RESET
Body styles
Color settings
Link color
Menu color
User color
Background pattern
Background image