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.
main.cpp@0:18d069b5f9f4, 2014-01-01 (annotated)
- Committer:
- GerritPathuis
- Date:
- Wed Jan 01 12:25:57 2014 +0000
- Revision:
- 0:18d069b5f9f4
- Child:
- 1:4bd14d5374c3
Stolen from PwmIn library
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
GerritPathuis | 0:18d069b5f9f4 | 1 | #include "mbed.h" |
GerritPathuis | 0:18d069b5f9f4 | 2 | #define NR 20 |
GerritPathuis | 0:18d069b5f9f4 | 3 | |
GerritPathuis | 0:18d069b5f9f4 | 4 | class PwmIn |
GerritPathuis | 0:18d069b5f9f4 | 5 | { |
GerritPathuis | 0:18d069b5f9f4 | 6 | public: |
GerritPathuis | 0:18d069b5f9f4 | 7 | PwmIn(PinName p); |
GerritPathuis | 0:18d069b5f9f4 | 8 | float period(); |
GerritPathuis | 0:18d069b5f9f4 | 9 | float pulsewidth(); |
GerritPathuis | 0:18d069b5f9f4 | 10 | float dutycycle(); |
GerritPathuis | 0:18d069b5f9f4 | 11 | |
GerritPathuis | 0:18d069b5f9f4 | 12 | protected: |
GerritPathuis | 0:18d069b5f9f4 | 13 | void rise(); |
GerritPathuis | 0:18d069b5f9f4 | 14 | void fall(); |
GerritPathuis | 0:18d069b5f9f4 | 15 | |
GerritPathuis | 0:18d069b5f9f4 | 16 | InterruptIn _p; |
GerritPathuis | 0:18d069b5f9f4 | 17 | Timer _t; |
GerritPathuis | 0:18d069b5f9f4 | 18 | float _pulsewidth, _period; |
GerritPathuis | 0:18d069b5f9f4 | 19 | }; |
GerritPathuis | 0:18d069b5f9f4 | 20 | |
GerritPathuis | 0:18d069b5f9f4 | 21 | PwmIn::PwmIn(PinName p) : _p(p) |
GerritPathuis | 0:18d069b5f9f4 | 22 | { |
GerritPathuis | 0:18d069b5f9f4 | 23 | _p.rise(this, &PwmIn::rise); |
GerritPathuis | 0:18d069b5f9f4 | 24 | _p.fall(this, &PwmIn::fall); |
GerritPathuis | 0:18d069b5f9f4 | 25 | _period = 0.0; |
GerritPathuis | 0:18d069b5f9f4 | 26 | _pulsewidth = 0.0; |
GerritPathuis | 0:18d069b5f9f4 | 27 | _t.start(); |
GerritPathuis | 0:18d069b5f9f4 | 28 | } |
GerritPathuis | 0:18d069b5f9f4 | 29 | |
GerritPathuis | 0:18d069b5f9f4 | 30 | PwmIn b(PTA17); // Sensor 1 Y direction |
GerritPathuis | 0:18d069b5f9f4 | 31 | PwmIn d(PTA16); // Sensor 2 Y direction |
GerritPathuis | 0:18d069b5f9f4 | 32 | Serial pc(USBTX, USBRX); // tx, rx |
GerritPathuis | 0:18d069b5f9f4 | 33 | |
GerritPathuis | 0:18d069b5f9f4 | 34 | float PwmIn::period() |
GerritPathuis | 0:18d069b5f9f4 | 35 | { |
GerritPathuis | 0:18d069b5f9f4 | 36 | return _period; |
GerritPathuis | 0:18d069b5f9f4 | 37 | } |
GerritPathuis | 0:18d069b5f9f4 | 38 | |
GerritPathuis | 0:18d069b5f9f4 | 39 | float PwmIn::pulsewidth() |
GerritPathuis | 0:18d069b5f9f4 | 40 | { |
GerritPathuis | 0:18d069b5f9f4 | 41 | return _pulsewidth; |
GerritPathuis | 0:18d069b5f9f4 | 42 | } |
GerritPathuis | 0:18d069b5f9f4 | 43 | |
GerritPathuis | 0:18d069b5f9f4 | 44 | float PwmIn::dutycycle() |
GerritPathuis | 0:18d069b5f9f4 | 45 | { |
GerritPathuis | 0:18d069b5f9f4 | 46 | return _pulsewidth / _period; |
GerritPathuis | 0:18d069b5f9f4 | 47 | } |
GerritPathuis | 0:18d069b5f9f4 | 48 | |
GerritPathuis | 0:18d069b5f9f4 | 49 | void PwmIn::rise() |
GerritPathuis | 0:18d069b5f9f4 | 50 | { |
GerritPathuis | 0:18d069b5f9f4 | 51 | _period = _t.read(); |
GerritPathuis | 0:18d069b5f9f4 | 52 | _t.reset(); |
GerritPathuis | 0:18d069b5f9f4 | 53 | } |
GerritPathuis | 0:18d069b5f9f4 | 54 | |
GerritPathuis | 0:18d069b5f9f4 | 55 | void PwmIn::fall() |
GerritPathuis | 0:18d069b5f9f4 | 56 | { |
GerritPathuis | 0:18d069b5f9f4 | 57 | _pulsewidth = _t.read(); |
GerritPathuis | 0:18d069b5f9f4 | 58 | } |
GerritPathuis | 0:18d069b5f9f4 | 59 | |
GerritPathuis | 0:18d069b5f9f4 | 60 | struct product { |
GerritPathuis | 0:18d069b5f9f4 | 61 | float pulse; |
GerritPathuis | 0:18d069b5f9f4 | 62 | float width; |
GerritPathuis | 0:18d069b5f9f4 | 63 | float duty; |
GerritPathuis | 0:18d069b5f9f4 | 64 | } acc_a[NR], acc_b[NR], acc_c[NR],acc_d[NR]; |
GerritPathuis | 0:18d069b5f9f4 | 65 | |
GerritPathuis | 0:18d069b5f9f4 | 66 | int main() |
GerritPathuis | 0:18d069b5f9f4 | 67 | { |
GerritPathuis | 0:18d069b5f9f4 | 68 | char pbuffer[60]="---"; |
GerritPathuis | 0:18d069b5f9f4 | 69 | int i; |
GerritPathuis | 0:18d069b5f9f4 | 70 | |
GerritPathuis | 0:18d069b5f9f4 | 71 | while(1) { |
GerritPathuis | 0:18d069b5f9f4 | 72 | //d.disable(); |
GerritPathuis | 0:18d069b5f9f4 | 73 | for (i=0; i<NR; i++) { |
GerritPathuis | 0:18d069b5f9f4 | 74 | acc_b[i].duty= b.dutycycle(); |
GerritPathuis | 0:18d069b5f9f4 | 75 | //acc_d[i].duty= d.dutycycle(); |
GerritPathuis | 0:18d069b5f9f4 | 76 | } |
GerritPathuis | 0:18d069b5f9f4 | 77 | |
GerritPathuis | 0:18d069b5f9f4 | 78 | for (i=0; i<NR; i++) { |
GerritPathuis | 0:18d069b5f9f4 | 79 | //acc_b[i].duty= b.dutycycle(); |
GerritPathuis | 0:18d069b5f9f4 | 80 | acc_d[i].duty= d.dutycycle(); |
GerritPathuis | 0:18d069b5f9f4 | 81 | } |
GerritPathuis | 0:18d069b5f9f4 | 82 | |
GerritPathuis | 0:18d069b5f9f4 | 83 | for (i=0; i<NR; i++) { |
GerritPathuis | 0:18d069b5f9f4 | 84 | sprintf(pbuffer, " b= %f, d= %f \n\r", acc_b[i].duty, acc_d[i].duty); |
GerritPathuis | 0:18d069b5f9f4 | 85 | pc.puts(pbuffer); |
GerritPathuis | 0:18d069b5f9f4 | 86 | } |
GerritPathuis | 0:18d069b5f9f4 | 87 | } |
GerritPathuis | 0:18d069b5f9f4 | 88 | } |