Biblioteca para uso do ultrassom HC-SR04
Dependents: Sonar-HC-SR04 CarrinhoLabirinto Nucleo_Us_ticker_20160803 ProjetoSO ... more
HCSR04.cpp@3:12d6d751f4fc, 2015-02-22 (annotated)
- Committer:
- Nestordp
- Date:
- Sun Feb 22 17:15:47 2015 +0000
- Revision:
- 3:12d6d751f4fc
- Parent:
- 0:d1d7bb1c1f6c
Controlador proporcional
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Nestordp | 3:12d6d751f4fc | 1 | /** |
Nestordp | 3:12d6d751f4fc | 2 | * @author Nestor Pereira-Neto Team: SalvadorEngenharia |
Nestordp | 3:12d6d751f4fc | 3 | * |
Nestordp | 3:12d6d751f4fc | 4 | * @section LICENSE |
Nestordp | 3:12d6d751f4fc | 5 | * |
Nestordp | 3:12d6d751f4fc | 6 | * Copyright (c) 2010 ARM Limited |
Nestordp | 3:12d6d751f4fc | 7 | * |
Nestordp | 3:12d6d751f4fc | 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
Nestordp | 3:12d6d751f4fc | 9 | * of this software and associated documentation files (the "Software"), to deal |
Nestordp | 3:12d6d751f4fc | 10 | * in the Software without restriction, including without limitation the rights |
Nestordp | 3:12d6d751f4fc | 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
Nestordp | 3:12d6d751f4fc | 12 | * copies of the Software, and to permit persons to whom the Software is |
Nestordp | 3:12d6d751f4fc | 13 | * furnished to do so, subject to the following conditions: |
Nestordp | 3:12d6d751f4fc | 14 | * |
Nestordp | 3:12d6d751f4fc | 15 | * The above copyright notice and this permission notice shall be included in |
Nestordp | 3:12d6d751f4fc | 16 | * all copies or substantial portions of the Software. |
Nestordp | 3:12d6d751f4fc | 17 | * |
Nestordp | 3:12d6d751f4fc | 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
Nestordp | 3:12d6d751f4fc | 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
Nestordp | 3:12d6d751f4fc | 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
Nestordp | 3:12d6d751f4fc | 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
Nestordp | 3:12d6d751f4fc | 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
Nestordp | 3:12d6d751f4fc | 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
Nestordp | 3:12d6d751f4fc | 24 | * THE SOFTWARE. |
Nestordp | 3:12d6d751f4fc | 25 | * |
Nestordp | 3:12d6d751f4fc | 26 | * @section DESCRIPTION |
Nestordp | 3:12d6d751f4fc | 27 | * |
Nestordp | 3:12d6d751f4fc | 28 | * Biblioteca para uso do sonar HC-SR04, possui rotinas para permitir medições |
Nestordp | 3:12d6d751f4fc | 29 | * de distências retornado valores em centímetros, polegadas ou o tempo |
Nestordp | 3:12d6d751f4fc | 30 | * percorrido pelo som (em microsegundos). |
Nestordp | 3:12d6d751f4fc | 31 | * Esta biblioteca pode ser usada com qualquer pino do microcontrolador. |
Nestordp | 3:12d6d751f4fc | 32 | */ |
Nestordp | 3:12d6d751f4fc | 33 | |
Nestordp | 3:12d6d751f4fc | 34 | |
Nestordp | 0:d1d7bb1c1f6c | 35 | #include "HCSR04.h" |
Nestordp | 0:d1d7bb1c1f6c | 36 | #include "mbed.h" |
Nestordp | 0:d1d7bb1c1f6c | 37 | |
Nestordp | 0:d1d7bb1c1f6c | 38 | |
Nestordp | 0:d1d7bb1c1f6c | 39 | HCSR04::HCSR04(PinName trigger, PinName echo) : _t(trigger), _e(echo) { |
Nestordp | 0:d1d7bb1c1f6c | 40 | wait(0.1); |
Nestordp | 0:d1d7bb1c1f6c | 41 | } |
Nestordp | 0:d1d7bb1c1f6c | 42 | |
Nestordp | 0:d1d7bb1c1f6c | 43 | float HCSR04::getCm(void){ |
Nestordp | 0:d1d7bb1c1f6c | 44 | distcm = readEcho()/58; |
Nestordp | 0:d1d7bb1c1f6c | 45 | return distcm; |
Nestordp | 0:d1d7bb1c1f6c | 46 | } |
Nestordp | 0:d1d7bb1c1f6c | 47 | |
Nestordp | 0:d1d7bb1c1f6c | 48 | float HCSR04::getIn(void){ |
Nestordp | 0:d1d7bb1c1f6c | 49 | distin = readEcho()/148; |
Nestordp | 0:d1d7bb1c1f6c | 50 | return distin; |
Nestordp | 0:d1d7bb1c1f6c | 51 | } |
Nestordp | 0:d1d7bb1c1f6c | 52 | |
Nestordp | 0:d1d7bb1c1f6c | 53 | float HCSR04::readEcho(void){ |
Nestordp | 0:d1d7bb1c1f6c | 54 | _t=1; //Inicio do trigger |
Nestordp | 0:d1d7bb1c1f6c | 55 | wait_us(10); //10us de pulso |
Nestordp | 0:d1d7bb1c1f6c | 56 | _t=0; //Fim do trigger |
Nestordp | 0:d1d7bb1c1f6c | 57 | while(!_e); |
Nestordp | 0:d1d7bb1c1f6c | 58 | _tempo.start(); |
Nestordp | 0:d1d7bb1c1f6c | 59 | while(_e); |
Nestordp | 0:d1d7bb1c1f6c | 60 | tdist = _tempo.read_us(); //Leitura do tempo transcorrido |
Nestordp | 0:d1d7bb1c1f6c | 61 | _tempo.stop(); //Paro o temporizador |
Nestordp | 0:d1d7bb1c1f6c | 62 | _tempo.reset(); //Reset para o próximo ciclo |
Nestordp | 0:d1d7bb1c1f6c | 63 | return tdist; |
Nestordp | 0:d1d7bb1c1f6c | 64 | } |