TUKS MCU Introductory course / TUKS-COURSE-TIMER
Committer:
elmot
Date:
Fri Feb 24 21:13:56 2017 +0000
Revision:
1:d0dfbce63a89
Ready-to-copy

Who changed what in which revision?

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