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

Committer:
GerritPathuis
Date:
Thu Jan 02 14:01:30 2014 +0000
Revision:
1:4bd14d5374c3
Parent:
0:18d069b5f9f4
Child:
2:79b52803da22
Does not work properly

Who changed what in which revision?

UserRevisionLine numberNew contents of line
GerritPathuis 1:4bd14d5374c3 1 //////////////////////////////////////
GerritPathuis 1:4bd14d5374c3 2 // basen on PwmIn Library
GerritPathuis 1:4bd14d5374c3 3 // Basis is to disable the interrupts
GerritPathuis 1:4bd14d5374c3 4 // of the other PwmIn inputs
GerritPathuis 1:4bd14d5374c3 5 ///////////////////////////////////////
GerritPathuis 0:18d069b5f9f4 6 #include "mbed.h"
GerritPathuis 1:4bd14d5374c3 7 #include "InterruptIn.h"
GerritPathuis 0:18d069b5f9f4 8 #define NR 20
GerritPathuis 0:18d069b5f9f4 9
GerritPathuis 0:18d069b5f9f4 10 class PwmIn
GerritPathuis 0:18d069b5f9f4 11 {
GerritPathuis 0:18d069b5f9f4 12 public:
GerritPathuis 0:18d069b5f9f4 13 PwmIn(PinName p);
GerritPathuis 0:18d069b5f9f4 14 float period();
GerritPathuis 0:18d069b5f9f4 15 float pulsewidth();
GerritPathuis 0:18d069b5f9f4 16 float dutycycle();
GerritPathuis 1:4bd14d5374c3 17 void disable();
GerritPathuis 0:18d069b5f9f4 18
GerritPathuis 0:18d069b5f9f4 19 protected:
GerritPathuis 0:18d069b5f9f4 20 void rise();
GerritPathuis 0:18d069b5f9f4 21 void fall();
GerritPathuis 0:18d069b5f9f4 22
GerritPathuis 0:18d069b5f9f4 23 InterruptIn _p;
GerritPathuis 0:18d069b5f9f4 24 Timer _t;
GerritPathuis 1:4bd14d5374c3 25 volatile float _pulsewidth, _period;
GerritPathuis 0:18d069b5f9f4 26 };
GerritPathuis 0:18d069b5f9f4 27
GerritPathuis 0:18d069b5f9f4 28 PwmIn::PwmIn(PinName p) : _p(p)
GerritPathuis 0:18d069b5f9f4 29 {
GerritPathuis 0:18d069b5f9f4 30 _p.rise(this, &PwmIn::rise);
GerritPathuis 0:18d069b5f9f4 31 _p.fall(this, &PwmIn::fall);
GerritPathuis 0:18d069b5f9f4 32 _period = 0.0;
GerritPathuis 0:18d069b5f9f4 33 _pulsewidth = 0.0;
GerritPathuis 0:18d069b5f9f4 34 _t.start();
GerritPathuis 0:18d069b5f9f4 35 }
GerritPathuis 0:18d069b5f9f4 36
GerritPathuis 1:4bd14d5374c3 37 PwmIn a(PTD2); // Sensor 1 X direction
GerritPathuis 0:18d069b5f9f4 38 PwmIn b(PTA17); // Sensor 1 Y direction
GerritPathuis 0:18d069b5f9f4 39 Serial pc(USBTX, USBRX); // tx, rx
GerritPathuis 0:18d069b5f9f4 40
GerritPathuis 0:18d069b5f9f4 41 float PwmIn::period()
GerritPathuis 0:18d069b5f9f4 42 {
GerritPathuis 0:18d069b5f9f4 43 return _period;
GerritPathuis 0:18d069b5f9f4 44 }
GerritPathuis 0:18d069b5f9f4 45
GerritPathuis 0:18d069b5f9f4 46 float PwmIn::pulsewidth()
GerritPathuis 0:18d069b5f9f4 47 {
GerritPathuis 0:18d069b5f9f4 48 return _pulsewidth;
GerritPathuis 0:18d069b5f9f4 49 }
GerritPathuis 0:18d069b5f9f4 50
GerritPathuis 0:18d069b5f9f4 51 float PwmIn::dutycycle()
GerritPathuis 0:18d069b5f9f4 52 {
GerritPathuis 0:18d069b5f9f4 53 return _pulsewidth / _period;
GerritPathuis 0:18d069b5f9f4 54 }
GerritPathuis 0:18d069b5f9f4 55
GerritPathuis 1:4bd14d5374c3 56 void PwmIn::disable()
GerritPathuis 1:4bd14d5374c3 57 {
GerritPathuis 1:4bd14d5374c3 58 // _p.enable_irq();
GerritPathuis 1:4bd14d5374c3 59 }
GerritPathuis 1:4bd14d5374c3 60
GerritPathuis 0:18d069b5f9f4 61 void PwmIn::rise()
GerritPathuis 0:18d069b5f9f4 62 {
GerritPathuis 0:18d069b5f9f4 63 _period = _t.read();
GerritPathuis 0:18d069b5f9f4 64 _t.reset();
GerritPathuis 0:18d069b5f9f4 65 }
GerritPathuis 0:18d069b5f9f4 66
GerritPathuis 0:18d069b5f9f4 67 void PwmIn::fall()
GerritPathuis 0:18d069b5f9f4 68 {
GerritPathuis 0:18d069b5f9f4 69 _pulsewidth = _t.read();
GerritPathuis 0:18d069b5f9f4 70 }
GerritPathuis 0:18d069b5f9f4 71
GerritPathuis 0:18d069b5f9f4 72 struct product {
GerritPathuis 0:18d069b5f9f4 73 float pulse;
GerritPathuis 0:18d069b5f9f4 74 float width;
GerritPathuis 0:18d069b5f9f4 75 float duty;
GerritPathuis 0:18d069b5f9f4 76 } acc_a[NR], acc_b[NR], acc_c[NR],acc_d[NR];
GerritPathuis 0:18d069b5f9f4 77
GerritPathuis 1:4bd14d5374c3 78
GerritPathuis 0:18d069b5f9f4 79 int main()
GerritPathuis 0:18d069b5f9f4 80 {
GerritPathuis 0:18d069b5f9f4 81 char pbuffer[60]="---";
GerritPathuis 0:18d069b5f9f4 82 int i;
GerritPathuis 0:18d069b5f9f4 83
GerritPathuis 0:18d069b5f9f4 84 while(1) {
GerritPathuis 1:4bd14d5374c3 85 // a.disable(); // Stop interrupts for sensor B
GerritPathuis 0:18d069b5f9f4 86 for (i=0; i<NR; i++) {
GerritPathuis 1:4bd14d5374c3 87 acc_a[i].duty= a.dutycycle(); // Sensor A
GerritPathuis 0:18d069b5f9f4 88 acc_b[i].duty= b.dutycycle();
GerritPathuis 0:18d069b5f9f4 89 }
GerritPathuis 0:18d069b5f9f4 90
GerritPathuis 0:18d069b5f9f4 91 for (i=0; i<NR; i++) {
GerritPathuis 1:4bd14d5374c3 92 sprintf(pbuffer, " a= %f, b= %f \n\r", acc_a[i].duty, acc_b[i].duty);
GerritPathuis 0:18d069b5f9f4 93 pc.puts(pbuffer);
GerritPathuis 0:18d069b5f9f4 94 }
GerritPathuis 0:18d069b5f9f4 95 }
GerritPathuis 0:18d069b5f9f4 96 }