lpcware

Need some help to read data from ADC,

Discussion created by lpcware Employee on Jun 15, 2016
Latest reply on Jun 15, 2016 by lpcware
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,

Outcomes