LPC11U35 ADC Tick & USBSerial

Dependencies:   mbed

Dependents:   SmallDoseMeter_SingleCH_AE_lpc11u35_V1_00

Committer:
H_Tsunemoto
Date:
Mon Feb 19 09:09:52 2018 +0000
Revision:
1:b1a3be5f48ab
Parent:
0:871ab6846b18
test

Who changed what in which revision?

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