かんたくんのやつです。

Fork of HCSR04 by Prabhu Desai

Committer:
tektomo
Date:
Sat Sep 22 11:00:29 2018 +0000
Revision:
8:0fda00df4357
Parent:
6:4be4fcd0c171
????????

Who changed what in which revision?

UserRevisionLine numberNew contents of line
prabhuvd 0:fb0929f37ebe 1 /* Copyright (c) 2013 Prabhu Desai
prabhuvd 0:fb0929f37ebe 2 * pdtechworld@gmail.com
prabhuvd 0:fb0929f37ebe 3 *
prabhuvd 0:fb0929f37ebe 4 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
prabhuvd 0:fb0929f37ebe 5 * and associated documentation files (the "Software"), to deal in the Software without restriction,
prabhuvd 0:fb0929f37ebe 6 * including without limitation the rights to use, copy, modify, merge, publish, distribute,
prabhuvd 0:fb0929f37ebe 7 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
prabhuvd 0:fb0929f37ebe 8 * furnished to do so, subject to the following conditions:
prabhuvd 0:fb0929f37ebe 9 *
prabhuvd 0:fb0929f37ebe 10 * The above copyright notice and this permission notice shall be included in all copies or
prabhuvd 0:fb0929f37ebe 11 * substantial portions of the Software.
prabhuvd 0:fb0929f37ebe 12 *
prabhuvd 0:fb0929f37ebe 13 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
prabhuvd 0:fb0929f37ebe 14 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
prabhuvd 0:fb0929f37ebe 15 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
prabhuvd 0:fb0929f37ebe 16 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
prabhuvd 0:fb0929f37ebe 17 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
prabhuvd 0:fb0929f37ebe 18 */
prabhuvd 0:fb0929f37ebe 19
prabhuvd 0:fb0929f37ebe 20
tektomo 8:0fda00df4357 21
tektomo 8:0fda00df4357 22
prabhuvd 0:fb0929f37ebe 23 #include "hcsr04.h"
prabhuvd 0:fb0929f37ebe 24
prabhuvd 0:fb0929f37ebe 25
tektomo 8:0fda00df4357 26
tektomo 8:0fda00df4357 27
prabhuvd 6:4be4fcd0c171 28 HCSR04::HCSR04(PinName TrigPin,PinName EchoPin):
prabhuvd 2:0acb6ade091c 29 trigger(TrigPin), echo(EchoPin)
prabhuvd 0:fb0929f37ebe 30 {
tektomo 8:0fda00df4357 31 setTemp(15);
prabhuvd 0:fb0929f37ebe 32 pulsetime.stop();
prabhuvd 0:fb0929f37ebe 33 pulsetime.reset();
prabhuvd 6:4be4fcd0c171 34 echo.rise(this,&HCSR04::isr_rise);
prabhuvd 6:4be4fcd0c171 35 echo.fall(this,&HCSR04::isr_fall);
prabhuvd 2:0acb6ade091c 36 trigger=0;
prabhuvd 0:fb0929f37ebe 37 }
prabhuvd 0:fb0929f37ebe 38
tektomo 8:0fda00df4357 39
prabhuvd 6:4be4fcd0c171 40 HCSR04::~HCSR04()
prabhuvd 0:fb0929f37ebe 41 {
prabhuvd 0:fb0929f37ebe 42 }
prabhuvd 0:fb0929f37ebe 43
tektomo 8:0fda00df4357 44
tektomo 8:0fda00df4357 45 void HCSR04::start(void)
tektomo 8:0fda00df4357 46 {
tektomo 8:0fda00df4357 47 pulsetime_us = pulsetime.read_us();
tektomo 8:0fda00df4357 48 if(pulsetime_us > maxTime){
tektomo 8:0fda00df4357 49 pulsetime.stop();
tektomo 8:0fda00df4357 50 pulsetime.reset();
tektomo 8:0fda00df4357 51 trigger=1;
tektomo 8:0fda00df4357 52 trigger=0;
tektomo 8:0fda00df4357 53 }
tektomo 8:0fda00df4357 54 trigger=1;
tektomo 8:0fda00df4357 55 wait_us(10);
tektomo 8:0fda00df4357 56 trigger=0;
tektomo 8:0fda00df4357 57 }
tektomo 8:0fda00df4357 58
tektomo 8:0fda00df4357 59
prabhuvd 6:4be4fcd0c171 60 void HCSR04::isr_rise(void)
prabhuvd 0:fb0929f37ebe 61 {
prabhuvd 0:fb0929f37ebe 62 pulsetime.start();
prabhuvd 0:fb0929f37ebe 63 }
tektomo 8:0fda00df4357 64
prabhuvd 0:fb0929f37ebe 65
prabhuvd 6:4be4fcd0c171 66 void HCSR04::isr_fall(void)
prabhuvd 0:fb0929f37ebe 67 {
prabhuvd 0:fb0929f37ebe 68 pulsetime.stop();
tektomo 8:0fda00df4357 69 pulsetime_us = pulsetime.read_us();
tektomo 8:0fda00df4357 70 if(pulsetime_us > maxTime){
tektomo 8:0fda00df4357 71 trigger=1;
tektomo 8:0fda00df4357 72 trigger=0;
tektomo 8:0fda00df4357 73 distance = 0;
tektomo 8:0fda00df4357 74 }else{
tektomo 8:0fda00df4357 75 distance = pulsetime_us*sonic/20000;
tektomo 8:0fda00df4357 76 }
prabhuvd 0:fb0929f37ebe 77 pulsetime.reset();
prabhuvd 0:fb0929f37ebe 78 }
prabhuvd 4:33938a97d904 79
tektomo 8:0fda00df4357 80
prabhuvd 6:4be4fcd0c171 81 void HCSR04::rise (void (*fptr)(void))
prabhuvd 0:fb0929f37ebe 82 {
prabhuvd 0:fb0929f37ebe 83 echo.rise(fptr);
prabhuvd 0:fb0929f37ebe 84 }
prabhuvd 6:4be4fcd0c171 85 void HCSR04::fall (void (*fptr)(void))
prabhuvd 0:fb0929f37ebe 86 {
prabhuvd 0:fb0929f37ebe 87 echo.fall(fptr);
prabhuvd 0:fb0929f37ebe 88 }
prabhuvd 0:fb0929f37ebe 89
tektomo 8:0fda00df4357 90
prabhuvd 6:4be4fcd0c171 91 unsigned int HCSR04::get_dist_cm()
prabhuvd 0:fb0929f37ebe 92 {
prabhuvd 2:0acb6ade091c 93 return distance;
prabhuvd 0:fb0929f37ebe 94 }
prabhuvd 5:e70a24a88131 95
prabhuvd 5:e70a24a88131 96
tektomo 8:0fda00df4357 97 void HCSR04::setTemp(int temp){
tektomo 8:0fda00df4357 98 temperature = temp;
tektomo 8:0fda00df4357 99 sonic = 331.5+0.61*temperature;
tektomo 8:0fda00df4357 100 maxTime = 10*1000000/sonic;
tektomo 8:0fda00df4357 101 }