need explaination on adc example.

hi experts!

i need further explaination about the used of 'usleep(500* 1000);' in adc
example. what it is for?



Perhaps a crude method of (hopefully) waiting until the ADC conversion is

Paste the whole code in your reply.

Normally, uPs have a conversion complete flag you can poll or generate a
conversion complete interrupt.

I don't have the datasheet in front of me.

hi davef. this is the source code of the program.

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include <linux/fs.h>
#include <errno.h>
#include <string.h>

int main(void)
  fprintf(stderr, "press Ctrl-C to stop\n");
  int fd = open("/dev/adc", 0);
  if (fd < 0) {
    perror("open ADC device:");
    return 1;
  for(;;) {
    char buffer[30];
    int len = read(fd, buffer, sizeof buffer -1);
    if (len > 0) {
      buffer[len] = '\0';
      int value = -1;
      sscanf(buffer, "%d", &value);
      printf("ADC Value: %d\n", value);
    } else {
      perror("read ADC device:");
      return 1;
    usleep(500* 1000);

where can i get the adc data sheet? and correct me if im wrong, adc is
converting analog voltage to digital voltage right? how am i want to know
if the converting value is correct? let say, i apply 8.71V and results is

I think, in this case:

usleep(500* 1000);

is creating a pause in your FOREVER loop to allow other applications to get
a chance to run, without having to use interrupts.

ADC converts an analogue voltage into a digital representation of that
voltage.  I.E. a 10 bit ADC with a 3V3 reference will say that 1024 equals
3.3Volts.  In this case the ADC block sends out a bunch of data bytes on a
SPI bus that the uP can use.

The data for the ADC is found here:

You don't want to apply more than 3V3 to the ADC or you could blow it up. 

Read this:

ouh. thanx davef! you are really my savior for understanding this mini2440.

ok, i think my input voltage is only a mV. not more than that. but, im once
had give more than 3.3V. how am i know that my adc blow? is it of it didn't
give any values anymore?

and again, correct me if im wrong, if i put an 3.3V to pin AIN2, so it
still will read as 1024?

thanx. :)

How will you know if you blow up the ADC input?  The easiest way would be,
if it was giving you a reading of about 500 with a 1.5Volt battery
connected to it (correct polarity) or with the pot on the dev board set to
half-way THEN at a later time you did that again AND it didn't give you the
same result you MIGHT come to the conclusion that you had damaged it.

3.3V should give a reading on somewhere close to 1024.  You should use a
digital voltmeter of known accuracy and apply no more than 3.3V to any
input on the mini2440.

Jorge Moreno
Good night

I would like to know, what compiler are used to generate this code?

What is the maximum voltage across the ADC, 5V or 3.3V?

Thank you very much.


What compiler is used to cross-compile this .c file . . . generally,
ARM-Linux GCC 4.3.2, but now there is 4.4.3 on the downloads page.

hi davef.

thanx again for your explaination. 

i guess my adc pin burn. as i apply voltage to AIN2, theres no reading at
all. T___T

Before you jump to this conclusion does the ADC application on the Qt 2.2
desktop work?  On the FriendlyArm distro (DVD).

davef, do you mean the adc converter application in friendly arm?

I need source code that is same as above  code  but I need for mini6410 and
mini 6410 does not have /dev/adc file ? how can achieve mini 6410 adc ? 
what  I want to make = bind  2 sensors on analog inputs
(AIN1-AIN2(PIN27-PIN28)) than I must read this datas on register 

problem : I dont know related register id - I dont know how can use adc

platform : mini6410 - linux

Also I need sample c/c++ code 

best regards 

hello there,
i started the mini2440 10 days ago...
i am trying to understand the concept of this adc programme but i couldn't
understand these things:
2.why buffer is used and read(fd, buffer, sizeof buffer -1) value = -1;
can anybody plese tell me these things.


you should read the corresponding man pages of open() and read(). Using
"/dev/adc" is implementation specific and used only in the FA kernel.

i read the open() and read() function and understand the programme.
three doubt are here
1./dev/adc,0. why we use zero.
2. int len = read(fd, buffer, sizeof (buffer -1));
why we use the buffer-1 and val is -1.
actually i don't understand the concept of this example.
please can you give me a little guide so that i can.
thank you.

Ports and in this case peripherals start at 0, so:

Starting at zero is a computer science convention.  Maybe, because binary
is a 0 or a 1.  Google should help.

-1  When you allocate space in an array for strings you (always) need an
extra space for a terminating null.  I guess that you don't need to read
the value of the last position in.

A related term could be <buffer overflow> because lots of problems occur if
you don't get the size of any buffer correct.

Hope that helps

thanks for it.
helped, very much
now i want to use an AN1 channel for adc ho wcan i do that as open
/dev/adc1 like this??
second i want to play with gpio's as input and output mean blink an led and
connect the switch.

Is this for a mini2440?  If so, be aware that ADC0, 1, 2 and 3 are used for
the touchscreen.  If you really want to use AN1 then you need to hack the
kernel.  Use one of the other remaining ADC channels.

Read this one for GPIO. 
If you are happy with talking to GPIO from userspace see here and especially the avfrfreaks

ADC.  A Google search with mini2440 ADC should show up some examples

hey davef
can i take buffer size more then this code of can less than 30.

I would guess that you could.  Perhaps if you copy the code you are
referring to here I or others could make a better guess.

Am using adc7476. In my case adc and thermal printer connected in same spi.
When am reading the adc value am always getting 0 value.

Am using adc7476. My adc and thermal printer connected in same spi. When am
reading the adc value am always getting 0 value.

There is a loop-back test you can do,  There is a program called spidev
that would need cross-compiling for your target.

Search <spidev>.

Posting your code might help.

Hi davef, 
Am not getting what you are saying. Can you please send any sample program

How do you read the ADC?

Reading the SPI value

What happens if you disconnect the thermal printer?

Post your code.

I cant share my code in public can you please give your email id or skype
id ?

Let us try a bit harder to solve this without the code.

> What happens if you disconnect the thermal printer?

Will the ADC work properly?

No, ADC will not work without thermal printer. we are reading the value of
SPI then we are converting analog to digital value but still we are
gettting value as zero

Google search <spidev> so that you can check SPI using a loopback test.

I assume you have enabled SPI in the kernel and re-compiled.

Sushant Bhangale
Dear sir,
I need a help in adc reading with Qt.I am facing some issue at the time of
adc read touchscreen working stop.After close this application touch
properly work.


cat /dev/adc  

adc read properly  but that time adc stop

Please Help me ASAP.

There was a fairly long recent thread on this issue, was it you?

What is your target machine, what rootfs, which kernel, which display (4
wire resistive or 1 wire).