Support KL25z USB logger

Dependents:   kl25z_Usb_Logger

Fork of FastAnalogIn by Erik -

Committer:
neilh20
Date:
Sun Nov 09 20:27:42 2014 +0000
Revision:
11:9bf2f6aba4de
Parent:
10:e671743d1b91
FRDM_K20D50 additions. Runs basic writing

Who changed what in which revision?

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