This is a repository for all my programs or modified programs.
AnalogIn.h@0:72480818e4a9, 2016-09-11 (annotated)
- Committer:
- mturner5
- Date:
- Sun Sep 11 23:48:09 2016 +0000
- Revision:
- 0:72480818e4a9
Made delays for the LEDS and button presses
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mturner5 | 0:72480818e4a9 | 1 | /* mbed Microcontroller Library |
mturner5 | 0:72480818e4a9 | 2 | * Copyright (c) 2006-2013 ARM Limited |
mturner5 | 0:72480818e4a9 | 3 | * |
mturner5 | 0:72480818e4a9 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
mturner5 | 0:72480818e4a9 | 5 | * you may not use this file except in compliance with the License. |
mturner5 | 0:72480818e4a9 | 6 | * You may obtain a copy of the License at |
mturner5 | 0:72480818e4a9 | 7 | * |
mturner5 | 0:72480818e4a9 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
mturner5 | 0:72480818e4a9 | 9 | * |
mturner5 | 0:72480818e4a9 | 10 | * Unless required by applicable law or agreed to in writing, software |
mturner5 | 0:72480818e4a9 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
mturner5 | 0:72480818e4a9 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
mturner5 | 0:72480818e4a9 | 13 | * See the License for the specific language governing permissions and |
mturner5 | 0:72480818e4a9 | 14 | * limitations under the License. |
mturner5 | 0:72480818e4a9 | 15 | */ |
mturner5 | 0:72480818e4a9 | 16 | #ifndef MBED_ANALOGIN_H |
mturner5 | 0:72480818e4a9 | 17 | #define MBED_ANALOGIN_H |
mturner5 | 0:72480818e4a9 | 18 | |
mturner5 | 0:72480818e4a9 | 19 | #include "platform.h" |
mturner5 | 0:72480818e4a9 | 20 | |
mturner5 | 0:72480818e4a9 | 21 | #if DEVICE_ANALOGIN |
mturner5 | 0:72480818e4a9 | 22 | |
mturner5 | 0:72480818e4a9 | 23 | #include "analogin_api.h" |
mturner5 | 0:72480818e4a9 | 24 | |
mturner5 | 0:72480818e4a9 | 25 | namespace mbed { |
mturner5 | 0:72480818e4a9 | 26 | |
mturner5 | 0:72480818e4a9 | 27 | /** An analog input, used for reading the voltage on a pin |
mturner5 | 0:72480818e4a9 | 28 | * |
mturner5 | 0:72480818e4a9 | 29 | * Example: |
mturner5 | 0:72480818e4a9 | 30 | * @code |
mturner5 | 0:72480818e4a9 | 31 | * // Print messages when the AnalogIn is greater than 50% |
mturner5 | 0:72480818e4a9 | 32 | * |
mturner5 | 0:72480818e4a9 | 33 | * #include "mbed.h" |
mturner5 | 0:72480818e4a9 | 34 | * |
mturner5 | 0:72480818e4a9 | 35 | * AnalogIn temperature(p20); |
mturner5 | 0:72480818e4a9 | 36 | * |
mturner5 | 0:72480818e4a9 | 37 | * int main() { |
mturner5 | 0:72480818e4a9 | 38 | * while(1) { |
mturner5 | 0:72480818e4a9 | 39 | * if(temperature > 0.5) { |
mturner5 | 0:72480818e4a9 | 40 | * printf("Too hot! (%f)", temperature.read()); |
mturner5 | 0:72480818e4a9 | 41 | * } |
mturner5 | 0:72480818e4a9 | 42 | * } |
mturner5 | 0:72480818e4a9 | 43 | * } |
mturner5 | 0:72480818e4a9 | 44 | * @endcode |
mturner5 | 0:72480818e4a9 | 45 | */ |
mturner5 | 0:72480818e4a9 | 46 | class AnalogIn { |
mturner5 | 0:72480818e4a9 | 47 | |
mturner5 | 0:72480818e4a9 | 48 | public: |
mturner5 | 0:72480818e4a9 | 49 | |
mturner5 | 0:72480818e4a9 | 50 | /** Create an AnalogIn, connected to the specified pin |
mturner5 | 0:72480818e4a9 | 51 | * |
mturner5 | 0:72480818e4a9 | 52 | * @param pin AnalogIn pin to connect to |
mturner5 | 0:72480818e4a9 | 53 | * @param name (optional) A string to identify the object |
mturner5 | 0:72480818e4a9 | 54 | */ |
mturner5 | 0:72480818e4a9 | 55 | AnalogIn(PinName pin) { |
mturner5 | 0:72480818e4a9 | 56 | analogin_init(&_adc, pin); |
mturner5 | 0:72480818e4a9 | 57 | } |
mturner5 | 0:72480818e4a9 | 58 | |
mturner5 | 0:72480818e4a9 | 59 | /** Read the input voltage, represented as a float in the range [0.0, 1.0] |
mturner5 | 0:72480818e4a9 | 60 | * |
mturner5 | 0:72480818e4a9 | 61 | * @returns A floating-point value representing the current input voltage, measured as a percentage |
mturner5 | 0:72480818e4a9 | 62 | */ |
mturner5 | 0:72480818e4a9 | 63 | float read() { |
mturner5 | 0:72480818e4a9 | 64 | return analogin_read(&_adc); |
mturner5 | 0:72480818e4a9 | 65 | } |
mturner5 | 0:72480818e4a9 | 66 | |
mturner5 | 0:72480818e4a9 | 67 | /** Read the input voltage, represented as an unsigned short in the range [0x0, 0xFFFF] |
mturner5 | 0:72480818e4a9 | 68 | * |
mturner5 | 0:72480818e4a9 | 69 | * @returns |
mturner5 | 0:72480818e4a9 | 70 | * 16-bit unsigned short representing the current input voltage, normalised to a 16-bit value |
mturner5 | 0:72480818e4a9 | 71 | */ |
mturner5 | 0:72480818e4a9 | 72 | unsigned short read_u16() { |
mturner5 | 0:72480818e4a9 | 73 | return analogin_read_u16(&_adc); |
mturner5 | 0:72480818e4a9 | 74 | } |
mturner5 | 0:72480818e4a9 | 75 | |
mturner5 | 0:72480818e4a9 | 76 | #ifdef MBED_OPERATORS |
mturner5 | 0:72480818e4a9 | 77 | /** An operator shorthand for read() |
mturner5 | 0:72480818e4a9 | 78 | * |
mturner5 | 0:72480818e4a9 | 79 | * The float() operator can be used as a shorthand for read() to simplify common code sequences |
mturner5 | 0:72480818e4a9 | 80 | * |
mturner5 | 0:72480818e4a9 | 81 | * Example: |
mturner5 | 0:72480818e4a9 | 82 | * @code |
mturner5 | 0:72480818e4a9 | 83 | * float x = volume.read(); |
mturner5 | 0:72480818e4a9 | 84 | * float x = volume; |
mturner5 | 0:72480818e4a9 | 85 | * |
mturner5 | 0:72480818e4a9 | 86 | * if(volume.read() > 0.25) { ... } |
mturner5 | 0:72480818e4a9 | 87 | * if(volume > 0.25) { ... } |
mturner5 | 0:72480818e4a9 | 88 | * @endcode |
mturner5 | 0:72480818e4a9 | 89 | */ |
mturner5 | 0:72480818e4a9 | 90 | operator float() { |
mturner5 | 0:72480818e4a9 | 91 | return read(); |
mturner5 | 0:72480818e4a9 | 92 | } |
mturner5 | 0:72480818e4a9 | 93 | #endif |
mturner5 | 0:72480818e4a9 | 94 | |
mturner5 | 0:72480818e4a9 | 95 | protected: |
mturner5 | 0:72480818e4a9 | 96 | analogin_t _adc; |
mturner5 | 0:72480818e4a9 | 97 | }; |
mturner5 | 0:72480818e4a9 | 98 | |
mturner5 | 0:72480818e4a9 | 99 | } // namespace mbed |
mturner5 | 0:72480818e4a9 | 100 | |
mturner5 | 0:72480818e4a9 | 101 | #endif |
mturner5 | 0:72480818e4a9 | 102 | |
mturner5 | 0:72480818e4a9 | 103 | #endif |