Class similar to AnalogIn that uses burst mode to run continious background conversions so when the input is read, the last value can immediatly be returned. This slightly modified version allows NC pins.
Dependents: Pinscape_Controller
Fork of FastAnalogIn by
FastAnalogIn.h@0:c2a7b899e6c7, 2013-05-09 (annotated)
- Committer:
- Sissors
- Date:
- Thu May 09 17:46:32 2013 +0000
- Revision:
- 0:c2a7b899e6c7
- Child:
- 2:9b61d0792927
v0.1
;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Sissors | 0:c2a7b899e6c7 | 1 | #ifndef FASTANALOGIN_H |
Sissors | 0:c2a7b899e6c7 | 2 | #define FASTANALOGIN_H |
Sissors | 0:c2a7b899e6c7 | 3 | |
Sissors | 0:c2a7b899e6c7 | 4 | /* |
Sissors | 0:c2a7b899e6c7 | 5 | * Includes |
Sissors | 0:c2a7b899e6c7 | 6 | */ |
Sissors | 0:c2a7b899e6c7 | 7 | #include "mbed.h" |
Sissors | 0:c2a7b899e6c7 | 8 | #include "pinmap.h" |
Sissors | 0:c2a7b899e6c7 | 9 | |
Sissors | 0:c2a7b899e6c7 | 10 | #ifndef TARGET_LPC1768 |
Sissors | 0:c2a7b899e6c7 | 11 | #error "Target not supported" |
Sissors | 0:c2a7b899e6c7 | 12 | #endif |
Sissors | 0:c2a7b899e6c7 | 13 | |
Sissors | 0:c2a7b899e6c7 | 14 | /** A class that is similar to AnalogIn, only faster |
Sissors | 0:c2a7b899e6c7 | 15 | * |
Sissors | 0:c2a7b899e6c7 | 16 | * AnalogIn does a single conversion when you read a value (actually several conversions and it takes the median of that). |
Sissors | 0:c2a7b899e6c7 | 17 | * This library has all used ADC channels running automatically in the background, and if you read a value it will immediatly return the last converted value. |
Sissors | 0:c2a7b899e6c7 | 18 | * |
Sissors | 0:c2a7b899e6c7 | 19 | * You can use several FastAnalogIn objects per ADC pin, and several ADC pins at the same time. Using more ADC pins will decrease the conversion rate. |
Sissors | 0:c2a7b899e6c7 | 20 | * If you need to generally convert one pin very fast, but sometimes also need to do AD conversions on another pin, you can disable the ADC channel and still read its value. |
Sissors | 0:c2a7b899e6c7 | 21 | * Only now it will block until conversion is complete, so more like the regular AnalogIn library. Of course you can also disable an ADC channel and enable it later. |
Sissors | 0:c2a7b899e6c7 | 22 | * |
Sissors | 0:c2a7b899e6c7 | 23 | * It does not play nicely with regular AnalogIn objects, so use only this library or only AnalogIn. Also currently only LPC1768 is supported. |
Sissors | 0:c2a7b899e6c7 | 24 | */ |
Sissors | 0:c2a7b899e6c7 | 25 | class FastAnalogIn { |
Sissors | 0:c2a7b899e6c7 | 26 | |
Sissors | 0:c2a7b899e6c7 | 27 | public: |
Sissors | 0:c2a7b899e6c7 | 28 | /** Create a FastAnalogIn, connected to the specified pin |
Sissors | 0:c2a7b899e6c7 | 29 | * |
Sissors | 0:c2a7b899e6c7 | 30 | * @param pin AnalogIn pin to connect to |
Sissors | 0:c2a7b899e6c7 | 31 | * @param enabled Enable the ADC channel (default = true) |
Sissors | 0:c2a7b899e6c7 | 32 | */ |
Sissors | 0:c2a7b899e6c7 | 33 | FastAnalogIn( PinName pin, bool enabled = true ); |
Sissors | 0:c2a7b899e6c7 | 34 | |
Sissors | 0:c2a7b899e6c7 | 35 | ~FastAnalogIn( void ); |
Sissors | 0:c2a7b899e6c7 | 36 | |
Sissors | 0:c2a7b899e6c7 | 37 | /** Enable the ADC channel |
Sissors | 0:c2a7b899e6c7 | 38 | * |
Sissors | 0:c2a7b899e6c7 | 39 | * @param enabled Bool that is true for enable, false is equivalent to calling disable |
Sissors | 0:c2a7b899e6c7 | 40 | */ |
Sissors | 0:c2a7b899e6c7 | 41 | void enable(bool enabled = true); |
Sissors | 0:c2a7b899e6c7 | 42 | |
Sissors | 0:c2a7b899e6c7 | 43 | /** Disable the ADC channel |
Sissors | 0:c2a7b899e6c7 | 44 | * |
Sissors | 0:c2a7b899e6c7 | 45 | * Disabling unused channels speeds up conversion in used channels. |
Sissors | 0:c2a7b899e6c7 | 46 | * When disabled you can still call read, that will do a single conversion (actually two since the first one always returns 0 for unknown reason). |
Sissors | 0:c2a7b899e6c7 | 47 | * Then the function blocks until the value is read. This is handy when you sometimes needs a single conversion besides the automatic conversion |
Sissors | 0:c2a7b899e6c7 | 48 | */ |
Sissors | 0:c2a7b899e6c7 | 49 | void disable( void ); |
Sissors | 0:c2a7b899e6c7 | 50 | |
Sissors | 0:c2a7b899e6c7 | 51 | /** Returns the raw value |
Sissors | 0:c2a7b899e6c7 | 52 | * |
Sissors | 0:c2a7b899e6c7 | 53 | * @param return Unsigned integer with converted value |
Sissors | 0:c2a7b899e6c7 | 54 | */ |
Sissors | 0:c2a7b899e6c7 | 55 | unsigned short read_u16( void ); |
Sissors | 0:c2a7b899e6c7 | 56 | |
Sissors | 0:c2a7b899e6c7 | 57 | /** Returns the scaled value |
Sissors | 0:c2a7b899e6c7 | 58 | * |
Sissors | 0:c2a7b899e6c7 | 59 | * @param return Float with scaled converted value to 0.0-1.0 |
Sissors | 0:c2a7b899e6c7 | 60 | */ |
Sissors | 0:c2a7b899e6c7 | 61 | float read( void ); |
Sissors | 0:c2a7b899e6c7 | 62 | |
Sissors | 0:c2a7b899e6c7 | 63 | /** An operator shorthand for read() |
Sissors | 0:c2a7b899e6c7 | 64 | */ |
Sissors | 0:c2a7b899e6c7 | 65 | operator float() { |
Sissors | 0:c2a7b899e6c7 | 66 | return read(); |
Sissors | 0:c2a7b899e6c7 | 67 | } |
Sissors | 0:c2a7b899e6c7 | 68 | |
Sissors | 0:c2a7b899e6c7 | 69 | |
Sissors | 0:c2a7b899e6c7 | 70 | private: |
Sissors | 0:c2a7b899e6c7 | 71 | static const PinMap PinMap_ADC[9]; |
Sissors | 0:c2a7b899e6c7 | 72 | static int channel_usage[8]; |
Sissors | 0:c2a7b899e6c7 | 73 | |
Sissors | 0:c2a7b899e6c7 | 74 | bool running; |
Sissors | 0:c2a7b899e6c7 | 75 | char ADCnumber; |
Sissors | 0:c2a7b899e6c7 | 76 | uint32_t *datareg; |
Sissors | 0:c2a7b899e6c7 | 77 | |
Sissors | 0:c2a7b899e6c7 | 78 | |
Sissors | 0:c2a7b899e6c7 | 79 | |
Sissors | 0:c2a7b899e6c7 | 80 | |
Sissors | 0:c2a7b899e6c7 | 81 | }; |
Sissors | 0:c2a7b899e6c7 | 82 | |
Sissors | 0:c2a7b899e6c7 | 83 | #endif |