Development mbed library for MAX32630FTHR
Dependents: blinky_max32630fthr
drivers/AnalogIn.h@3:1198227e6421, 2016-12-16 (annotated)
- Committer:
- switches
- Date:
- Fri Dec 16 16:27:57 2016 +0000
- Revision:
- 3:1198227e6421
- Parent:
- 0:5c4d7b2438d3
Changed ADC scale for MAX32625 platforms to 1.2V full scale to match MAX32630 platforms
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
switches | 0:5c4d7b2438d3 | 1 | /* mbed Microcontroller Library |
switches | 0:5c4d7b2438d3 | 2 | * Copyright (c) 2006-2013 ARM Limited |
switches | 0:5c4d7b2438d3 | 3 | * |
switches | 0:5c4d7b2438d3 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
switches | 0:5c4d7b2438d3 | 5 | * you may not use this file except in compliance with the License. |
switches | 0:5c4d7b2438d3 | 6 | * You may obtain a copy of the License at |
switches | 0:5c4d7b2438d3 | 7 | * |
switches | 0:5c4d7b2438d3 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
switches | 0:5c4d7b2438d3 | 9 | * |
switches | 0:5c4d7b2438d3 | 10 | * Unless required by applicable law or agreed to in writing, software |
switches | 0:5c4d7b2438d3 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
switches | 0:5c4d7b2438d3 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
switches | 0:5c4d7b2438d3 | 13 | * See the License for the specific language governing permissions and |
switches | 0:5c4d7b2438d3 | 14 | * limitations under the License. |
switches | 0:5c4d7b2438d3 | 15 | */ |
switches | 0:5c4d7b2438d3 | 16 | #ifndef MBED_ANALOGIN_H |
switches | 0:5c4d7b2438d3 | 17 | #define MBED_ANALOGIN_H |
switches | 0:5c4d7b2438d3 | 18 | |
switches | 0:5c4d7b2438d3 | 19 | #include "platform/platform.h" |
switches | 0:5c4d7b2438d3 | 20 | |
switches | 0:5c4d7b2438d3 | 21 | #if DEVICE_ANALOGIN |
switches | 0:5c4d7b2438d3 | 22 | |
switches | 0:5c4d7b2438d3 | 23 | #include "hal/analogin_api.h" |
switches | 0:5c4d7b2438d3 | 24 | #include "platform/SingletonPtr.h" |
switches | 0:5c4d7b2438d3 | 25 | #include "platform/PlatformMutex.h" |
switches | 0:5c4d7b2438d3 | 26 | |
switches | 0:5c4d7b2438d3 | 27 | namespace mbed { |
switches | 0:5c4d7b2438d3 | 28 | /** \addtogroup drivers */ |
switches | 0:5c4d7b2438d3 | 29 | /** @{*/ |
switches | 0:5c4d7b2438d3 | 30 | |
switches | 0:5c4d7b2438d3 | 31 | /** An analog input, used for reading the voltage on a pin |
switches | 0:5c4d7b2438d3 | 32 | * |
switches | 0:5c4d7b2438d3 | 33 | * @Note Synchronization level: Thread safe |
switches | 0:5c4d7b2438d3 | 34 | * |
switches | 0:5c4d7b2438d3 | 35 | * Example: |
switches | 0:5c4d7b2438d3 | 36 | * @code |
switches | 0:5c4d7b2438d3 | 37 | * // Print messages when the AnalogIn is greater than 50% |
switches | 0:5c4d7b2438d3 | 38 | * |
switches | 0:5c4d7b2438d3 | 39 | * #include "mbed.h" |
switches | 0:5c4d7b2438d3 | 40 | * |
switches | 0:5c4d7b2438d3 | 41 | * AnalogIn temperature(p20); |
switches | 0:5c4d7b2438d3 | 42 | * |
switches | 0:5c4d7b2438d3 | 43 | * int main() { |
switches | 0:5c4d7b2438d3 | 44 | * while(1) { |
switches | 0:5c4d7b2438d3 | 45 | * if(temperature > 0.5) { |
switches | 0:5c4d7b2438d3 | 46 | * printf("Too hot! (%f)", temperature.read()); |
switches | 0:5c4d7b2438d3 | 47 | * } |
switches | 0:5c4d7b2438d3 | 48 | * } |
switches | 0:5c4d7b2438d3 | 49 | * } |
switches | 0:5c4d7b2438d3 | 50 | * @endcode |
switches | 0:5c4d7b2438d3 | 51 | */ |
switches | 0:5c4d7b2438d3 | 52 | class AnalogIn { |
switches | 0:5c4d7b2438d3 | 53 | |
switches | 0:5c4d7b2438d3 | 54 | public: |
switches | 0:5c4d7b2438d3 | 55 | |
switches | 0:5c4d7b2438d3 | 56 | /** Create an AnalogIn, connected to the specified pin |
switches | 0:5c4d7b2438d3 | 57 | * |
switches | 0:5c4d7b2438d3 | 58 | * @param pin AnalogIn pin to connect to |
switches | 0:5c4d7b2438d3 | 59 | * @param name (optional) A string to identify the object |
switches | 0:5c4d7b2438d3 | 60 | */ |
switches | 0:5c4d7b2438d3 | 61 | AnalogIn(PinName pin) { |
switches | 0:5c4d7b2438d3 | 62 | lock(); |
switches | 0:5c4d7b2438d3 | 63 | analogin_init(&_adc, pin); |
switches | 0:5c4d7b2438d3 | 64 | unlock(); |
switches | 0:5c4d7b2438d3 | 65 | } |
switches | 0:5c4d7b2438d3 | 66 | |
switches | 0:5c4d7b2438d3 | 67 | /** Read the input voltage, represented as a float in the range [0.0, 1.0] |
switches | 0:5c4d7b2438d3 | 68 | * |
switches | 0:5c4d7b2438d3 | 69 | * @returns A floating-point value representing the current input voltage, measured as a percentage |
switches | 0:5c4d7b2438d3 | 70 | */ |
switches | 0:5c4d7b2438d3 | 71 | float read() { |
switches | 0:5c4d7b2438d3 | 72 | lock(); |
switches | 0:5c4d7b2438d3 | 73 | float ret = analogin_read(&_adc); |
switches | 0:5c4d7b2438d3 | 74 | unlock(); |
switches | 0:5c4d7b2438d3 | 75 | return ret; |
switches | 0:5c4d7b2438d3 | 76 | } |
switches | 0:5c4d7b2438d3 | 77 | |
switches | 0:5c4d7b2438d3 | 78 | /** Read the input voltage, represented as an unsigned short in the range [0x0, 0xFFFF] |
switches | 0:5c4d7b2438d3 | 79 | * |
switches | 0:5c4d7b2438d3 | 80 | * @returns |
switches | 0:5c4d7b2438d3 | 81 | * 16-bit unsigned short representing the current input voltage, normalised to a 16-bit value |
switches | 0:5c4d7b2438d3 | 82 | */ |
switches | 0:5c4d7b2438d3 | 83 | unsigned short read_u16() { |
switches | 0:5c4d7b2438d3 | 84 | lock(); |
switches | 0:5c4d7b2438d3 | 85 | unsigned short ret = analogin_read_u16(&_adc); |
switches | 0:5c4d7b2438d3 | 86 | unlock(); |
switches | 0:5c4d7b2438d3 | 87 | return ret; |
switches | 0:5c4d7b2438d3 | 88 | } |
switches | 0:5c4d7b2438d3 | 89 | |
switches | 0:5c4d7b2438d3 | 90 | /** An operator shorthand for read() |
switches | 0:5c4d7b2438d3 | 91 | * |
switches | 0:5c4d7b2438d3 | 92 | * The float() operator can be used as a shorthand for read() to simplify common code sequences |
switches | 0:5c4d7b2438d3 | 93 | * |
switches | 0:5c4d7b2438d3 | 94 | * Example: |
switches | 0:5c4d7b2438d3 | 95 | * @code |
switches | 0:5c4d7b2438d3 | 96 | * float x = volume.read(); |
switches | 0:5c4d7b2438d3 | 97 | * float x = volume; |
switches | 0:5c4d7b2438d3 | 98 | * |
switches | 0:5c4d7b2438d3 | 99 | * if(volume.read() > 0.25) { ... } |
switches | 0:5c4d7b2438d3 | 100 | * if(volume > 0.25) { ... } |
switches | 0:5c4d7b2438d3 | 101 | * @endcode |
switches | 0:5c4d7b2438d3 | 102 | */ |
switches | 0:5c4d7b2438d3 | 103 | operator float() { |
switches | 0:5c4d7b2438d3 | 104 | // Underlying call is thread safe |
switches | 0:5c4d7b2438d3 | 105 | return read(); |
switches | 0:5c4d7b2438d3 | 106 | } |
switches | 0:5c4d7b2438d3 | 107 | |
switches | 0:5c4d7b2438d3 | 108 | virtual ~AnalogIn() { |
switches | 0:5c4d7b2438d3 | 109 | // Do nothing |
switches | 0:5c4d7b2438d3 | 110 | } |
switches | 0:5c4d7b2438d3 | 111 | |
switches | 0:5c4d7b2438d3 | 112 | protected: |
switches | 0:5c4d7b2438d3 | 113 | |
switches | 0:5c4d7b2438d3 | 114 | virtual void lock() { |
switches | 0:5c4d7b2438d3 | 115 | _mutex->lock(); |
switches | 0:5c4d7b2438d3 | 116 | } |
switches | 0:5c4d7b2438d3 | 117 | |
switches | 0:5c4d7b2438d3 | 118 | virtual void unlock() { |
switches | 0:5c4d7b2438d3 | 119 | _mutex->unlock(); |
switches | 0:5c4d7b2438d3 | 120 | } |
switches | 0:5c4d7b2438d3 | 121 | |
switches | 0:5c4d7b2438d3 | 122 | analogin_t _adc; |
switches | 0:5c4d7b2438d3 | 123 | static SingletonPtr<PlatformMutex> _mutex; |
switches | 0:5c4d7b2438d3 | 124 | }; |
switches | 0:5c4d7b2438d3 | 125 | |
switches | 0:5c4d7b2438d3 | 126 | } // namespace mbed |
switches | 0:5c4d7b2438d3 | 127 | |
switches | 0:5c4d7b2438d3 | 128 | #endif |
switches | 0:5c4d7b2438d3 | 129 | |
switches | 0:5c4d7b2438d3 | 130 | #endif |
switches | 0:5c4d7b2438d3 | 131 | |
switches | 0:5c4d7b2438d3 | 132 | /** @}*/ |