Support KL25z USB logger

Dependents:   kl25z_Usb_Logger

Fork of FastAnalogIn by Erik -

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers FastAnalogIn.h Source File

FastAnalogIn.h

00001 #ifndef FASTANALOGIN_H
00002 #define FASTANALOGIN_H
00003 
00004 //#define TARGET_KLXX
00005 /*
00006  * Includes
00007  */
00008 #include "mbed.h"
00009 #include "pinmap.h"
00010 
00011 #if !defined TARGET_LPC1768 && !defined TARGET_KLXX && !defined TARGET_LPC408X && !defined TARGET_LPC11UXX && !defined TARGET_K20D5M && !defined TARGET_K20D50M
00012     #error "Target not supported"
00013 #endif
00014 
00015  /** A class similar to AnalogIn, only faster, for LPC1768, LPC408X and KLxx
00016  *
00017  * AnalogIn does a single conversion when you read a value (actually several conversions and it takes the median of that).
00018  * This library runns the ADC conversion automatically in the background.
00019  * When read is called, it immediatly returns the last sampled value.
00020  *
00021  * LPC1768 / LPC4088
00022  * Using more ADC pins in continuous mode will decrease the conversion rate (LPC1768:200kHz/LPC4088:400kHz).
00023  * If you need to sample one pin very fast and sometimes also need to do AD conversions on another pin,
00024  * you can disable the continuous conversion on that ADC channel and still read its value.
00025  *
00026  * KLXX
00027  * Multiple Fast instances can be declared of which only ONE can be continuous (all others must be non-continuous).
00028  *
00029  * When continuous conversion is disabled, a read will block until the conversion is complete
00030  * (much like the regular AnalogIn library does).
00031  * Each ADC channel can be enabled/disabled separately.
00032  *
00033  * IMPORTANT : It does not play nicely with regular AnalogIn objects, so either use this library or AnalogIn, not both at the same time!!
00034  *
00035  * Example for the KLxx processors:
00036  * @code
00037  * // Print messages when the AnalogIn is greater than 50%
00038  *
00039  * #include "mbed.h"
00040  *
00041  * FastAnalogIn temperature(PTC2); //Fast continuous sampling on PTC2
00042  * FastAnalogIn speed(PTB3, 0);    //Fast non-continuous sampling on PTB3
00043  *
00044  * int main() {
00045  *     while(1) {
00046  *         if(temperature > 0.5) {
00047  *             printf("Too hot! (%f) at speed %f", temperature.read(), speed.read());
00048  *         }
00049  *     }
00050  * }
00051  * @endcode
00052  * Example for the LPC1768 processor:
00053  * @code
00054  * // Print messages when the AnalogIn is greater than 50%
00055  *
00056  * #include "mbed.h"
00057  *
00058  * FastAnalogIn temperature(p20);
00059  *
00060  * int main() {
00061  *     while(1) {
00062  *         if(temperature > 0.5) {
00063  *             printf("Too hot! (%f)", temperature.read());
00064  *         }
00065  *     }
00066  * }
00067  * @endcode
00068 */
00069 class FastAnalogIn {
00070 
00071 public:
00072      /** Create a FastAnalogIn, connected to the specified pin
00073      *
00074      * @param pin AnalogIn pin to connect to
00075      * @param enabled Enable the ADC channel (default = true)
00076      */
00077     FastAnalogIn( PinName pin, bool enabled = true );
00078     
00079     ~FastAnalogIn( void )
00080     {
00081         disable();
00082     }
00083     
00084     /** Enable the ADC channel
00085     *
00086     * @param enabled Bool that is true for enable, false is equivalent to calling disable
00087     */
00088     void enable(bool enabled = true);
00089     
00090     /** Disable the ADC channel
00091     *
00092     * Disabling unused channels speeds up conversion in used channels. 
00093     * 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).
00094     * Then the function blocks until the value is read. This is handy when you sometimes needs a single conversion besides the automatic conversion
00095     */
00096     void disable( void );
00097     
00098     /** Returns the raw value
00099     *
00100     * @param return Unsigned integer with converted value
00101     */
00102     unsigned short read_u16( void );
00103     
00104     /** Returns the scaled value
00105     *
00106     * @param return Float with scaled converted value to 0.0-1.0
00107     */
00108     float read( void )
00109     {
00110         unsigned short value = read_u16();
00111         return (float)value * (1.0f/65535.0f);
00112     }
00113     
00114     /** An operator shorthand for read()
00115     */
00116     operator float() {
00117         return read();
00118     }
00119 
00120     
00121 private:
00122     bool running;    
00123     char ADCnumber;
00124     volatile uint32_t *datareg;
00125 };
00126 
00127 #endif