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