ADC in Atmega8

Download Report

Transcript ADC in Atmega8

ADC TUTORIAL
-DONE BY
HARESH MIRIYALA
ADC IN ATMEGA8
Topics that will get into your head in a few minutes :
•Types of Sensors
•What is ADC ?
•Prescaler and Clock
•ADC in Atmega8 microcontroller
•Sample Code
•Some applications of the ADC peripheral
TYPES OF SENSORS
Based on output type :
•Analog sensor
•Digital sensor
Analog , Digital , output, sensor .. ? whaat !
•It's pretty easy . Analog sensor gives analog output
of specific values within a range . Digital sensor gives
digital output in binary .
WHAT IS 'ADC' ?
•
ADC stands for Analog To Digital Conversion
•
Most of the sensors what we use are analog type sensors . Why ? Who would want to buy
those costly digital ones !
•
Most of the microcontrollers have an ADC peripheral and so does AVR Atmega8 .
The Atmega8 microcontroller possesses a builtin ADC unit .
•
A uC cannot work with analog values unless it is converted to digital . This is where
ADC plays a role.
•
The uC that you have on table right now, has a 7 channel 10bit ADC that's enough to
build a powerful robot.
PRESCALER AND CLOCK
• This is how the Analog to digital conversion is done : The ADC peripheral
needs to be powered up separately by AVCC pin . The ADC reference
voltage is set in AREF pin – This is generally set 5Volts .
• After ADC starts conversion , the uC adopts a method of successive
approximation to the Analog value and the conversion is done .
• It keeps sampling the analog value based on the clock frequency set . Clock
frequency is generally the clock that drives the uC but a prescaler can be set
to reduce the frequency .
• Always remember : Lower the frequency , lesser is the percentage of error .
ADC IN ATMEGA8 MICROCONTROLLER
• Atmega8 has a 10bit ADC located in PORTC
• ADC can be used to read voltage values and take actions accordingly . All
this must be properly implemented in programming .
• There are many modes in which ADC could function . Refer DataSheet of
Atmega8 given in the GENESIS Folder .
QUICK INTRO TO REGISTERS
SAMPLE CODE
void Init_ADC()
//initialises the ADC peripheral
{
ADMUX=(1<<REFS0);
//LINE 1
ADCSRA=(1<<ADEN)|(1<<ADPS2)|(1<<ADPS1)|(1<<ADPS0); //LINE 2
// prescalar division factor=128 }
uint16_t Read_ADC(uint16_t ch)
•
ch=ch&0b00000111;
//function to read the ADC value from the adc pins .
//LINE 3
ADMUX|=ch;
//LINE 4
ADCSRA|=(1<<ADSC);
//LINE 5
while(!(ADCSRA & (1<<ADIF)));
//LINE 6
ADCSRA|=(1<<ADIF);
//LINE 7
return(ADC); }
//LINE 8
ADMUX REGISTER
ADCSRA
APPLICATIONS OF ADC
•
The ultrasonic sensor given in the RMI kit gives an analog output .Now you know how
to deal with this analog value!
•
Use the ADC peripheral to get the digital value . 0-5Volts analog value will be
converted to 0-1023 in digital value for this 10bit ADC peripheral .
•
Do whatever you want with this digital value . For ex, in the ultrasonic sensor ,
multiplying the digital value with a constant given by the sensor manufacturer will
give you the distance in meters , accurately ! Then maybe you could display the
distance by interfacing the uC with a LCD display ! .