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 Erik Olieman

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