lpcware

ADC in LPC 824

Discussion created by lpcware Employee on Jun 15, 2016
Latest reply on Jun 15, 2016 by lpcware
Content originally posted in LPCWare by krishna.mohan on Mon Oct 19 00:16:17 MST 2015
Hi,

I've downloaded the sample project from https://www.lpcware.com/content/nxpfile/lpcopen-software-development-platform-lpc8xx-packages.
and used the adc initialization code as shown below. ADC pins are 3, 9,10.

        /* Setup ADC for 12-bit mode and normal power */
Chip_ADC_Init(LPC_ADC, 0);

/* Need to do a calibration after initialization and trim */
Chip_ADC_StartCalibration(LPC_ADC);
while (!(Chip_ADC_IsCalibrationDone(LPC_ADC))) {}

/* Setup for maximum ADC clock rate using sycnchronous clocking */
Chip_ADC_SetClockRate(LPC_ADC, ADC_MAX_SAMPLE_RATE);

/* Optionally, you can setup the ADC to use asycnchronous clocking mode.
   To enable this, mode use 'LPC_ADC->CTRL |= ADC_CR_ASYNMODE;'.
   In asycnchronous clocking mode mode, the following functions are
   used to set and determine ADC rates:
   Chip_Clock_SetADCASYNCSource();
   Chip_Clock_SetADCASYNCClockDiv();
   Chip_Clock_GetADCASYNCRate();
   clkRate = Chip_Clock_GetADCASYNCRate() / Chip_Clock_GetADCASYNCClockDiv; */

/* Setup sequencer A for ADC channel 0, EOS interrupt */
//#if defined(BOARD_NXP_LPCXPRESSO_824)
/* Setup a sequencer to do the following:
   Perform ADC conversion of ADC channels 0 only */
Chip_ADC_SetupSequencer(LPC_ADC, ADC_SEQA_IDX, (ADC_SEQ_CTRL_CHANSEL(3) | ADC_SEQ_CTRL_CHANSEL(9) |
ADC_SEQ_CTRL_CHANSEL(10) | ADC_SEQ_CTRL_MODE_EOS));


/* Enable the clock to the Switch Matrix */
Chip_Clock_EnablePeriphClock(SYSCTL_CLOCK_SWM);

/* Configure the SWM for P0-23 as the input for the ADC3 */
Chip_SWM_EnableFixedPin(SWM_FIXED_ADC3);
/* Configure the SWM for P0-17 as the input for the ADC9 */
Chip_SWM_EnableFixedPin(SWM_FIXED_ADC9);
/* Configure the SWM for P0-13 as the input for the ADC10 */
Chip_SWM_EnableFixedPin(SWM_FIXED_ADC10);

/* Disable the clock to the Switch Matrix to save power */
//Chip_Clock_DisablePeriphClock(SYSCTL_CLOCK_SWM);
//#else
//#warning "No ADC setup for this example"
//#endif

/* Setup threshold 0 low and high values to about 25% and 75% of max */
Chip_ADC_SetThrLowValue(LPC_ADC, 0, ((1 * 0xFFF) / 4));
Chip_ADC_SetThrHighValue(LPC_ADC, 0, ((3 * 0xFFF) / 4));

/* Clear all pending interrupts */
Chip_ADC_ClearFlags(LPC_ADC, Chip_ADC_GetFlags(LPC_ADC));

/* Enable ADC overrun and sequence A completion interrupts */
Chip_ADC_EnableInt(LPC_ADC, (ADC_INTEN_SEQA_ENABLE | ADC_INTEN_OVRRUN_ENABLE));

/* Use threshold 0 for ADC channel and enable threshold interrupt mode for
   channel as crossing */
Chip_ADC_SelectTH0Channels(LPC_ADC, ADC_THRSEL_CHAN_SEL_THR1(BOARD_ADC_CH));
Chip_ADC_SetThresholdInt(LPC_ADC, BOARD_ADC_CH, ADC_INTEN_THCMP_CROSSING);

/* Enable ADC NVIC interrupt */
NVIC_EnableIRQ(ADC_SEQA_IRQn);

/* Enable sequencer */
Chip_ADC_EnableSequencer(LPC_ADC, ADC_SEQA_IDX);


And for reading the ADC result

                                rawSample = Chip_ADC_GetDataReg(LPC_ADC, 3);
adc_value = ADC_DR_RESULT(rawSample);
/* Show some ADC data */
if (rawSample & (ADC_DR_OVERRUN | ADC_SEQ_GDAT_DATAVALID)) {
putLineUART("Chan: 3 \r\n");
if(rawSample & ADC_DR_OVERRUN)
putLineUART("Overrun: True \r\n");
else
putLineUART("Overrun: False \r\n");
if(rawSample & ADC_SEQ_GDAT_DATAVALID)
putLineUART("Data Valid: True\r\n");
else
putLineUART("Data Valid: False\r\n");
}
rawSample = Chip_ADC_GetDataReg(LPC_ADC, 9);
adc_value = ADC_DR_RESULT(rawSample);
/* Show some ADC data */
if (rawSample & (ADC_DR_OVERRUN | ADC_SEQ_GDAT_DATAVALID)) {
putLineUART("Chan: 9 \r\n");
if(rawSample & ADC_DR_OVERRUN)
putLineUART("Overrun: True \r\n");
else
putLineUART("Overrun: False \r\n");
if(rawSample & ADC_SEQ_GDAT_DATAVALID)
putLineUART("Data Valid: True\r\n");
else
putLineUART("Data Valid: False\r\n");
}
rawSample = Chip_ADC_GetDataReg(LPC_ADC, 10);
adc_value = ADC_DR_RESULT(rawSample);
/* Show some ADC data */
if (rawSample & (ADC_DR_OVERRUN | ADC_SEQ_GDAT_DATAVALID)) {
putLineUART("Chan: 10 \r\n");
if(rawSample & ADC_DR_OVERRUN)
putLineUART("Overrun: True \r\n");
else
putLineUART("Overrun: False \r\n");
if(rawSample & ADC_SEQ_GDAT_DATAVALID)
putLineUART("Data Valid: True\r\n");
else
putLineUART("Data Valid: False\r\n");
}

All the adc results are showing the same result. Please let me know where is the mistake. I'm not figure it out.

Regards,
Krishnamohan.

Outcomes