wu

Dependencies:   mbed-rtos mbed

Fork of Boboobooov4 by kao yi

Committer:
backman
Date:
Wed Jul 02 06:44:48 2014 +0000
Revision:
20:30799cbda86b
Parent:
19:4869b10a962e
al

Who changed what in which revision?

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