h

Dependents:   frdm_rtos frdm_gpiokath Sumo

Committer:
lavioros
Date:
Wed Jan 09 19:24:29 2019 +0000
Revision:
0:4947ffc8c310
hh

Who changed what in which revision?

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