dad

Dependencies:   mbed-os FXAS21000 LED_Bar FXOS8700Q

Committer:
mukundy8
Date:
Wed Apr 27 00:22:09 2022 +0000
Revision:
4:91893b37450e
Parent:
2:d08625231a9f
final;

Who changed what in which revision?

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