lpcware

RE: ADC in LP1769

Discussion created by lpcware Employee on Jun 15, 2016
Latest reply on Jun 15, 2016 by lpcware
Content originally posted in LPCWare by viwi_yezi on Sun Sep 30 01:09:26 MST 2012
hi, i m currently doing a project where i want to make use of ADC in LPC1769 to give me the corresponding value of my analog input signal. i am using AD0.0 function of the pin P0.23. i am trying with the following codes, but it could just give me one reading then just stops after that. what should i do to get continuous conversion from the pin?

void adc_init(void)
{
LPC_SC->PCONP |= (1 << 12);//set adc power

LPC_PINCON->PINSEL1 &= ~(3<<14);
LPC_PINCON->PINSEL1 |=  (1<<14);        //set P0.23 to AD0.0

LPC_ADC->ADCR = ( 1 << 0 ) |         //choose channel 0
( 4 <<  8 ) |           //CLKDIV 4 => 100kHz /(4+1) = 20 kHz
( 1 << 21 ) |          //enable PDN
( 1 << 24 );           // start conversion

LPC_ADC->ADINTEN = 0x0;               //disable channel0 interrupt
//NVIC_EnableIRQ(ADC_IRQn);
}


void ADC_read(void)
{
unsigned int regVal,regOver;
regVal = LPC_ADC->ADSTAT;                //read Status register
if (regVal & 0x0000FF00)                 //check if any channel has OVERRUN error
{
regOver = (regVal & 0x0000FF00) >> 0x08; //indicate which channel is overrun

switch (regOver)
{
case 0x01:regOver = LPC_ADC->ADDR0;
              break;
case 0x02:regOver = LPC_ADC->ADDR1;
break;
case 0x04:regOver = LPC_ADC->ADDR2;
break;
case 0x08:regOver = LPC_ADC->ADDR3;
break;
case 0x10:regOver = LPC_ADC->ADDR4;
              break;
case 0x20:regOver = LPC_ADC->ADDR5;
              break;
case 0x40:regOver = LPC_ADC->ADDR6;
              break;
case 0x80:regOver = LPC_ADC->ADDR7;
              break;
default:
break;
}
}
else                                    //no overrun
{
regVal = (regVal & 0x000000FF);        //check done bit for every channel, if any channel is done
switch (regVal)
{
case 0x01:
regVal = LPC_ADC->ADDR0;    //read channel 0, clear interrupt
             adc0_val = (regVal >> 4) & 0xFFF;//calc 12 bit value
             adc0_ready = 1;                //set adc ready

             break;
case 0x02:regVal = LPC_ADC->ADDR1;
break;
case 0x04:regVal = LPC_ADC->ADDR2;
                  break;
case 0x08:regVal = LPC_ADC->ADDR3;
                break;
case 0x10:regVal = LPC_ADC->ADDR4;
                  break;
case 0x20:regVal = LPC_ADC->ADDR5;
                  break;
case 0x40:regVal = LPC_ADC->ADDR6;
                  break;
case 0x80:regVal = LPC_ADC->ADDR7;
                  break;
default:
break;
}
}
}
int main(void)
{
SysTick_Config(SystemCoreClock / 1000);

//init_GPIO();
adc_init();

//LPC_GPIOINT->IO2IntEnF |=1<<6;//enable GPIO interrupt P2.6
//NVIC_EnableIRQ(EINT3_IRQn);

// Enter an infinite loop, just incrementing a counter
while(1)
{
ADC_read();
if(adc0_ready)                        //adc ready
{
printf("light level is %d\n", adc0_val);
adc0_ready=0;                        //reset adc ready
}
}
return 0 ;
}

this is my first time trying ADC, i am a bit confused after reading the user manual.

1)does different setting of clock affect the conversion except the conversion rate~?

2)each ADC pin(AD0.0 to AD0.7)has 8 channels`? do i understand correctly? if so, do we need to enable all of them and read from all of them if we just have input signal on one ADC pin~? e.g. i configured AD0.0, which is P0.23

3)once i set the START bit in the control register AD0CR to be 001, which is to start conversion, does it automatically do conversion from the figured pin continuously~? or does it need to be set again once it completes one conversion~?

thank you very much and i would really appreciate if anyone could help me clear my doubts~!!

Outcomes