Hi everyone, I'm getting started with ARM microcontrollers and I'm having a issue with ADC in LPC2148, I'm trying to make work a code that is supposed to read the analog input in AD0.6 (P0.4), and show it on an lcd, Clock and Timer is perfectly setted, the only problem is ADC, it is definetely converting something, but isn't the right results, it oscilate when the tension in the pin is constant and things like that, so I would really appreciate if anyone could help me. Thanks in advance, John. Here is the code.
#include <lpc214x.h> /////////////////////// #include "clocks.h" // including // #include "types.h" // libraries // #include "lcd.h" /////////////////////// #define AD06 ((1<<9)|(1<<8)) //Select AD0.6 function for P0.4 #define SEL_AD06 (1<<6) #define CLKDIV (15-1) // 4Mhz ADC clock #define BURST_MODE_OFF (0<<16) // 1 for on and 0 for off #define PowerUP (1<<21) //ADC on #define START_NOW ((0<<26)|(0<<25)|(1<<24)) //001 for starting the conversion immediately #define ADC_DONE (1<<31) //bit done check #define VREF 3.3 //Reference Voltage at VREF pin unsigned long AD0CR_setup; //declaration global variable int result=0; //declaration global variable int main(void) { char s[100]; initClocks(); //Initialize CPU and Peripheral Clocks @ 60Mhz initTimer0(); //Initialize Timer0 lcd_init(); //initialize lcd PINSEL0 |= AD06 ; //select AD0.6 for P0.4 AD0CR_setup = (CLKDIV<<8) | BURST_MODE_OFF | PowerUP;//AD0CR settings lcd_puts("\f"); //clear lcd while(1) { AD0CR = AD0CR_setup | SEL_AD06; //setting up AD0CR register AD0CR |= START_NOW; //Start new Conversion while( (AD0DR6 & ADC_DONE) == 0 );//wait until conversion is done result = (AD0DR6>>6) & 0x3ff;//extracting conversion result of AD0DR6 register sprintf(s,"\rV = %.4d",result);//printing result in the lcd lcd_puts(s); //printing result in the lcd delayMS(1000); //wait 1sec } }
Maybe, when A/D conversion completes, you should stop the A/D conversion first, then read the result.
Sorry if that is a dumb question, but how can I stop the conversion ??
Did you mean that I have to power ADC down?
Clear bit-26:24 of LPC_ADC->CR.
0x0 No start 0x1 Start conversion now.
Still doesn't work :/, but I'm using a multimeter to measure voltage directly in the potentiometer, and the value approaches to the one in the lcd, but isn't right, do you know what might be?
So, the current status is: the ADC value on LCD is stable, and close to the value you want, but you prefer a better value.
Am I right?
Actually not, it keep oscillating, but close to the real value
(I have no hardware knowledge.)
Have you ever refer to the NXP example code?
www.lpcware.com/.../sample-code-bundle-lpc213xlpc214x-peripherals-using-keils-μvision
--
For newer LPC Chips, you should disable the on-chip pull-up/down resistor for ADC pin. But it seems that LPC2148 doesn't have such option.
Sorry, no idea.
I don't think you have mentioned how much the read value varies.
But one issue with the ADC is that it does not measure better than what you can properly stabilize the reference voltage. Any failure to keep the reference stable will result in very bad measurement values. It's easy to quickly get jitter in measurements of 10% or more. So besides an exact reference to get the measurement scale correct, you also want some LC construct to remove all jitter on the reference. Especially important if the reference has some relation to VCC and the wildly varying current consumption of the processor.
Well, I'm using a standard board, and in the documentation there is the circuit utilized in VREF pin of ADC, it is completely isolated of VCC, but now that I'm having this problems, the board seems suspect. I will post the image of the circuit and if you could take a look at it I would really appreciate, because I'm no expert on hardware, so thanks in advance.
circuit image link bellow.
fbcdn-sphotos-h-a.akamaihd.net/.../10659290_858569154176497_7143437423836064933_n.jpg
Per, the problem isn't in VREF pin, I just tested, with a regulated DC power supply on 3V3 and the result was the same :/ and John, thanks for all the helping :)
How to Remove fbcdn-sphotos-a-a.akamaihd.net Popup Tabs.
Most regular visitors would not click suspect links.
There is no popups, actually is just the address of the image in web. But doesn't metter anymore, because I've found out that the circuit isn't the problem, sorry about the link ..