Christian Weiß / Mbed 2 deprecated Diplomarbeit_MW_CW

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers AnalogOut.h Source File

AnalogOut.h

00001 /* mbed Microcontroller Library
00002  * Copyright (c) 2006-2013 ARM Limited
00003  *
00004  * Permission is hereby granted, free of charge, to any person obtaining a copy
00005  * of this software and associated documentation files (the "Software"), to deal
00006  * in the Software without restriction, including without limitation the rights
00007  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00008  * copies of the Software, and to permit persons to whom the Software is
00009  * furnished to do so, subject to the following conditions:
00010  *
00011  * The above copyright notice and this permission notice shall be included in
00012  * all copies or substantial portions of the Software.
00013  *
00014  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00015  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00016  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00017  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00018  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00019  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
00020  * SOFTWARE.
00021  */
00022 #ifndef MBED_ANALOGOUT_H
00023 #define MBED_ANALOGOUT_H
00024 
00025 #include "platform.h"
00026 
00027 #if DEVICE_ANALOGOUT
00028 
00029 #include "analogout_api.h"
00030 
00031 namespace mbed {
00032 
00033 /** An analog output, used for setting the voltage on a pin
00034  *
00035  * Example:
00036  * @code
00037  * // Make a sawtooth output
00038  *
00039  * #include "mbed.h"
00040  *
00041  * AnalogOut tri(p18);
00042  * int main() {
00043  *     while(1) {
00044  *         tri = tri + 0.01;
00045  *         wait_us(1);
00046  *         if(tri == 1) {
00047  *             tri = 0;
00048  *         }
00049  *     }
00050  * }
00051  * @endcode
00052  */
00053 class AnalogOut {
00054 
00055 public:
00056 
00057     /** Create an AnalogOut connected to the specified pin
00058      *
00059      *  @param AnalogOut pin to connect to (18)
00060      */
00061     AnalogOut(PinName pin) {
00062         analogout_init(&_dac, pin);
00063     }
00064 
00065     /** Set the output voltage, specified as a percentage (float)
00066      *
00067      *  @param value A floating-point value representing the output voltage,
00068      *    specified as a percentage. The value should lie between
00069      *    0.0f (representing 0v / 0%) and 1.0f (representing 3.3v / 100%).
00070      *    Values outside this range will be saturated to 0.0f or 1.0f.
00071      */
00072     void write(float value) {
00073         analogout_write(&_dac, value);
00074     }
00075 
00076     /** Set the output voltage, represented as an unsigned short in the range [0x0, 0xFFFF]
00077      *
00078      *  @param value 16-bit unsigned short representing the output voltage,
00079      *            normalised to a 16-bit value (0x0000 = 0v, 0xFFFF = 3.3v)
00080      */
00081     void write_u16(unsigned short value) {
00082         analogout_write_u16(&_dac, value);
00083     }
00084 
00085     /** Return the current output voltage setting, measured as a percentage (float)
00086      *
00087      *  @returns
00088      *    A floating-point value representing the current voltage being output on the pin,
00089      *    measured as a percentage. The returned value will lie between
00090      *    0.0f (representing 0v / 0%) and 1.0f (representing 3.3v / 100%).
00091      *
00092      *  @note
00093      *    This value may not match exactly the value set by a previous write().
00094      */
00095     float read() {
00096         return analogout_read(&_dac);
00097     }
00098 
00099 #ifdef MBED_OPERATORS
00100     /** An operator shorthand for write()
00101      */
00102     AnalogOut& operator= (float percent) {
00103         write(percent);
00104         return *this;
00105     }
00106 
00107     AnalogOut& operator= (AnalogOut& rhs) {
00108         write(rhs.read());
00109         return *this;
00110     }
00111 
00112     /** An operator shorthand for read()
00113      */
00114     operator float() {
00115         return read();
00116     }
00117 #endif
00118 
00119 protected:
00120     dac_t _dac;
00121 };
00122 
00123 } // namespace mbed
00124 
00125 #endif
00126 
00127 #endif
00128