User manual
LPCXpresso Experiment Kit - User’s Guide
Page 55
Copyright 2013 © Embedded Artists AB
Tip #3: After a conversion, the ADC is stopped by resetting bit 24 in the CR register.
Tip #4: When reading the converted value, note that the register value must be shifted in order to be in
the interval of 0-1023 (bit 0-9 valid).
Below is the program structure to use to read the trimming potentiometer value once every 250 ms.
//Include needed libraries
#include<stdio.h>
...
//Define constants
#define AIN0 0
...
//Add ADC functions for initializing and reading values
...
int32_t main(void)
{
printf(“\nThis program reads AIN0 repeatedly...\n”);
//Initialize ADC peripheral and pin-mixing
ADCInit(4500000); //4.5MHz ADC clock
while(1)
{
uint16_t analogValue;
analogValue = getADC(AIN0);
printf(“\nAIN0=%d”, analogValue);
//Delay 250ms
...
}
return 0;
}
You will notice some noise in the converted values. It is not always a stable value. This is quite normal
to expect in a not-noise-optimized setup that we have with the breadboard and the LPCXpresso board.
Besides proper hardware design, a method to handle noise is to low-pass filter the converted values.
Below are two examples of how this can be done. It is a simple first-order filter. The closer to 1 ALFA
is, the more filter effect is applied (the lower the cut-off frequency will be in the filter). Floating point
calculations are not to recommend in smaller embedded systems. The execution time will be long for
these calculations but the biggest problem is typically that the code-size will increase considerable
when the C-runtime floating point libraries are linked to the program. An integer solution is to
recommend instead. One example (where ALFA is 0.875) is outlined below.
//Floating point calculations
#define ALFA 0.95
newValue = ALFA*newValue + (1-ALFA)*newSample;
OR
//Integer calculations
newValue = ((7*newValue) + newSample) >> 3;
Test to filter the samples and observe that they will be more smooth and stable.
Place the ADC read functions in file adc.c.