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