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