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