initial

Dependencies:   mbed

Committer:
yihui
Date:
Mon Jan 11 02:32:24 2016 +0000
Revision:
0:638edba3adf6
initial

Who changed what in which revision?

UserRevisionLine numberNew contents of line
yihui 0:638edba3adf6 1 /* mbed Microcontroller Library
yihui 0:638edba3adf6 2 * Copyright (c) 2006-2013 ARM Limited
yihui 0:638edba3adf6 3 *
yihui 0:638edba3adf6 4 * Licensed under the Apache License, Version 2.0 (the "License");
yihui 0:638edba3adf6 5 * you may not use this file except in compliance with the License.
yihui 0:638edba3adf6 6 * You may obtain a copy of the License at
yihui 0:638edba3adf6 7 *
yihui 0:638edba3adf6 8 * http://www.apache.org/licenses/LICENSE-2.0
yihui 0:638edba3adf6 9 *
yihui 0:638edba3adf6 10 * Unless required by applicable law or agreed to in writing, software
yihui 0:638edba3adf6 11 * distributed under the License is distributed on an "AS IS" BASIS,
yihui 0:638edba3adf6 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
yihui 0:638edba3adf6 13 * See the License for the specific language governing permissions and
yihui 0:638edba3adf6 14 * limitations under the License.
yihui 0:638edba3adf6 15 */
yihui 0:638edba3adf6 16 #ifndef MBED_ANALOGOUT_H
yihui 0:638edba3adf6 17 #define MBED_ANALOGOUT_H
yihui 0:638edba3adf6 18
yihui 0:638edba3adf6 19 #include "platform.h"
yihui 0:638edba3adf6 20
yihui 0:638edba3adf6 21 #if DEVICE_ANALOGOUT
yihui 0:638edba3adf6 22
yihui 0:638edba3adf6 23 #include "analogout_api.h"
yihui 0:638edba3adf6 24
yihui 0:638edba3adf6 25 namespace mbed {
yihui 0:638edba3adf6 26
yihui 0:638edba3adf6 27 /** An analog output, used for setting the voltage on a pin
yihui 0:638edba3adf6 28 *
yihui 0:638edba3adf6 29 * Example:
yihui 0:638edba3adf6 30 * @code
yihui 0:638edba3adf6 31 * // Make a sawtooth output
yihui 0:638edba3adf6 32 *
yihui 0:638edba3adf6 33 * #include "mbed.h"
yihui 0:638edba3adf6 34 *
yihui 0:638edba3adf6 35 * AnalogOut tri(p18);
yihui 0:638edba3adf6 36 * int main() {
yihui 0:638edba3adf6 37 * while(1) {
yihui 0:638edba3adf6 38 * tri = tri + 0.01;
yihui 0:638edba3adf6 39 * wait_us(1);
yihui 0:638edba3adf6 40 * if(tri == 1) {
yihui 0:638edba3adf6 41 * tri = 0;
yihui 0:638edba3adf6 42 * }
yihui 0:638edba3adf6 43 * }
yihui 0:638edba3adf6 44 * }
yihui 0:638edba3adf6 45 * @endcode
yihui 0:638edba3adf6 46 */
yihui 0:638edba3adf6 47 class AnalogOut {
yihui 0:638edba3adf6 48
yihui 0:638edba3adf6 49 public:
yihui 0:638edba3adf6 50
yihui 0:638edba3adf6 51 /** Create an AnalogOut connected to the specified pin
yihui 0:638edba3adf6 52 *
yihui 0:638edba3adf6 53 * @param AnalogOut pin to connect to (18)
yihui 0:638edba3adf6 54 */
yihui 0:638edba3adf6 55 AnalogOut(PinName pin) {
yihui 0:638edba3adf6 56 analogout_init(&_dac, pin);
yihui 0:638edba3adf6 57 }
yihui 0:638edba3adf6 58
yihui 0:638edba3adf6 59 /** Set the output voltage, specified as a percentage (float)
yihui 0:638edba3adf6 60 *
yihui 0:638edba3adf6 61 * @param value A floating-point value representing the output voltage,
yihui 0:638edba3adf6 62 * specified as a percentage. The value should lie between
yihui 0:638edba3adf6 63 * 0.0f (representing 0v / 0%) and 1.0f (representing 3.3v / 100%).
yihui 0:638edba3adf6 64 * Values outside this range will be saturated to 0.0f or 1.0f.
yihui 0:638edba3adf6 65 */
yihui 0:638edba3adf6 66 void write(float value) {
yihui 0:638edba3adf6 67 analogout_write(&_dac, value);
yihui 0:638edba3adf6 68 }
yihui 0:638edba3adf6 69
yihui 0:638edba3adf6 70 /** Set the output voltage, represented as an unsigned short in the range [0x0, 0xFFFF]
yihui 0:638edba3adf6 71 *
yihui 0:638edba3adf6 72 * @param value 16-bit unsigned short representing the output voltage,
yihui 0:638edba3adf6 73 * normalised to a 16-bit value (0x0000 = 0v, 0xFFFF = 3.3v)
yihui 0:638edba3adf6 74 */
yihui 0:638edba3adf6 75 void write_u16(unsigned short value) {
yihui 0:638edba3adf6 76 analogout_write_u16(&_dac, value);
yihui 0:638edba3adf6 77 }
yihui 0:638edba3adf6 78
yihui 0:638edba3adf6 79 /** Return the current output voltage setting, measured as a percentage (float)
yihui 0:638edba3adf6 80 *
yihui 0:638edba3adf6 81 * @returns
yihui 0:638edba3adf6 82 * A floating-point value representing the current voltage being output on the pin,
yihui 0:638edba3adf6 83 * measured as a percentage. The returned value will lie between
yihui 0:638edba3adf6 84 * 0.0f (representing 0v / 0%) and 1.0f (representing 3.3v / 100%).
yihui 0:638edba3adf6 85 *
yihui 0:638edba3adf6 86 * @note
yihui 0:638edba3adf6 87 * This value may not match exactly the value set by a previous write().
yihui 0:638edba3adf6 88 */
yihui 0:638edba3adf6 89 float read() {
yihui 0:638edba3adf6 90 return analogout_read(&_dac);
yihui 0:638edba3adf6 91 }
yihui 0:638edba3adf6 92
yihui 0:638edba3adf6 93 #ifdef MBED_OPERATORS
yihui 0:638edba3adf6 94 /** An operator shorthand for write()
yihui 0:638edba3adf6 95 */
yihui 0:638edba3adf6 96 AnalogOut& operator= (float percent) {
yihui 0:638edba3adf6 97 write(percent);
yihui 0:638edba3adf6 98 return *this;
yihui 0:638edba3adf6 99 }
yihui 0:638edba3adf6 100
yihui 0:638edba3adf6 101 AnalogOut& operator= (AnalogOut& rhs) {
yihui 0:638edba3adf6 102 write(rhs.read());
yihui 0:638edba3adf6 103 return *this;
yihui 0:638edba3adf6 104 }
yihui 0:638edba3adf6 105
yihui 0:638edba3adf6 106 /** An operator shorthand for read()
yihui 0:638edba3adf6 107 */
yihui 0:638edba3adf6 108 operator float() {
yihui 0:638edba3adf6 109 return read();
yihui 0:638edba3adf6 110 }
yihui 0:638edba3adf6 111 #endif
yihui 0:638edba3adf6 112
yihui 0:638edba3adf6 113 protected:
yihui 0:638edba3adf6 114 dac_t _dac;
yihui 0:638edba3adf6 115 };
yihui 0:638edba3adf6 116
yihui 0:638edba3adf6 117 } // namespace mbed
yihui 0:638edba3adf6 118
yihui 0:638edba3adf6 119 #endif
yihui 0:638edba3adf6 120
yihui 0:638edba3adf6 121 #endif