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