Anda di halaman 1dari 10

ADC

Analog to Digital Converter


As the name implies that Analog to Digital, means to convert analog
signal into digital signal.
We all knows Analog signal is continues in nature and Digital signal is
discrete in nature
Needs of ADC
As we know that our microcontroller understand only digital language
(i.e. 1 & 0)
And to interface any analog device to the microcontroller we have to
convert the analog signal into digital.
How it work
Registers
ADMUX: ADC Multiplexer Selection Register

7 6 5 4 3 2 1
0
REFS1 REFS0 ADLAR MUX4 MUX3 MUX2 MUX1 MUX0 8
Bit
This is 8 bit register and used in :

Selection of channel
set reference voltage of ADC
And select output as left adjustment or right adjustment
Registers
ACSRA: ADC Control
ADC Enable andthis
Writing Status Register
bit to A
one enables the ADC. By writing it to zero, the ADC
is turned off. Turning the ADC off while a conversion is in progress, will terminate
this conversion.
ADC Start Conversion To start each conversion we have to write one to this bit,
ADSC will be high as long as a conversion is in progress. When the conversion
is7complete, it6returns to5zero. Writing4 zero to this3bit has no effect.
2 1
ADC auto 0 trigger enable When this bit is written to one, Auto Triggering of the8
ADEN ADSC ADATE ADIF ADIE ADPS2 ADPS1 ADPS0
ADC is enabled. The ADC will start a conversion on a positive edge of theBit
selected trigger signal. The trigger source is selected by setting the ADC Trigger
Select
This bits,register
is 8 bit ADTSused
in SFIOR.
to control the ADC and also show its status.
ADC Interrupt Flag This bit is set when an ADC conversion completes. ADIF is
cleared by hardware when executing the corresponding interrupt handling
vector. Alternatively, ADIF is cleared by writing a logical one to the flag.
ADC Interrupt Enable When this bit is written to one, the ADC Conversion
Complete Interrupt is activated.

ADC Prescaler Select Bits These bits determine the division factor between the
XTAL frequency and the input clock to the ADC.
//Program for ADC to read from channel 1 and show the 8 bit o/p
on PORTB

#include<avr/io.h>
#include<util/delay.h>

void ADC_init(void);
unsigned int ADC_read(unsigned char);

void ADC_init(void) // Initialization of ADC


{
ADMUX=(1<<REFS0); // AVcc with external capacitor at
AREF
ADCSRA=(1<<ADEN)|(1<<ADPS2)|(1<<ADPS1)|
(1<<ADPS0);
// Enable ADC and set Prescaler division factor as 128
}
unsigned int ADC_read(unsigned char ch)
{
ch= ch & 0b00000111; // channel must be b/w 0 to 7
ADMUX |= ch; // selecting channel

ADCSRA|=(1<<ADSC); // start conversion


while(!(ADCSRA & (1<<ADIF))); // waiting for ADIF, conversion complete
ADCSRA|=(1<<ADIF); // clearing of ADIF, it is done by writing 1 to it

return (ADC);
}
int main(void)
{
unsigned int value;
DDRB=0xFF;
ADC_init(); // Initialization of ADC
while(1)
{
value=ADC_read(0);
PORTB=value;
_delay_ms(500);
}
}