Mangue Baja Box

Dependencies:   mbed

Committer:
einsteingustavo
Date:
Mon Jul 29 20:38:00 2019 +0000
Revision:
0:0dee8840a1c0
Mangue Baja Box

Who changed what in which revision?

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