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

This site uses cookies to store information on your computer. By continuing to use our site, you consent to our cookies. If you are not happy with the use of these cookies, please review our Cookie Policy to learn how they can be disabled. By disabling cookies, some features of the site will not work.