Based the Library PwmIn by Simon Ford expanded with enable_irq and disable_irq for separate input signals. Program gives no real advantage above the original Library.

Dependencies:   mbed

main.cpp

Committer:
GerritPathuis
Date:
2014-01-01
Revision:
0:18d069b5f9f4
Child:
1:4bd14d5374c3

File content as of revision 0:18d069b5f9f4:

#include "mbed.h"
#define NR 20

class PwmIn
{
public:
    PwmIn(PinName p);
    float period();
    float pulsewidth();
    float dutycycle();

protected:
    void rise();
    void fall();

    InterruptIn _p;
    Timer _t;
    float _pulsewidth, _period;
};

PwmIn::PwmIn(PinName p) : _p(p)
{
    _p.rise(this, &PwmIn::rise);
    _p.fall(this, &PwmIn::fall);
    _period = 0.0;
    _pulsewidth = 0.0;
    _t.start();
}

PwmIn b(PTA17); // Sensor 1 Y direction
PwmIn d(PTA16); // Sensor 2 Y direction
Serial pc(USBTX, USBRX); // tx, rx

float PwmIn::period()
{
    return _period;
}

float PwmIn::pulsewidth()
{
    return _pulsewidth;
}

float PwmIn::dutycycle()
{
    return _pulsewidth / _period;
}

void PwmIn::rise()
{
    _period = _t.read();
    _t.reset();
}

void PwmIn::fall()
{
    _pulsewidth = _t.read();
}

struct product {
    float pulse;
    float width;
    float duty;
} acc_a[NR], acc_b[NR], acc_c[NR],acc_d[NR];

int main()
{
    char pbuffer[60]="---";
    int i;

    while(1) {
        //d.disable();
        for (i=0; i<NR; i++) {
            acc_b[i].duty= b.dutycycle();
            //acc_d[i].duty= d.dutycycle();
        }

        for (i=0; i<NR; i++) {
            //acc_b[i].duty= b.dutycycle();
            acc_d[i].duty= d.dutycycle();
        }

        for (i=0; i<NR; i++) {
            sprintf(pbuffer, " b= %f, d= %f \n\r", acc_b[i].duty, acc_d[i].duty);
            pc.puts(pbuffer);
        }
    }
}