Author Topic: Analog to Digital conversion  (Read 1905 times)

0 Members and 1 Guest are viewing this topic.

Offline praveen_khmTopic starter

  • Full Member
  • ***
  • Posts: 48
  • Helpful? 1
    • Robot Platform
Analog to Digital conversion
« on: May 12, 2010, 12:17:30 PM »
Hi,

Can someone help me on a/d conversion.
I know there are few links which can guide me on a2d conversion. However, somehow I feel that I am not able to understand that.

I tried google and also in avrfreaks, but am lost somewhere in between as I just get an overall brief of a2d.

If someone can write a tut on this, my request would be a detailed description on the following:

ARef (reference voltage)
ADC Prescaler
ADC Channels
ADC registers: ADMux, ADCSRA(ADEN, ADSC, ADIF, helpful if all bit names and explanation is given), ADCL, ADCH

A complete code for conversion, using any ATmega. (I am using ATmega8).

I have the 50$ code which I use for conversion. However it is like I dont understand anything out of it. If I need something, I just add the entire file and then call the function, without understanding what and how it does.

I have already added this in "request a tutorial"... but feel the queue for tutorials are too long.

Kindly help.

Cheers,
Praveen
Robot building is all about sharing and learning
-------------------------------------------
www.robotplatform.com : Beginners guide to Electronics & Robotics

Offline Webbot

  • Expert Roboticist
  • Supreme Robot
  • *****
  • Posts: 2,165
  • Helpful? 111
    • Webbot stuff
Re: Analog to Digital conversion
« Reply #1 on: May 12, 2010, 12:46:17 PM »
Have you read the ATmel datasheet for the processor?  :P
Webbot Home: http://webbot.org.uk/
WebbotLib online docs: http://webbot.org.uk/WebbotLibDocs
If your in the neighbourhood: http://www.hovinghamspa.co.uk

Offline waltr

  • Supreme Robot
  • *****
  • Posts: 1,944
  • Helpful? 99
Re: Analog to Digital conversion
« Reply #2 on: May 12, 2010, 01:32:27 PM »
The ATmel datasheet will fill in the details specific to that processor.

Or do you need a tutorial on ADC basics?

Offline corrado33

  • Supreme Robot
  • *****
  • Posts: 611
  • Helpful? 11
Re: Analog to Digital conversion
« Reply #3 on: May 12, 2010, 08:02:21 PM »
Exactly.  The atmega8 datasheet tells you every single one of your questions.  It's just a bit of light reading.  ;)  It tells you what every bit in those registers does, and what to change it to to make certain things happen.

Offline praveen_khmTopic starter

  • Full Member
  • ***
  • Posts: 48
  • Helpful? 1
    • Robot Platform
Re: Analog to Digital conversion
« Reply #4 on: May 20, 2010, 04:59:46 AM »
Thanks, will give it a try.
Robot building is all about sharing and learning
-------------------------------------------
www.robotplatform.com : Beginners guide to Electronics & Robotics

Offline TrickyNekro

  • Contest Winner
  • Supreme Robot
  • ****
  • Posts: 1,208
  • Helpful? 15
  • Hardware and Firmware Designer
    • The Hellinic Robots Portal
Re: Analog to Digital conversion
« Reply #5 on: May 20, 2010, 07:30:00 AM »
Take a look here... The code is not finished, and it's tested on a Atmega2560
But it should work with most atmegas...  ;)

Code: [Select]
/*! \file a2d.c \brief Analog-to-Digital converter function library. */
//*****************************************************************************
//
// File Name : 'a2d.c'
// Title : Analog-to-digital converter functions
// Author : Lefteris Provatos - Copyright (C) 2004 - 2010
// Created : 2010-05-20
// Revised :
// Version : 1.0
// Target MCU : Atmel AVR series
// Editor Tabs : 4
//
// This code is distributed under the GNU Public License
// which can be found at http://www.gnu.org/licenses/gpl.txt
//
//*****************************************************************************

// defines

// A2D clock prescaler select
// *selects how much the CPU clock frequency is divided
// to create the A2D clock frequency
// *lower division ratios make conversion go faster
// *higher division ratios make conversions more accurate
#define ADC_PRESCALE_DIV2 0x00 ///< 0x01,0x00 -> CPU clk/2
#define ADC_PRESCALE_DIV4 0x02 ///< 0x02 -> CPU clk/4
#define ADC_PRESCALE_DIV8 0x03 ///< 0x03 -> CPU clk/8
#define ADC_PRESCALE_DIV16 0x04 ///< 0x04 -> CPU clk/16
#define ADC_PRESCALE_DIV32 0x05 ///< 0x05 -> CPU clk/32
#define ADC_PRESCALE_DIV64 0x06 ///< 0x06 -> CPU clk/64
#define ADC_PRESCALE_DIV128 0x07 ///< 0x07 -> CPU clk/128
// default value
#define ADC_PRESCALE ADC_PRESCALE_DIV128
// do not change the mask value
#define ADC_PRESCALE_MASK 0x07

// A2D voltage reference select
// *this determines what is used as the
// full-scale voltage point for A2D conversions
#define ADC_REFERENCE_AREF 0x00 ///< 0x00 -> AREF pin, internal VREF turned off
#define ADC_REFERENCE_AVCC 0x01 ///< 0x01 -> AVCC pin, internal VREF turned off
#define ADC_REFERENCE_RSVD 0x02 ///< 0x02 -> Reserved
#define ADC_REFERENCE_256V 0x03 ///< 0x03 -> Internal 2.56V VREF
// default value
#define ADC_REFERENCE ADC_REFERENCE_AREF
// do not change the mask value
#define ADC_REFERENCE_MASK 0xC0

// bit mask for A2D channel multiplexer
#define ADC_MUX_MASK 0x1F

// ADLAR settings
#define ADC_DATA_RIGHT_ADJ 0x00
#define ADC_DATA_LEFT_ADJ 0x20

// Set the library to wait for the
// ADC to complete convertion or not
#define WAITING_EN


//*****************************************************************************

// turn off a2d converter
void adcOff(void)
{
cbi(ADCSRA, ADIE); // disable ADC interrupts
cbi(ADCSRA, ADEN); // disable ADC (turn off ADC power)
}

// Setting the Prescaller
void setadcPresc(u08 Presc)
{

ADCSRA &= ~ADC_PRESCALE_MASK;
ADCSRA |= (Presc & ADC_PRESCALE_MASK);

}

//Setting the Voltage Reference
void setadcVolta(u08 voltR)
{
ADMUX &= ~ADC_REFERENCE_MASK;
ADMUX |= (voltR & ADC_REFERENCE_MASK);
}

void setadcdatao(u08 direction)
{
// If told set the data to left adjust
if (direction == ADC_DATA_LEFT_ADJ)
{
ADMUX |= ADC_DATA_LEFT_ADJ;
}
// Else set the data to right adjust
else
{
ADMUX &= ~ADC_DATA_LEFT_ADJ;
}
}

// Initialize ADC
void adcInit (void)
{
// Enable the ADC converter
ADCSRA |= 0x80;
// Ensure that we have the single convertion option
ADCSRA &= ~0x20;
// Set ADC default Prescaller
setadcPresc(ADC_PRESCALE);
// Set ADC reference Voltage
setadcVolta(ADC_REFERENCE);
// Ensure data are right adjusted
//setadcdatao(ADC_DATA_RIGHT_ADJ);
}

// Get ADC channel value
u16 Getadc(u08 ch)
{
u16 temp;
// Set channel for convertion
if (ch > 7)
{
ch -= 7;
// If the microcontroller has more channels
// then it should have ADCSRB register
// If not this offers compatibility with other
// microcontrollers
#ifdef ADCSRB

ADCSRB |= 0x08;

#endif
}

// Set ADCMUX
ADMUX = ((ADMUX & ~ADC_MUX_MASK) | (ch & ADC_MUX_MASK));
sbi(ADCSRA, ADIF); // clear hardware "conversion complete" flag
sbi(ADCSRA, ADSC); // start conversion

// if you define the library to wait for the convertion to complete

while( ADCSRA & 0x40 );

// if the program has indeed waited then it should have
// cleared the ADSC so return the data else
// return to the program and wait for the data there

temp = ADCL;
temp |= (ADCH<<8);
return temp; // read ADC (full 10 bits);

}
For whom the interrupts toll...

Offline praveen_khmTopic starter

  • Full Member
  • ***
  • Posts: 48
  • Helpful? 1
    • Robot Platform
Re: Analog to Digital conversion
« Reply #6 on: May 20, 2010, 10:46:25 AM »
Thanks for the code TrickyNekro. Trying to understand the code with the help of those comments.

@corrado33,

I never understood the meaning of these words unless I read the data sheet. "It's just a bit of light reading.  ;)". The datasheet is too too complex for my understanding.

@waltr,

Thatz exactly what I am looking for. A basic tutorial on ADC and how it is configured and the code. If someone would be able to guide me through so that I can write a code rather than copying code, it would be helpful.

I will go through the code given here and try to understand this rocket science in the meanwhile.

Cheers,
Praveen
Robot building is all about sharing and learning
-------------------------------------------
www.robotplatform.com : Beginners guide to Electronics & Robotics

Offline waltr

  • Supreme Robot
  • *****
  • Posts: 1,944
  • Helpful? 99
Re: Analog to Digital conversion
« Reply #7 on: May 20, 2010, 12:25:08 PM »
Quote
A basic tutorial on ADC and how it is configured and the code.
These are two separate topics. The first can be found in app notes from a number of ADC manufactures.

The second is specific to the uController you are using. A nice example is given for an Atmega.

Here are a few ADC pages:
http://hyperphysics.phy-astr.gsu.edu/hbase/electronic/adc.html
http://en.wikipedia.org/wiki/Analog-to-digital_converter

Offline Actives

  • Beginner
  • *
  • Posts: 6
  • Helpful? 0
Re: Analog to Digital conversion
« Reply #8 on: May 21, 2010, 12:54:37 AM »
simple using arduino it will be easy to understand and less configuration

ADC will be able to convert from voltage level to digital level like you using 0-5 volt and 8 bit ADC will be resolution 2^8 = 1024

it mean 0-1023 value (including zero) in microcontroller, when the 5 volt is max and 0 volt is min, 5/1024 = 0.00488 volt per 1 ADC tick.

if you reading value from ADC as 512 it will be = 512 * 0.00488 = 2.498 volt

also see : http://arduino.cc/en/Reference/AnalogRead

Offline praveen_khmTopic starter

  • Full Member
  • ***
  • Posts: 48
  • Helpful? 1
    • Robot Platform
Re: Analog to Digital conversion
« Reply #9 on: May 21, 2010, 03:32:32 AM »
Thanks everyone. Am trying my best to understand the logic.
Robot building is all about sharing and learning
-------------------------------------------
www.robotplatform.com : Beginners guide to Electronics & Robotics

Offline Soeren

  • Supreme Robot
  • *****
  • Posts: 4,672
  • Helpful? 227
  • Mind Reading: 0.0
Re: Analog to Digital conversion
« Reply #10 on: May 21, 2010, 09:06:22 PM »
Hi,

[...] and 8 bit ADC will be resolution 2^8 = 1024
2^8 used to be 256 when I was young... But everything seems to be rising these days  ;)
Regards,
Søren

A rather fast and fairly heavy robot with quite large wheels needs what? A lot of power?
Please remember...
Engineering is based on numbers - not adjectives

Offline chelmi

  • Supreme Robot
  • *****
  • Posts: 496
  • Helpful? 15
    • Current projects
Re: Analog to Digital conversion
« Reply #11 on: May 22, 2010, 06:57:23 AM »
Hi,

[...] and 8 bit ADC will be resolution 2^8 = 1024
2^8 used to be 256 when I was young... But everything seems to be rising these days  ;)


:D
He is probably using imperial bits :p

Offline Soeren

  • Supreme Robot
  • *****
  • Posts: 4,672
  • Helpful? 227
  • Mind Reading: 0.0
Re: Analog to Digital conversion
« Reply #12 on: May 23, 2010, 06:05:16 AM »
Hehe, point taken  ;D
Regards,
Søren

A rather fast and fairly heavy robot with quite large wheels needs what? A lot of power?
Please remember...
Engineering is based on numbers - not adjectives

 


Get Your Ad Here

data_list