Search Code
About PwmIn

First published 17 Feb 2010, with 2 revisions since.
Last update: 17 Feb 2010.
View history

Last change message: N/A

Related to
PwmIn_ModSerial
Problems with compilation of MODSERIAL
tag MODSERIAL, PwmIn

CHR6dm_reading
CHR-6dm trial
tag CHR-6dm, MODSERIAL

Import this program

PwmIn

Published 17 Feb 2010, by   user Simon Ford   tag PwmIn
Embed: (wiki syntax)

« Back to documentation index

You are viewing an out of date revision of PwmIn! View latest revision

Show/hide line numbers main.cpp Source File

main.cpp

00001 // Example PwmIn class to detect PWM inputs, sford
00002 //  - Note: uses InterruptIn, so not available on p19/p20
00003 
00004 #include "mbed.h"
00005 
00006 class PwmIn {
00007 public:
00008     PwmIn(PinName p) : _p(p) {
00009         _p.rise(this, &PwmIn::rise);
00010         _p.fall(this, &PwmIn::fall);
00011         _period = 0.0;
00012         _pulsewidth = 0.0;
00013         _t.start();
00014     }
00015     
00016     void rise() {
00017         _period = _t.read();
00018         _t.reset();
00019     }
00020     
00021     void fall() {
00022         _pulsewidth = _t.read();
00023     }
00024     
00025     float period() { return _period; }
00026     float pulsewidth() { return _pulsewidth; }
00027     float dutycycle() { return _pulsewidth / _period; }
00028 
00029 protected:        
00030     InterruptIn _p;
00031     Timer _t;
00032     float _pulsewidth, _period;
00033 };
00034 
00035 PwmOut x(p21);
00036 PwmOut y(p22);
00037 
00038 PwmIn a(p5);
00039 PwmIn b(p6);
00040 
00041 int main() {
00042     x = 0.5;
00043     y = 0.2;
00044     while(1) {
00045         printf("a: pw = %f, period = %f\n", a.pulsewidth(), a.period());
00046         printf("b: pw = %f, period = %f\n", b.pulsewidth(), b.period());
00047         wait(2);
00048     }
00049 }