prog

Dependents:   progetto2 progetto3 progetto4 progetto5 ... more

Committer:
CiroSamu
Date:
Tue Mar 26 12:54:18 2019 +0000
Revision:
0:568f3374c9a7
prog

Who changed what in which revision?

UserRevisionLine numberNew contents of line
CiroSamu 0:568f3374c9a7 1 /* Copyright (c) 2013 Prabhu Desai
CiroSamu 0:568f3374c9a7 2 * pdtechworld@gmail.com
CiroSamu 0:568f3374c9a7 3 *
CiroSamu 0:568f3374c9a7 4 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
CiroSamu 0:568f3374c9a7 5 * and associated documentation files (the "Software"), to deal in the Software without restriction,
CiroSamu 0:568f3374c9a7 6 * including without limitation the rights to use, copy, modify, merge, publish, distribute,
CiroSamu 0:568f3374c9a7 7 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
CiroSamu 0:568f3374c9a7 8 * furnished to do so, subject to the following conditions:
CiroSamu 0:568f3374c9a7 9 *
CiroSamu 0:568f3374c9a7 10 * The above copyright notice and this permission notice shall be included in all copies or
CiroSamu 0:568f3374c9a7 11 * substantial portions of the Software.
CiroSamu 0:568f3374c9a7 12 *
CiroSamu 0:568f3374c9a7 13 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
CiroSamu 0:568f3374c9a7 14 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
CiroSamu 0:568f3374c9a7 15 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
CiroSamu 0:568f3374c9a7 16 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
CiroSamu 0:568f3374c9a7 17 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
CiroSamu 0:568f3374c9a7 18 */
CiroSamu 0:568f3374c9a7 19
CiroSamu 0:568f3374c9a7 20
CiroSamu 0:568f3374c9a7 21 #include "hcsr04.h"
CiroSamu 0:568f3374c9a7 22
CiroSamu 0:568f3374c9a7 23 HCSR04::HCSR04(PinName TrigPin,PinName EchoPin):
CiroSamu 0:568f3374c9a7 24 trigger(TrigPin), echo(EchoPin)
CiroSamu 0:568f3374c9a7 25 {
CiroSamu 0:568f3374c9a7 26 pulsetime.stop();
CiroSamu 0:568f3374c9a7 27 pulsetime.reset();
CiroSamu 0:568f3374c9a7 28 echo.rise(this,&HCSR04::isr_rise);
CiroSamu 0:568f3374c9a7 29 echo.fall(this,&HCSR04::isr_fall);
CiroSamu 0:568f3374c9a7 30 trigger=0;
CiroSamu 0:568f3374c9a7 31 }
CiroSamu 0:568f3374c9a7 32
CiroSamu 0:568f3374c9a7 33 HCSR04::~HCSR04()
CiroSamu 0:568f3374c9a7 34 {
CiroSamu 0:568f3374c9a7 35 }
CiroSamu 0:568f3374c9a7 36
CiroSamu 0:568f3374c9a7 37 void HCSR04::isr_rise(void)
CiroSamu 0:568f3374c9a7 38 {
CiroSamu 0:568f3374c9a7 39 pulsetime.start();
CiroSamu 0:568f3374c9a7 40 }
CiroSamu 0:568f3374c9a7 41 void HCSR04::start(void)
CiroSamu 0:568f3374c9a7 42 {
CiroSamu 0:568f3374c9a7 43 trigger=1;
CiroSamu 0:568f3374c9a7 44 wait_us(10);
CiroSamu 0:568f3374c9a7 45 trigger=0;
CiroSamu 0:568f3374c9a7 46 }
CiroSamu 0:568f3374c9a7 47
CiroSamu 0:568f3374c9a7 48 void HCSR04::isr_fall(void)
CiroSamu 0:568f3374c9a7 49 {
CiroSamu 0:568f3374c9a7 50 pulsetime.stop();
CiroSamu 0:568f3374c9a7 51 pulsedur = pulsetime.read_us();
CiroSamu 0:568f3374c9a7 52 distance= (pulsedur*343)/20000;
CiroSamu 0:568f3374c9a7 53 pulsetime.reset();
CiroSamu 0:568f3374c9a7 54 }
CiroSamu 0:568f3374c9a7 55
CiroSamu 0:568f3374c9a7 56 void HCSR04::rise (void (*fptr)(void))
CiroSamu 0:568f3374c9a7 57 {
CiroSamu 0:568f3374c9a7 58 echo.rise(fptr);
CiroSamu 0:568f3374c9a7 59 }
CiroSamu 0:568f3374c9a7 60 void HCSR04::fall (void (*fptr)(void))
CiroSamu 0:568f3374c9a7 61 {
CiroSamu 0:568f3374c9a7 62 echo.fall(fptr);
CiroSamu 0:568f3374c9a7 63 }
CiroSamu 0:568f3374c9a7 64
CiroSamu 0:568f3374c9a7 65 unsigned int HCSR04::get_dist_cm()
CiroSamu 0:568f3374c9a7 66 {
CiroSamu 0:568f3374c9a7 67 return distance;
CiroSamu 0:568f3374c9a7 68 }
CiroSamu 0:568f3374c9a7 69 unsigned int HCSR04::get_pulse_us()
CiroSamu 0:568f3374c9a7 70 {
CiroSamu 0:568f3374c9a7 71 return pulsedur;
CiroSamu 0:568f3374c9a7 72 }
CiroSamu 0:568f3374c9a7 73
CiroSamu 0:568f3374c9a7 74 float HCSR04::filter(int* measure, int n) {
CiroSamu 0:568f3374c9a7 75 int temp;
CiroSamu 0:568f3374c9a7 76 for(int i=0; i<n-1; i++) {
CiroSamu 0:568f3374c9a7 77 for(int k=0; k<n-1-i; k++) {
CiroSamu 0:568f3374c9a7 78 if(measure[k] > measure[k+1]) {
CiroSamu 0:568f3374c9a7 79 temp = measure[k];
CiroSamu 0:568f3374c9a7 80 measure[k] = measure[k+1];
CiroSamu 0:568f3374c9a7 81 measure[k+1] = temp;
CiroSamu 0:568f3374c9a7 82 }
CiroSamu 0:568f3374c9a7 83 }
CiroSamu 0:568f3374c9a7 84 }
CiroSamu 0:568f3374c9a7 85 if(n%2==0) {
CiroSamu 0:568f3374c9a7 86 int pos_1 = (int)n/2.0 + .5;
CiroSamu 0:568f3374c9a7 87 int pos_2 = (int)n/2.0 - .5;
CiroSamu 0:568f3374c9a7 88 value = (measure[pos_1]+measure[pos_2])/2;
CiroSamu 0:568f3374c9a7 89 } else {
CiroSamu 0:568f3374c9a7 90 value = measure[n/2];
CiroSamu 0:568f3374c9a7 91 }
CiroSamu 0:568f3374c9a7 92
CiroSamu 0:568f3374c9a7 93 return value;
CiroSamu 0:568f3374c9a7 94 }
CiroSamu 0:568f3374c9a7 95
CiroSamu 0:568f3374c9a7 96 /*******************************************************
CiroSamu 0:568f3374c9a7 97 Here is a sample code usage
CiroSamu 0:568f3374c9a7 98 *********************************************************
CiroSamu 0:568f3374c9a7 99 #include "hcsr04.h"
CiroSamu 0:568f3374c9a7 100 HCSR04 usensor(p25,p6);
CiroSamu 0:568f3374c9a7 101 int main()
CiroSamu 0:568f3374c9a7 102 {
CiroSamu 0:568f3374c9a7 103 unsigned char count=0;
CiroSamu 0:568f3374c9a7 104 while(1) {
CiroSamu 0:568f3374c9a7 105 usensor.start();
CiroSamu 0:568f3374c9a7 106 wait_ms(500);
CiroSamu 0:568f3374c9a7 107 dist=usensor.get_dist_cm();
CiroSamu 0:568f3374c9a7 108 lcd.cls();
CiroSamu 0:568f3374c9a7 109 lcd.locate(0,0);
CiroSamu 0:568f3374c9a7 110 lcd.printf("cm:%ld",dist );
CiroSamu 0:568f3374c9a7 111
CiroSamu 0:568f3374c9a7 112 count++;
CiroSamu 0:568f3374c9a7 113 lcd.locate(0,1);
CiroSamu 0:568f3374c9a7 114 lcd.printf("Distance =%d",count);
CiroSamu 0:568f3374c9a7 115
CiroSamu 0:568f3374c9a7 116 }
CiroSamu 0:568f3374c9a7 117 */