kao yi
/
Bov3
wu
Fork of CCC by
bx-adc.h@17:3dac99cf2b89, 2014-06-30 (annotated)
- Committer:
- backman
- Date:
- Mon Jun 30 03:37:05 2014 +0000
- Revision:
- 17:3dac99cf2b89
- Parent:
- 1:82bc25a7b68b
wu
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
backman | 1:82bc25a7b68b | 1 | #include "mbed.h" |
backman | 1:82bc25a7b68b | 2 | #include "pinmap.h" |
backman | 1:82bc25a7b68b | 3 | |
backman | 1:82bc25a7b68b | 4 | |
backman | 1:82bc25a7b68b | 5 | |
backman | 1:82bc25a7b68b | 6 | /** A class similar to AnalogIn, only faster, for LPC1768, LPC408X and KLxx |
backman | 1:82bc25a7b68b | 7 | * |
backman | 1:82bc25a7b68b | 8 | * AnalogIn does a single conversion when you read a value (actually several conversions and it takes the median of that). |
backman | 1:82bc25a7b68b | 9 | * This library runns the ADC conversion automatically in the background. |
backman | 1:82bc25a7b68b | 10 | * When read is called, it immediatly returns the last sampled value. |
backman | 1:82bc25a7b68b | 11 | * |
backman | 1:82bc25a7b68b | 12 | * LPC1768 / LPC4088 |
backman | 1:82bc25a7b68b | 13 | * Using more ADC pins in continuous mode will decrease the conversion rate (LPC1768:200kHz/LPC4088:400kHz). |
backman | 1:82bc25a7b68b | 14 | * If you need to sample one pin very fast and sometimes also need to do AD conversions on another pin, |
backman | 1:82bc25a7b68b | 15 | * you can disable the continuous conversion on that ADC channel and still read its value. |
backman | 1:82bc25a7b68b | 16 | * |
backman | 1:82bc25a7b68b | 17 | * KLXX |
backman | 1:82bc25a7b68b | 18 | * Multiple Fast instances can be declared of which only ONE can be continuous (all others must be non-continuous). |
backman | 1:82bc25a7b68b | 19 | * |
backman | 1:82bc25a7b68b | 20 | * When continuous conversion is disabled, a read will block until the conversion is complete |
backman | 1:82bc25a7b68b | 21 | * (much like the regular AnalogIn library does). |
backman | 1:82bc25a7b68b | 22 | * Each ADC channel can be enabled/disabled separately. |
backman | 1:82bc25a7b68b | 23 | * |
backman | 1:82bc25a7b68b | 24 | * IMPORTANT : It does not play nicely with regular AnalogIn objects, so either use this library or AnalogIn, not both at the same time!! |
backman | 1:82bc25a7b68b | 25 | * |
backman | 1:82bc25a7b68b | 26 | * Example for the KLxx processors: |
backman | 1:82bc25a7b68b | 27 | * @code |
backman | 1:82bc25a7b68b | 28 | * // Print messages when the AnalogIn is greater than 50% |
backman | 1:82bc25a7b68b | 29 | * |
backman | 1:82bc25a7b68b | 30 | * #include "mbed.h" |
backman | 1:82bc25a7b68b | 31 | * |
backman | 1:82bc25a7b68b | 32 | * FastAnalogIn temperature(PTC2); //Fast continuous sampling on PTC2 |
backman | 1:82bc25a7b68b | 33 | * FastAnalogIn speed(PTB3, 0); //Fast non-continuous sampling on PTB3 |
backman | 1:82bc25a7b68b | 34 | * |
backman | 1:82bc25a7b68b | 35 | * int main() { |
backman | 1:82bc25a7b68b | 36 | * while(1) { |
backman | 1:82bc25a7b68b | 37 | * if(temperature > 0.5) { |
backman | 1:82bc25a7b68b | 38 | * printf("Too hot! (%f) at speed %f", temperature.read(), speed.read()); |
backman | 1:82bc25a7b68b | 39 | * } |
backman | 1:82bc25a7b68b | 40 | * } |
backman | 1:82bc25a7b68b | 41 | * } |
backman | 1:82bc25a7b68b | 42 | * @endcode |
backman | 1:82bc25a7b68b | 43 | * Example for the LPC1768 processor: |
backman | 1:82bc25a7b68b | 44 | * @code |
backman | 1:82bc25a7b68b | 45 | * // Print messages when the AnalogIn is greater than 50% |
backman | 1:82bc25a7b68b | 46 | * |
backman | 1:82bc25a7b68b | 47 | * #include "mbed.h" |
backman | 1:82bc25a7b68b | 48 | * |
backman | 1:82bc25a7b68b | 49 | * FastAnalogIn temperature(p20); |
backman | 1:82bc25a7b68b | 50 | * |
backman | 1:82bc25a7b68b | 51 | * int main() { |
backman | 1:82bc25a7b68b | 52 | * while(1) { |
backman | 1:82bc25a7b68b | 53 | * if(temperature > 0.5) { |
backman | 1:82bc25a7b68b | 54 | * printf("Too hot! (%f)", temperature.read()); |
backman | 1:82bc25a7b68b | 55 | * } |
backman | 1:82bc25a7b68b | 56 | * } |
backman | 1:82bc25a7b68b | 57 | * } |
backman | 1:82bc25a7b68b | 58 | * @endcode |
backman | 1:82bc25a7b68b | 59 | */ |
backman | 1:82bc25a7b68b | 60 | class FastAnalogIn { |
backman | 1:82bc25a7b68b | 61 | |
backman | 1:82bc25a7b68b | 62 | public: |
backman | 1:82bc25a7b68b | 63 | /** Create a FastAnalogIn, connected to the specified pin |
backman | 1:82bc25a7b68b | 64 | * |
backman | 1:82bc25a7b68b | 65 | * @param pin AnalogIn pin to connect to |
backman | 1:82bc25a7b68b | 66 | * @param enabled Enable the ADC channel (default = true) |
backman | 1:82bc25a7b68b | 67 | */ |
backman | 1:82bc25a7b68b | 68 | FastAnalogIn( PinName pin, bool enabled = true ); |
backman | 1:82bc25a7b68b | 69 | |
backman | 1:82bc25a7b68b | 70 | ~FastAnalogIn( void ) |
backman | 1:82bc25a7b68b | 71 | { |
backman | 1:82bc25a7b68b | 72 | disable(); |
backman | 1:82bc25a7b68b | 73 | } |
backman | 1:82bc25a7b68b | 74 | |
backman | 1:82bc25a7b68b | 75 | /** Enable the ADC channel |
backman | 1:82bc25a7b68b | 76 | * |
backman | 1:82bc25a7b68b | 77 | * @param enabled Bool that is true for enable, false is equivalent to calling disable |
backman | 1:82bc25a7b68b | 78 | */ |
backman | 1:82bc25a7b68b | 79 | void enable(bool enabled = true); |
backman | 1:82bc25a7b68b | 80 | |
backman | 1:82bc25a7b68b | 81 | /** Disable the ADC channel |
backman | 1:82bc25a7b68b | 82 | * |
backman | 1:82bc25a7b68b | 83 | * Disabling unused channels speeds up conversion in used channels. |
backman | 1:82bc25a7b68b | 84 | * 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). |
backman | 1:82bc25a7b68b | 85 | * Then the function blocks until the value is read. This is handy when you sometimes needs a single conversion besides the automatic conversion |
backman | 1:82bc25a7b68b | 86 | */ |
backman | 1:82bc25a7b68b | 87 | void disable( void ); |
backman | 1:82bc25a7b68b | 88 | |
backman | 1:82bc25a7b68b | 89 | /** Returns the raw value |
backman | 1:82bc25a7b68b | 90 | * |
backman | 1:82bc25a7b68b | 91 | * @param return Unsigned integer with converted value |
backman | 1:82bc25a7b68b | 92 | */ |
backman | 1:82bc25a7b68b | 93 | unsigned short read_u16( void ); |
backman | 1:82bc25a7b68b | 94 | |
backman | 1:82bc25a7b68b | 95 | /** Returns the scaled value |
backman | 1:82bc25a7b68b | 96 | * |
backman | 1:82bc25a7b68b | 97 | * @param return Float with scaled converted value to 0.0-1.0 |
backman | 1:82bc25a7b68b | 98 | */ |
backman | 1:82bc25a7b68b | 99 | float read( void ) |
backman | 1:82bc25a7b68b | 100 | { |
backman | 1:82bc25a7b68b | 101 | unsigned short value = read_u16(); |
backman | 1:82bc25a7b68b | 102 | return (float)value * (1.0f/65535.0f); |
backman | 1:82bc25a7b68b | 103 | } |
backman | 1:82bc25a7b68b | 104 | |
backman | 1:82bc25a7b68b | 105 | /** An operator shorthand for read() |
backman | 1:82bc25a7b68b | 106 | */ |
backman | 1:82bc25a7b68b | 107 | operator float() { |
backman | 1:82bc25a7b68b | 108 | return read(); |
backman | 1:82bc25a7b68b | 109 | } |
backman | 1:82bc25a7b68b | 110 | |
backman | 1:82bc25a7b68b | 111 | |
backman | 1:82bc25a7b68b | 112 | private: |
backman | 1:82bc25a7b68b | 113 | bool running; |
backman | 1:82bc25a7b68b | 114 | char ADCnumber; |
backman | 1:82bc25a7b68b | 115 | uint32_t *datareg; |
backman | 1:82bc25a7b68b | 116 | }; |
backman | 1:82bc25a7b68b | 117 |