library for ulrasonic sensonr

Dependents:   Wall-e

Committer:
Andres0131
Date:
Fri May 18 01:13:12 2018 +0000
Revision:
0:3e33a89d3a9f
worked better this way

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Andres0131 0:3e33a89d3a9f 1 /* Copyright (c) 2013 Prabhu Desai
Andres0131 0:3e33a89d3a9f 2 * pdtechworld@gmail.com
Andres0131 0:3e33a89d3a9f 3 *
Andres0131 0:3e33a89d3a9f 4 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
Andres0131 0:3e33a89d3a9f 5 * and associated documentation files (the "Software"), to deal in the Software without restriction,
Andres0131 0:3e33a89d3a9f 6 * including without limitation the rights to use, copy, modify, merge, publish, distribute,
Andres0131 0:3e33a89d3a9f 7 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
Andres0131 0:3e33a89d3a9f 8 * furnished to do so, subject to the following conditions:
Andres0131 0:3e33a89d3a9f 9 *
Andres0131 0:3e33a89d3a9f 10 * The above copyright notice and this permission notice shall be included in all copies or
Andres0131 0:3e33a89d3a9f 11 * substantial portions of the Software.
Andres0131 0:3e33a89d3a9f 12 *
Andres0131 0:3e33a89d3a9f 13 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
Andres0131 0:3e33a89d3a9f 14 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
Andres0131 0:3e33a89d3a9f 15 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
Andres0131 0:3e33a89d3a9f 16 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
Andres0131 0:3e33a89d3a9f 17 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Andres0131 0:3e33a89d3a9f 18 */
Andres0131 0:3e33a89d3a9f 19
Andres0131 0:3e33a89d3a9f 20
Andres0131 0:3e33a89d3a9f 21 #include "hcsr04.h"
Andres0131 0:3e33a89d3a9f 22
Andres0131 0:3e33a89d3a9f 23
Andres0131 0:3e33a89d3a9f 24 HCSR04::HCSR04(PinName TrigPin,PinName EchoPin):
Andres0131 0:3e33a89d3a9f 25 trigger(TrigPin), echo(EchoPin)
Andres0131 0:3e33a89d3a9f 26 {
Andres0131 0:3e33a89d3a9f 27 pulsetime.stop();
Andres0131 0:3e33a89d3a9f 28 pulsetime.reset();
Andres0131 0:3e33a89d3a9f 29 echo.rise(this,&HCSR04::isr_rise);
Andres0131 0:3e33a89d3a9f 30 echo.fall(this,&HCSR04::isr_fall);
Andres0131 0:3e33a89d3a9f 31 trigger=0;
Andres0131 0:3e33a89d3a9f 32 }
Andres0131 0:3e33a89d3a9f 33
Andres0131 0:3e33a89d3a9f 34 HCSR04::~HCSR04()
Andres0131 0:3e33a89d3a9f 35 {
Andres0131 0:3e33a89d3a9f 36 }
Andres0131 0:3e33a89d3a9f 37
Andres0131 0:3e33a89d3a9f 38 void HCSR04::isr_rise(void)
Andres0131 0:3e33a89d3a9f 39 {
Andres0131 0:3e33a89d3a9f 40 pulsetime.start();
Andres0131 0:3e33a89d3a9f 41 }
Andres0131 0:3e33a89d3a9f 42 void HCSR04::start(void)
Andres0131 0:3e33a89d3a9f 43 {
Andres0131 0:3e33a89d3a9f 44 trigger=1;
Andres0131 0:3e33a89d3a9f 45 wait_us(10);
Andres0131 0:3e33a89d3a9f 46 trigger=0;
Andres0131 0:3e33a89d3a9f 47 }
Andres0131 0:3e33a89d3a9f 48
Andres0131 0:3e33a89d3a9f 49 void HCSR04::isr_fall(void)
Andres0131 0:3e33a89d3a9f 50 {
Andres0131 0:3e33a89d3a9f 51 pulsetime.stop();
Andres0131 0:3e33a89d3a9f 52 pulsedur = pulsetime.read_us();
Andres0131 0:3e33a89d3a9f 53 distance= (pulsedur*343)/20000;
Andres0131 0:3e33a89d3a9f 54 pulsetime.reset();
Andres0131 0:3e33a89d3a9f 55 }
Andres0131 0:3e33a89d3a9f 56
Andres0131 0:3e33a89d3a9f 57 void HCSR04::rise (void (*fptr)(void))
Andres0131 0:3e33a89d3a9f 58 {
Andres0131 0:3e33a89d3a9f 59 echo.rise(fptr);
Andres0131 0:3e33a89d3a9f 60 }
Andres0131 0:3e33a89d3a9f 61 void HCSR04::fall (void (*fptr)(void))
Andres0131 0:3e33a89d3a9f 62 {
Andres0131 0:3e33a89d3a9f 63 echo.fall(fptr);
Andres0131 0:3e33a89d3a9f 64 }
Andres0131 0:3e33a89d3a9f 65
Andres0131 0:3e33a89d3a9f 66 unsigned int HCSR04::get_dist_cm()
Andres0131 0:3e33a89d3a9f 67 {
Andres0131 0:3e33a89d3a9f 68 return distance;
Andres0131 0:3e33a89d3a9f 69 }
Andres0131 0:3e33a89d3a9f 70 unsigned int HCSR04::get_pulse_us()
Andres0131 0:3e33a89d3a9f 71 {
Andres0131 0:3e33a89d3a9f 72 return pulsedur;
Andres0131 0:3e33a89d3a9f 73 }
Andres0131 0:3e33a89d3a9f 74
Andres0131 0:3e33a89d3a9f 75
Andres0131 0:3e33a89d3a9f 76
Andres0131 0:3e33a89d3a9f 77 /*******************************************************
Andres0131 0:3e33a89d3a9f 78 Here is a sample code usage
Andres0131 0:3e33a89d3a9f 79 *********************************************************
Andres0131 0:3e33a89d3a9f 80 #include "hcsr04.h"
Andres0131 0:3e33a89d3a9f 81 HCSR04 usensor(p25,p6);
Andres0131 0:3e33a89d3a9f 82 int main()
Andres0131 0:3e33a89d3a9f 83 {
Andres0131 0:3e33a89d3a9f 84 unsigned char count=0;
Andres0131 0:3e33a89d3a9f 85 while(1) {
Andres0131 0:3e33a89d3a9f 86 usensor.start();
Andres0131 0:3e33a89d3a9f 87 wait_ms(500);
Andres0131 0:3e33a89d3a9f 88 dist=usensor.get_dist_cm();
Andres0131 0:3e33a89d3a9f 89 lcd.cls();
Andres0131 0:3e33a89d3a9f 90 lcd.locate(0,0);
Andres0131 0:3e33a89d3a9f 91 lcd.printf("cm:%ld",dist );
Andres0131 0:3e33a89d3a9f 92
Andres0131 0:3e33a89d3a9f 93 count++;
Andres0131 0:3e33a89d3a9f 94 lcd.locate(0,1);
Andres0131 0:3e33a89d3a9f 95 lcd.printf("Distance =%d",count);
Andres0131 0:3e33a89d3a9f 96
Andres0131 0:3e33a89d3a9f 97 }
Andres0131 0:3e33a89d3a9f 98 */