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.

Dependents:   KL25Z_FFT_Demo test_armmath KL25Z_FFT_Demo_tony KL25Z_FFT_Demo_tony ... more

Supported devices

  • LPC1768
  • LPC4088
  • LPC11u24
  • KLxx
  • K20D50M

Introduction

When you read an AnalogIn object it will enable the corresponding ADC channel, depending on the implementation do either one or multiple measurements for more accuracy, and return that value to your program. This way the ADC is only active when it is required, and it is fairly straightforward. However the downside is, is that an ADC is relatively slow. On the LPC1768 it runs at 200kHz -> in that time it could also have done 500 instructions.

FastAnalogIn

This library uses the 'burst' feature of the microcontroller. This allows the ADC on the background to perform the AD conversions without requiring intervention from the microcontroller's core. Also there are no interrupts used, so also your time-sensitive code is not affected.

What the burst feature does is check which AD-channels are enabled, and he converts the enabled AD-channels one at a time. The result he stores in a register, where each channel has its own register. So this library checks which pins are used (you may make several FastAnalogIn objects, both for different pins and for the same pin, generally not extremely useful, but it is supported), and enables the relevant channels.

Reading a pin is done exactly the same for the user as AnalogIn, the read and read_us functions both work the same, and also the float operator is supported. However now it doesn't have to start a new conversion, so minus some overhead it can almost directly return the last measured value, no need to wait on the ADC!

Enable/Disable

FastAnalogIn has a few extra options that normal AnalogIn does not have: specifically you can either choose to have a FastAnalogIn object enabled or disabled. This is done with either the enable(bool enabled) and disable() functions, where enable(false) is equal to disable(), or by adding a second true/false argument to the constructor to either have it enabled at the beginning or disabled. By default it will be enabled.

LPC1768 & LPC4088
When a FastAnalogIn object is enabled, its corresponding ADC channel is also being scanned by the ADC and so it works as described above. When it is disabled you can still use the read functions, only now it will only enable the ADC channel for one conversion (actually two since for some reason the first conversion seems a bit weird), and when that conversion is done it will disable it again.

Since the ADC has to do the conversions one channel at a time, it becomes slower per channel if you enable many channels. For example, if you want to sample a sensor at a very high rate, and you also want to monitor your battery voltage. Then there is no reason to run an AD conversion on your battery continiously, so you can disable that channel and only run it once in a while.

KLxx
Multiple Fast instances can be declared of which only ONE can be continuous (all others must be non-continuous).
Example:

FastAnalogIn   speed(PTC2);           // Fast continuous
FastAnalogIn   temp1(PTC2, 0);        // Fast non-continuous.
FastAnalogIn   temp2(PTB3, 0);        // Fast non-continuous.

Downsides

Of course there are always downsides present. The extra power consumption probably won't be relevant for most, but still it is there. Aditionally there is no median filter like the normal AnalogIn has. Finally if you use AnalogIn you know exactly when the conversion happened, with FastAnalogIn you only know it was recently done but not exactly when.

AnalogIn + FastAnalogIn

Don't run both AnalogIn and FastAnalogIn objects in parallel as the results are unpredictable.
Both objects modify microcontroller registers, and neither of them bothers to inform the other one.
That's also the reason the disable() function was added.

Committer:
Sissors
Date:
Mon Mar 21 07:41:52 2016 +0000
Revision:
12:46fbc645de4d
Parent:
9:31184aa1449c
Fixed pinmapping of LPC1114

Who changed what in which revision?

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