test

Dependencies:   mbed Watchdog

Dependents:   STM32-MC_node

Committer:
ommpy
Date:
Mon Jul 06 17:18:59 2020 +0530
Revision:
0:d383e2dee0f7
first commit

Who changed what in which revision?

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