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