mbed library sources
Fork of mbed-src by
cpp/PwmOut.h@2:143cac498751, 2013-02-18 (annotated)
- Committer:
- emilmont
- Date:
- Mon Feb 18 11:44:18 2013 +0000
- Revision:
- 2:143cac498751
- Parent:
- 0:fd0d7bdfcdc2
Update mbed sources to Rev 59
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mbed_official | 0:fd0d7bdfcdc2 | 1 | /* mbed Microcontroller Library |
emilmont | 2:143cac498751 | 2 | * Copyright (c) 2006-2013 ARM Limited |
mbed_official | 0:fd0d7bdfcdc2 | 3 | * |
emilmont | 2:143cac498751 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
emilmont | 2:143cac498751 | 5 | * you may not use this file except in compliance with the License. |
emilmont | 2:143cac498751 | 6 | * You may obtain a copy of the License at |
mbed_official | 0:fd0d7bdfcdc2 | 7 | * |
emilmont | 2:143cac498751 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
mbed_official | 0:fd0d7bdfcdc2 | 9 | * |
emilmont | 2:143cac498751 | 10 | * Unless required by applicable law or agreed to in writing, software |
emilmont | 2:143cac498751 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
emilmont | 2:143cac498751 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
emilmont | 2:143cac498751 | 13 | * See the License for the specific language governing permissions and |
emilmont | 2:143cac498751 | 14 | * limitations under the License. |
mbed_official | 0:fd0d7bdfcdc2 | 15 | */ |
mbed_official | 0:fd0d7bdfcdc2 | 16 | #ifndef MBED_PWMOUT_H |
mbed_official | 0:fd0d7bdfcdc2 | 17 | #define MBED_PWMOUT_H |
mbed_official | 0:fd0d7bdfcdc2 | 18 | |
mbed_official | 0:fd0d7bdfcdc2 | 19 | #include "platform.h" |
mbed_official | 0:fd0d7bdfcdc2 | 20 | |
mbed_official | 0:fd0d7bdfcdc2 | 21 | #if DEVICE_PWMOUT |
mbed_official | 0:fd0d7bdfcdc2 | 22 | #include "pwmout_api.h" |
mbed_official | 0:fd0d7bdfcdc2 | 23 | |
mbed_official | 0:fd0d7bdfcdc2 | 24 | namespace mbed { |
mbed_official | 0:fd0d7bdfcdc2 | 25 | |
mbed_official | 0:fd0d7bdfcdc2 | 26 | /** A pulse-width modulation digital output |
mbed_official | 0:fd0d7bdfcdc2 | 27 | * |
mbed_official | 0:fd0d7bdfcdc2 | 28 | * Example |
mbed_official | 0:fd0d7bdfcdc2 | 29 | * @code |
mbed_official | 0:fd0d7bdfcdc2 | 30 | * // Fade a led on. |
mbed_official | 0:fd0d7bdfcdc2 | 31 | * #include "mbed.h" |
mbed_official | 0:fd0d7bdfcdc2 | 32 | * |
mbed_official | 0:fd0d7bdfcdc2 | 33 | * PwmOut led(LED1); |
emilmont | 2:143cac498751 | 34 | * |
mbed_official | 0:fd0d7bdfcdc2 | 35 | * int main() { |
mbed_official | 0:fd0d7bdfcdc2 | 36 | * while(1) { |
mbed_official | 0:fd0d7bdfcdc2 | 37 | * led = led + 0.01; |
mbed_official | 0:fd0d7bdfcdc2 | 38 | * wait(0.2); |
mbed_official | 0:fd0d7bdfcdc2 | 39 | * if(led == 1.0) { |
mbed_official | 0:fd0d7bdfcdc2 | 40 | * led = 0; |
mbed_official | 0:fd0d7bdfcdc2 | 41 | * } |
mbed_official | 0:fd0d7bdfcdc2 | 42 | * } |
mbed_official | 0:fd0d7bdfcdc2 | 43 | * } |
mbed_official | 0:fd0d7bdfcdc2 | 44 | * @endcode |
mbed_official | 0:fd0d7bdfcdc2 | 45 | * |
mbed_official | 0:fd0d7bdfcdc2 | 46 | * @note |
mbed_official | 0:fd0d7bdfcdc2 | 47 | * On the LPC1768 and LPC2368, the PWMs all share the same |
mbed_official | 0:fd0d7bdfcdc2 | 48 | * period - if you change the period for one, you change it for all. |
mbed_official | 0:fd0d7bdfcdc2 | 49 | * Although routines that change the period maintain the duty cycle |
mbed_official | 0:fd0d7bdfcdc2 | 50 | * for its PWM, all other PWMs will require their duty cycle to be |
mbed_official | 0:fd0d7bdfcdc2 | 51 | * refreshed. |
mbed_official | 0:fd0d7bdfcdc2 | 52 | */ |
mbed_official | 0:fd0d7bdfcdc2 | 53 | class PwmOut { |
mbed_official | 0:fd0d7bdfcdc2 | 54 | |
mbed_official | 0:fd0d7bdfcdc2 | 55 | public: |
mbed_official | 0:fd0d7bdfcdc2 | 56 | |
mbed_official | 0:fd0d7bdfcdc2 | 57 | /** Create a PwmOut connected to the specified pin |
mbed_official | 0:fd0d7bdfcdc2 | 58 | * |
mbed_official | 0:fd0d7bdfcdc2 | 59 | * @param pin PwmOut pin to connect to |
mbed_official | 0:fd0d7bdfcdc2 | 60 | */ |
mbed_official | 0:fd0d7bdfcdc2 | 61 | PwmOut(PinName pin) { |
mbed_official | 0:fd0d7bdfcdc2 | 62 | pwmout_init(&_pwm, pin); |
mbed_official | 0:fd0d7bdfcdc2 | 63 | } |
mbed_official | 0:fd0d7bdfcdc2 | 64 | |
mbed_official | 0:fd0d7bdfcdc2 | 65 | /** Set the ouput duty-cycle, specified as a percentage (float) |
mbed_official | 0:fd0d7bdfcdc2 | 66 | * |
emilmont | 2:143cac498751 | 67 | * @param value A floating-point value representing the output duty-cycle, |
mbed_official | 0:fd0d7bdfcdc2 | 68 | * specified as a percentage. The value should lie between |
mbed_official | 0:fd0d7bdfcdc2 | 69 | * 0.0f (representing on 0%) and 1.0f (representing on 100%). |
mbed_official | 0:fd0d7bdfcdc2 | 70 | * Values outside this range will be saturated to 0.0f or 1.0f. |
mbed_official | 0:fd0d7bdfcdc2 | 71 | */ |
mbed_official | 0:fd0d7bdfcdc2 | 72 | void write(float value) { |
mbed_official | 0:fd0d7bdfcdc2 | 73 | pwmout_write(&_pwm, value); |
mbed_official | 0:fd0d7bdfcdc2 | 74 | } |
mbed_official | 0:fd0d7bdfcdc2 | 75 | |
mbed_official | 0:fd0d7bdfcdc2 | 76 | /** Return the current output duty-cycle setting, measured as a percentage (float) |
mbed_official | 0:fd0d7bdfcdc2 | 77 | * |
mbed_official | 0:fd0d7bdfcdc2 | 78 | * @returns |
emilmont | 2:143cac498751 | 79 | * A floating-point value representing the current duty-cycle being output on the pin, |
mbed_official | 0:fd0d7bdfcdc2 | 80 | * measured as a percentage. The returned value will lie between |
mbed_official | 0:fd0d7bdfcdc2 | 81 | * 0.0f (representing on 0%) and 1.0f (representing on 100%). |
mbed_official | 0:fd0d7bdfcdc2 | 82 | * |
mbed_official | 0:fd0d7bdfcdc2 | 83 | * @note |
mbed_official | 0:fd0d7bdfcdc2 | 84 | * This value may not match exactly the value set by a previous <write>. |
mbed_official | 0:fd0d7bdfcdc2 | 85 | */ |
mbed_official | 0:fd0d7bdfcdc2 | 86 | float read() { |
mbed_official | 0:fd0d7bdfcdc2 | 87 | return pwmout_read(&_pwm); |
mbed_official | 0:fd0d7bdfcdc2 | 88 | } |
emilmont | 2:143cac498751 | 89 | |
mbed_official | 0:fd0d7bdfcdc2 | 90 | /** Set the PWM period, specified in seconds (float), keeping the duty cycle the same. |
mbed_official | 0:fd0d7bdfcdc2 | 91 | * |
mbed_official | 0:fd0d7bdfcdc2 | 92 | * @note |
mbed_official | 0:fd0d7bdfcdc2 | 93 | * The resolution is currently in microseconds; periods smaller than this |
mbed_official | 0:fd0d7bdfcdc2 | 94 | * will be set to zero. |
mbed_official | 0:fd0d7bdfcdc2 | 95 | */ |
mbed_official | 0:fd0d7bdfcdc2 | 96 | void period(float seconds) { |
mbed_official | 0:fd0d7bdfcdc2 | 97 | pwmout_period(&_pwm, seconds); |
mbed_official | 0:fd0d7bdfcdc2 | 98 | } |
mbed_official | 0:fd0d7bdfcdc2 | 99 | |
mbed_official | 0:fd0d7bdfcdc2 | 100 | /** Set the PWM period, specified in milli-seconds (int), keeping the duty cycle the same. |
mbed_official | 0:fd0d7bdfcdc2 | 101 | */ |
mbed_official | 0:fd0d7bdfcdc2 | 102 | void period_ms(int ms) { |
mbed_official | 0:fd0d7bdfcdc2 | 103 | pwmout_period_ms(&_pwm, ms); |
mbed_official | 0:fd0d7bdfcdc2 | 104 | } |
mbed_official | 0:fd0d7bdfcdc2 | 105 | |
mbed_official | 0:fd0d7bdfcdc2 | 106 | /** Set the PWM period, specified in micro-seconds (int), keeping the duty cycle the same. |
mbed_official | 0:fd0d7bdfcdc2 | 107 | */ |
mbed_official | 0:fd0d7bdfcdc2 | 108 | void period_us(int us) { |
mbed_official | 0:fd0d7bdfcdc2 | 109 | pwmout_period_us(&_pwm, us); |
mbed_official | 0:fd0d7bdfcdc2 | 110 | } |
mbed_official | 0:fd0d7bdfcdc2 | 111 | |
mbed_official | 0:fd0d7bdfcdc2 | 112 | /** Set the PWM pulsewidth, specified in seconds (float), keeping the period the same. |
mbed_official | 0:fd0d7bdfcdc2 | 113 | */ |
mbed_official | 0:fd0d7bdfcdc2 | 114 | void pulsewidth(float seconds) { |
mbed_official | 0:fd0d7bdfcdc2 | 115 | pwmout_pulsewidth(&_pwm, seconds); |
mbed_official | 0:fd0d7bdfcdc2 | 116 | } |
mbed_official | 0:fd0d7bdfcdc2 | 117 | |
mbed_official | 0:fd0d7bdfcdc2 | 118 | /** Set the PWM pulsewidth, specified in milli-seconds (int), keeping the period the same. |
mbed_official | 0:fd0d7bdfcdc2 | 119 | */ |
mbed_official | 0:fd0d7bdfcdc2 | 120 | void pulsewidth_ms(int ms) { |
mbed_official | 0:fd0d7bdfcdc2 | 121 | pwmout_pulsewidth_ms(&_pwm, ms); |
mbed_official | 0:fd0d7bdfcdc2 | 122 | } |
mbed_official | 0:fd0d7bdfcdc2 | 123 | |
mbed_official | 0:fd0d7bdfcdc2 | 124 | /** Set the PWM pulsewidth, specified in micro-seconds (int), keeping the period the same. |
mbed_official | 0:fd0d7bdfcdc2 | 125 | */ |
mbed_official | 0:fd0d7bdfcdc2 | 126 | void pulsewidth_us(int us) { |
mbed_official | 0:fd0d7bdfcdc2 | 127 | pwmout_pulsewidth_us(&_pwm, us); |
mbed_official | 0:fd0d7bdfcdc2 | 128 | } |
mbed_official | 0:fd0d7bdfcdc2 | 129 | |
mbed_official | 0:fd0d7bdfcdc2 | 130 | #ifdef MBED_OPERATORS |
mbed_official | 0:fd0d7bdfcdc2 | 131 | /** A operator shorthand for write() |
mbed_official | 0:fd0d7bdfcdc2 | 132 | */ |
mbed_official | 0:fd0d7bdfcdc2 | 133 | PwmOut& operator= (float value) { |
mbed_official | 0:fd0d7bdfcdc2 | 134 | write(value); |
mbed_official | 0:fd0d7bdfcdc2 | 135 | return *this; |
mbed_official | 0:fd0d7bdfcdc2 | 136 | } |
emilmont | 2:143cac498751 | 137 | |
mbed_official | 0:fd0d7bdfcdc2 | 138 | PwmOut& operator= (PwmOut& rhs) { |
mbed_official | 0:fd0d7bdfcdc2 | 139 | write(rhs.read()); |
mbed_official | 0:fd0d7bdfcdc2 | 140 | return *this; |
mbed_official | 0:fd0d7bdfcdc2 | 141 | } |
emilmont | 2:143cac498751 | 142 | |
mbed_official | 0:fd0d7bdfcdc2 | 143 | /** An operator shorthand for read() |
mbed_official | 0:fd0d7bdfcdc2 | 144 | */ |
mbed_official | 0:fd0d7bdfcdc2 | 145 | operator float() { |
mbed_official | 0:fd0d7bdfcdc2 | 146 | return read(); |
mbed_official | 0:fd0d7bdfcdc2 | 147 | } |
mbed_official | 0:fd0d7bdfcdc2 | 148 | #endif |
mbed_official | 0:fd0d7bdfcdc2 | 149 | |
mbed_official | 0:fd0d7bdfcdc2 | 150 | protected: |
mbed_official | 0:fd0d7bdfcdc2 | 151 | pwmout_t _pwm; |
mbed_official | 0:fd0d7bdfcdc2 | 152 | }; |
mbed_official | 0:fd0d7bdfcdc2 | 153 | |
mbed_official | 0:fd0d7bdfcdc2 | 154 | } // namespace mbed |
mbed_official | 0:fd0d7bdfcdc2 | 155 | |
mbed_official | 0:fd0d7bdfcdc2 | 156 | #endif |
mbed_official | 0:fd0d7bdfcdc2 | 157 | |
mbed_official | 0:fd0d7bdfcdc2 | 158 | #endif |