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