driver for the ranger the 2 piezo version

Committer:
cstevens
Date:
Tue Jun 14 13:33:39 2016 +0000
Revision:
0:5871422998cd
poster for the oxford CWM code

Who changed what in which revision?

UserRevisionLine numberNew contents of line
cstevens 0:5871422998cd 1 #include "Ranger.h"
cstevens 0:5871422998cd 2
cstevens 0:5871422998cd 3
cstevens 0:5871422998cd 4
cstevens 0:5871422998cd 5 // IO bits for the rangefinder
cstevens 0:5871422998cd 6 DigitalOut trig(PTD7);
cstevens 0:5871422998cd 7 DigitalIn echo(PTD6);
cstevens 0:5871422998cd 8 // timer to handle rangefinder
cstevens 0:5871422998cd 9 Timer t;
cstevens 0:5871422998cd 10
cstevens 0:5871422998cd 11 int range(){
cstevens 0:5871422998cd 12 int rangecm=0,rtime=0;
cstevens 0:5871422998cd 13
cstevens 0:5871422998cd 14 // reset thetimer to zero
cstevens 0:5871422998cd 15 t.reset();
cstevens 0:5871422998cd 16
cstevens 0:5871422998cd 17
cstevens 0:5871422998cd 18 //send a trigger pulse
cstevens 0:5871422998cd 19 trig=1;
cstevens 0:5871422998cd 20 wait_us(12);
cstevens 0:5871422998cd 21 trig=0;
cstevens 0:5871422998cd 22 wait_us(12);
cstevens 0:5871422998cd 23 trig=1; // thing starts on trigger going high NB - this is buffered with an npn and is connected to its base. high on this pin
cstevens 0:5871422998cd 24 // sets the actual trigger pin to low and low on this pin sets it to high - this is becausethe ranger needs 5V for high
cstevens 0:5871422998cd 25 // wait for echo to go high
cstevens 0:5871422998cd 26 while(echo==0);
cstevens 0:5871422998cd 27 t.start();
cstevens 0:5871422998cd 28 while(echo==1 && t.read_us()<58*300); // wait for echo to go low again or for about 3m of range
cstevens 0:5871422998cd 29 t.stop();
cstevens 0:5871422998cd 30 rtime=t.read_us();
cstevens 0:5871422998cd 31 t.reset();
cstevens 0:5871422998cd 32 rangecm=rtime/58; // convert time in us to range in cm
cstevens 0:5871422998cd 33 return rangecm; // return range in cm
cstevens 0:5871422998cd 34
cstevens 0:5871422998cd 35
cstevens 0:5871422998cd 36 }