Forum: ARM programming with GCC/GNU tools LPC2148 ADC Help

Author: alex (Guest)
Posted on:

Rate this post
0 useful
not useful
Hi everyone,

I have been working on this for about a week, looking at examples from 
all over the place and pouring through datasheets, but I cannot get this 
to work(or many ARM Features for that matter)

I have worked with AVRs for a while and I love them, but I wanted to 
move up to ARM.  I know a reasonable amount of C, and no assembler.  I 
don't really want to get this example working as much as I want to learn 
what im not doing right.  I really want to learn how to be able to do 
ARM programs on my own and not rely on forums to help me, so any 
tutorials or books would be greatly appreciated.

So, I am trying to get the ADC working so it will print the 10-bit value 
of ADC0.3 over uart0 at 19200 baud.  Here is my code:
#include "LPC214x.h"

#define ADC_CLK    1000000    /* set to 1Mhz */

#include "serial.h"
#include "rprintf.h"
#include "target.h"

void delay_ms(int x);

int main(void)

  PINSEL0 = 0x10000000;

  AD0CR = (0x01 << 0) |   
  (1 << 3) | //Turn analog 3 on
  (( Fpclk / ADC_CLK - 1 ) << 8) |
  (0 << 16) |     // BURST = 0, no BURST, software controlled
  (0 << 17) |      // CLKS = 000, 11 clocks/10 bits 
  (0 << 18) |    //^
  (0 << 19) |    //^
  (1 << 21) |      // PDN = 1, normal operation 
  (0 << 22) |      // TEST1:0 = 00 
  (0 << 24);      // START = 0 A/D conversion stops

  while(1) {
    AD0CR |= (1 << 24); //Start Conversion
    delay_ms(1); //Give it time to convert(I doubt this is necessary?)
    rprintf("%luH", ((AD0DR3 >> 6) & 0x000003ff) * 0x40);
    AD0CR &= (1 << 24);  //Stop Conversion

void delay_ms(int x)
//Rough, not meant to be accurate
    int a,b;

And when I run the code this is all I get:

Any ideas for what I am doing wrong?


Author: alex (Guest)
Posted on:
Attached files:

Rate this post
0 useful
not useful
After I posted this I thought I should mention that I am using an 
LPC2148, on the Olimex Header Board.  I am compiling with GCC.

I also just noticed you prefer code attached.  Sorry for the large paste 

Author: Andreas Schwarz (andreas) (Admin) Flattr this
Posted on:

Rate this post
0 useful
not useful
Your delay loop is not going to work if you compile with optimization 
enabled (-Ox), because the compiler will detect an empty loop and remove 
it. Use volatile variables for the counter, then it won't get removed.

If your controller hangs, the reason is usually an exception caused by 
an invalid memory access. Do you have a JTAG debugger? If yes, try to 
find out what the controller is doing after you have received the output 
(it is probably hanging at the abort handler), then rerun and use single 
step to locate the statement that's causing it. If you don't have a 
debugger, you can for example comment out lines until you have found the 

Author: mthomas (Guest)
Posted on:

Rate this post
0 useful
not useful
- Strange output might just be caused by missing functionality in 
rprintf. Since rprintf is not shown it's difficult to analyse. Test with 
a known uint32_t value first, see rprintf source and check for "l", test 
with "L", test with cast to uin32_t. Try to use sprintf from the newlib.
- add volatile to counters definition (a,b) in delay-function.

Author: Noel Le Moult (Guest)
Posted on:

Rate this post
0 useful
not useful
You can try to read the Logomatic-v2 code from sparkfun electronic.

It use the LPC2148 ADC, I have tested and it works very well with Uart 

Link to the Logomatic-v2 product info: 
There is a link to get the source code (main firmware)

Author: alex (Guest)
Posted on:
Attached files:

Rate this post
0 useful
not useful
Thank you everyone!

I do not have an ARM JTAG, dongle, but I do have a USB Xilinx FPGA one, 
would that work?

I played around with it some more, commenting out lines until I got it 
working.  I found that PINSEL0 = 0x10000000; was the problem, and by 
removing it it will now actually print the ADC value.

The problem is, it always prints zero.  I know that the ADC is actually 
reading zero because I did a test with if(adcval == 0), and it always 
returned true.

I did like mthomas suggested as use sprintf from newlib, but 
unfortunately this did not help.  I have attached the code and all other 
files I include(except the standard ones).  Any help is greatly 

Thank you!

Author: alex (Guest)
Posted on:

Rate this post
0 useful
not useful
Neal Le Moult:  I looked at the Logomatic Code, but the ADC Part seemed 
really jumbled in with everything else, and what I am really looking for 
is learning how to use the ADC, which is hard when its combined with 
interrupts and timers and the like.

Thanks anyway!

Author: Noel Le Moult (Guest)
Posted on:

Rate this post
0 useful
not useful
Here is an application note from Nxp about LPC21xx ADC



Entering an e-mail address is optional. If you want to receive reply notifications by e-mail, please log in.

Rules — please read before posting

  • Post long source code as attachment, not in the text
  • Posting advertisements is forbidden.

Formatting options

  • [c]C code[/c]
  • [avrasm]AVR assembler code[/avrasm]
  • [code]code in other languages, ASCII drawings[/code]
  • [math]formula (LaTeX syntax)[/math]

Bild automatisch verkleinern, falls nötig
Note: the original post is older than 6 months. Please don't ask any new questions in this thread, but start a new one.