yuto kawamura
/
MR2_n_1_class
l
hcsr04/hcsr04.cpp@6:75cfa1a66382, 2019-04-27 (annotated)
- Committer:
- yuto17320508
- Date:
- Sat Apr 27 11:22:00 2019 +0000
- Revision:
- 6:75cfa1a66382
- Parent:
- 0:111abd91b0cb
l
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
eri | 0:111abd91b0cb | 1 | #include "hcsr04.h" |
eri | 0:111abd91b0cb | 2 | |
eri | 0:111abd91b0cb | 3 | #define v_o 343 |
eri | 0:111abd91b0cb | 4 | |
eri | 0:111abd91b0cb | 5 | HCSR04::HCSR04(PinName TrigPin,PinName EchoPin): |
eri | 0:111abd91b0cb | 6 | trigger(TrigPin), echo(EchoPin) |
eri | 0:111abd91b0cb | 7 | { |
eri | 0:111abd91b0cb | 8 | pulsetime.stop(); |
eri | 0:111abd91b0cb | 9 | pulsetime.reset(); |
eri | 0:111abd91b0cb | 10 | echo.rise(this,&HCSR04::isr_rise); |
eri | 0:111abd91b0cb | 11 | echo.fall(this,&HCSR04::isr_fall); |
eri | 0:111abd91b0cb | 12 | trigger=0; |
eri | 0:111abd91b0cb | 13 | } |
eri | 0:111abd91b0cb | 14 | |
eri | 0:111abd91b0cb | 15 | HCSR04::~HCSR04() |
eri | 0:111abd91b0cb | 16 | { |
eri | 0:111abd91b0cb | 17 | } |
eri | 0:111abd91b0cb | 18 | |
eri | 0:111abd91b0cb | 19 | void HCSR04::isr_rise(void) |
eri | 0:111abd91b0cb | 20 | { |
eri | 0:111abd91b0cb | 21 | pulsetime.start(); |
eri | 0:111abd91b0cb | 22 | } |
eri | 0:111abd91b0cb | 23 | void HCSR04::start(void) |
eri | 0:111abd91b0cb | 24 | { |
eri | 0:111abd91b0cb | 25 | trigger=1; |
eri | 0:111abd91b0cb | 26 | wait_us(10); |
eri | 0:111abd91b0cb | 27 | trigger=0; |
eri | 0:111abd91b0cb | 28 | } |
eri | 0:111abd91b0cb | 29 | |
eri | 0:111abd91b0cb | 30 | void HCSR04::isr_fall(void) |
eri | 0:111abd91b0cb | 31 | { |
eri | 0:111abd91b0cb | 32 | pulsetime.stop(); |
eri | 0:111abd91b0cb | 33 | pulsedur = pulsetime.read_us(); |
eri | 0:111abd91b0cb | 34 | distance= (pulsedur*v_o)/20000; |
eri | 0:111abd91b0cb | 35 | pulsetime.reset(); |
eri | 0:111abd91b0cb | 36 | } |
eri | 0:111abd91b0cb | 37 | |
eri | 0:111abd91b0cb | 38 | void HCSR04::rise (void (*fptr)(void)) |
eri | 0:111abd91b0cb | 39 | { |
eri | 0:111abd91b0cb | 40 | echo.rise(fptr); |
eri | 0:111abd91b0cb | 41 | } |
eri | 0:111abd91b0cb | 42 | void HCSR04::fall (void (*fptr)(void)) |
eri | 0:111abd91b0cb | 43 | { |
eri | 0:111abd91b0cb | 44 | echo.fall(fptr); |
eri | 0:111abd91b0cb | 45 | } |
eri | 0:111abd91b0cb | 46 | |
eri | 0:111abd91b0cb | 47 | float HCSR04::get_dist_cm() |
eri | 0:111abd91b0cb | 48 | { |
eri | 0:111abd91b0cb | 49 | return distance; |
eri | 0:111abd91b0cb | 50 | } |
eri | 0:111abd91b0cb | 51 | float HCSR04::get_pulse_us() |
eri | 0:111abd91b0cb | 52 | { |
eri | 0:111abd91b0cb | 53 | return pulsedur; |
eri | 0:111abd91b0cb | 54 | } |