Ultrasonic
Dependents: 2014_Ensoul_Capstone Thrower_KRAI ECE4180Finalproject 4180Lab3Part2
Revision 0:4a3021604777, committed 2014-01-09
- Comitter:
- leejong87
- Date:
- Thu Jan 09 07:10:03 2014 +0000
- Child:
- 1:d8a93321fc01
- Commit message:
- Ultrasonic
Changed in this revision
Ultrasonic.cpp | Show annotated file Show diff for this revision Revisions of this file |
Ultrasonic.h | Show annotated file Show diff for this revision Revisions of this file |
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Ultrasonic.cpp Thu Jan 09 07:10:03 2014 +0000 @@ -0,0 +1,40 @@ +#include "Ultrasonic.h" +#include "mbed.h" + +Ultrasonic::Ultrasonic(PinName tx, PinName rx) : _tx(tx),_rx(rx) +{ + _tx.input(); + t.reset(); + triger.attach(this, &Ultrasonic::triger_func, 0.1); + _rx.rise(this, &Ultrasonic::start_func); + _rx.fall(this, &Ultrasonic::end_func); +} + +void Ultrasonic::triger_func() +{ + _tx.output(); + _tx = 1; + wait_us(10); + _tx = 0; + _tx.input(); + +} + +void Ultrasonic::start_func() +{ + t.reset(); + t.start(); +} + +void Ultrasonic::end_func() +{ + t.stop(); + if (( t > 0.00012 ) & ( t < 0.0187)) { + eco = 331.5*(t/2.0)*1000; // 30mm ~ 3000mm + } +} + +float Ultrasonic::read() +{ + return eco; +} \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Ultrasonic.h Thu Jan 09 07:10:03 2014 +0000 @@ -0,0 +1,23 @@ +#ifndef MBED_Ultrasonic_H +#define MBED_Ultrasonic_H + +#include "mbed.h" + +class Ultrasonic +{ +public: + Ultrasonic(PinName tx, PinName rx); + float read(); + +private: + DigitalInOut _tx; + InterruptIn _rx; + Timer t; + Ticker triger; + float eco; + void triger_func(void); + void start_func(void); + void end_func(void); +}; + +#endif