Mistake on this page?
Report an issue in GitHub or email us
AnalogOut.h
1 /* mbed Microcontroller Library
2  * Copyright (c) 2006-2013 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_ANALOGOUT_H
18 #define MBED_ANALOGOUT_H
19 
20 #include "platform/platform.h"
21 
22 #if DEVICE_ANALOGOUT || defined(DOXYGEN_ONLY)
23 
24 #include "hal/analogout_api.h"
25 #include "platform/PlatformMutex.h"
26 
27 namespace mbed {
28 /** \addtogroup drivers */
29 
30 /** An analog output, used for setting the voltage on a pin
31  *
32  * @note Synchronization level: Thread safe
33  *
34  * Example:
35  * @code
36  * // Make a sawtooth output
37  *
38  * #include "mbed.h"
39  *
40  * AnalogOut tri(p18);
41  * int main() {
42  * while(1) {
43  * tri = tri + 0.01;
44  * wait_us(1);
45  * if(tri == 1) {
46  * tri = 0;
47  * }
48  * }
49  * }
50  * @endcode
51  * @ingroup drivers
52  */
53 class AnalogOut {
54 
55 public:
56 
57  /** Create an AnalogOut connected to the specified pin
58  *
59  * @param pin AnalogOut pin to connect to
60  */
61  AnalogOut(PinName pin)
62  {
63  analogout_init(&_dac, pin);
64  }
65 
66  /** Set the output voltage, specified as a percentage (float)
67  *
68  * @param value A floating-point value representing the output voltage,
69  * specified as a percentage. The value should lie between
70  * 0.0f (representing 0v / 0%) and 1.0f (representing 3.3v / 100%).
71  * Values outside this range will be saturated to 0.0f or 1.0f.
72  */
73  void write(float value)
74  {
75  lock();
76  analogout_write(&_dac, value);
77  unlock();
78  }
79 
80  /** Set the output voltage, represented as an unsigned short in the range [0x0, 0xFFFF]
81  *
82  * @param value 16-bit unsigned short representing the output voltage,
83  * normalized to a 16-bit value (0x0000 = 0v, 0xFFFF = 3.3v)
84  */
85  void write_u16(unsigned short value)
86  {
87  lock();
88  analogout_write_u16(&_dac, value);
89  unlock();
90  }
91 
92  /** Return the current output voltage setting, measured as a percentage (float)
93  *
94  * @returns
95  * A floating-point value representing the current voltage being output on the pin,
96  * measured as a percentage. The returned value will lie between
97  * 0.0f (representing 0v / 0%) and 1.0f (representing 3.3v / 100%).
98  *
99  * @note
100  * This value may not match exactly the value set by a previous write().
101  */
102  float read()
103  {
104  lock();
105  float ret = analogout_read(&_dac);
106  unlock();
107  return ret;
108  }
109 
110  /** An operator shorthand for write()
111  * \sa AnalogOut::write()
112  */
113  AnalogOut &operator= (float percent)
114  {
115  // Underlying write call is thread safe
116  write(percent);
117  return *this;
118  }
119 
120  /** An operator shorthand for write()
121  * \sa AnalogOut::write()
122  */
124  {
125  // Underlying write call is thread safe
126  write(rhs.read());
127  return *this;
128  }
129 
130  /** An operator shorthand for read()
131  * \sa AnalogOut::read()
132  */
133  operator float()
134  {
135  // Underlying read call is thread safe
136  return read();
137  }
138 
139  virtual ~AnalogOut()
140  {
141  // Do nothing
142  }
143 
144 protected:
145 #if !defined(DOXYGEN_ONLY)
146  virtual void lock()
147  {
148  _mutex.lock();
149  }
150 
151  virtual void unlock()
152  {
153  _mutex.unlock();
154  }
155 
156  dac_t _dac;
157  PlatformMutex _mutex;
158 #endif //!defined(DOXYGEN_ONLY)
159 };
160 
161 } // namespace mbed
162 
163 #endif
164 
165 #endif
AnalogOut & operator=(float percent)
An operator shorthand for write()
Definition: AnalogOut.h:113
AnalogOut(PinName pin)
Create an AnalogOut connected to the specified pin.
Definition: AnalogOut.h:61
void analogout_write(dac_t *obj, float value)
Set the output voltage, specified as a percentage (float)
struct dac_s dac_t
Analogout hal structure.
Definition: analogout_api.h:34
An analog output, used for setting the voltage on a pin.
Definition: AnalogOut.h:53
float read()
Return the current output voltage setting, measured as a percentage (float)
Definition: AnalogOut.h:102
void write(float value)
Set the output voltage, specified as a percentage (float)
Definition: AnalogOut.h:73
The PlatformMutex class is used to synchronize the execution of threads.
Definition: PlatformMutex.h:47
float analogout_read(dac_t *obj)
Read the current voltage value on the pin.
void analogout_init(dac_t *obj, PinName pin)
Initialize the analogout peripheral.
void write_u16(unsigned short value)
Set the output voltage, represented as an unsigned short in the range [0x0, 0xFFFF].
Definition: AnalogOut.h:85
void analogout_write_u16(dac_t *obj, uint16_t value)
Set the output voltage, specified as unsigned 16-bit.
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.