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@2:79b52803da22, 2014-01-03 (annotated)
- Committer:
- GerritPathuis
- Date:
- Fri Jan 03 18:47:33 2014 +0000
- Revision:
- 2:79b52803da22
- Parent:
- 1:4bd14d5374c3
Tested with two channels connected to a function generator
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
GerritPathuis | 1:4bd14d5374c3 | 1 | ////////////////////////////////////// |
GerritPathuis | 2:79b52803da22 | 2 | // Based on PwmIn Library |
GerritPathuis | 2:79b52803da22 | 3 | // The basis is to disable the interrupts |
GerritPathuis | 1:4bd14d5374c3 | 4 | // of the other PwmIn inputs |
GerritPathuis | 2:79b52803da22 | 5 | // Connected to the function generator |
GerritPathuis | 2:79b52803da22 | 6 | // puls on both inputs at the exact |
GerritPathuis | 2:79b52803da22 | 7 | // same time. |
GerritPathuis | 2:79b52803da22 | 8 | // Works excellent but further testing |
GerritPathuis | 2:79b52803da22 | 9 | // showed that there is no real advantage |
GerritPathuis | 2:79b52803da22 | 10 | // above the original PmwIn library |
GerritPathuis | 2:79b52803da22 | 11 | ////////////////////////////////////// |
GerritPathuis | 0:18d069b5f9f4 | 12 | #include "mbed.h" |
GerritPathuis | 1:4bd14d5374c3 | 13 | #include "InterruptIn.h" |
GerritPathuis | 0:18d069b5f9f4 | 14 | #define NR 20 |
GerritPathuis | 0:18d069b5f9f4 | 15 | |
GerritPathuis | 0:18d069b5f9f4 | 16 | class PwmIn |
GerritPathuis | 0:18d069b5f9f4 | 17 | { |
GerritPathuis | 0:18d069b5f9f4 | 18 | public: |
GerritPathuis | 0:18d069b5f9f4 | 19 | PwmIn(PinName p); |
GerritPathuis | 0:18d069b5f9f4 | 20 | float period(); |
GerritPathuis | 0:18d069b5f9f4 | 21 | float pulsewidth(); |
GerritPathuis | 0:18d069b5f9f4 | 22 | float dutycycle(); |
GerritPathuis | 2:79b52803da22 | 23 | void disable(); |
GerritPathuis | 2:79b52803da22 | 24 | void enable(); |
GerritPathuis | 0:18d069b5f9f4 | 25 | |
GerritPathuis | 0:18d069b5f9f4 | 26 | protected: |
GerritPathuis | 0:18d069b5f9f4 | 27 | void rise(); |
GerritPathuis | 0:18d069b5f9f4 | 28 | void fall(); |
GerritPathuis | 0:18d069b5f9f4 | 29 | |
GerritPathuis | 0:18d069b5f9f4 | 30 | InterruptIn _p; |
GerritPathuis | 0:18d069b5f9f4 | 31 | Timer _t; |
GerritPathuis | 1:4bd14d5374c3 | 32 | volatile float _pulsewidth, _period; |
GerritPathuis | 0:18d069b5f9f4 | 33 | }; |
GerritPathuis | 0:18d069b5f9f4 | 34 | |
GerritPathuis | 0:18d069b5f9f4 | 35 | PwmIn::PwmIn(PinName p) : _p(p) |
GerritPathuis | 0:18d069b5f9f4 | 36 | { |
GerritPathuis | 0:18d069b5f9f4 | 37 | _p.rise(this, &PwmIn::rise); |
GerritPathuis | 0:18d069b5f9f4 | 38 | _p.fall(this, &PwmIn::fall); |
GerritPathuis | 0:18d069b5f9f4 | 39 | _period = 0.0; |
GerritPathuis | 0:18d069b5f9f4 | 40 | _pulsewidth = 0.0; |
GerritPathuis | 0:18d069b5f9f4 | 41 | _t.start(); |
GerritPathuis | 0:18d069b5f9f4 | 42 | } |
GerritPathuis | 0:18d069b5f9f4 | 43 | |
GerritPathuis | 1:4bd14d5374c3 | 44 | PwmIn a(PTD2); // Sensor 1 X direction |
GerritPathuis | 0:18d069b5f9f4 | 45 | PwmIn b(PTA17); // Sensor 1 Y direction |
GerritPathuis | 0:18d069b5f9f4 | 46 | Serial pc(USBTX, USBRX); // tx, rx |
GerritPathuis | 0:18d069b5f9f4 | 47 | |
GerritPathuis | 0:18d069b5f9f4 | 48 | float PwmIn::period() |
GerritPathuis | 0:18d069b5f9f4 | 49 | { |
GerritPathuis | 0:18d069b5f9f4 | 50 | return _period; |
GerritPathuis | 0:18d069b5f9f4 | 51 | } |
GerritPathuis | 0:18d069b5f9f4 | 52 | |
GerritPathuis | 0:18d069b5f9f4 | 53 | float PwmIn::pulsewidth() |
GerritPathuis | 0:18d069b5f9f4 | 54 | { |
GerritPathuis | 0:18d069b5f9f4 | 55 | return _pulsewidth; |
GerritPathuis | 0:18d069b5f9f4 | 56 | } |
GerritPathuis | 0:18d069b5f9f4 | 57 | |
GerritPathuis | 0:18d069b5f9f4 | 58 | float PwmIn::dutycycle() |
GerritPathuis | 0:18d069b5f9f4 | 59 | { |
GerritPathuis | 0:18d069b5f9f4 | 60 | return _pulsewidth / _period; |
GerritPathuis | 0:18d069b5f9f4 | 61 | } |
GerritPathuis | 0:18d069b5f9f4 | 62 | |
GerritPathuis | 1:4bd14d5374c3 | 63 | void PwmIn::disable() |
GerritPathuis | 1:4bd14d5374c3 | 64 | { |
GerritPathuis | 2:79b52803da22 | 65 | _p.disable_irq(); |
GerritPathuis | 2:79b52803da22 | 66 | } |
GerritPathuis | 2:79b52803da22 | 67 | |
GerritPathuis | 2:79b52803da22 | 68 | void PwmIn::enable() |
GerritPathuis | 2:79b52803da22 | 69 | { |
GerritPathuis | 2:79b52803da22 | 70 | _p.enable_irq(); |
GerritPathuis | 1:4bd14d5374c3 | 71 | } |
GerritPathuis | 1:4bd14d5374c3 | 72 | |
GerritPathuis | 0:18d069b5f9f4 | 73 | void PwmIn::rise() |
GerritPathuis | 0:18d069b5f9f4 | 74 | { |
GerritPathuis | 0:18d069b5f9f4 | 75 | _period = _t.read(); |
GerritPathuis | 0:18d069b5f9f4 | 76 | _t.reset(); |
GerritPathuis | 0:18d069b5f9f4 | 77 | } |
GerritPathuis | 0:18d069b5f9f4 | 78 | |
GerritPathuis | 0:18d069b5f9f4 | 79 | void PwmIn::fall() |
GerritPathuis | 0:18d069b5f9f4 | 80 | { |
GerritPathuis | 0:18d069b5f9f4 | 81 | _pulsewidth = _t.read(); |
GerritPathuis | 0:18d069b5f9f4 | 82 | } |
GerritPathuis | 0:18d069b5f9f4 | 83 | |
GerritPathuis | 0:18d069b5f9f4 | 84 | struct product { |
GerritPathuis | 2:79b52803da22 | 85 | float pulsewidth; |
GerritPathuis | 2:79b52803da22 | 86 | float period; |
GerritPathuis | 2:79b52803da22 | 87 | float cycle; |
GerritPathuis | 0:18d069b5f9f4 | 88 | } acc_a[NR], acc_b[NR], acc_c[NR],acc_d[NR]; |
GerritPathuis | 0:18d069b5f9f4 | 89 | |
GerritPathuis | 1:4bd14d5374c3 | 90 | |
GerritPathuis | 0:18d069b5f9f4 | 91 | int main() |
GerritPathuis | 0:18d069b5f9f4 | 92 | { |
GerritPathuis | 2:79b52803da22 | 93 | char pbuffer[512]="---"; |
GerritPathuis | 0:18d069b5f9f4 | 94 | int i; |
GerritPathuis | 0:18d069b5f9f4 | 95 | |
GerritPathuis | 2:79b52803da22 | 96 | pc.baud(9600); |
GerritPathuis | 2:79b52803da22 | 97 | sprintf(pbuffer, "Start test, baudrate is set to 9600 \n\r"); |
GerritPathuis | 2:79b52803da22 | 98 | pc.puts(pbuffer); |
GerritPathuis | 2:79b52803da22 | 99 | |
GerritPathuis | 2:79b52803da22 | 100 | a.disable(); |
GerritPathuis | 2:79b52803da22 | 101 | b.disable(); |
GerritPathuis | 0:18d069b5f9f4 | 102 | while(1) { |
GerritPathuis | 2:79b52803da22 | 103 | //-------------- sensor a-------------------- |
GerritPathuis | 2:79b52803da22 | 104 | a.enable(); // signal on input 50Hz is period of 20 milli second |
GerritPathuis | 2:79b52803da22 | 105 | wait_ms(40); // signal on input 50Hz is period of 20 milli second |
GerritPathuis | 0:18d069b5f9f4 | 106 | for (i=0; i<NR; i++) { |
GerritPathuis | 2:79b52803da22 | 107 | acc_a[i].pulsewidth= a.pulsewidth(); // Sensor A |
GerritPathuis | 2:79b52803da22 | 108 | acc_a[i].period= a.period(); // Sensor A |
GerritPathuis | 2:79b52803da22 | 109 | acc_a[i].cycle= a.dutycycle(); // Sensor A |
GerritPathuis | 0:18d069b5f9f4 | 110 | } |
GerritPathuis | 2:79b52803da22 | 111 | a.disable(); |
GerritPathuis | 2:79b52803da22 | 112 | |
GerritPathuis | 2:79b52803da22 | 113 | //-------------- sensor b-------------------- |
GerritPathuis | 2:79b52803da22 | 114 | b.enable(); // signal on input 50Hz is period of 20 milli second |
GerritPathuis | 2:79b52803da22 | 115 | wait_ms(40); // 50Hz is period of 20 milli second |
GerritPathuis | 2:79b52803da22 | 116 | for (i=0; i<NR; i++) { |
GerritPathuis | 2:79b52803da22 | 117 | acc_b[i].pulsewidth= b.pulsewidth(); // Sensor B |
GerritPathuis | 2:79b52803da22 | 118 | acc_b[i].period= b.period(); // Sensor B |
GerritPathuis | 2:79b52803da22 | 119 | acc_b[i].cycle= b.dutycycle(); // Sensor B |
GerritPathuis | 2:79b52803da22 | 120 | } |
GerritPathuis | 2:79b52803da22 | 121 | b.disable(); |
GerritPathuis | 0:18d069b5f9f4 | 122 | |
GerritPathuis | 0:18d069b5f9f4 | 123 | for (i=0; i<NR; i++) { |
GerritPathuis | 2:79b52803da22 | 124 | 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); |
GerritPathuis | 0:18d069b5f9f4 | 125 | pc.puts(pbuffer); |
GerritPathuis | 0:18d069b5f9f4 | 126 | } |
GerritPathuis | 2:79b52803da22 | 127 | sprintf(pbuffer, "\n\r"); |
GerritPathuis | 2:79b52803da22 | 128 | pc.puts(pbuffer); |
GerritPathuis | 2:79b52803da22 | 129 | wait_ms(100); |
GerritPathuis | 0:18d069b5f9f4 | 130 | } |
GerritPathuis | 0:18d069b5f9f4 | 131 | } |