Wednesday, 15 June 2011

Basic ADC program in Atmega32


ADC (Analog to digital converter) is required because all the real world data is analog and computer cannot process analog data due to its continuous nature. So ADC is used to digitize the analog so that the processor can understand.

Atmega32 has an inbuilt 8-channel multiplexed ADC. Only one of the eight channels can be accessed at a time. ADC is of successive approximation type and hence highly accurate.

Atmega32 has three registers for ADC - ADCSRA , ADMUX and ADCW.

ADCSRA is the control and status register responsible for indicating ADC start and end of converstion , the clock speed or the rate of conversion and interrupt enable.
ADMUX selects one of the 8 adc channels.
ADCW contains the 10 bit  digitized value.

For more details of these registers refer the datasheet here.


The code example is for thresholding an analog sensor. We assume the sensor gives maximum of 5V output. The ADC returns a 10 bit value therefore for maximum 5V the adc output is 2^10 = 1024.
So,  5V --> 1024
      2.5V--> 512
There is no need to convert the digital value back to analog, just basic math will do. Additionally, we increase the efficiency by not going into float values.
So if the input to the PORTA0 is greater than 2.5V then the led will glow on PORTB else it will remain off.

ADC input greater than threshold (I simply gave 5V which is obviously greater than 2.5V)


ADC input less than threshold (I simply gave 0V which is obviously less than 2.5V












No comments:

Post a Comment

Popular Posts