A PWM/duty cycle measurement library using Timer2
Dependents: PWMAverage_test pwm_duty_measurement
PWMAverage.h@4:a3d3a60e30b7, 2012-08-29 (annotated)
- Committer:
- p07gbar
- Date:
- Wed Aug 29 12:02:45 2012 +0000
- Revision:
- 4:a3d3a60e30b7
- Parent:
- 2:449ea283d634
Working
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
p07gbar | 0:5da51898a166 | 1 | /** |
p07gbar | 0:5da51898a166 | 2 | * @author Giles Barton-Owen |
p07gbar | 0:5da51898a166 | 3 | * |
p07gbar | 0:5da51898a166 | 4 | * @section LICENSE |
p07gbar | 0:5da51898a166 | 5 | * |
p07gbar | 0:5da51898a166 | 6 | * Copyright (c) 2011 mbed |
p07gbar | 0:5da51898a166 | 7 | * |
p07gbar | 0:5da51898a166 | 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
p07gbar | 0:5da51898a166 | 9 | * of this software and associated documentation files (the "Software"), to deal |
p07gbar | 0:5da51898a166 | 10 | * in the Software without restriction, including without limitation the rights |
p07gbar | 0:5da51898a166 | 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
p07gbar | 0:5da51898a166 | 12 | * copies of the Software, and to permit persons to whom the Software is |
p07gbar | 0:5da51898a166 | 13 | * furnished to do so, subject to the following conditions: |
p07gbar | 0:5da51898a166 | 14 | * |
p07gbar | 0:5da51898a166 | 15 | * The above copyright notice and this permission notice shall be included in |
p07gbar | 0:5da51898a166 | 16 | * all copies or substantial portions of the Software. |
p07gbar | 0:5da51898a166 | 17 | * |
p07gbar | 0:5da51898a166 | 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
p07gbar | 0:5da51898a166 | 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
p07gbar | 0:5da51898a166 | 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
p07gbar | 0:5da51898a166 | 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
p07gbar | 0:5da51898a166 | 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
p07gbar | 0:5da51898a166 | 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
p07gbar | 0:5da51898a166 | 24 | * THE SOFTWARE. |
p07gbar | 0:5da51898a166 | 25 | * |
p07gbar | 0:5da51898a166 | 26 | * @section DESCRIPTION |
p07gbar | 0:5da51898a166 | 27 | * PWM/Duty cycle measurement library using Timer2: library h file for NXP LPC1768 |
p07gbar | 0:5da51898a166 | 28 | * |
p07gbar | 0:5da51898a166 | 29 | */ |
p07gbar | 0:5da51898a166 | 30 | |
p07gbar | 0:5da51898a166 | 31 | #ifndef PWMAVERAGE_H |
p07gbar | 0:5da51898a166 | 32 | #define PWMAVERAGE_H |
p07gbar | 0:5da51898a166 | 33 | #include "mbed.h" |
p07gbar | 0:5da51898a166 | 34 | |
p07gbar | 0:5da51898a166 | 35 | |
p07gbar | 0:5da51898a166 | 36 | /** A class for measuring PWM/Duty cycle of a signal connected to pins 30 and 29 |
p07gbar | 0:5da51898a166 | 37 | * |
p07gbar | 4:a3d3a60e30b7 | 38 | <<<<<<< local |
p07gbar | 2:449ea283d634 | 39 | * Example: |
p07gbar | 4:a3d3a60e30b7 | 40 | ======= |
p07gbar | 4:a3d3a60e30b7 | 41 | * |
p07gbar | 4:a3d3a60e30b7 | 42 | >>>>>>> other |
p07gbar | 1:34a9269390d9 | 43 | * @code |
p07gbar | 1:34a9269390d9 | 44 | * // Measure and print the average duty cycle of a signal connected to p29 and p30 (together) while the button (p16) is pulled high |
p07gbar | 1:34a9269390d9 | 45 | * |
p07gbar | 1:34a9269390d9 | 46 | * #include "mbed.h" |
p07gbar | 1:34a9269390d9 | 47 | * #include "PWMAverage.h" |
p07gbar | 1:34a9269390d9 | 48 | * |
p07gbar | 1:34a9269390d9 | 49 | * DigitalOut myled(LED1); |
p07gbar | 1:34a9269390d9 | 50 | * |
p07gbar | 1:34a9269390d9 | 51 | * PWMAverage pa(p29,p30); |
p07gbar | 1:34a9269390d9 | 52 | * |
p07gbar | 1:34a9269390d9 | 53 | * DigitalIn button (p16); |
p07gbar | 1:34a9269390d9 | 54 | * |
p07gbar | 1:34a9269390d9 | 55 | * Timer tmr; |
p07gbar | 1:34a9269390d9 | 56 | * |
p07gbar | 1:34a9269390d9 | 57 | * int main() |
p07gbar | 1:34a9269390d9 | 58 | * { |
p07gbar | 1:34a9269390d9 | 59 | * button.mode(PullDown); |
p07gbar | 1:34a9269390d9 | 60 | * while(1) |
p07gbar | 1:34a9269390d9 | 61 | * { |
p07gbar | 1:34a9269390d9 | 62 | * pa.reset(); |
p07gbar | 1:34a9269390d9 | 63 | * |
p07gbar | 1:34a9269390d9 | 64 | * while (!button) {} |
p07gbar | 1:34a9269390d9 | 65 | * pa.start(); |
p07gbar | 1:34a9269390d9 | 66 | * tmr.start(); |
p07gbar | 1:34a9269390d9 | 67 | * myled=1; |
p07gbar | 1:34a9269390d9 | 68 | * |
p07gbar | 1:34a9269390d9 | 69 | * while (button) {} |
p07gbar | 1:34a9269390d9 | 70 | * pa.stop(); |
p07gbar | 1:34a9269390d9 | 71 | * tmr.stop(); |
p07gbar | 1:34a9269390d9 | 72 | * myled=0; |
p07gbar | 1:34a9269390d9 | 73 | * |
p07gbar | 1:34a9269390d9 | 74 | * printf("Average dudy cycle over %d us was %.4f\n\r",tmr.read_us(),pa.read()); |
p07gbar | 1:34a9269390d9 | 75 | * } |
p07gbar | 1:34a9269390d9 | 76 | * } |
p07gbar | 1:34a9269390d9 | 77 | * @endcode |
p07gbar | 0:5da51898a166 | 78 | */ |
p07gbar | 0:5da51898a166 | 79 | class PWMAverage |
p07gbar | 0:5da51898a166 | 80 | { |
p07gbar | 0:5da51898a166 | 81 | public: |
p07gbar | 0:5da51898a166 | 82 | /** Create a PWMAverage object |
p07gbar | 0:5da51898a166 | 83 | * |
p07gbar | 0:5da51898a166 | 84 | * @param cap0 Pin connected to signal (ignored in program) |
p07gbar | 0:5da51898a166 | 85 | * @param cap1 Pin connected to signal (ignored in program) |
p07gbar | 0:5da51898a166 | 86 | */ |
p07gbar | 0:5da51898a166 | 87 | PWMAverage(PinName cap0, PinName cap1); |
p07gbar | 0:5da51898a166 | 88 | |
p07gbar | 0:5da51898a166 | 89 | /** Reset the counters and values |
p07gbar | 0:5da51898a166 | 90 | */ |
p07gbar | 0:5da51898a166 | 91 | void reset(); |
p07gbar | 0:5da51898a166 | 92 | /** Start the timer |
p07gbar | 0:5da51898a166 | 93 | */ |
p07gbar | 0:5da51898a166 | 94 | void start(); |
p07gbar | 0:5da51898a166 | 95 | /** Stop the timer |
p07gbar | 0:5da51898a166 | 96 | */ |
p07gbar | 0:5da51898a166 | 97 | void stop(); |
p07gbar | 0:5da51898a166 | 98 | /** Read the duty cycle measured |
p07gbar | 0:5da51898a166 | 99 | * @returns The Duty cycle |
p07gbar | 0:5da51898a166 | 100 | */ |
p07gbar | 0:5da51898a166 | 101 | float read(); |
p07gbar | 0:5da51898a166 | 102 | /** Read the average length of time the signal was high per cycle in seconds |
p07gbar | 0:5da51898a166 | 103 | * @returns The Length of time the signal was high per cycle in seconds |
p07gbar | 0:5da51898a166 | 104 | */ |
p07gbar | 0:5da51898a166 | 105 | double avg_up(); |
p07gbar | 0:5da51898a166 | 106 | /** Read the average length of time the signal was high per cycle in seconds |
p07gbar | 0:5da51898a166 | 107 | * @returns The Length of time the signal was high per cycle in seconds |
p07gbar | 0:5da51898a166 | 108 | */ |
p07gbar | 0:5da51898a166 | 109 | float avg_UP(); |
p07gbar | 0:5da51898a166 | 110 | /** Read the average length of time the signal was low per cycle in seconds |
p07gbar | 0:5da51898a166 | 111 | * @returns The Length of time the signal was low per cycle in seconds |
p07gbar | 0:5da51898a166 | 112 | */ |
p07gbar | 0:5da51898a166 | 113 | double avg_down(); |
p07gbar | 0:5da51898a166 | 114 | /** Read the average period in seconds |
p07gbar | 0:5da51898a166 | 115 | * @returns The Period |
p07gbar | 0:5da51898a166 | 116 | */ |
p07gbar | 0:5da51898a166 | 117 | double period(); |
p07gbar | 0:5da51898a166 | 118 | /** Read the number of cycles counted over |
p07gbar | 0:5da51898a166 | 119 | * @returns The number of cycles |
p07gbar | 0:5da51898a166 | 120 | */ |
p07gbar | 0:5da51898a166 | 121 | int count(); |
p07gbar | 0:5da51898a166 | 122 | |
p07gbar | 1:34a9269390d9 | 123 | private: |
p07gbar | 1:34a9269390d9 | 124 | |
p07gbar | 1:34a9269390d9 | 125 | static void _tisr(); |
p07gbar | 1:34a9269390d9 | 126 | static PWMAverage * instance; |
p07gbar | 1:34a9269390d9 | 127 | void enable(bool yn); |
p07gbar | 1:34a9269390d9 | 128 | void configure(); |
p07gbar | 1:34a9269390d9 | 129 | |
p07gbar | 0:5da51898a166 | 130 | long count_; |
p07gbar | 0:5da51898a166 | 131 | long totalup; |
p07gbar | 0:5da51898a166 | 132 | long total; |
p07gbar | 0:5da51898a166 | 133 | |
p07gbar | 0:5da51898a166 | 134 | double timeMult; |
p07gbar | 0:5da51898a166 | 135 | |
p07gbar | 0:5da51898a166 | 136 | double timeDiv; |
p07gbar | 0:5da51898a166 | 137 | |
p07gbar | 0:5da51898a166 | 138 | bool running; |
p07gbar | 0:5da51898a166 | 139 | bool starting; |
p07gbar | 0:5da51898a166 | 140 | |
p07gbar | 0:5da51898a166 | 141 | uint32_t prescaler_point; |
p07gbar | 0:5da51898a166 | 142 | |
p07gbar | 0:5da51898a166 | 143 | }; |
p07gbar | 0:5da51898a166 | 144 | |
p07gbar | 0:5da51898a166 | 145 | |
p07gbar | 0:5da51898a166 | 146 | #endif |