ADC Read giving me 0!

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

ADC Read giving me 0!

1,694 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by kamhagh on Fri Sep 26 06:52:20 MST 2014
this is my library!
:
 #include "LPC17xx.H"

void adc_init()
{
  LPC_SC->PCONP |= (1 << 12); //set PCADC for power on ADC ;
  LPC_ADC->ADCR |= (1 << 21);  // start adc
  LPC_PINCON->PINSEL1 &= ~0x003FC000; /* P0.23~26, A0.0~3, function 01 */
  LPC_PINCON->PINSEL1 |= 0x00154000;
  LPC_PINCON->PINMODE1 &= ~0x003FC000;
  LPC_PINCON->PINMODE1 |= 0x002A8000;
}

int ADCRead(char channelNum) {
int data;
  LPC_ADC->ADCR &= 0xFFFFFF00;  // clear channels
  LPC_ADC->ADCR |= (1 << 24) | (1 << channelNum);  // set chanel and start convert
  while (!(LPC_ADC->ADGDR & (1<<31)));    //wait to done
  data = LPC_ADC->ADDR7;
  LPC_ADC->ADCR &= 0xF8FFFFFF;  /* stop ADC now */ 

  data = ( data >> 4 ) & 0xFFF;
  return ( data );  /* return A/D conversion value */
}


the code is not good but its just a testing library !

here's my main.c

/*-------------------------------------------------------------------------------
 www.ECA.ir
   LPC1768 Dev Board Rev.A
--------------------------------------------------------------------------------*/
#include <LPC17xx.h>
#include <stdio.h>
#include "uart.h"
#include "adc.h"


void Delay (uint32_t Time)
{
    uint32_t i;
    
    i = 0;
    while (Time--) {
        for (i = 0; i < 5000; i++);
    }
}

int main(void)
{
volatile int i=0;
unsigned char lcd_buf[32];

SystemInit();
adc_init();
UART0_Init();

    while (1) 
  {
i++;
sprintf(lcd_buf,"AnalogRead(%d)(A0): %d\r\n\n",i,ADCRead(0));
UART0_SendString(lcd_buf);
    Delay(2000);
    }
}


i get this:

AnalogRead(numer of AR)(A0) : 0, its always 0

why i keep getting 0?
Labels (1)
0 Kudos
15 Replies

1,407 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by Pacman on Fri Oct 03 04:04:30 MST 2014
In general, all ARM microcontrollers behave more or less the same, unless the manufacturer decides to implement things differently, of course.
Things you should be aware of is that you need to turn on clocking & power to a peripheral, before you use it.
Many peripherals are turned off when your ARM microcontroller starts up.
This is in order to save power, so battery-driven devices won't just purge the batteries in no time.
If you're using a ready-built library, the init functions takes care of this for you.
-Yet another good reason for using the libraries.

If you're going to write your own routines to do things in a low-level way, I recommend copying the library routine and then modify it.
That is because it's already tested by many developers, and if they find a bug, they usually report it, so it's fixed as quickly as possible.
If you write your own from scratch, then you might not see that you've missed something; and the device(s) might behave strange in some situations.
0 Kudos

1,407 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by Pacman on Fri Oct 03 03:55:56 MST 2014

Quote: kamhagh
while(!(dr >> 1))



That's exactly why it's a good idea to use the libraries.

I made a typo, probably wasn't very awake.

The following might be a little better... But I'm pretty sleepy right now too, so expect that it's incorrect.
    data = LPC_ADC->ADGDR;
    while(!(data >> 31)) /* test the DONE bit, which is bit 31 */
    {
        data = LPC_ADC->ADGDR;
    }
    return(0xfff & (data >> 4));

0 Kudos

1,407 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by kamhagh on Sun Sep 28 09:46:01 MST 2014
thanks a lot for helping me till here guys but turns out it was that the aref jumper was off, i thought it uses the 3.3v for aref if i leave it there like avr ! i thought thats a general thing :P

0 Kudos

1,407 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by kamhagh on Sun Sep 28 06:41:43 MST 2014
also, i included the libraries in the code, but it disappeard !
0 Kudos

1,407 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by kamhagh on Sun Sep 28 06:36:07 MST 2014
lol xD

its just 2 fucntion library(can't call it library), i tried coding myself and it didn't work ! its not much i would do the same , it just does what it says in  manual step by step ! its an example for my Board(on the board forums, he was mod, my boards name is eca, and i found it on eca.ir

their forum is still down :S damn it, it gets down every 1 hour ! and stays ok for 4 seconds after
0 Kudos

1,407 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by LabRat on Sun Sep 28 05:41:13 MST 2014
Then it could be helpul to use a library supplied by your chip manufacturer, instead a  'written by some guy at some forum' code...

See: http://www.lpcware.com/lpcopen
0 Kudos

1,407 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by kamhagh on Sun Sep 28 05:29:04 MST 2014
i use keil! and include "LPC17xx.h" sorry if this sounds silly, im a total noob here !!!!
0 Kudos

1,407 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by LabRat on Sun Sep 28 05:09:02 MST 2014

Quote: kamhagh
and i don't know what library you'r talking about :D



LPCOpen  :quest:
0 Kudos

1,407 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by kamhagh on Sun Sep 28 05:04:29 MST 2014
hmm i use keil

and i don't know what library you'r talking about i include lpc17xx and the libraries are written by some guy at some forum (only so i cansee how it works !! (example )

thanks i will try that, but i thought i first have to wait for the conversation to finish 0_0)

wait,

 while(!(dr >> 1))


where did dr come from !?!!!

compile gives me error undefined !!

also dr never changes so if itst rue it will be endless!!!
0 Kudos

1,407 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by Pacman on Sat Sep 27 19:31:12 MST 2014
I think it might be because of the following...
  while (!(LPC_ADC->ADGDR & (1<<31)));    //wait to done
  data = LPC_ADC->ADDR7;


You read ADGDR's done bit, and then you assume that the data that is ready is in ADDR7 and is also 'ready'.

Since ADGDR contains the data, why not just read the value from there ?

data = LPC_ADC->ADGDR;
while(!(dr >> 1))
{
    data = LPC_ADC->ADGDR;
}
return(0xfff &(data >> 4));


Anyway, I can see you're using the library from sw.lpcware.com, so I would recommend you to use the #defines from the header files.
1: It makes the code more readable.
5: It makes it less likely that you have bugs in the code.
0 Kudos

1,407 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by LabRat on Sat Sep 27 14:42:03 MST 2014

Quote: kamhagh
nobody?!:(



Nobody?! :(  knows which toolchain and libraries you are using  :quest:

So the only thing I can say is: use your debugger  :)

If you are expecting help it could be useful to post the project (and the board library, if it's a LPCOpen project)...

Or just use a CMSIS or LPCOpen sample  :bigsmile:
0 Kudos

1,407 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by kamhagh on Sat Sep 27 05:14:26 MST 2014
nobody?!:(
0 Kudos

1,407 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by kamhagh on Fri Sep 26 10:04:19 MST 2014
double post :| stupid net
0 Kudos

1,407 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by kamhagh on Fri Sep 26 09:49:59 MST 2014
sorry triple post ! stupid net :S
0 Kudos

1,407 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by kamhagh on Fri Sep 26 07:16:28 MST 2014
/*-------------------------------------------------------------------------------
 www.ECA.ir
   LPC1768 Dev Board Rev.A
--------------------------------------------------------------------------------*/
#include <lpc17xx.h>

main(){
int a;
SystemInit();

LPC_GPIO2->FIODIR = 0xfff;
LPC_PINCON->PINSEL3 |=  (3UL<<30);             /* P1.31 is AD0.5 */
LPC_SC->PCONP       |=  (1<<12);               /* Enable power to ADC block */
LPC_ADC->ADCR        =  (1<< 5) |              /* select AD0.5 pin */
                        (4<< 8) |              /* ADC clock is 25MHz/5 */
                        (1<<21);               /* enable ADC */

while(1){
LPC_ADC->ADCR &= ~(7<<24);                     /* stop conversion  */
LPC_ADC->ADCR |=  (1<<24);                     /* start conversion */
while (!(LPC_ADC->ADGDR & (1UL<<31)));         /* Wait for Conversion end */
a = (LPC_ADC->ADGDR>>4 & 0xfff);
LPC_GPIO2->FIOPIN = a>>4;
} 
}


all leds are on at levitate/ground and all or off with any voltage ! even if i touch them!
0 Kudos