mbed library for NZ32-SC151

Committer:
modtronix-com
Date:
Mon Aug 15 14:57:08 2016 +1000
Revision:
16:076cbe3e55be
Parent:
1:71204b8406f2
Added tag v1.0 for changeset 8123a070ade6

Who changed what in which revision?

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