Need some help to read data from ADC,

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

Need some help to read data from ADC,

744 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by Sukruth on Tue Sep 03 01:43:50 MST 2013
Hi
I’m trying to read a variable input(Voltage 0-3V) from a potentiometer and
Switch LEDs based on the input, the code I’ve written is ,
Could you kindly check if the code is good,



#include "lpc17xx_pinsel.h"
#include "lpc_types.h"
#include "lpc17xx_gpio.h"
#include "lpc17xx_libcfg_default.h"
#include "lpc17xx.h"
#include "lpc_types.h"
#include "lpc17xx_adc.h"


#define pclk
uint16_t  ADCDone;
int i,j;


int main(void)


{
   
   
    PINSEL_CFG_Type PinCfg;
    PinCfg.Funcnum=1;
    PinCfg.OpenDrain=0;
    PinCfg.Pinmode=0;
    PinCfg.Pinnum=23;
    PinCfg.Portnum=0;
    PINSEL_ConfigPin(&PinCfg);
   
   
    LPC_SC->PCONP |=(1<<12);
   
   
    //pclk = SystemCoreClock/4;
   
   
    LPC_SC->PCONP |= (1<<15); /* power up GPIO */
    LPC_SC->PCONP |= (1<<12); /*Power up ADC*/
   
   
    LPC_PINCON->PINSEL1 |= ((1<<16));// P0.24 = ADC0.1
    LPC_PINCON->PINSEL1 |= ((1<<17)|(1<<19)); /*Neither pull-up/pull-down resistor*/
    LPC_SC-> PCLKSEL0 |=(1<<24); /*set the clock for adc peripheral (pclk=cclk)*/
   
   
    LPC_PINCON->PINSEL1 |= ((1<<16)); // SELECT AD0.1
   
   
    LPC_GPIO1->FIOSET = 0x00800000;
   
   
   
   
    ADC_Init(LPC_ADC, 10000);
    ADC_IntConfig(LPC_ADC,ADC_ADINTEN1,ENABLE);
    ADC_ChannelCmd(LPC_ADC,ADC_CHANNEL_1,ENABLE);
    ADC_BurstCmd(LPC_ADC, ENABLE);
   
   
    //NVIC_SetPriority(ADC_IRQn, 5);
   
   
    ADC_StartCmd(LPC_ADC,ADC_START_CONTINUOUS);
//          NVIC_EnableIRQ(ADC_IRQn);
   
   
    while(1)
    {
       
       
       
       
        ADCDone = ADC_ChannelGetData(LPC_ADC,ADC_CHANNEL_1);
        if (LPC_ADC->ADDR0 & (1 << 31 ) == 0)
        {
            //ADCDone = (LPC_ADC->ADDR0>>4) & (0xfff);
           
           
            LPC_GPIO1->FIODIR = 0x080000;       /* P2.xx defined as Outputs */
            LPC_GPIO1->FIOCLR = 0x080000;       /* turn off all the LEDs */
           
           
            LPC_GPIO1->FIODIR = 0x0100000;      /* P2.xx defined as Outputs */
            LPC_GPIO1->FIOCLR = 0x0100000;      /* turn off all the LEDs */
           
           
            LPC_GPIO1->FIODIR = 0x0200000;      /* P2.xx defined as Outputs */
            LPC_GPIO1->FIOCLR = 0x0200000;      /* turn off all the LEDs */
           
           
            LPC_GPIO1->FIODIR = 0x0400000;      /* P2.xx defined as Outputs */
            LPC_GPIO1->FIOCLR = 0x0400000;      /* turn off all the LEDs */
           
           
            LPC_GPIO1->FIODIR = 0x0800000;      /* P2.xx defined as Outputs */
            LPC_GPIO1->FIOCLR = 0x0800000;      /* turn off all the LEDs */
           
           
           
           
            if (ADCDone >=0x2800 )//FA0 0x4000
            {
                LPC_GPIO1->FIOSET = 0x0080000;//19
                LPC_GPIO1->FIOCLR = 0x00100000;     /* turn off all the LEDs */
                LPC_GPIO1->FIOCLR = 0x00200000;     /* turn off all the LEDs */
                LPC_GPIO1->FIOCLR = 0x00400000;     /* turn off all the LEDs */
                LPC_GPIO1->FIOCLR = 0x00800000;     /* turn off all the LEDs */
            }
            else if (ADCDone >= 0x2600)//BB8   0x3000
            {
                LPC_GPIO1->FIOSET = 0x00100000;//20
                LPC_GPIO1->FIOCLR = 0x0080000;      /* turn off all the LEDs */
                LPC_GPIO1->FIOCLR = 0x00200000;     /* turn off all the LEDs */
                LPC_GPIO1->FIOCLR = 0x00400000;     /* turn off all the LEDs */
                LPC_GPIO1->FIOCLR = 0x00800000;     /* turn off all the LEDs */
               
               
            }
            else if (ADCDone >= 0x2300)//7D0
            {
                LPC_GPIO1->FIOSET = 0x00200000;//21
                LPC_GPIO1->FIOCLR = 0x00100000;     /* turn off all the LEDs */
                LPC_GPIO1->FIOCLR = 0x0080000;      /* turn off all the LEDs */
                LPC_GPIO1->FIOCLR = 0x00400000;     /* turn off all the LEDs */
                LPC_GPIO1->FIOCLR = 0x00800000;     /* turn off all the LEDs */
            }
            else if (ADCDone >= 0x2100)//3E8
            {
                LPC_GPIO1->FIOSET = 0x00400000;//22
                LPC_GPIO1->FIOCLR = 0x00100000;     /* turn off all the LEDs */
                LPC_GPIO1->FIOCLR = 0x00200000;     /* turn off all the LEDs */
                LPC_GPIO1->FIOCLR = 0x0080000;      /* turn off all the LEDs */
                LPC_GPIO1->FIOCLR = 0x00800000;     /* turn off all the LEDs */
            }
            else
            {
                /*LPC_GPIO1->FIOSET = 0x0080000;//19
                 * LPC_GPIO1->FIOSET = 0x00100000;//20
                 * LPC_GPIO1->FIOSET = 0x00200000;//21
                 * LPC_GPIO1->FIOSET = 0x00400000;//22*/
              
            }
        }
       
       
       
       
    }
}

Thanks,
Labels (1)
0 Kudos
6 Replies

711 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by Sukruth on Fri Sep 06 02:16:27 MST 2013
Thanks Paul,

Will try out your suggestions and let you know,


Regards,
Sukruth
0 Kudos

711 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by NXP_Paul on Thu Sep 05 06:20:59 MST 2013
I am making the assumption that you are using the sample code provided on the NXP website:

[color=#3c0]/*********************************************************************/
* @brief Get ADC result
* @param[in]ADCx pointer to LPC_ADC_TypeDef, should be: LPC_ADC
* @param[in]channel: channel number, should be 0...7
* @return Data conversion
**********************************************************************/[/color]
uint16_t ADC_ChannelGetData(LPC_ADC_TypeDef *ADCx, uint8_t channel)
{
uint32_t adc_value;

CHECK_PARAM(PARAM_ADCx(ADCx));
CHECK_PARAM(PARAM_ADC_CHANNEL_SELECTION(channel));

adc_value = *(uint32_t *) ((&ADCx->ADDR0) + channel);
return ADC_DR_RESULT(adc_value);
}

The line in the function:
adc_value = *(uint32_t *) ((&ADCx->ADDR0) + channel);
returns the value of the selected ADDR register (for example, ADDR0, ADDR1, ADDR2, etc.)

The macro definition is then applied to the adc_value:
[color=#3c0]#define ADC_DR_RESULT(n)(((n>>4)&0xFFF))[/color]
which returns the 12-bit binary value for the selected ADC channel as a uint16_t

0 Kudos

711 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by Sukruth on Wed Sep 04 23:15:40 MST 2013
Hi,

wht is the return value of this function? Hex (uint16_t 0 to 65,536 ) or Decimal

ADCDone = ADC_ChannelGetData(LPC_ADC,ADC_CHANNEL_1);


Thanks

Sukruth
0 Kudos

711 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by NXP_Paul on Wed Sep 04 05:58:51 MST 2013
Sukurth
Please take a look at the LPC17xx example code that can be downloaded from:
http://www.lpcware.com/node/11538/129
This code includes examples for the ADC.
If you have a specific problem, please identify what issue you are experiencing.

-Paul
0 Kudos

711 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by Sukruth on Tue Sep 03 21:31:05 MST 2013
Thanks for the help
I have a 2 simple questions regarding ADC initialization, will the following lines allow me to read and measure the input voltage???
    ADC_Init(LPC_ADC, 10000);
    ADC_IntConfig(LPC_ADC,ADC_ADINTEN1,ENABLE);
    ADC_ChannelCmd(LPC_ADC,ADC_CHANNEL_1,ENABLE);
    ADC_BurstCmd(LPC_ADC, ENABLE);
   
    ADC_StartCmd(LPC_ADC,ADC_START_CONTINUOUS);
       for(i=0;i<1500000;i++)
       
       
       
        ADCDone = ADC_ChannelGetData(LPC_ADC,ADC_CHANNEL_1);

now suppose the ADC is initialised, and say I will be reading a Voltage value between 0-3.3V and I have to put switch cases based on this value, wht will be the ADCDone units will it be in hex (uint16_t 0 to 65,536 ) or will it be in decimal values ???

0 Kudos

711 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by NXP_Paul on Tue Sep 03 06:20:50 MST 2013
It would be useful to know the type of problem you are experiencing so it would be easier to review the code.

Here are some things you may want to look at.

In this initialization code:
PINSEL_CFG_Type PinCfg;
PinCfg.Funcnum=1;
PinCfg.OpenDrain=0;
PinCfg.Pinmode=0;
PinCfg.Pinnum=23;
PinCfg.Portnum=0;
PINSEL_ConfigPin(&PinCfg);
If you want P0.23 to be an ADC, then you can set PinCfg.Pinmode to be 2 (neither pull-up or pull-down resistors)

You can remove the following line since this function is already performed earlier in the code:
LPC_SC->PCONP |= (1<<12); /*Power up ADC*/

If you would like to remove the pull-up/pull-down resistors in this line of code:
LPC_PINCON->PINSEL1 |= ((1<<17)|(1<<19)); /*Neither pull-up/pull-down resistor*/

Then the line should read:
LPC_PINCON->PINMODE1 |= (1<<17);         /*Neither pull-up/pull-down resistor for P0.24*/

This line is a duplication and can be removed
LPC_PINCON->PINSEL1 |= ((1<<16)); // SELECT AD0.1
0 Kudos