Ultrasound Ranging Sensor module

Dependents:   HC-SR04

Committer:
shina
Date:
Sat Apr 27 13:38:55 2019 +0000
Revision:
8:095071ae3cac
Parent:
6:4be4fcd0c171
This is a prototype program about HC-SR04.

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 {
prabhuvd 0:fb0929f37ebe 27 pulsetime.stop();
prabhuvd 0:fb0929f37ebe 28 pulsetime.reset();
prabhuvd 6:4be4fcd0c171 29 echo.rise(this,&HCSR04::isr_rise);
prabhuvd 6:4be4fcd0c171 30 echo.fall(this,&HCSR04::isr_fall);
prabhuvd 2:0acb6ade091c 31 trigger=0;
shina 8:095071ae3cac 32 this->temperature=25;
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::isr_rise(void)
prabhuvd 0:fb0929f37ebe 40 {
prabhuvd 0:fb0929f37ebe 41 pulsetime.start();
prabhuvd 0:fb0929f37ebe 42 }
prabhuvd 6:4be4fcd0c171 43 void HCSR04::start(void)
prabhuvd 2:0acb6ade091c 44 {
prabhuvd 2:0acb6ade091c 45 trigger=1;
prabhuvd 2:0acb6ade091c 46 wait_us(10);
prabhuvd 2:0acb6ade091c 47 trigger=0;
prabhuvd 2:0acb6ade091c 48 }
shina 8:095071ae3cac 49 void HCSR04::set_temperature(int temperature)
shina 8:095071ae3cac 50 {
shina 8:095071ae3cac 51 this->temperature=temperature;
shina 8:095071ae3cac 52 }
prabhuvd 0:fb0929f37ebe 53
prabhuvd 6:4be4fcd0c171 54 void HCSR04::isr_fall(void)
prabhuvd 0:fb0929f37ebe 55 {
prabhuvd 0:fb0929f37ebe 56 pulsetime.stop();
prabhuvd 0:fb0929f37ebe 57 pulsedur = pulsetime.read_us();
shina 8:095071ae3cac 58 distance=(pulsedur*(331.5+temperature*0.6)/20000);
prabhuvd 0:fb0929f37ebe 59 pulsetime.reset();
prabhuvd 0:fb0929f37ebe 60 }
prabhuvd 4:33938a97d904 61
prabhuvd 6:4be4fcd0c171 62 void HCSR04::rise (void (*fptr)(void))
prabhuvd 0:fb0929f37ebe 63 {
prabhuvd 0:fb0929f37ebe 64 echo.rise(fptr);
prabhuvd 0:fb0929f37ebe 65 }
prabhuvd 6:4be4fcd0c171 66 void HCSR04::fall (void (*fptr)(void))
prabhuvd 0:fb0929f37ebe 67 {
prabhuvd 0:fb0929f37ebe 68 echo.fall(fptr);
prabhuvd 0:fb0929f37ebe 69 }
prabhuvd 0:fb0929f37ebe 70
prabhuvd 6:4be4fcd0c171 71 unsigned int HCSR04::get_dist_cm()
prabhuvd 0:fb0929f37ebe 72 {
prabhuvd 2:0acb6ade091c 73 return distance;
prabhuvd 0:fb0929f37ebe 74 }
prabhuvd 6:4be4fcd0c171 75 unsigned int HCSR04::get_pulse_us()
prabhuvd 2:0acb6ade091c 76 {
prabhuvd 2:0acb6ade091c 77 return pulsedur;
prabhuvd 2:0acb6ade091c 78 }
prabhuvd 5:e70a24a88131 79
prabhuvd 5:e70a24a88131 80
prabhuvd 5:e70a24a88131 81
prabhuvd 5:e70a24a88131 82 /*******************************************************
prabhuvd 5:e70a24a88131 83 Here is a sample code usage
prabhuvd 5:e70a24a88131 84 *********************************************************
prabhuvd 5:e70a24a88131 85 #include "hcsr04.h"
prabhuvd 6:4be4fcd0c171 86 HCSR04 usensor(p25,p6);
prabhuvd 5:e70a24a88131 87 int main()
prabhuvd 5:e70a24a88131 88 {
prabhuvd 5:e70a24a88131 89 unsigned char count=0;
prabhuvd 5:e70a24a88131 90 while(1) {
prabhuvd 5:e70a24a88131 91 usensor.start();
prabhuvd 5:e70a24a88131 92 wait_ms(500);
prabhuvd 5:e70a24a88131 93 dist=usensor.get_dist_cm();
prabhuvd 5:e70a24a88131 94 lcd.cls();
prabhuvd 5:e70a24a88131 95 lcd.locate(0,0);
prabhuvd 5:e70a24a88131 96 lcd.printf("cm:%ld",dist );
prabhuvd 5:e70a24a88131 97
prabhuvd 5:e70a24a88131 98 count++;
prabhuvd 5:e70a24a88131 99 lcd.locate(0,1);
prabhuvd 5:e70a24a88131 100 lcd.printf("Distance =%d",count);
prabhuvd 5:e70a24a88131 101
prabhuvd 5:e70a24a88131 102 }
prabhuvd 5:e70a24a88131 103 */