Jump to content
Sign in to follow this  
L.R.A

Pulse legth measure - srf04 example

Recommended Posts

I actualy had trouble finding anytigh about this so here it is. I hope it helps someone when they are searching for examples. Any sugestions would be apreciated

 

This is using the tm4c129 at 120Mhz

This code will calculate any pulse legth until sometigh like huge 35 seconds ones, from 1/(120Mhz) ones. This in teory of course, you still need time to process the data, so you should get easily any pulse as with 1 micro-second legth. For less i would have to run some tests with the osciloscope.

This uses a 32bit timer to achieve that measurement wich is controled by a interrupt in the GPIO.

 

In this example i use a SRF04 so this measures only the legth of a high pulse. Also i use a value of 216000000 in the TimerLoadSet(); because i only need a maximum of 18ms pulses.



#include "inc/hw_ints.h"
#include "driverlib/interrupt.c"
#include "driverlib/sysctl.c"
#include "driverlib/Timer.c"
#include "driverlib/gpio.c"
#include"inc/tm4c1294ncpdt.h"


const double temp = 1.0/120.0;
volatile uint32_t pulse=0;
volatile byte echowait=0;


void setup()
{
  Serial.begin(9600);
  Captureinit();


  SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOA);                 
  GPIOPinTypeGPIOOutput(GPIO_PORTA_BASE, GPIO_PIN_3);


  SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOA);              
  GPIOPinTypeGPIOInput(GPIO_PORTA_BASE, GPIO_PIN_2);
  GPIOIntEnable(GPIO_PORTA_BASE, GPIO_PIN_2); 
  GPIOIntTypeSet(GPIO_PORTA_BASE, GPIO_PIN_2,GPIO_BOTH_EDGES);
  GPIOIntRegister(GPIO_PORTA_BASE,inputInt);


}


void loop()
{




  if(echowait != 1){
    GPIOPinWrite(GPIO_PORTA_BASE, GPIO_PIN_3, GPIO_PIN_3);
    delayMicroseconds(10);
    GPIOPinWrite(GPIO_PORTA_BASE, GPIO_PIN_3, ~GPIO_PIN_3);


    pulse = temp * (216000000-pulse);
    pulse = pulse / 58;
    Serial.println(pulse);
  }
  while(echowait ==0);  
  delay(15);




}


void inputInt(){
  GPIOIntClear(GPIO_PORTA_BASE, GPIO_PIN_2); //clear interrupt flag
  if ( GPIOPinRead(GPIO_PORTA_BASE, GPIO_PIN_2) == GPIO_PIN_2){
    TimerLoadSet(TIMER2_BASE, TIMER_A, 216000000);
    //TimerEnable(TIMER2_BASE,TIMER_A); //start timer to recor
    echowait=1;
  }
  else{


   // TimerDisable(TIMER2_BASE,TIMER_A); //stop timer
    pulse = TimerValueGet(TIMER2_BASE,TIMER_A); //record value
    
    echowait=0;                       
  }




}
void Captureinit(){
  SysCtlPeripheralEnable(SYSCTL_PERIPH_TIMER2);                        
  TimerConfigure(TIMER2_BASE, TIMER_CFG_PERIODIC);  
 TimerEnable(TIMER2_BASE,TIMER_A); 
}

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Sign in to follow this  

×
×
  • Create New...