A PWM/duty cycle measurement library using Timer2
Dependents: PWMAverage_test pwm_duty_measurement
PWMAverage.h@0:5da51898a166, 2012-08-29 (annotated)
- Committer:
- p07gbar
- Date:
- Wed Aug 29 11:25:55 2012 +0000
- Revision:
- 0:5da51898a166
- Child:
- 1:34a9269390d9
Initial commit
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 | 0:5da51898a166 | 38 | */ |
p07gbar | 0:5da51898a166 | 39 | class PWMAverage |
p07gbar | 0:5da51898a166 | 40 | { |
p07gbar | 0:5da51898a166 | 41 | public: |
p07gbar | 0:5da51898a166 | 42 | /** Create a PWMAverage object |
p07gbar | 0:5da51898a166 | 43 | * |
p07gbar | 0:5da51898a166 | 44 | * |
p07gbar | 0:5da51898a166 | 45 | * @param cap0 Pin connected to signal (ignored in program) |
p07gbar | 0:5da51898a166 | 46 | * @param cap1 Pin connected to signal (ignored in program) |
p07gbar | 0:5da51898a166 | 47 | */ |
p07gbar | 0:5da51898a166 | 48 | PWMAverage(PinName cap0, PinName cap1); |
p07gbar | 0:5da51898a166 | 49 | |
p07gbar | 0:5da51898a166 | 50 | /** Reset the counters and values |
p07gbar | 0:5da51898a166 | 51 | */ |
p07gbar | 0:5da51898a166 | 52 | void reset(); |
p07gbar | 0:5da51898a166 | 53 | /** Start the timer |
p07gbar | 0:5da51898a166 | 54 | */ |
p07gbar | 0:5da51898a166 | 55 | void start(); |
p07gbar | 0:5da51898a166 | 56 | /** Stop the timer |
p07gbar | 0:5da51898a166 | 57 | */ |
p07gbar | 0:5da51898a166 | 58 | void stop(); |
p07gbar | 0:5da51898a166 | 59 | /** Read the duty cycle measured |
p07gbar | 0:5da51898a166 | 60 | * @returns The Duty cycle |
p07gbar | 0:5da51898a166 | 61 | */ |
p07gbar | 0:5da51898a166 | 62 | float read(); |
p07gbar | 0:5da51898a166 | 63 | /** Read the average length of time the signal was high per cycle in seconds |
p07gbar | 0:5da51898a166 | 64 | * @returns The Length of time the signal was high per cycle in seconds |
p07gbar | 0:5da51898a166 | 65 | */ |
p07gbar | 0:5da51898a166 | 66 | double avg_up(); |
p07gbar | 0:5da51898a166 | 67 | /** Read the average length of time the signal was high per cycle in seconds |
p07gbar | 0:5da51898a166 | 68 | * @returns The Length of time the signal was high per cycle in seconds |
p07gbar | 0:5da51898a166 | 69 | */ |
p07gbar | 0:5da51898a166 | 70 | float avg_UP(); |
p07gbar | 0:5da51898a166 | 71 | /** Read the average length of time the signal was low per cycle in seconds |
p07gbar | 0:5da51898a166 | 72 | * @returns The Length of time the signal was low per cycle in seconds |
p07gbar | 0:5da51898a166 | 73 | */ |
p07gbar | 0:5da51898a166 | 74 | double avg_down(); |
p07gbar | 0:5da51898a166 | 75 | /** Read the average period in seconds |
p07gbar | 0:5da51898a166 | 76 | * @returns The Period |
p07gbar | 0:5da51898a166 | 77 | */ |
p07gbar | 0:5da51898a166 | 78 | double period(); |
p07gbar | 0:5da51898a166 | 79 | /** Read the number of cycles counted over |
p07gbar | 0:5da51898a166 | 80 | * @returns The number of cycles |
p07gbar | 0:5da51898a166 | 81 | */ |
p07gbar | 0:5da51898a166 | 82 | int count(); |
p07gbar | 0:5da51898a166 | 83 | |
p07gbar | 0:5da51898a166 | 84 | long count_; |
p07gbar | 0:5da51898a166 | 85 | long totalup; |
p07gbar | 0:5da51898a166 | 86 | long total; |
p07gbar | 0:5da51898a166 | 87 | |
p07gbar | 0:5da51898a166 | 88 | double timeMult; |
p07gbar | 0:5da51898a166 | 89 | |
p07gbar | 0:5da51898a166 | 90 | double timeDiv; |
p07gbar | 0:5da51898a166 | 91 | |
p07gbar | 0:5da51898a166 | 92 | private: |
p07gbar | 0:5da51898a166 | 93 | |
p07gbar | 0:5da51898a166 | 94 | static void _tisr(); |
p07gbar | 0:5da51898a166 | 95 | static PWMAverage * instance; |
p07gbar | 0:5da51898a166 | 96 | void enable(bool yn); |
p07gbar | 0:5da51898a166 | 97 | void configure(); |
p07gbar | 0:5da51898a166 | 98 | |
p07gbar | 0:5da51898a166 | 99 | |
p07gbar | 0:5da51898a166 | 100 | |
p07gbar | 0:5da51898a166 | 101 | bool running; |
p07gbar | 0:5da51898a166 | 102 | bool starting; |
p07gbar | 0:5da51898a166 | 103 | |
p07gbar | 0:5da51898a166 | 104 | uint32_t prescaler_point; |
p07gbar | 0:5da51898a166 | 105 | |
p07gbar | 0:5da51898a166 | 106 | }; |
p07gbar | 0:5da51898a166 | 107 | |
p07gbar | 0:5da51898a166 | 108 | |
p07gbar | 0:5da51898a166 | 109 | #endif |