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