ADC Register Reading Problem

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

ADC Register Reading Problem

175 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by danmec on Mon Mar 07 13:37:09 MST 2011
I am trying out the LPC1114 LPCXpresso board and I am having some issues with the ADC.  I currently have a program running that starts an adc conversion on one of AD0-AD3 initiated by the timer.  I set a break point inside the adc irq handler after I transfer LPC_ADC->DR[selectedChannel] to a variable named value1.
The issue I am having is that value1 is not getting the correct value every time, just most of the time. 
This is based on the debugger showing ADDR0 and value1 as different.
I tied AD0 to 3.3V and I get the expected value from the debugger window.

Below is the ADC related code and attached is the debugger view stopped at the "selectedChannel++;" line

void initAdc() {

/* Disable Power down bit to the ADC block. */
LPC_SYSCON->PDRUNCFG &= ~(0x1 << 4);

/* Enable AHB clock to the ADC. */
LPC_SYSCON->SYSAHBCLKCTRL |= (1 << 13);

LPC_IOCON->R_PIO0_11 = 0x02; // Select AD0 pin function
LPC_IOCON->R_PIO1_0 = 0x02; // Select AD0 pin function
LPC_IOCON->R_PIO1_1 = 0x02; // Select AD0 pin function
LPC_IOCON->R_PIO1_2 = 0x02; // Select AD0 pin function

LPC_ADC->CR = ((SystemCoreClock / LPC_SYSCON->SYSAHBCLKDIV) / 2400000 - 1) << 8; // Set clock to 2.4 MHz

LPC_ADC->CR |= 0x01; // Select channel AD0


NVIC_EnableIRQ(ADC_IRQn);
LPC_ADC->INTEN = 0x00F;// enable ADC0 interrupt
}

void ADC_IRQHandler (void)
{
static volatile uint8_t selectedChannel = 0;
volatile uint32_t value1 = 0, value2 = 0, value3 = 0;
value1 = LPC_ADC->DR[selectedChannel]; //this line doesn't seem to transfer the result correctly all the time
value2 = value1 >> 6;
value3 = value2 & 0x3FF;

selectedChannel++;
if(selectedChannel > 3){
selectedChannel = 0;
}

LPC_ADC->CR &= ~0x00FF;
LPC_ADC->CR |= (1<<selectedChannel);
}
0 Kudos
1 Reply

148 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by CodeRedSupport on Wed Mar 09 03:11:48 MST 2011
What level of optimisation is your code built at?

I suspect that this is a "debug illusion" issue caused by your code being built, say, at -Os (the default for a release build). Effectively at the point your breakpoint is set, your locals are "no longer required" within your code, and the debug table entries are no longer valid, and you are effectively seeing garbage in the variables view.

For more information on the effects of optimisation on debugging see the FAQ at:

http://support.code-red-tech.com/CodeRedWiki/CompilerOptimization

To workaround this, you could either change the optimisation level on just the file containing ADC_IRQHandler(), as per:
[FONT=monospace]
[/FONT]http://support.code-red-tech.com/CodeRedWiki/PerFileProperties

or you could perhaps make value1/2/3 globals temporarily whilst you are debugging the code.

Regards,
CodeRedSupport
0 Kudos