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.

pulse_decoder.cpp

Committer:
salarian
Date:
2016-05-15
Revision:
0:b0ec6b86deaa

File content as of revision 0:b0ec6b86deaa:

#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();
}