lpcware

ADC Register Reading Problem

Discussion created by lpcware Employee on Jun 15, 2016
Latest reply on Jun 15, 2016 by lpcware
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);
}

Outcomes