SPKT

Dependencies:   F746_GUI SD_PlayerSkeleton F746_SAI_IO

Committer:
phungductung
Date:
Tue Jun 04 21:37:21 2019 +0000
Revision:
0:8ede47d38d10
SPKT

Who changed what in which revision?

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