Wednesday, October 30, 2013

Analog to Digital Conversion

Components Required



  • ATmega32 microcontroller
  • AVR Programmer Board
  • Crystal
  • Capacitors
  • LEDs
  • +5V supply
  • Breadboard
  • Connecting Wires

Circuit Diagram


Description

The digital value of the analog input at PA0 will be shown in the 8 leds PC0 to PC7 from LSB to MSB.

Connections

  • LEDs are connected to the Pins 0 to 7 of PORT C.
  • The analog input is given to PIN A0 (ADC 0).
  • A crystal is connected to the XTAL1 and XTAL2 pins to provide the clock pulse.
  • Reset is connected to +5V.

    Source Code

    /*The following code will keep on converting the analog value applied at the pin PA0, convert it into digital value and store the most significant 8 bits in ADCH register and the lesser two bits in the ADCL register.
    Since the lesser two values can be ignored the 8 bits present in the ADCH are only taken care of and the LEDs are glown according to the ADCH register*/

    /* Go through the blog http://rickruling.blogspot.in/2013/10/adc-control-and-status-registers.html for more details on adc control and status registers*/

    #include <avr/io.h>
    #include <avr/interrupt.h>                     //For interrupt handling

    #ifndef F_CPU
    #define F_CPU 1000000UL               //frequency of the external crystal
    #endif

    int main(void)
    {
    DDRC = 0xFF;                     //All ports of port C as output
    PORTB = 0x00;                            //Initially all pins as output low
    //configure the ADC
    ADCSRA |=1<<ADPS2;       //ADC Prescaler Select Bits (Division Factor is 16 for 100)
    ADMUX |=1<<ADLAR;      //MSB in ADCH
    ADMUX |=1<<REFS0;        // AREF pin
    ADCSRA |=1<<ADIE;         //ADC Interrupt Enable
    ADCSRA |=1<<ADEN;        //ADC Enable
    sei();                                      //set global interrupt enable
    ADCSRA |=1<<ADSC;        //ADC Start Conversion
        while(1)                                      //Infinite loop
              ;
    }

    ISR(ADC_vect)
    {
    PORTB = ADCH;                     //Result of conversion to PORTB
    ADCSRA |=1<<ADSC;            //Restart the conversion
    }


    Thank you for reading.

    For more tutorial and information, like the Facebook Page 

    No comments:

    Post a Comment