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