.

INCLUDES RFID interfacing with avr GPS interfacing with avr RF Module interfacing with avr Stepper Motor With AVR

Saturday, December 17, 2011

How to use inbuilt ADC of AVR microcontroller (ATmega16)

Microcontroller understands only digital language. However, the inputs available from the environment to the microcontroller are mostly analog in nature, i.e., they vary continuously with time. In order to understand the inputs by the digital processor, a device called Analog to Digital Converter (ADC) is used. As the name suggests this peripheral gathers the analog information supplied from the environment and converts it to the controller understandable digital format, microcontroller then processes the information and provides the desired result at the output end.

ATmega16 has an inbuilt 10 bit, 8-channel ADC system. Some of the basic features of Armega16 ADC are:
·         8 Channels.
·         10-bit Resolution.
·         Input voltage range of 0 to Vcc.
·         Selectable 2.56V of internal Reference voltage source.
·      AREF pin for External Reference voltage.
·      ADC Conversion Complete Interrupt.

ADC channels in Atmega16 are multiplexed with PORTA and use the common pins (pin33 to pin40) with PORTA. ADC system  of Atmega16 microcontroller consists of following pins:
        i.            ADC0-ADC7: 8 Channels from Pin 40 to Pin 33 of Atmega16 ADC peripheral.
      ii.            AREF: Pin32 of Atmega16 microcontroller, the voltage on AREF pin acts as the reference voltage for ADC conversion, reference voltage is always less than or equal to the supply voltage, i.e., Vcc.
      iii.            AVCC: Pin30, this pin is the supply voltage pin for using PORTA and the ADC; AVCC pin must be connected to Vcc (microcontroller supply voltage) to use PORTA and ADC.

Note:  External reference voltage source can be used at AREF pin. However, Atmega16 also has internal reference voltage options of 2.56V and Vref = Vcc.

Learn More About ADC REGISTERS form datasheet of ATMEGA16 available at downloads.



Circuit description
Connect the circuit as shown in the circuit diagram. A ceramic capacitor 104 is connected in between AVcc (pin 30) and Aref (pin 32). AVcc (pin 30) is connected to external supply +5V.

Code explanation
To interface analog device with AVR microcontroller, follow the following steps for programming it.


Step1: To initialize ADC
        i.            Set the value in ADMUX register according to the ADC channel and the reference voltage.
       ii.            Set the Prescaler bits accordingly in ADCSRA register.
      iii.            Set the ADEN bit to enable the ADC.
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
}

Step2: To read the analog value
        i.            Put the channel value in ADMUX
       ii.            Start the conversion by setting the ADSC bit.
      iii.            Monitor the ADIF bit for conversion complete.
      iv.            Clear the conversion bit ADIF. By writing it 1.
       v.            Digital converted result is now available in ADCH and ADCL registers.
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);
}


//Program for ADC to read from channel 0 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);

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

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);
}

No comments:

Post a Comment