mbed w/ spi bug fig
Fork of mbed-src by
cpp/PwmOut.h@0:fd0d7bdfcdc2, 2012-11-20 (annotated)
- Committer:
- mbed_official
- Date:
- Tue Nov 20 17:24:08 2012 +0000
- Revision:
- 0:fd0d7bdfcdc2
- Child:
- 2:143cac498751
mbed sources
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mbed_official | 0:fd0d7bdfcdc2 | 1 | /* mbed Microcontroller Library |
mbed_official | 0:fd0d7bdfcdc2 | 2 | * Copyright (c) 2006-2012 ARM Limited |
mbed_official | 0:fd0d7bdfcdc2 | 3 | * |
mbed_official | 0:fd0d7bdfcdc2 | 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
mbed_official | 0:fd0d7bdfcdc2 | 5 | * of this software and associated documentation files (the "Software"), to deal |
mbed_official | 0:fd0d7bdfcdc2 | 6 | * in the Software without restriction, including without limitation the rights |
mbed_official | 0:fd0d7bdfcdc2 | 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
mbed_official | 0:fd0d7bdfcdc2 | 8 | * copies of the Software, and to permit persons to whom the Software is |
mbed_official | 0:fd0d7bdfcdc2 | 9 | * furnished to do so, subject to the following conditions: |
mbed_official | 0:fd0d7bdfcdc2 | 10 | * |
mbed_official | 0:fd0d7bdfcdc2 | 11 | * The above copyright notice and this permission notice shall be included in |
mbed_official | 0:fd0d7bdfcdc2 | 12 | * all copies or substantial portions of the Software. |
mbed_official | 0:fd0d7bdfcdc2 | 13 | * |
mbed_official | 0:fd0d7bdfcdc2 | 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
mbed_official | 0:fd0d7bdfcdc2 | 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
mbed_official | 0:fd0d7bdfcdc2 | 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
mbed_official | 0:fd0d7bdfcdc2 | 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
mbed_official | 0:fd0d7bdfcdc2 | 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
mbed_official | 0:fd0d7bdfcdc2 | 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
mbed_official | 0:fd0d7bdfcdc2 | 20 | * SOFTWARE. |
mbed_official | 0:fd0d7bdfcdc2 | 21 | */ |
mbed_official | 0:fd0d7bdfcdc2 | 22 | #ifndef MBED_PWMOUT_H |
mbed_official | 0:fd0d7bdfcdc2 | 23 | #define MBED_PWMOUT_H |
mbed_official | 0:fd0d7bdfcdc2 | 24 | |
mbed_official | 0:fd0d7bdfcdc2 | 25 | #include "platform.h" |
mbed_official | 0:fd0d7bdfcdc2 | 26 | |
mbed_official | 0:fd0d7bdfcdc2 | 27 | #if DEVICE_PWMOUT |
mbed_official | 0:fd0d7bdfcdc2 | 28 | #include "pwmout_api.h" |
mbed_official | 0:fd0d7bdfcdc2 | 29 | |
mbed_official | 0:fd0d7bdfcdc2 | 30 | namespace mbed { |
mbed_official | 0:fd0d7bdfcdc2 | 31 | |
mbed_official | 0:fd0d7bdfcdc2 | 32 | /** A pulse-width modulation digital output |
mbed_official | 0:fd0d7bdfcdc2 | 33 | * |
mbed_official | 0:fd0d7bdfcdc2 | 34 | * Example |
mbed_official | 0:fd0d7bdfcdc2 | 35 | * @code |
mbed_official | 0:fd0d7bdfcdc2 | 36 | * // Fade a led on. |
mbed_official | 0:fd0d7bdfcdc2 | 37 | * #include "mbed.h" |
mbed_official | 0:fd0d7bdfcdc2 | 38 | * |
mbed_official | 0:fd0d7bdfcdc2 | 39 | * PwmOut led(LED1); |
mbed_official | 0:fd0d7bdfcdc2 | 40 | * |
mbed_official | 0:fd0d7bdfcdc2 | 41 | * int main() { |
mbed_official | 0:fd0d7bdfcdc2 | 42 | * while(1) { |
mbed_official | 0:fd0d7bdfcdc2 | 43 | * led = led + 0.01; |
mbed_official | 0:fd0d7bdfcdc2 | 44 | * wait(0.2); |
mbed_official | 0:fd0d7bdfcdc2 | 45 | * if(led == 1.0) { |
mbed_official | 0:fd0d7bdfcdc2 | 46 | * led = 0; |
mbed_official | 0:fd0d7bdfcdc2 | 47 | * } |
mbed_official | 0:fd0d7bdfcdc2 | 48 | * } |
mbed_official | 0:fd0d7bdfcdc2 | 49 | * } |
mbed_official | 0:fd0d7bdfcdc2 | 50 | * @endcode |
mbed_official | 0:fd0d7bdfcdc2 | 51 | * |
mbed_official | 0:fd0d7bdfcdc2 | 52 | * @note |
mbed_official | 0:fd0d7bdfcdc2 | 53 | * On the LPC1768 and LPC2368, the PWMs all share the same |
mbed_official | 0:fd0d7bdfcdc2 | 54 | * period - if you change the period for one, you change it for all. |
mbed_official | 0:fd0d7bdfcdc2 | 55 | * Although routines that change the period maintain the duty cycle |
mbed_official | 0:fd0d7bdfcdc2 | 56 | * for its PWM, all other PWMs will require their duty cycle to be |
mbed_official | 0:fd0d7bdfcdc2 | 57 | * refreshed. |
mbed_official | 0:fd0d7bdfcdc2 | 58 | */ |
mbed_official | 0:fd0d7bdfcdc2 | 59 | class PwmOut { |
mbed_official | 0:fd0d7bdfcdc2 | 60 | |
mbed_official | 0:fd0d7bdfcdc2 | 61 | public: |
mbed_official | 0:fd0d7bdfcdc2 | 62 | |
mbed_official | 0:fd0d7bdfcdc2 | 63 | /** Create a PwmOut connected to the specified pin |
mbed_official | 0:fd0d7bdfcdc2 | 64 | * |
mbed_official | 0:fd0d7bdfcdc2 | 65 | * @param pin PwmOut pin to connect to |
mbed_official | 0:fd0d7bdfcdc2 | 66 | */ |
mbed_official | 0:fd0d7bdfcdc2 | 67 | PwmOut(PinName pin) { |
mbed_official | 0:fd0d7bdfcdc2 | 68 | pwmout_init(&_pwm, pin); |
mbed_official | 0:fd0d7bdfcdc2 | 69 | } |
mbed_official | 0:fd0d7bdfcdc2 | 70 | |
mbed_official | 0:fd0d7bdfcdc2 | 71 | /** Set the ouput duty-cycle, specified as a percentage (float) |
mbed_official | 0:fd0d7bdfcdc2 | 72 | * |
mbed_official | 0:fd0d7bdfcdc2 | 73 | * @param value A floating-point value representing the output duty-cycle, |
mbed_official | 0:fd0d7bdfcdc2 | 74 | * specified as a percentage. The value should lie between |
mbed_official | 0:fd0d7bdfcdc2 | 75 | * 0.0f (representing on 0%) and 1.0f (representing on 100%). |
mbed_official | 0:fd0d7bdfcdc2 | 76 | * Values outside this range will be saturated to 0.0f or 1.0f. |
mbed_official | 0:fd0d7bdfcdc2 | 77 | */ |
mbed_official | 0:fd0d7bdfcdc2 | 78 | void write(float value) { |
mbed_official | 0:fd0d7bdfcdc2 | 79 | pwmout_write(&_pwm, value); |
mbed_official | 0:fd0d7bdfcdc2 | 80 | } |
mbed_official | 0:fd0d7bdfcdc2 | 81 | |
mbed_official | 0:fd0d7bdfcdc2 | 82 | /** Return the current output duty-cycle setting, measured as a percentage (float) |
mbed_official | 0:fd0d7bdfcdc2 | 83 | * |
mbed_official | 0:fd0d7bdfcdc2 | 84 | * @returns |
mbed_official | 0:fd0d7bdfcdc2 | 85 | * A floating-point value representing the current duty-cycle being output on the pin, |
mbed_official | 0:fd0d7bdfcdc2 | 86 | * measured as a percentage. The returned value will lie between |
mbed_official | 0:fd0d7bdfcdc2 | 87 | * 0.0f (representing on 0%) and 1.0f (representing on 100%). |
mbed_official | 0:fd0d7bdfcdc2 | 88 | * |
mbed_official | 0:fd0d7bdfcdc2 | 89 | * @note |
mbed_official | 0:fd0d7bdfcdc2 | 90 | * This value may not match exactly the value set by a previous <write>. |
mbed_official | 0:fd0d7bdfcdc2 | 91 | */ |
mbed_official | 0:fd0d7bdfcdc2 | 92 | float read() { |
mbed_official | 0:fd0d7bdfcdc2 | 93 | return pwmout_read(&_pwm); |
mbed_official | 0:fd0d7bdfcdc2 | 94 | } |
mbed_official | 0:fd0d7bdfcdc2 | 95 | |
mbed_official | 0:fd0d7bdfcdc2 | 96 | /** Set the PWM period, specified in seconds (float), keeping the duty cycle the same. |
mbed_official | 0:fd0d7bdfcdc2 | 97 | * |
mbed_official | 0:fd0d7bdfcdc2 | 98 | * @note |
mbed_official | 0:fd0d7bdfcdc2 | 99 | * The resolution is currently in microseconds; periods smaller than this |
mbed_official | 0:fd0d7bdfcdc2 | 100 | * will be set to zero. |
mbed_official | 0:fd0d7bdfcdc2 | 101 | */ |
mbed_official | 0:fd0d7bdfcdc2 | 102 | void period(float seconds) { |
mbed_official | 0:fd0d7bdfcdc2 | 103 | pwmout_period(&_pwm, seconds); |
mbed_official | 0:fd0d7bdfcdc2 | 104 | } |
mbed_official | 0:fd0d7bdfcdc2 | 105 | |
mbed_official | 0:fd0d7bdfcdc2 | 106 | /** Set the PWM period, specified in milli-seconds (int), keeping the duty cycle the same. |
mbed_official | 0:fd0d7bdfcdc2 | 107 | */ |
mbed_official | 0:fd0d7bdfcdc2 | 108 | void period_ms(int ms) { |
mbed_official | 0:fd0d7bdfcdc2 | 109 | pwmout_period_ms(&_pwm, ms); |
mbed_official | 0:fd0d7bdfcdc2 | 110 | } |
mbed_official | 0:fd0d7bdfcdc2 | 111 | |
mbed_official | 0:fd0d7bdfcdc2 | 112 | /** Set the PWM period, specified in micro-seconds (int), keeping the duty cycle the same. |
mbed_official | 0:fd0d7bdfcdc2 | 113 | */ |
mbed_official | 0:fd0d7bdfcdc2 | 114 | void period_us(int us) { |
mbed_official | 0:fd0d7bdfcdc2 | 115 | pwmout_period_us(&_pwm, us); |
mbed_official | 0:fd0d7bdfcdc2 | 116 | } |
mbed_official | 0:fd0d7bdfcdc2 | 117 | |
mbed_official | 0:fd0d7bdfcdc2 | 118 | /** Set the PWM pulsewidth, specified in seconds (float), keeping the period the same. |
mbed_official | 0:fd0d7bdfcdc2 | 119 | */ |
mbed_official | 0:fd0d7bdfcdc2 | 120 | void pulsewidth(float seconds) { |
mbed_official | 0:fd0d7bdfcdc2 | 121 | pwmout_pulsewidth(&_pwm, seconds); |
mbed_official | 0:fd0d7bdfcdc2 | 122 | } |
mbed_official | 0:fd0d7bdfcdc2 | 123 | |
mbed_official | 0:fd0d7bdfcdc2 | 124 | /** Set the PWM pulsewidth, specified in milli-seconds (int), keeping the period the same. |
mbed_official | 0:fd0d7bdfcdc2 | 125 | */ |
mbed_official | 0:fd0d7bdfcdc2 | 126 | void pulsewidth_ms(int ms) { |
mbed_official | 0:fd0d7bdfcdc2 | 127 | pwmout_pulsewidth_ms(&_pwm, ms); |
mbed_official | 0:fd0d7bdfcdc2 | 128 | } |
mbed_official | 0:fd0d7bdfcdc2 | 129 | |
mbed_official | 0:fd0d7bdfcdc2 | 130 | /** Set the PWM pulsewidth, specified in micro-seconds (int), keeping the period the same. |
mbed_official | 0:fd0d7bdfcdc2 | 131 | */ |
mbed_official | 0:fd0d7bdfcdc2 | 132 | void pulsewidth_us(int us) { |
mbed_official | 0:fd0d7bdfcdc2 | 133 | pwmout_pulsewidth_us(&_pwm, us); |
mbed_official | 0:fd0d7bdfcdc2 | 134 | } |
mbed_official | 0:fd0d7bdfcdc2 | 135 | |
mbed_official | 0:fd0d7bdfcdc2 | 136 | #ifdef MBED_OPERATORS |
mbed_official | 0:fd0d7bdfcdc2 | 137 | /** A operator shorthand for write() |
mbed_official | 0:fd0d7bdfcdc2 | 138 | */ |
mbed_official | 0:fd0d7bdfcdc2 | 139 | PwmOut& operator= (float value) { |
mbed_official | 0:fd0d7bdfcdc2 | 140 | write(value); |
mbed_official | 0:fd0d7bdfcdc2 | 141 | return *this; |
mbed_official | 0:fd0d7bdfcdc2 | 142 | } |
mbed_official | 0:fd0d7bdfcdc2 | 143 | |
mbed_official | 0:fd0d7bdfcdc2 | 144 | PwmOut& operator= (PwmOut& rhs) { |
mbed_official | 0:fd0d7bdfcdc2 | 145 | write(rhs.read()); |
mbed_official | 0:fd0d7bdfcdc2 | 146 | return *this; |
mbed_official | 0:fd0d7bdfcdc2 | 147 | } |
mbed_official | 0:fd0d7bdfcdc2 | 148 | |
mbed_official | 0:fd0d7bdfcdc2 | 149 | /** An operator shorthand for read() |
mbed_official | 0:fd0d7bdfcdc2 | 150 | */ |
mbed_official | 0:fd0d7bdfcdc2 | 151 | operator float() { |
mbed_official | 0:fd0d7bdfcdc2 | 152 | return read(); |
mbed_official | 0:fd0d7bdfcdc2 | 153 | } |
mbed_official | 0:fd0d7bdfcdc2 | 154 | #endif |
mbed_official | 0:fd0d7bdfcdc2 | 155 | |
mbed_official | 0:fd0d7bdfcdc2 | 156 | protected: |
mbed_official | 0:fd0d7bdfcdc2 | 157 | pwmout_t _pwm; |
mbed_official | 0:fd0d7bdfcdc2 | 158 | }; |
mbed_official | 0:fd0d7bdfcdc2 | 159 | |
mbed_official | 0:fd0d7bdfcdc2 | 160 | } // namespace mbed |
mbed_official | 0:fd0d7bdfcdc2 | 161 | |
mbed_official | 0:fd0d7bdfcdc2 | 162 | #endif |
mbed_official | 0:fd0d7bdfcdc2 | 163 | |
mbed_official | 0:fd0d7bdfcdc2 | 164 | #endif |