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