Main Menu
Home
AVR News
Atmel AVR
AVR Development Tools
Valuable Tools
WinAVR toolset
Makefile for WinAVR
AVR Virtual Simulators
Hardware for prototyping
GCC and AVR-GCC
Short introduction to C
AVR-GCC Tutorial
Example AVR Projects
AVR-GCC articles
WinAVR Site Map
Scienceprog FORUM
ScienceProg BLOG
Disclaimer
WinARM Tutorial
Login Form





Lost Password?
No account yet? Register
Latest comments
Accessing AVR microcontroller...
You need to read PORTD 5th bit and...
More...

Accessing AVR microcontroller...
Hello! How can I do this with GCC?:...
More...

Running TX433 and RX433 RF...
I will consider this. Thank you for...
More...

Running TX433 and RX433 RF...
Please write a simple article about how...
More...

Using Eclipse as IDE for...
It is only a GUI, that makes easier to...
More...

Syndicate
Friendly sites

Related Items:

Latest News
Programming AVR ADC module with WinAVR PDF Print E-mail
Written by Administrator   
Thursday, 31 January 2008

Most of AVR microcontrollers have Analog to Digital Converter (ADC) integrated in to chip. Such solution makes embedded designers life much easier when creating projects and programming them. With no need external ADC PCB takes less space, easier to create programs – it saves time and money. As an example lets take Atmega8 microcontroller which have up to 8 ADC inputs most with 10-bit resolution(excluding ADC4 and ADC5 inputs that are 8-bit). All features of AVR internal ADC can be found on official ATMEL AVR datasheets, but most important to mention are:

  • ±2 LSB accuracy – so measurements aren't very accurate. If AREF voltage is 5V then error may reach ±0.04V but this is still good results for most of tasks;

  • Integral nonlinearity ±0.5 LSB;

  • Conversion speed up to 15kSPS at maximum resolution. This is far not enough for 20kHz audio signal sampling.

ADC unit is powered with separate power supply pins AVCC with AGND, but AVCC must not differ ±0.3V of VCC. Also ADC unit can have different voltage reference sources selectable in ADMUX register. References may be taken from AREF pin, AVCC with external capacitor or internal 2.56V voltage reference. All ADC inputs are multiplexed via multiplexer. Each channels can be selected by changing 4 bits in ADMUX register. ADC unit can operata in two modes:

  • Single conversion – when ADC converter makes one conversion and then stops. As this mode require ADC initialization for each conversion it takes 25ADC clock cycles;

  • Free running conversion – conversion is continuous. Once initialized it takes 13 ADC cycles for single conversion. In this mode ADC data register has to be read before new value is written.

AVR ADC has a nice feature ADC noise reduction technique which allows to perform conversion with minimal noise induced from AVR core and io peripherals. It is simple – when noise canceling is enabled MCU is put to sleep(CPU clock stops). After conversion completes interrupt wakes processor to read and process converted data.

All theory is nicely explained in AVR datasheets better lets go to some practical issues. Connect photo resistor to ADC0 input and simple potentiometer to ADC2. As an example lets do single conversions with AVCC voltage as reference. Lets generate an interrupt when conversion is complete. Interrupt service routine will output ADC value on LCD. Circuit is really simple – on Proteus simulator it is even simpler:

AVR ADC Proteus simulation

There are three simple steps needed to achieve desired results. First of all we need to initialize ADC. For this adc_init() function is written which prepares reference voltage source (AVCC with external capacitor at AREF pin), then enables ADC peripheral and performs single dummy conversion to initialize ADC.

void adc_init(void)
{
//select reference voltage
//AVCC with external capacitor at AREF pin
ADMUX|=(0<<REFS1)|(1<<REFS0);
//set prescaller and enable ADC
ADCSRA|=(1<<ADEN)|(1<<ADIE);//enable ADC with dummy conversion
//set sleep mode for ADC noise reduction conversion
set_sleep_mode(SLEEP_MODE_ADC);
}

Next step is to convert data itself. As we need to read values from two channels, there also multiplexing is needed.

void adc_start_conversion(uint8_t channel)
{
//remember current ADC channel;
ch=channel;
//select ADC channel
ADMUX=(ADMUX&0xF0)|channel;
//Start conversion with Interrupt after conversion
//enable global interrupts
sei();
ADCSRA |= (1<<ADSC)|(1<<ADIE);
}

If this conversion mode invokes an interrupt after conversion is complete, thidr step is writing interrupt service routine, which takes 10 ADC value and displays it on LCD:

ISR(ADC_vect)
{
}

And finally working main.c file contents. Full zipped project including Proteus simulator file can be downloaded here .


//*****************************************************************************
//
// File Name	: 'main.c'
// Title		: ADC single conversion witn interrupt after conversion
//
//*****************************************************************************
#include <stdio.h>
#include <avr/io.h>
#include <avr/pgmspace.h>
#include <avr/interrupt.h>
#include <util/delay.h>
#include "lcd_lib.h"

//adjust LCDsendChar() function for stream
static int LCDsendstream(char c, FILE *stream);
//----set output stream to LCD-------
static FILE lcd_str = FDEV_SETUP_STREAM(LCDsendstream, NULL, _FDEV_SETUP_WRITE);


void delay1s(void);
void adc_init(void);
void adc_start_conversion(uint8_t);
static int LCDsendstream(char c , FILE *stream);
void init(void);

//current channel
uint8_t ch;
//strings for LCD stored in Flash memory
const uint8_t LDR[] PROGMEM="LDR:\0";
const uint8_t POT[] PROGMEM="POT:\0";
const uint8_t CLRROW[] PROGMEM="                \0";

const uint8_t *LCDXY[] ={
		LDR,	//for ADC0
		POT};	//for ADC1


//*****************************************************************************
//
//  ADC module initialization  
//
//*****************************************************************************
void adc_init(void)
{
//select reference voltage
//AVCC with external capacitor at AREF pin
ADMUX|=(0<<REFS1)|(1<<REFS0);
//set prescaller and enable ADC
ADCSRA|=(1<<ADEN)|(1<<ADIE);//enable ADC with dummy conversion
//set sleep mode for ADC noise reduction conversion
set_sleep_mode(SLEEP_MODE_ADC);
}
//*****************************************************************************
//
//  ADC single conversion routine  
//
//*****************************************************************************
void adc_start_conversion(uint8_t channel)
{
//remember current ADC channel;
ch=channel;
//set ADC channel
ADMUX=(ADMUX&0xF0)|channel;
//Start conversion with Interrupt after conversion
//enable global interrupts
sei();
ADCSRA |= (1<<ADSC)|(1<<ADIE);
}

//*****************************************************************************
//
//  delay 1s  
//
//*****************************************************************************

//delay 1s
void delay1s(void)
{
	uint8_t i;
	for(i=0;i<100;i++)
	{
		_delay_ms(10);
	}
}

//*****************************************************************************
//
//  Set LCD stream function  
//
//*****************************************************************************
static int LCDsendstream(char c , FILE *stream)
{
LCDsendChar(c);
return 0;
}

//*****************************************************************************
//
//  init AVR
//
//*****************************************************************************

void init(void)
{
	//init stream
	stdout = &lcd_str;
	//init LCD
	LCDinit();//init LCD bit, dual line, cursor right
	LCDclr();//clears LCD
	LCDGotoXY(0, 0);//Cursor Home
	//Init ADC
	adc_init();
}

//*****************************************************************************
//
//  ADC conversion complete service routine  
//
//*****************************************************************************
ISR(ADC_vect)
{
	uint16_t adc_value;
	adc_value = ADCL;   
	/*shift from low level to high level ADC, from 8bit to 10bit*/
	adc_value += (ADCH<<8);
	CopyStringtoLCD(CLRROW, 0, ch );
	CopyStringtoLCD(LCDXY[(ch)], 0, ch );
	printf("%d", adc_value);
}

//*****************************************************************************
//
//  run analog digital converter, timer.  
//
//*****************************************************************************
int main(void)
{
	init();
	printf("ADC Test");
	delay1s();
	while(1)//loop demos
	{
	//read LDR
	adc_start_conversion(0);
	_delay_ms(30);
	//read potentiometer
	adc_start_conversion(1);
	_delay_ms(30);
	//continue infinitely
	}
	return 0;
}

Related Items:

   

Users' Comments  
 

Average user rating

 

Display 1 of 1 comments

Comments are on now!

By: admin on 20-02-2008 12:30

Registered users may comment articles. :? :roll add smilies and even insert image: 

 

» Report this comment to administrator

» Reply to this comment...

Display 1 of 1 comments



Add your comment
Only registered users can comment an article. Please login or register.


mXcomment 1.0.6 © 2007-2008 - visualclinic.fr
License Creative Commons - Some rights reserved
< Prev   Next >

© 2008 WinAVR AVR-GCC Tutorial
Joomla! is Free Software released under the GNU/GPL License.