done
HCSR04.cpp@0:d1d7bb1c1f6c, 2015-02-05 (annotated)
- Committer:
- Nestordp
- Date:
- Thu Feb 05 19:00:51 2015 +0000
- Revision:
- 0:d1d7bb1c1f6c
- Child:
- 2:9daf23ed9d84
- Child:
- 3:12d6d751f4fc
Biblioteca para uso do sonar HC-SR04, implementado na FRDM-LZ25Z
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Nestordp | 0:d1d7bb1c1f6c | 1 | #include "HCSR04.h" |
Nestordp | 0:d1d7bb1c1f6c | 2 | #include "mbed.h" |
Nestordp | 0:d1d7bb1c1f6c | 3 | |
Nestordp | 0:d1d7bb1c1f6c | 4 | |
Nestordp | 0:d1d7bb1c1f6c | 5 | HCSR04::HCSR04(PinName trigger, PinName echo) : _t(trigger), _e(echo) { |
Nestordp | 0:d1d7bb1c1f6c | 6 | wait(0.1); |
Nestordp | 0:d1d7bb1c1f6c | 7 | } |
Nestordp | 0:d1d7bb1c1f6c | 8 | |
Nestordp | 0:d1d7bb1c1f6c | 9 | float HCSR04::getCm(void){ |
Nestordp | 0:d1d7bb1c1f6c | 10 | distcm = readEcho()/58; |
Nestordp | 0:d1d7bb1c1f6c | 11 | return distcm; |
Nestordp | 0:d1d7bb1c1f6c | 12 | } |
Nestordp | 0:d1d7bb1c1f6c | 13 | |
Nestordp | 0:d1d7bb1c1f6c | 14 | float HCSR04::getIn(void){ |
Nestordp | 0:d1d7bb1c1f6c | 15 | distin = readEcho()/148; |
Nestordp | 0:d1d7bb1c1f6c | 16 | return distin; |
Nestordp | 0:d1d7bb1c1f6c | 17 | } |
Nestordp | 0:d1d7bb1c1f6c | 18 | |
Nestordp | 0:d1d7bb1c1f6c | 19 | float HCSR04::readEcho(void){ |
Nestordp | 0:d1d7bb1c1f6c | 20 | _t=1; //Inicio do trigger |
Nestordp | 0:d1d7bb1c1f6c | 21 | wait_us(10); //10us de pulso |
Nestordp | 0:d1d7bb1c1f6c | 22 | _t=0; //Fim do trigger |
Nestordp | 0:d1d7bb1c1f6c | 23 | while(!_e); |
Nestordp | 0:d1d7bb1c1f6c | 24 | _tempo.start(); |
Nestordp | 0:d1d7bb1c1f6c | 25 | while(_e); |
Nestordp | 0:d1d7bb1c1f6c | 26 | tdist = _tempo.read_us(); //Leitura do tempo transcorrido |
Nestordp | 0:d1d7bb1c1f6c | 27 | _tempo.stop(); //Paro o temporizador |
Nestordp | 0:d1d7bb1c1f6c | 28 | _tempo.reset(); //Reset para o próximo ciclo |
Nestordp | 0:d1d7bb1c1f6c | 29 | return tdist; |
Nestordp | 0:d1d7bb1c1f6c | 30 | } |