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:
Sat May 11 08:56:22 2013 +0000
Revision:
1:575f4d2d6e9c
Parent:
0:c2a7b899e6c7
Read_u16 left alligned now, same as normal mbed lib (lower 4 bits = upper 4 bits)

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Sissors 0:c2a7b899e6c7 1 #include "FastAnalogIn.h"
Sissors 0:c2a7b899e6c7 2 static inline int div_round_up(int x, int y)
Sissors 0:c2a7b899e6c7 3 {
Sissors 0:c2a7b899e6c7 4 return (x + (y - 1)) / y;
Sissors 0:c2a7b899e6c7 5 }
Sissors 0:c2a7b899e6c7 6
Sissors 0:c2a7b899e6c7 7 const PinMap FastAnalogIn::PinMap_ADC[] = {
Sissors 0:c2a7b899e6c7 8 {P0_23, ADC0_0, 1},
Sissors 0:c2a7b899e6c7 9 {P0_24, ADC0_1, 1},
Sissors 0:c2a7b899e6c7 10 {P0_25, ADC0_2, 1},
Sissors 0:c2a7b899e6c7 11 {P0_26, ADC0_3, 1},
Sissors 0:c2a7b899e6c7 12 {P1_30, ADC0_4, 3},
Sissors 0:c2a7b899e6c7 13 {P1_31, ADC0_5, 3},
Sissors 0:c2a7b899e6c7 14 {P0_2, ADC0_7, 2},
Sissors 0:c2a7b899e6c7 15 {P0_3, ADC0_6, 2},
Sissors 0:c2a7b899e6c7 16 {NC, NC, 0}
Sissors 0:c2a7b899e6c7 17 };
Sissors 0:c2a7b899e6c7 18
Sissors 0:c2a7b899e6c7 19 int FastAnalogIn::channel_usage[] = {0};
Sissors 0:c2a7b899e6c7 20
Sissors 0:c2a7b899e6c7 21
Sissors 0:c2a7b899e6c7 22 FastAnalogIn::FastAnalogIn(PinName pin, bool enabled)
Sissors 0:c2a7b899e6c7 23 {
Sissors 0:c2a7b899e6c7 24 ADCnumber = (ADCName)pinmap_peripheral(pin, PinMap_ADC);
Sissors 0:c2a7b899e6c7 25 if (ADCnumber == (uint32_t)NC)
Sissors 0:c2a7b899e6c7 26 error("ADC pin mapping failed");
Sissors 0:c2a7b899e6c7 27 datareg = (uint32_t*) (&LPC_ADC->ADDR0 + ADCnumber);
Sissors 0:c2a7b899e6c7 28
Sissors 0:c2a7b899e6c7 29 // ensure power is turned on
Sissors 0:c2a7b899e6c7 30 LPC_SC->PCONP |= (1 << 12);
Sissors 0:c2a7b899e6c7 31 // set PCLK of ADC to /1
Sissors 0:c2a7b899e6c7 32 LPC_SC->PCLKSEL0 &= ~(0x3 << 24);
Sissors 0:c2a7b899e6c7 33 LPC_SC->PCLKSEL0 |= (0x1 << 24);
Sissors 0:c2a7b899e6c7 34 uint32_t PCLK = SystemCoreClock;
Sissors 0:c2a7b899e6c7 35
Sissors 0:c2a7b899e6c7 36 // calculate minimum clock divider
Sissors 0:c2a7b899e6c7 37 // clkdiv = divider - 1
Sissors 0:c2a7b899e6c7 38 uint32_t MAX_ADC_CLK = 13000000;
Sissors 0:c2a7b899e6c7 39 uint32_t clkdiv = div_round_up(PCLK, MAX_ADC_CLK) - 1;
Sissors 0:c2a7b899e6c7 40 // Set the clkdiv
Sissors 0:c2a7b899e6c7 41 LPC_ADC->ADCR &= ~(255<<8);
Sissors 0:c2a7b899e6c7 42 LPC_ADC->ADCR |= clkdiv<<8;
Sissors 0:c2a7b899e6c7 43
Sissors 0:c2a7b899e6c7 44 //Enable ADC:
Sissors 0:c2a7b899e6c7 45 LPC_ADC->ADCR |= 1<<21;
Sissors 0:c2a7b899e6c7 46
Sissors 0:c2a7b899e6c7 47 //Enable burstmode, set start as zero
Sissors 0:c2a7b899e6c7 48 LPC_ADC->ADCR |= 1<<16;
Sissors 0:c2a7b899e6c7 49 LPC_ADC->ADCR &= ~(7<<24);
Sissors 0:c2a7b899e6c7 50
Sissors 0:c2a7b899e6c7 51 //Map pins
Sissors 0:c2a7b899e6c7 52 pinmap_pinout(pin, PinMap_ADC);
Sissors 0:c2a7b899e6c7 53
Sissors 0:c2a7b899e6c7 54 //Enable channel
Sissors 0:c2a7b899e6c7 55 running = false;
Sissors 0:c2a7b899e6c7 56 enable(enabled);
Sissors 0:c2a7b899e6c7 57
Sissors 0:c2a7b899e6c7 58 };
Sissors 0:c2a7b899e6c7 59
Sissors 0:c2a7b899e6c7 60 FastAnalogIn::~FastAnalogIn(void)
Sissors 0:c2a7b899e6c7 61 {
Sissors 0:c2a7b899e6c7 62 disable();
Sissors 0:c2a7b899e6c7 63 }
Sissors 0:c2a7b899e6c7 64
Sissors 0:c2a7b899e6c7 65
Sissors 0:c2a7b899e6c7 66 void FastAnalogIn::enable(bool enabled)
Sissors 0:c2a7b899e6c7 67 {
Sissors 0:c2a7b899e6c7 68 //If currently not running
Sissors 0:c2a7b899e6c7 69 if (!running) {
Sissors 0:c2a7b899e6c7 70 if (enabled) {
Sissors 0:c2a7b899e6c7 71 //Enable the ADC channel
Sissors 0:c2a7b899e6c7 72 channel_usage[ADCnumber]++;
Sissors 0:c2a7b899e6c7 73 LPC_ADC->ADCR |= (1<<ADCnumber);
Sissors 0:c2a7b899e6c7 74 running = true;
Sissors 0:c2a7b899e6c7 75 } else
Sissors 0:c2a7b899e6c7 76 disable();
Sissors 0:c2a7b899e6c7 77 }
Sissors 0:c2a7b899e6c7 78 };
Sissors 0:c2a7b899e6c7 79
Sissors 0:c2a7b899e6c7 80 void FastAnalogIn::disable( void )
Sissors 0:c2a7b899e6c7 81 {
Sissors 0:c2a7b899e6c7 82 //If currently running
Sissors 0:c2a7b899e6c7 83 if (running) {
Sissors 0:c2a7b899e6c7 84 channel_usage[ADCnumber]--;
Sissors 0:c2a7b899e6c7 85
Sissors 0:c2a7b899e6c7 86 if (channel_usage[ADCnumber]==0)
Sissors 0:c2a7b899e6c7 87 LPC_ADC->ADCR &= ~(1<<ADCnumber);
Sissors 0:c2a7b899e6c7 88 }
Sissors 0:c2a7b899e6c7 89 running = false;
Sissors 0:c2a7b899e6c7 90 };
Sissors 0:c2a7b899e6c7 91
Sissors 0:c2a7b899e6c7 92 unsigned short FastAnalogIn::read_u16( void )
Sissors 0:c2a7b899e6c7 93 {
Sissors 1:575f4d2d6e9c 94 volatile unsigned int retval;
Sissors 0:c2a7b899e6c7 95 //If object is enabled return current value of datareg
Sissors 1:575f4d2d6e9c 96 if (running )
Sissors 1:575f4d2d6e9c 97 retval = *datareg;
Sissors 1:575f4d2d6e9c 98
Sissors 0:c2a7b899e6c7 99 //If it isn't running, enable it and wait until new value is written to datareg
Sissors 0:c2a7b899e6c7 100 else {
Sissors 0:c2a7b899e6c7 101 //Force a read to clear done bit, enable the ADC channel
Sissors 1:575f4d2d6e9c 102 retval = *datareg;
Sissors 0:c2a7b899e6c7 103 enable();
Sissors 0:c2a7b899e6c7 104 //Wait until it is converted
Sissors 0:c2a7b899e6c7 105 while(1) {
Sissors 0:c2a7b899e6c7 106 wait_us(1);
Sissors 0:c2a7b899e6c7 107 retval = *datareg;
Sissors 0:c2a7b899e6c7 108 if ((retval>>31) == 1)
Sissors 0:c2a7b899e6c7 109 break;
Sissors 0:c2a7b899e6c7 110 }
Sissors 0:c2a7b899e6c7 111
Sissors 0:c2a7b899e6c7 112 //Do a second conversion since first one always fails for some reason
Sissors 0:c2a7b899e6c7 113 while(1) {
Sissors 0:c2a7b899e6c7 114 wait_us(1);
Sissors 0:c2a7b899e6c7 115 retval = *datareg;
Sissors 0:c2a7b899e6c7 116 if ((retval>>31) == 1)
Sissors 0:c2a7b899e6c7 117 break;
Sissors 0:c2a7b899e6c7 118 }
Sissors 0:c2a7b899e6c7 119
Sissors 0:c2a7b899e6c7 120 //Disable again
Sissors 0:c2a7b899e6c7 121 disable();
Sissors 0:c2a7b899e6c7 122 }
Sissors 1:575f4d2d6e9c 123
Sissors 1:575f4d2d6e9c 124 //Do same thing as standard mbed lib, unused bit 0-3, replicate 4-7 in it
Sissors 1:575f4d2d6e9c 125 retval &= ~0xFFFF000F;
Sissors 1:575f4d2d6e9c 126 retval |= (retval >> 8) & 0x000F;
Sissors 1:575f4d2d6e9c 127 return retval;
Sissors 0:c2a7b899e6c7 128
Sissors 0:c2a7b899e6c7 129 };
Sissors 0:c2a7b899e6c7 130
Sissors 0:c2a7b899e6c7 131 float FastAnalogIn::read( void )
Sissors 0:c2a7b899e6c7 132 {
Sissors 0:c2a7b899e6c7 133 unsigned short value = read_u16();
Sissors 1:575f4d2d6e9c 134 return (float)value/65535;
Sissors 0:c2a7b899e6c7 135 }