Analog/Digital Conversion Code for NXP LPC21XX (LPC2138)

Does anyone have code for A/D conversion for the LPC2138?

thanks

I just need code to access the A/D converter on the chip.

Here’s some code of mine extracted from a working application:

  // initialise ADC

  PINSEL0 = PINSEL0 | 0x00003C00; // P0.5, P0.6 setup for AD0.7, AD1.0 inputs
                // bits  11:10  13:12 
                //         11    11
  PINSEL1 = 0x15000000; // P0.28, P0.29, P0.30 set for AD0.1, AD0.2, AD0.3 inputs
                  // bits  25:24  27:26  30:29
                  //         01    01      01
  AD0CR = 0x00200604; // enable ADC, 11 clocks/10 bits, ADC clock = 4.286 MHz
  AD1CR = 0x00208001;



//-------------------------- getAnalogueInput_AD0 function ---------------//

unsigned short int getAnalogueInput_AD0(unsigned char channel)
{
  unsigned short int val;

  //start conversion (for selected channel)
  AD0CR = (AD0CR & 0xFFFFFF00) | (1 << channel) | (1 << 24);

  //wait until done
  while((AD0GDR & 0x80000000) == 0)
    ;

  // get ADC data
  val = ((AD0GDR & 0x0000FFC0)) >> 6;       
  return(val);
}

//----------------------- getAnalogueInput_AD1 function ------------------//
unsigned short int getAnalogueInput_AD1(unsigned char channel)
{
  unsigned short int val;

  //start conversion now (for selected channel)
  AD1CR = (AD1CR & 0xFFFFFF00) | (1 << channel) | (1 << 24);
  
  //wait til done
  while ((AD1GDR & 0x80000000) == 0)
    ;

  // get ADC data
  val = ((AD1GDR & 0x0000FFC0)) >> 6; 
  return(val);
}

You’ll need to fill in the rest yourself.

Leon