Gerrit Pathuis / Mbed 2 deprecated PwmIn_Irq-disable

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 //////////////////////////////////////
00002 // Based on PwmIn Library
00003 // The basis is to disable the interrupts
00004 // of the other PwmIn inputs
00005 // Connected to the function generator
00006 // puls on both inputs at the exact
00007 // same time.
00008 // Works excellent but further testing
00009 // showed that there is no real advantage 
00010 // above the original PmwIn library
00011 //////////////////////////////////////
00012 #include "mbed.h"
00013 #include "InterruptIn.h"
00014 #define NR 20
00015 
00016 class PwmIn
00017 {
00018 public:
00019     PwmIn(PinName p);
00020     float period();
00021     float pulsewidth();
00022     float dutycycle();
00023     void disable();
00024     void enable();
00025 
00026 protected:
00027     void rise();
00028     void fall();
00029 
00030     InterruptIn _p;
00031     Timer _t;
00032     volatile float _pulsewidth, _period;
00033 };
00034 
00035 PwmIn::PwmIn(PinName p) : _p(p)
00036 {
00037     _p.rise(this, &PwmIn::rise);
00038     _p.fall(this, &PwmIn::fall);
00039     _period = 0.0;
00040     _pulsewidth = 0.0;
00041     _t.start();
00042 }
00043 
00044 PwmIn a(PTD2);  // Sensor 1 X direction
00045 PwmIn b(PTA17); // Sensor 1 Y direction
00046 Serial pc(USBTX, USBRX); // tx, rx
00047 
00048 float PwmIn::period()
00049 {
00050     return _period;
00051 }
00052 
00053 float PwmIn::pulsewidth()
00054 {
00055     return _pulsewidth;
00056 }
00057 
00058 float PwmIn::dutycycle()
00059 {
00060     return _pulsewidth / _period;
00061 }
00062 
00063 void PwmIn::disable()
00064 {
00065     _p.disable_irq();
00066 }
00067 
00068 void PwmIn::enable()
00069 {
00070     _p.enable_irq();
00071 }
00072 
00073 void PwmIn::rise()
00074 {
00075     _period = _t.read();
00076     _t.reset();
00077 }
00078 
00079 void PwmIn::fall()
00080 {
00081     _pulsewidth = _t.read();
00082 }
00083 
00084 struct product {
00085     float pulsewidth;
00086     float period;
00087     float cycle;
00088 } acc_a[NR], acc_b[NR], acc_c[NR],acc_d[NR];
00089 
00090 
00091 int main()
00092 {
00093     char pbuffer[512]="---";
00094     int i;
00095 
00096     pc.baud(9600);
00097     sprintf(pbuffer, "Start test, baudrate is set to 9600 \n\r");
00098     pc.puts(pbuffer);
00099 
00100     a.disable();
00101     b.disable();
00102     while(1) {
00103         //-------------- sensor a--------------------
00104         a.enable(); // signal on input 50Hz is period of 20 milli second
00105         wait_ms(40); // signal on input 50Hz is period of 20 milli second
00106         for (i=0; i<NR; i++) {
00107             acc_a[i].pulsewidth= a.pulsewidth();    // Sensor A
00108             acc_a[i].period= a.period();            // Sensor A
00109             acc_a[i].cycle= a.dutycycle();          // Sensor A
00110         }
00111         a.disable();
00112 
00113         //-------------- sensor b--------------------
00114         b.enable(); // signal on input 50Hz is period of 20 milli second
00115         wait_ms(40); // 50Hz is period of 20 milli second
00116         for (i=0; i<NR; i++) {
00117             acc_b[i].pulsewidth= b.pulsewidth();    // Sensor B
00118             acc_b[i].period= b.period();            // Sensor B
00119             acc_b[i].cycle= b.dutycycle();          // Sensor B
00120         }
00121         b.disable();
00122 
00123         for (i=0; i<NR; i++) {
00124             sprintf(pbuffer, "a_pulse= %f, a_period= %f, a_c= %f, b_puls= %f, b_period= %f, b_cycle %f\n\r", acc_a[i].pulsewidth, acc_a[i].period, acc_a[i].cycle, acc_b[i].pulsewidth, acc_b[i].period, acc_b[i].cycle);
00125             pc.puts(pbuffer);
00126         }
00127         sprintf(pbuffer, "\n\r");
00128         pc.puts(pbuffer);
00129         wait_ms(100);
00130     }
00131 }