Ultrasound Ranging Sensor module

Dependents:   Lets_move_Seki2018_ver2 Lets_move_Seki2018_null UNKO_FINAL_PlamA_2 UNKO_FINAL_PlanA

Fork of HCSR04 by Prabhu Desai

Committer:
yuron
Date:
Wed Sep 05 15:06:24 2018 +0000
Revision:
8:e150242bce6b
Parent:
6:4be4fcd0c171
2018/09/05???B??????????????(??????????)

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
prabhuvd 0:fb0929f37ebe 21 #include "hcsr04.h"
prabhuvd 0:fb0929f37ebe 22
prabhuvd 0:fb0929f37ebe 23
prabhuvd 6:4be4fcd0c171 24 HCSR04::HCSR04(PinName TrigPin,PinName EchoPin):
prabhuvd 2:0acb6ade091c 25 trigger(TrigPin), echo(EchoPin)
prabhuvd 0:fb0929f37ebe 26 {
yuron 8:e150242bce6b 27 setTemp(15);
prabhuvd 0:fb0929f37ebe 28 pulsetime.stop();
prabhuvd 0:fb0929f37ebe 29 pulsetime.reset();
prabhuvd 6:4be4fcd0c171 30 echo.rise(this,&HCSR04::isr_rise);
prabhuvd 6:4be4fcd0c171 31 echo.fall(this,&HCSR04::isr_fall);
prabhuvd 2:0acb6ade091c 32 trigger=0;
prabhuvd 0:fb0929f37ebe 33 }
prabhuvd 0:fb0929f37ebe 34
prabhuvd 6:4be4fcd0c171 35 HCSR04::~HCSR04()
prabhuvd 0:fb0929f37ebe 36 {
prabhuvd 0:fb0929f37ebe 37 }
prabhuvd 0:fb0929f37ebe 38
prabhuvd 6:4be4fcd0c171 39 void HCSR04::start(void)
prabhuvd 2:0acb6ade091c 40 {
yuron 8:e150242bce6b 41 pulsetime_us = pulsetime.read_us();
yuron 8:e150242bce6b 42 if(pulsetime_us > maxTime){
yuron 8:e150242bce6b 43 pulsetime.stop();
yuron 8:e150242bce6b 44 pulsetime.reset();
yuron 8:e150242bce6b 45 trigger=1;
yuron 8:e150242bce6b 46 trigger=0;
yuron 8:e150242bce6b 47 }
prabhuvd 2:0acb6ade091c 48 trigger=1;
prabhuvd 2:0acb6ade091c 49 wait_us(10);
prabhuvd 2:0acb6ade091c 50 trigger=0;
prabhuvd 2:0acb6ade091c 51 }
prabhuvd 0:fb0929f37ebe 52
yuron 8:e150242bce6b 53 void HCSR04::isr_rise(void)
yuron 8:e150242bce6b 54 {
yuron 8:e150242bce6b 55 pulsetime.start();
yuron 8:e150242bce6b 56 }
yuron 8:e150242bce6b 57
prabhuvd 6:4be4fcd0c171 58 void HCSR04::isr_fall(void)
prabhuvd 0:fb0929f37ebe 59 {
prabhuvd 0:fb0929f37ebe 60 pulsetime.stop();
yuron 8:e150242bce6b 61 pulsetime_us = pulsetime.read_us();
yuron 8:e150242bce6b 62 if(pulsetime_us > maxTime){
yuron 8:e150242bce6b 63 trigger=1;
yuron 8:e150242bce6b 64 trigger=0;
yuron 8:e150242bce6b 65 distance = 0;
yuron 8:e150242bce6b 66 }else{
yuron 8:e150242bce6b 67 distance = pulsetime_us*sonic/20000;
yuron 8:e150242bce6b 68 }
prabhuvd 0:fb0929f37ebe 69 pulsetime.reset();
prabhuvd 0:fb0929f37ebe 70 }
prabhuvd 4:33938a97d904 71
prabhuvd 6:4be4fcd0c171 72 void HCSR04::rise (void (*fptr)(void))
prabhuvd 0:fb0929f37ebe 73 {
prabhuvd 0:fb0929f37ebe 74 echo.rise(fptr);
prabhuvd 0:fb0929f37ebe 75 }
prabhuvd 6:4be4fcd0c171 76 void HCSR04::fall (void (*fptr)(void))
prabhuvd 0:fb0929f37ebe 77 {
prabhuvd 0:fb0929f37ebe 78 echo.fall(fptr);
prabhuvd 0:fb0929f37ebe 79 }
prabhuvd 0:fb0929f37ebe 80
prabhuvd 6:4be4fcd0c171 81 unsigned int HCSR04::get_dist_cm()
prabhuvd 0:fb0929f37ebe 82 {
prabhuvd 2:0acb6ade091c 83 return distance;
prabhuvd 0:fb0929f37ebe 84 }
yuron 8:e150242bce6b 85
yuron 8:e150242bce6b 86 void HCSR04::setTemp(int temp){
yuron 8:e150242bce6b 87 temperature = temp;
yuron 8:e150242bce6b 88 sonic = 331.5+0.61*temperature;
yuron 8:e150242bce6b 89 maxTime = 10*1000000/sonic;
prabhuvd 2:0acb6ade091c 90 }
prabhuvd 5:e70a24a88131 91
prabhuvd 5:e70a24a88131 92 /*******************************************************
prabhuvd 5:e70a24a88131 93 Here is a sample code usage
prabhuvd 5:e70a24a88131 94 *********************************************************
prabhuvd 5:e70a24a88131 95 #include "hcsr04.h"
prabhuvd 6:4be4fcd0c171 96 HCSR04 usensor(p25,p6);
prabhuvd 5:e70a24a88131 97 int main()
prabhuvd 5:e70a24a88131 98 {
prabhuvd 5:e70a24a88131 99 unsigned char count=0;
yuron 8:e150242bce6b 100 setTep(20);
prabhuvd 5:e70a24a88131 101 while(1) {
prabhuvd 5:e70a24a88131 102 usensor.start();
prabhuvd 5:e70a24a88131 103 wait_ms(500);
prabhuvd 5:e70a24a88131 104 dist=usensor.get_dist_cm();
prabhuvd 5:e70a24a88131 105 lcd.cls();
prabhuvd 5:e70a24a88131 106 lcd.locate(0,0);
prabhuvd 5:e70a24a88131 107 lcd.printf("cm:%ld",dist );
prabhuvd 5:e70a24a88131 108
prabhuvd 5:e70a24a88131 109 count++;
prabhuvd 5:e70a24a88131 110 lcd.locate(0,1);
prabhuvd 5:e70a24a88131 111 lcd.printf("Distance =%d",count);
prabhuvd 5:e70a24a88131 112
prabhuvd 5:e70a24a88131 113 }
prabhuvd 5:e70a24a88131 114 */