![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Proejct2
hcsr04.cpp@2:0acb6ade091c, 2013-03-30 (annotated)
- Committer:
- prabhuvd
- Date:
- Sat Mar 30 18:41:34 2013 +0000
- Revision:
- 2:0acb6ade091c
- Parent:
- 1:093521f56089
- Child:
- 3:e6795cb9439c
Updated libray
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
prabhuvd | 0:fb0929f37ebe | 1 | /* Copyright (c) 2013 Prabhu Desai |
prabhuvd | 0:fb0929f37ebe | 2 | * pdtechworld@gmail.com |
prabhuvd | 0:fb0929f37ebe | 3 | * |
prabhuvd | 0:fb0929f37ebe | 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy of this software |
prabhuvd | 0:fb0929f37ebe | 5 | * and associated documentation files (the "Software"), to deal in the Software without restriction, |
prabhuvd | 0:fb0929f37ebe | 6 | * including without limitation the rights to use, copy, modify, merge, publish, distribute, |
prabhuvd | 0:fb0929f37ebe | 7 | * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is |
prabhuvd | 0:fb0929f37ebe | 8 | * furnished to do so, subject to the following conditions: |
prabhuvd | 0:fb0929f37ebe | 9 | * |
prabhuvd | 0:fb0929f37ebe | 10 | * The above copyright notice and this permission notice shall be included in all copies or |
prabhuvd | 0:fb0929f37ebe | 11 | * substantial portions of the Software. |
prabhuvd | 0:fb0929f37ebe | 12 | * |
prabhuvd | 0:fb0929f37ebe | 13 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING |
prabhuvd | 0:fb0929f37ebe | 14 | * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
prabhuvd | 0:fb0929f37ebe | 15 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
prabhuvd | 0:fb0929f37ebe | 16 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
prabhuvd | 0:fb0929f37ebe | 17 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
prabhuvd | 0:fb0929f37ebe | 18 | */ |
prabhuvd | 0:fb0929f37ebe | 19 | |
prabhuvd | 0:fb0929f37ebe | 20 | |
prabhuvd | 0:fb0929f37ebe | 21 | #include "hcsr04.h" |
prabhuvd | 0:fb0929f37ebe | 22 | |
prabhuvd | 0:fb0929f37ebe | 23 | |
prabhuvd | 2:0acb6ade091c | 24 | DistMeasure::DistMeasure(PinName TrigPin,PinName EchoPin): |
prabhuvd | 2:0acb6ade091c | 25 | trigger(TrigPin), echo(EchoPin) |
prabhuvd | 0:fb0929f37ebe | 26 | { |
prabhuvd | 0:fb0929f37ebe | 27 | pulsetime.stop(); |
prabhuvd | 0:fb0929f37ebe | 28 | pulsetime.reset(); |
prabhuvd | 1:093521f56089 | 29 | echo.rise(this,&DistMeasure::isr_rise); |
prabhuvd | 2:0acb6ade091c | 30 | echo.fall(this,&DistMeasure::isr_fall); |
prabhuvd | 2:0acb6ade091c | 31 | trigger=0; |
prabhuvd | 0:fb0929f37ebe | 32 | } |
prabhuvd | 0:fb0929f37ebe | 33 | |
prabhuvd | 0:fb0929f37ebe | 34 | DistMeasure::~DistMeasure() |
prabhuvd | 0:fb0929f37ebe | 35 | { |
prabhuvd | 0:fb0929f37ebe | 36 | } |
prabhuvd | 0:fb0929f37ebe | 37 | |
prabhuvd | 0:fb0929f37ebe | 38 | void DistMeasure::isr_rise(void) |
prabhuvd | 0:fb0929f37ebe | 39 | { |
prabhuvd | 0:fb0929f37ebe | 40 | pulsetime.start(); |
prabhuvd | 0:fb0929f37ebe | 41 | } |
prabhuvd | 2:0acb6ade091c | 42 | void DistMeasure::start_measurement(void) |
prabhuvd | 2:0acb6ade091c | 43 | { |
prabhuvd | 2:0acb6ade091c | 44 | trigger=1; |
prabhuvd | 2:0acb6ade091c | 45 | wait_us(10); |
prabhuvd | 2:0acb6ade091c | 46 | trigger=0; |
prabhuvd | 2:0acb6ade091c | 47 | } |
prabhuvd | 0:fb0929f37ebe | 48 | |
prabhuvd | 0:fb0929f37ebe | 49 | void DistMeasure::isr_fall(void) |
prabhuvd | 0:fb0929f37ebe | 50 | { |
prabhuvd | 0:fb0929f37ebe | 51 | pulsetime.stop(); |
prabhuvd | 0:fb0929f37ebe | 52 | pulsedur = pulsetime.read_us(); |
prabhuvd | 0:fb0929f37ebe | 53 | distance= (pulsedur*343)/20000; |
prabhuvd | 0:fb0929f37ebe | 54 | pulsetime.reset(); |
prabhuvd | 0:fb0929f37ebe | 55 | } |
prabhuvd | 2:0acb6ade091c | 56 | |
prabhuvd | 0:fb0929f37ebe | 57 | void DistMeasure::rise (void (*fptr)(void)) |
prabhuvd | 0:fb0929f37ebe | 58 | { |
prabhuvd | 0:fb0929f37ebe | 59 | echo.rise(fptr); |
prabhuvd | 0:fb0929f37ebe | 60 | } |
prabhuvd | 0:fb0929f37ebe | 61 | void DistMeasure::fall (void (*fptr)(void)) |
prabhuvd | 0:fb0929f37ebe | 62 | { |
prabhuvd | 0:fb0929f37ebe | 63 | echo.fall(fptr); |
prabhuvd | 0:fb0929f37ebe | 64 | } |
prabhuvd | 0:fb0929f37ebe | 65 | |
prabhuvd | 2:0acb6ade091c | 66 | unsigned int DistMeasure::get_dist_cm() |
prabhuvd | 0:fb0929f37ebe | 67 | { |
prabhuvd | 2:0acb6ade091c | 68 | return distance; |
prabhuvd | 0:fb0929f37ebe | 69 | } |
prabhuvd | 2:0acb6ade091c | 70 | unsigned int DistMeasure::get_pulse_us() |
prabhuvd | 2:0acb6ade091c | 71 | { |
prabhuvd | 2:0acb6ade091c | 72 | return pulsedur; |
prabhuvd | 2:0acb6ade091c | 73 | } |