mbed w/ spi bug fig

Dependents:   display-puck

Fork of mbed-src by mbed official

Committer:
emilmont
Date:
Mon Jun 10 16:03:00 2013 +0100
Revision:
9:0ce32e54c9a7
Parent:
cpp/AnalogOut.h@2:143cac498751
Child:
10:3bc89ef62ce7
Refactoring of the mbed SDK:
- Provide a well defined HAL and API
- Keep separated the HAL implementations for the different targets

Who changed what in which revision?

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