The official Mbed 2 C/C++ SDK provides the software platform and libraries to build your applications.

Dependents:   hello SerialTestv11 SerialTestv12 Sierpinski ... more

mbed 2

This is the mbed 2 library. If you'd like to learn about Mbed OS please see the mbed-os docs.

Committer:
AnnaBridge
Date:
Fri Sep 15 14:46:57 2017 +0100
Revision:
151:675da3299148
Parent:
145:64910690c574
Child:
153:b484a57bc302
Release 151 of the mbed library.

Who changed what in which revision?

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