A simple PWM decoder class. It is based on using 2 interrupts, one for rising and another for falling edges of the input pulse. This is a very simple approach, possibly accurate enough to decode RC receiver pulses.
Diff: pulse_decoder.cpp
- Revision:
- 0:b0ec6b86deaa
diff -r 000000000000 -r b0ec6b86deaa pulse_decoder.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/pulse_decoder.cpp Sun May 15 11:08:46 2016 +0000 @@ -0,0 +1,27 @@ +#include "mbed.h" +#include "pulse_decoder.h" + +pulse_decoder::pulse_decoder(PinName pin) + : interrupt(pin), high_period(0), low_period(false) +{ + interrupt.rise(this, &pulse_decoder::rising_edge); + interrupt.fall(this, &pulse_decoder::falling_edge); +} + +void pulse_decoder::rising_edge() +{ + high_timer.start(); + low_timer.stop(); + + low_period = low_timer.read_us(); + low_timer.reset(); +} + +void pulse_decoder::falling_edge() +{ + high_timer.stop(); + low_timer.start(); + + high_period = high_timer.read_us(); + high_timer.reset(); +}