Lab 1 Program C
Fork of mbed by
AnalogIn.h@27:7110ebee3484, 2011-11-29 (annotated)
- Committer:
- emilmont
- Date:
- Tue Nov 29 14:59:27 2011 +0000
- Revision:
- 27:7110ebee3484
- Parent:
- 11:1c1ebd0324fa
- Child:
- 43:aff670d0d510
New Libraries 11.11
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
simon.ford@mbed.co.uk | 0:82220227f4fa | 1 | /* mbed Microcontroller Library - AnalogIn |
emilmont | 27:7110ebee3484 | 2 | * Copyright (c) 2006-2011 ARM Limited. All rights reserved. |
simon.ford@mbed.co.uk | 5:62573be585e9 | 3 | */ |
simon.ford@mbed.co.uk | 0:82220227f4fa | 4 | |
simon.ford@mbed.co.uk | 0:82220227f4fa | 5 | #ifndef MBED_ANALOGIN_H |
simon.ford@mbed.co.uk | 0:82220227f4fa | 6 | #define MBED_ANALOGIN_H |
simon.ford@mbed.co.uk | 0:82220227f4fa | 7 | |
emilmont | 27:7110ebee3484 | 8 | #include "device.h" |
emilmont | 27:7110ebee3484 | 9 | |
emilmont | 27:7110ebee3484 | 10 | #if DEVICE_ANALOGIN |
emilmont | 27:7110ebee3484 | 11 | |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 12 | #include "platform.h" |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 13 | #include "PinNames.h" |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 14 | #include "PeripheralNames.h" |
simon.ford@mbed.co.uk | 0:82220227f4fa | 15 | #include "Base.h" |
simon.ford@mbed.co.uk | 0:82220227f4fa | 16 | |
simon.ford@mbed.co.uk | 0:82220227f4fa | 17 | namespace mbed { |
simon.ford@mbed.co.uk | 0:82220227f4fa | 18 | |
simon.ford@mbed.co.uk | 0:82220227f4fa | 19 | /* Class: AnalogIn |
simon.ford@mbed.co.uk | 5:62573be585e9 | 20 | * An analog input, used for reading the voltage on a pin |
simon.ford@mbed.co.uk | 5:62573be585e9 | 21 | * |
simon.ford@mbed.co.uk | 5:62573be585e9 | 22 | * Example: |
simon.ford@mbed.co.uk | 5:62573be585e9 | 23 | * > // Print messages when the AnalogIn is greater than 50% |
simon.ford@mbed.co.uk | 5:62573be585e9 | 24 | * > |
simon.ford@mbed.co.uk | 5:62573be585e9 | 25 | * > #include "mbed.h" |
simon.ford@mbed.co.uk | 5:62573be585e9 | 26 | * > |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 27 | * > AnalogIn temperature(p20); |
simon.ford@mbed.co.uk | 5:62573be585e9 | 28 | * > |
simon.ford@mbed.co.uk | 5:62573be585e9 | 29 | * > int main() { |
simon.ford@mbed.co.uk | 5:62573be585e9 | 30 | * > while(1) { |
simon.ford@mbed.co.uk | 5:62573be585e9 | 31 | * > if(temperature > 0.5) { |
simon.ford@mbed.co.uk | 5:62573be585e9 | 32 | * > printf("Too hot! (%f)", temperature.read()); |
simon.ford@mbed.co.uk | 5:62573be585e9 | 33 | * > } |
simon.ford@mbed.co.uk | 5:62573be585e9 | 34 | * > } |
simon.ford@mbed.co.uk | 5:62573be585e9 | 35 | * > } |
simon.ford@mbed.co.uk | 0:82220227f4fa | 36 | */ |
simon.ford@mbed.co.uk | 0:82220227f4fa | 37 | class AnalogIn : public Base { |
simon.ford@mbed.co.uk | 0:82220227f4fa | 38 | |
simon.ford@mbed.co.uk | 0:82220227f4fa | 39 | public: |
simon.ford@mbed.co.uk | 0:82220227f4fa | 40 | |
simon.ford@mbed.co.uk | 5:62573be585e9 | 41 | /* Constructor: AnalogIn |
simon.ford@mbed.co.uk | 5:62573be585e9 | 42 | * Create an AnalogIn, connected to the specified pin |
simon.ford@mbed.co.uk | 5:62573be585e9 | 43 | * |
simon.ford@mbed.co.uk | 5:62573be585e9 | 44 | * Variables: |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 45 | * pin - AnalogIn pin to connect to |
simon.ford@mbed.co.uk | 5:62573be585e9 | 46 | * name - (optional) A string to identify the object |
simon.ford@mbed.co.uk | 5:62573be585e9 | 47 | */ |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 48 | AnalogIn(PinName pin, const char *name = NULL); |
simon.ford@mbed.co.uk | 0:82220227f4fa | 49 | |
simon.ford@mbed.co.uk | 5:62573be585e9 | 50 | /* Function: read |
simon.ford@mbed.co.uk | 5:62573be585e9 | 51 | * Read the input voltage, represented as a float in the range [0.0, 1.0] |
simon.ford@mbed.co.uk | 5:62573be585e9 | 52 | * |
simon.ford@mbed.co.uk | 5:62573be585e9 | 53 | * Variables: |
simon.ford@mbed.co.uk | 5:62573be585e9 | 54 | * returns - A floating-point value representing the current input voltage, |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 55 | * measured as a percentage |
simon.ford@mbed.co.uk | 5:62573be585e9 | 56 | */ |
simon.ford@mbed.co.uk | 5:62573be585e9 | 57 | float read(); |
simon.ford@mbed.co.uk | 5:62573be585e9 | 58 | |
simon.ford@mbed.co.uk | 5:62573be585e9 | 59 | /* Function: read_u16 |
simon.ford@mbed.co.uk | 5:62573be585e9 | 60 | * Read the input voltage, represented as an unsigned short in the range [0x0, 0xFFFF] |
simon.ford@mbed.co.uk | 5:62573be585e9 | 61 | * |
simon.ford@mbed.co.uk | 5:62573be585e9 | 62 | * Variables: |
simon.ford@mbed.co.uk | 5:62573be585e9 | 63 | * returns - 16-bit unsigned short representing the current input voltage, |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 64 | * normalised to a 16-bit value |
simon.ford@mbed.co.uk | 5:62573be585e9 | 65 | */ |
simon.ford@mbed.co.uk | 5:62573be585e9 | 66 | unsigned short read_u16(); |
simon.ford@mbed.co.uk | 5:62573be585e9 | 67 | |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 68 | #ifdef MBED_OPERATORS |
simon.ford@mbed.co.uk | 0:82220227f4fa | 69 | /* Function: operator float |
simon.ford@mbed.co.uk | 5:62573be585e9 | 70 | * An operator shorthand for <read()> |
simon.ford@mbed.co.uk | 5:62573be585e9 | 71 | * |
simon.ford@mbed.co.uk | 5:62573be585e9 | 72 | * The float() operator can be used as a shorthand for <read()> to simplify common code sequences |
simon.ford@mbed.co.uk | 5:62573be585e9 | 73 | * |
simon.ford@mbed.co.uk | 5:62573be585e9 | 74 | * Example: |
simon.ford@mbed.co.uk | 5:62573be585e9 | 75 | * > float x = volume.read(); |
simon.ford@mbed.co.uk | 5:62573be585e9 | 76 | * > float x = volume; |
simon.ford@mbed.co.uk | 5:62573be585e9 | 77 | * > |
simon.ford@mbed.co.uk | 5:62573be585e9 | 78 | * > if(volume.read() > 0.25) { ... } |
simon.ford@mbed.co.uk | 5:62573be585e9 | 79 | * > if(volume > 0.25) { ... } |
simon.ford@mbed.co.uk | 0:82220227f4fa | 80 | */ |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 81 | operator float(); |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 82 | #endif |
simon.ford@mbed.co.uk | 4:5d1359a283bc | 83 | |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 84 | #ifdef MBED_RPC |
simon.ford@mbed.co.uk | 5:62573be585e9 | 85 | virtual const struct rpc_method *get_rpc_methods(); |
simon.ford@mbed.co.uk | 5:62573be585e9 | 86 | static struct rpc_class *get_rpc_class(); |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 87 | #endif |
simon.ford@mbed.co.uk | 5:62573be585e9 | 88 | |
simon.ford@mbed.co.uk | 0:82220227f4fa | 89 | protected: |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 90 | |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 91 | ADCName _adc; |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 92 | |
simon.ford@mbed.co.uk | 0:82220227f4fa | 93 | }; |
simon.ford@mbed.co.uk | 0:82220227f4fa | 94 | |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 95 | } // namespace mbed |
simon.ford@mbed.co.uk | 0:82220227f4fa | 96 | |
simon.ford@mbed.co.uk | 1:6b7f447ca868 | 97 | #endif |
emilmont | 27:7110ebee3484 | 98 | |
emilmont | 27:7110ebee3484 | 99 | #endif |