change some io settings for TWR-K22F-120M

Dependents:   twr_helloworld

Committer:
Jasper_lee
Date:
Tue Dec 23 03:35:08 2014 +0000
Revision:
0:b16d94660a33
change some io setting used in TWR-K22F120M

Who changed what in which revision?

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