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