10 years, 9 months ago.

Need some help with ADC potentiometer inputs

Hi, I need to read an input from a potentiometer (0-3.3V) and need to switch LEDs based on the range, I've written a code , could any of you kindly have a look and see if its good,

  1. include "lpc17xx_pinsel.h"
  2. include "lpc_types.h"
  3. include "lpc17xx_gpio.h"
  4. include "lpc17xx_libcfg_default.h"
  5. include "lpc17xx.h"
  6. include "lpc_types.h"
  7. include "lpc17xx_adc.h"
  1. 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*/ LPC_GPIO1->FIOSET = 0x0080000; for(j = 10000; j > 0; j) for(i = 500; i > 0; i);

LPC_GPIO1->FIOCLR = 0x01F80000; for(j = 1000; j > 0; j) for(i = 500; i > 0; i); LPC_GPIO1->FIOSET = 0x00100000; for(j = 10000; j > 0; j) for(i = 500; i > 0; i);

LPC_GPIO1->FIOCLR = 0x01F80000; for(j = 1000; j > 0; j) for(i = 500; i > 0; i); LPC_GPIO1->FIOSET = 0x00200000; for(j = 10000; j > 0; j) for(i = 500; i > 0; i);

LPC_GPIO1->FIOCLR = 0x01F80000; for(j = 1000; j > 0; j) for(i = 500; i > 0; i); LPC_GPIO1->FIOSET = 0x00400000; for(j = 10000; j > 0; j) for(i = 500; i > 0; i);

LPC_GPIO1->FIOCLR = 0x01F80000; for(j = 1000; j > 0; j) for(i = 500; i > 0; i); LPC_GPIO1->FIOSET = 0x00800000; for(j = 10000; j > 0; j) for(i = 500; i > 0; i);

LPC_GPIO1->FIOCLR = 0x01F80000; for(j = 1000; j > 0; j) for(i = 500; i > 0; i); LPC_GPIO1->FIOSET = 0x01000000; for(j = 10000; j > 0; j) for(i = 500; i > 0; i);

LPC_GPIO1->FIOCLR = 0x01F80000; for(j = 1000; j > 0; j) for(i = 500; i > 0; i); } }

} }

Thanks

1 Answer

10 years, 9 months ago.

First of all, put <<code>> and <</code>> around your code for readability.

Second, why aren't you simply using mbed function? Looking at your includes I don't even think you are using mbed whatsoever. Also since no microcontroller is attached to your account. It makes alot more sense to simply use mbed functions (especially when you come to an mbed forum to ask help). (At platforms on top you can add a microcontroller to compile for, besides that look around on the site to see what it allows you to do).

Finally, does it work? We can't do much if you put down a bunch of code and asks if it is correct. You should check if it works. Does it compile fine? If it doesn't work, what doesn't work. And if you cannot figure out the problem yourself you can ask for help with relevant details.

Hi, Sorry I'm new to the forum, I know the code is not structured well, I can mail it to you if you want, Could you explain what mbed functions are? I am presently using a LPC 1768 microcontroller Ya the code is compling and I feel the ADC get data is not working

Thanks

posted by Sukruth Srikanth 03 Sep 2013

That your code may not be structured well can be, but if you put code taggs around it we get at least proper layout.

Look around on this site. It is specifically for mbed, not general lpc1768. You can start with looking in the handbook, and getting started stuff. For example ADC is done using http://mbed.org/handbook/AnalogIn

posted by Erik - 03 Sep 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 ??? .

posted by Sukruth Srikanth 04 Sep 2013