test

Dependents:   robotic_fish_6

Committer:
juansal12
Date:
Fri Dec 03 23:00:34 2021 +0000
Revision:
0:c792b17d9f78
uploaded sofi code ;

Who changed what in which revision?

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