Adc

Source code can be downloaded from source:/Examples/Adc

Description
This demo shows using of ADC module in ADC extension board.

From the main.c source file:

Init ADC0: 200 kHz sampling rate, 10 bits:

ADC_Init(LPC_ADC0, 200000, 10);

Getting ADC result on channel 3 using polling:

// Enable channel 3
ADC_ChannelCmd(LPC_ADC0, ADC_CHANNEL_3, ENABLE);
// Start single conversion
ADC_StartCmd(LPC_ADC0, ADC_START_NOW);
// Wait for DONE bit set
while (ADC_ChannelGetStatus(LPC_ADC0, ADC_CHANNEL_3, ADC_DATA_DONE) != SET);
// Read result
adcResults[3] = ADC_ChannelGetData(LPC_ADC0, ADC_CHANNEL_3);
// Disable channel
ADC_ChannelCmd(LPC_ADC0, ADC_CHANNEL_3, DISABLE);

Getting ADC result on channel 4 using interrupt:

// Enable channel 4
ADC_ChannelCmd(LPC_ADC0, ADC_CHANNEL_4, ENABLE);
// Enable interrupt for channel 1
ADC_IntConfig(LPC_ADC0, ADC_ADINTEN4, ENABLE);
// Start single conversion
ADC_StartCmd(LPC_ADC0, ADC_START_NOW);
// Wait for result to be read in interrupt
while (adcResults[4] == (uint16_t)~0);
// Disable channel
ADC_ChannelCmd(LPC_ADC0, ADC_CHANNEL_4, DISABLE);

Get ADC result on channels 0 and 1 using burst:

// Enable channel 0
ADC_ChannelCmd(LPC_ADC0, ADC_CHANNEL_0, ENABLE);
// Enable channel 1
ADC_ChannelCmd(LPC_ADC0, ADC_CHANNEL_1, ENABLE);
// Enable burst
ADC_StartCmd(LPC_ADC0, ADC_START_CONTINUOUS);   // start must be = 0 when enabling burst
ADC_BurstCmd(LPC_ADC0, ENABLE);

Data read can be done using ADC_ChannelGetStatus() and ADC_ChannelGetData(), but this functions read data register for 2 times:

while (1)
{
   uint32_t dr;

   // Data read can be done using ADC_ChannelGetStatus() and ADC_ChannelGetData(),
   // but this functions reads data register 2 times
   // Read channel 0 result
   dr = LPC_ADC0->DR[0];
   if (dr & ADC_DR_DONE_FLAG)
   {
      adcResults[0] = ADC_DR_RESULT(dr);
   }
   // Read channel 1 result
   dr = LPC_ADC0->DR[1];
   if (dr & ADC_DR_DONE_FLAG)
   {
      adcResults[1] = ADC_DR_RESULT(dr);
   }
}

 

Anglais