Giles Barton-Owen / PWMAverage

Dependents:   PWMAverage_test pwm_duty_measurement

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers PWMAverage.h Source File

PWMAverage.h

00001 /**
00002 * @author Giles Barton-Owen
00003 *
00004 * @section LICENSE
00005 *
00006 * Copyright (c) 2011 mbed
00007 *
00008 * Permission is hereby granted, free of charge, to any person obtaining a copy
00009 * of this software and associated documentation files (the "Software"), to deal
00010 * in the Software without restriction, including without limitation the rights
00011 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00012 * copies of the Software, and to permit persons to whom the Software is
00013 * furnished to do so, subject to the following conditions:
00014 *
00015 * The above copyright notice and this permission notice shall be included in
00016 * all copies or substantial portions of the Software.
00017 *
00018 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00019 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00020 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00021 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00022 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00023 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
00024 * THE SOFTWARE.
00025 *
00026 * @section DESCRIPTION
00027 *    PWM/Duty cycle measurement library using Timer2: library h file for NXP LPC1768
00028 *
00029 */ 
00030 
00031 #ifndef PWMAVERAGE_H
00032 #define PWMAVERAGE_H
00033 #include "mbed.h"
00034 
00035 
00036 /** A class for measuring PWM/Duty cycle of a signal connected to pins 30 and 29
00037  *
00038 <<<<<<< local
00039  * Example:
00040 =======
00041  *
00042 >>>>>>> other
00043  * @code
00044  * // Measure and print the average duty cycle of a signal connected to p29 and p30 (together) while the button (p16) is pulled high
00045  *
00046  * #include "mbed.h"
00047  * #include "PWMAverage.h"
00048  *
00049  * DigitalOut myled(LED1);
00050  *
00051  * PWMAverage pa(p29,p30);
00052  * 
00053  * DigitalIn button (p16);
00054  * 
00055  * Timer tmr;
00056  * 
00057  * int main()
00058  * {
00059  *     button.mode(PullDown);
00060  *     while(1)
00061  *     {
00062  *         pa.reset();
00063  *     
00064  *         while (!button) {}
00065  *         pa.start();
00066  *         tmr.start();
00067  *         myled=1;
00068  *     
00069  *         while (button) {}
00070  *         pa.stop();
00071  *         tmr.stop();
00072  *         myled=0;
00073  *     
00074  *         printf("Average dudy cycle over %d us was %.4f\n\r",tmr.read_us(),pa.read());
00075  *     }
00076  * }
00077  * @endcode
00078  */
00079 class PWMAverage
00080 {
00081     public:
00082     /** Create a PWMAverage object
00083      * 
00084      * @param cap0 Pin connected to signal (ignored in program)
00085      * @param cap1 Pin connected to signal (ignored in program)
00086      */
00087     PWMAverage(PinName cap0, PinName cap1);
00088     
00089     /** Reset the counters and values
00090      */
00091     void reset();
00092     /** Start the timer
00093      */
00094     void start();
00095     /** Stop the timer
00096      */
00097     void stop();
00098     /** Read the duty cycle measured
00099      * @returns The Duty cycle
00100      */
00101     float read();
00102     /** Read the average length of time the signal was high per cycle in seconds
00103      * @returns The Length of time the signal was high per cycle in seconds
00104      */
00105     double avg_up();
00106     /** Read the average length of time the signal was high per cycle in seconds
00107      * @returns The Length of time the signal was high per cycle in seconds
00108      */
00109     float avg_UP();
00110     /** Read the average length of time the signal was low per cycle in seconds
00111      * @returns The Length of time the signal was low per cycle in seconds
00112      */
00113     double avg_down();
00114     /** Read the average period in seconds
00115      * @returns The Period
00116      */
00117     double period();
00118     /** Read the number of cycles counted over
00119      * @returns The number of cycles
00120      */
00121     int count();
00122     
00123     private:
00124     
00125     static void _tisr();
00126     static PWMAverage * instance;
00127     void enable(bool yn);
00128     void configure();
00129     
00130     long count_;
00131     long totalup;
00132     long total;
00133     
00134     double timeMult;
00135     
00136     double timeDiv;
00137     
00138     bool running;
00139     bool starting;
00140     
00141     uint32_t prescaler_point;
00142     
00143 };
00144 
00145 
00146 #endif