++

Fork of mbed-stm32l0/l1-src by lzbp li

Committer:
lzbpli
Date:
Thu Sep 22 02:11:40 2016 +0000
Revision:
638:e94dc43c7733
Parent:
13:0645d8841f51
jianjianjian

Who changed what in which revision?

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