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