How to convert analog to digital using LPC1769 processor

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

How to convert analog to digital using LPC1769 processor

515 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by kplee on Sun May 13 21:15:24 MST 2012
I am currently doing project using LPC1769 processor.

This project make used of a lots of sensor which runs in AC signals.

I realized that it is necessary to convert the signal into DC in order to the processor to read.

However, I had no idea how to code the processor.

Can anyone enlighten me with the steps to convert analog to digital signal using LPC1769 processor??
0 Kudos
4 Replies

393 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by kplee on Mon May 14 04:43:14 MST 2012
Thank a lot
0 Kudos

393 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by Ex-Zero on Mon May 14 03:06:41 MST 2012

Quote: Superfred

...
 ADCInit(1000000); //1 MHZ clock rate
...



Could be useful (for reading fast AC) to follow UM:


Quote:

The APB clock (PCLK_ADC0) is divided by (this value plus one) to produce the clock for the A/D converter, which should be less than or equal to [COLOR=Red]13 MHz. [/COLOR]Typically, software should program the smallest value in this field that yields a clock of 13 MHz or slightly less, but in certain cases (such as a high-impedance analog source) a slower clock may be desirable.

0 Kudos

393 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by Superfred on Mon May 14 02:50:09 MST 2012
Hello kplee,

here is the code I am using:

Driver:
/*****************************************************************************
** Function name:        ADCInit
**
** Descriptions:        initialize ADC channel
**
** parameters:            ADC clock rate
** Returned value:        None
**
*****************************************************************************/
void ADCInit( uint32_t ADC_Clk )
{
  /* Enable CLOCK into ADC controller */
  LPC_SC->PCONP |= (1 << 12);


  /* all the related pins are set to ADC inputs, AD0.0~7 */
  LPC_PINCON->PINSEL0 &= ~0x000000F0;    /* P0.2~3, A0.6~7, function 10 */
  LPC_PINCON->PINSEL0 |= 0x000000A0;
  LPC_PINCON->PINSEL1 &= ~0x003FC000;    /* P0.23~26, A0.0~3, function 01 */
  LPC_PINCON->PINSEL1 |= 0x00154000;
  LPC_PINCON->PINSEL3 |= 0xF0000000;    /* P1.30~31, A0.4~5, function 11 */
  /* No pull-up no pull-down (function 10) on these ADC pins. */
  LPC_PINCON->PINMODE0 &= ~0x000000F0;
  LPC_PINCON->PINMODE0 |= 0x000000A0;
  LPC_PINCON->PINMODE1 &= ~0x003FC000;
  LPC_PINCON->PINMODE1 |= 0x002A8000;
  LPC_PINCON->PINMODE3 &= ~0xF0000000;
  LPC_PINCON->PINMODE3 |= 0xA0000000;

  /* By default, the PCLKSELx value is zero, thus, the PCLK for
  all the peripherals is 1/4 of the SystemFrequency. */
  /* Bit 24~25 is for ADC */



  LPC_ADC->ADCR = ( 0x01 << 0 ) |  /* SEL=1,select channel 0~7 on ADC0 */
        ( ( 25000000 / ADC_Clk - 1 ) << 8 ) |  /* CLKDIV = Fpclk / ADC_Clk - 1 */
        ( 0 << 16 ) |         /* BURST = 0, no BURST, software controlled */
        ( 0 << 17 ) |          /* CLKS = 0, 11 clocks/10 bits */
        ( 1 << 21 ) |          /* PDN = 1, normal operation */
        ( 0 << 24 ) |          /* START = 0 A/D conversion stops */
        ( 0 << 27 );        /* EDGE = 0 (CAP/MAT singal falling,trigger A/D conversion) */

  return;
}

/*****************************************************************************
** Function name:        ADCRead
**
** Descriptions:        Read ADC channel
**
** parameters:            Channel number
** Returned value:        Value read 
**
*****************************************************************************/
uint32_t ADCRead( uint8_t channelNum )
{
  uint32_t regVal, ADC_Data;

  /* channel number is 0 through 7 */

  LPC_ADC->ADCR &= 0xFFFFFF00;
  LPC_ADC->ADCR |= (1 << 24) | (1 << channelNum);
                /* switch channel,start A/D convert */

  while ( 1 )            /* wait until end of A/D convert */
  {
    regVal = LPC_ADC->ADGDR;
    /* read result of A/D conversion */
    if ( regVal & ADC_DONE )
    {
      break;
    }
  }

  LPC_ADC->ADCR &= 0xF8FFFFFF;    /* stop ADC now */
  if ( regVal & ADC_OVERRUN )    /* save data when it's not overrun, otherwise, return zero */
  {
    return ( 0 );
  }
  ADC_Data = ( regVal >> 4 ) & 0xFFF;
  return ( ADC_Data );    /* return A/D conversion value */

}


Main Function:

int main(void)
{
   ADCInit(1000000); //1 MHZ clock rate
    while(1)
    {
         int x;
         x = ADCRead(0);
    }


}


Fred
0 Kudos

393 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by gbm on Sun May 13 22:28:03 MST 2012
Use ADC peripheral, fully described in the User's Manual.

You don't always need to convert AC to DC. In many cases it's just a matter of smart level shifting using properly connected resistors.
0 Kudos