mbed library for NZ32-SC151

Committer:
modtronix
Date:
Fri Jul 24 21:01:44 2015 +1000
Revision:
1:71204b8406f2
Current mbed v103 (594)

Who changed what in which revision?

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