Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependents: TJPS UltraTest NavigationTest NavigationTest_ ... more
Fork of HCSR04 by
HCSR04.h
00001 /* File: HCSR04.h 00002 * Author: Robert Abad Copyright (c) 2013 00003 * 00004 * Desc: driver for HCSR04 Ultrasonic Range Finder. The returned range 00005 * is in units of meters. 00006 * 00007 * To use this driver you must call the methods ::startMeas() 00008 * and ::getMeas(). The HCSR04 requires a trigger time of 00009 * 10 usec (microseconds) which is initiated by ::startMeas(). 00010 * If a successful measurement is made, getMeas() will return 00011 * RANGE_MEAS_VALID. If unsuccessful, initiate a new measurement. 00012 * 00013 * The datasheet for this device can be found here: 00014 * http://www.elecfreaks.com/store/download/product/Sensor/HC-SR04/HC-SR04_Ultrasonic_Module_User_Guide.pdf 00015 * 00016 * Below is some sample code: 00017 * 00018 * #include "mbed.h" 00019 * #include "HCSR04.h" 00020 * 00021 * #define PIN_TRIGGER (p14) 00022 * #define PIN_ECHO (p15) 00023 * 00024 * int main(void) 00025 * { 00026 * HCSR04 rangeFinder( PIN_TRIGGER, PIN_ECHO ); 00027 * float range; 00028 * 00029 * while (1) 00030 * { 00031 * rangeFinder.startMeas(); 00032 * wait(0.1); 00033 * if ( rangeFinder.getMeas(range) == RANGE_MEAS_VALID ) 00034 * { 00035 * printf("range = %f\n\r", range); 00036 * } 00037 * } 00038 * } 00039 */ 00040 00041 #ifndef __HCSR04_H__ 00042 #define __HCSR04_H__ 00043 00044 #include "mbed.h" 00045 00046 #define MTRS_TO_INCH (39.3701) 00047 00048 typedef enum 00049 { 00050 RANGE_MEAS_INVALID, 00051 RANGE_MEAS_VALID 00052 } etHCSR04_RANGE_STATUS; 00053 00054 00055 00056 class HCSR04 00057 { 00058 public: 00059 HCSR04( PinName pinTrigger, PinName pinEcho ); 00060 void startMeas(void); 00061 etHCSR04_RANGE_STATUS getMeas(float &rRangeMeters); 00062 00063 00064 private: 00065 DigitalOut trigger; 00066 Ticker triggerTicker; 00067 InterruptIn echo; 00068 Timer echoTimer; 00069 unsigned long measTimeStart_us; 00070 unsigned long measTimeStop_us; 00071 unsigned long dTime_us; 00072 etHCSR04_RANGE_STATUS status; 00073 00074 void triggerTicker_cb(void); // trigger ticker callback function 00075 void ISR_echoRising(void); // ISR for rising edge 00076 void ISR_echoFalling(void); // ISR for falling edge 00077 }; 00078 00079 #endif /* __HCSR04_H__ */
Generated on Fri Jul 22 2022 04:29:04 by
