ADC conversion help

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

ADC conversion help

1,062 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by cthvx8@hotmail.com on Tue Mar 10 06:25:24 MST 2015
I need some help figuring out why my ADC is not working properly.  I copied the code for the conversion from the periph_adc example.  My code is supposed to read from PIO0_11, divide the value by 50, and then output the value to 8 pins which are connected to LEDs and are functioning as my display using the little function I named "bytewriter".  I have verified that my 8 bit "display" is working properly.  I am using the LPC1347 board with usb.

I am assuming that the board is providing the VREFN to VREFP/(V0 to Vdd) because the only power/ground pins on the board already have the correct voltage.  Is this an incorrect assumption?

The current behavior of my code is that it alternately displays values of 0 and 81 to my output pins.  81 is 4095/50 so it seems to sometimes read high and sometimes read low.  Pin PIO0_11 is currently connected to 0.3V which is being supplied by a voltage regulator and resistor voltage divider.  I checked to make sure that this voltage was holding steady during operation.


#if defined (__USE_LPCOPEN)
#if defined(NO_BOARD_LIB)
#include "chip.h"
#else
#include "board.h"
#endif
#endif

#include <cr_section_macros.h>

// TODO: insert other include files here
// TODO: insert other definitions and declarations here

#ifdef __cplusplus
extern "C" {
#endif

#define _LPC_ADC_ID LPC_ADC
volatile uint32_t msTicks=0;
static uint32_t rawTemp=0;
static ADC_CLOCK_SETUP_T ADCSetup;

// Systick Handler which is called each ms
void SysTick_Handler(void)
{
    msTicks++;
}

// blocks for dlyTicks ms...
static void Delay(uint32_t dlyTicks)
{
    uint32_t curTicks;

    curTicks = msTicks;
    while ((msTicks - curTicks) < dlyTicks);
}
}

static void bytewriter(uint32_t bytevalue)
{
Chip_GPIO_SetPortMask(LPC_GPIO_PORT, 0, 0xFFFFFFF0);
Chip_GPIO_SetMaskedPortValue(LPC_GPIO_PORT, 0, bytevalue);
Chip_GPIO_SetPortMask(LPC_GPIO_PORT, 1, 0xFFFE1FFF);
bytevalue <<=9;
Chip_GPIO_SetMaskedPortValue(LPC_GPIO_PORT, 1, bytevalue);
}

//------------------------------------------------------------------------------------
int main(void) {

#if defined (__USE_LPCOPEN)
#if !defined(NO_BOARD_LIB)
    // Read clock settings and update SystemCoreClock variable
    SystemCoreClockUpdate();
    // Set up and initialize all required blocks and
    // functions related to the board hardware
    Board_Init();
    Board_ADC_Init();
    // Set the LED to the state of "On"
    Board_LED_Set(0, true);
#endif
#endif

    // TODO: insert code here

    //Initialize Port GPIO
[/color]Chip_GPIO_Init(LPC_GPIO_PORT);

Chip_ADC_Init(_LPC_ADC_ID, &ADCSetup);

    Chip_IOCON_PinMuxSet(LPC_IOCON, 0, 0, (IOCON_FUNC1));
    Chip_IOCON_PinMuxSet(LPC_IOCON, 0, 1, (IOCON_FUNC0));
    Chip_IOCON_PinMuxSet(LPC_IOCON, 0, 2, (IOCON_FUNC0));
    Chip_IOCON_PinMuxSet(LPC_IOCON, 0, 3, (IOCON_FUNC0));
    Chip_IOCON_PinMuxSet(LPC_IOCON, 1, 13, (IOCON_FUNC0));
    Chip_IOCON_PinMuxSet(LPC_IOCON, 1, 14, (IOCON_FUNC0));
    Chip_IOCON_PinMuxSet(LPC_IOCON, 1, 15, (IOCON_FUNC0));
    Chip_IOCON_PinMuxSet(LPC_IOCON, 1, 16, (IOCON_FUNC0));
    Chip_IOCON_PinMuxSet(LPC_IOCON, 0, 11, (IOCON_FUNC2)|(IOCON_ADMODE_EN));


//Set Port 0 pins 0-3 and Port 1 pins 13-16 to output
    Chip_GPIO_SetPortDIROutput(LPC_GPIO_PORT, 0, 0x0F);
    Chip_GPIO_SetPortDIROutput(LPC_GPIO_PORT, 1, 0x1E000);


SysTick_Config(SystemCoreClock/1000);

while(1){

//Clear port bits
Chip_GPIO_ClearValue(LPC_GPIO_PORT,0,0xF);
Chip_GPIO_ClearValue(LPC_GPIO_PORT,1,0x1E000);

uint16_t dataADC=0;
rawTemp=0;

//Read value from ADC Pin11
dataADC=0;
Chip_ADC_EnableChannel(_LPC_ADC_ID, ADC_CH0, ENABLE);
Chip_ADC_SetStartMode(_LPC_ADC_ID, ADC_START_NOW, ADC_TRIGGERMODE_RISING);
while (Chip_ADC_ReadStatus(_LPC_ADC_ID, ADC_CH0, ADC_DR_DONE_STAT) != SET){}
Chip_ADC_ReadValue(_LPC_ADC_ID, ADC_CH0, &dataADC);

//Convert ADC data to value small enough to display on 8 bits
rawTemp = dataADC/50;

//Set 8 bits to rawTemp value
bytewriter(rawTemp);

Board_LED_Set(0, true);

//Wait 1 second
Delay(1000);

Board_LED_Set(0, false);
//Wait 1 second
Delay(1000);

}

    // Force the counter to be placed into memory
    volatile static int i = 0 ;
    // Enter an infinite loop, just incrementing a counter
    while(1) {
        i++ ;

    }
    return 0 ;
}
Labels (1)
0 Kudos
9 Replies

1,001 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by cthvx8@hotmail.com on Tue Mar 10 14:03:58 MST 2015
Ok, I re-tried my exact same code using a higher voltage at the measurement point (~1.54) and it worked.  I added a resistor to bring the voltage down further (~1) and that worked too.  I will have to do some more experimentation but is there a known issue with measureing small voltages?  I was trying to measure 0.3V before.
0 Kudos

1,001 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by R2D2 on Tue Mar 10 12:00:45 MST 2015

Quote: cthvx8@hotmail.com
Ok, I tried periph_adc.  It is reading 0 sometimes, 4095 other times.  I confirmed the voltage is holding at 0.3 so it should be measuring around 372.



Sample is working here without problems  :O

Are you reading dataADC in App_Polling_Test() ?
0 Kudos

1,001 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by cthvx8@hotmail.com on Tue Mar 10 11:41:17 MST 2015
Ok, I tried periph_adc.  It is reading 0 sometimes, 4095 other times.  I confirmed the voltage is holding at 0.3 so it should be measuring around 372.
0 Kudos

1,001 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by R2D2 on Tue Mar 10 11:12:18 MST 2015

Quote: cthvx8@hotmail.com
No I did not yet.  I will try it.  Should ADC work properly while connected through the usb LPC link?  That is how I am currently powering the board.



Yes, of course  :)

Just add a simple voltage divider (2 x 10kOhm) to your ADC pin and try to measure VDD/2...
0 Kudos

1,001 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by cthvx8@hotmail.com on Tue Mar 10 11:08:31 MST 2015
No I did not yet.  I will try it.  Should ADC work properly while connected through the usb LPC link?  That is how I am currently powering the board.
0 Kudos

1,001 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by R2D2 on Tue Mar 10 11:05:44 MST 2015
That's the LPC1347 LPCXpresso Board  :O

Schematic:

http://www.embeddedartists.com/products/lpcxpresso/lpc1347_xpr.php

LQFP48 has no VREFP / VREFN pins, they are connected to VDD / VSS...

Did you try / debug the original adc example?
0 Kudos

1,001 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by cthvx8@hotmail.com on Tue Mar 10 10:23:27 MST 2015
I have not been able to find a schematic for the board.
0 Kudos

1,001 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by cthvx8@hotmail.com on Tue Mar 10 10:22:08 MST 2015
My board is OM13045.
0 Kudos

1,000 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by R2D2 on Tue Mar 10 07:06:35 MST 2015

Quote: cthvx8@hotmail.com
I am assuming that the board is providing the VREFN to VREFP/(V0 to Vdd)...



:quest:

Which board?
0 Kudos