need explaination on adc example.

wanna
hi experts!

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

thanx.

regards, 
wanna

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

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.

wanna
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);
  }
  
  close(fd);
}

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
177.

davef
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:
http://www.friendlyarm.net/dl.php?file=S3C2440.pdf

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

Read this:
http://www.friendlyarm.net/dl.php?file=mini2440_schematic.zip

wanna
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. :)

davef
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.

davef
3.3V

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.

wanna
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

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

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

ocikgel
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
(converter) 

platform : mini6410 - linux 2.6.28.6

Also I need sample c/c++ code 

best regards 
:)

r@wboom
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:
1.("/dev/adc",0)
2.why buffer is used and read(fd, buffer, sizeof buffer -1)
3.int value = -1;
can anybody plese tell me these things.
thanks.

Juergen
r@wboom,

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

r@wboom
@ocikgel
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.

davef
Ports and in this case peripherals start at 0, so:
   /dev/adc0

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

r@wboom
@davef 
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.

davef
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
http://ebrombaugh.studionebula.com/embedded/mini2440/index.html for GPIO. 
If you are happy with talking to GPIO from userspace see here
http://www.friendlyarm.net/forum/topic/31 and especially the avfrfreaks
link.

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

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

davef
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.

haroon
Hi,
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.

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

davef
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.

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

davef
How do you read the ADC?

haroon
Reading the SPI value

davef
What happens if you disconnect the thermal printer?

Post your code.

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

davef
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?

haroon
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

davef
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.

or 

cat /dev/adc  

adc read properly  but that time adc stop

Please Help me ASAP.

davef
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).