Tuesday, July 06, 2010

Implementing ADC (Most Important Core Concept)

Theory of Operation Of ADC (Core Concepts)

What we have seen till now that the input given to uC was digital, i.e., either +5 V (logic 1) or 0V (logic 0). But what if we have an analog input, i.e., value varies over a range, say 0V to +5V? Then we require a tool that converts this analog voltage to discrete values. Analog to Digital Converter (ADC) is such a tool.

adc avr

ADC is available at PORTA of Atmega16. Thus we have 8 pins available where we can apply analog voltage and get corresponding digital values. The ADC register is a 10 bit register, i.e., the digital value ranges from 0 to 1023. But we can also use only 8 bit out of it (0 to 255) as too much precision is not required.

Reference voltage is the voltage to which the ADC assigns the maximum value (255 in case of 8 bit and 1023 for 10 bit). Hence, the ADC of Atmega16 divides the input analog voltage range (0V to Reference Voltage) into 1024 or 256 equal parts, depending upon whether 10 bit or 8 bit ADC is used. For example, if the reference voltage is 5V and we use 10bit ADC, 0V has digital equivalent 0, +5V is digitally 1023 and 2.5V is approximately equal to 512.

ADC =Vin x 255/Vref (8 bit)

ADC =Vin x 1023/Vref (10 bit)


Setting up Microcontroller

To enable ADC in Atmega16, click on the ADC tab in Code Wizard and enable the checkbox. You can also check “use 8 bits” as that is sufficient for our purpose and 10 bit accuracy is not required. If the input voltage ranges from 0 to less than +5V, then apply that voltage at AREF (pin 32) and select the Volt. Ref. as AREF pin. But if it ranges from 0 to +5 V, you can select the Volt. Ref. as AVCC pin itself. Keep the clock at its default value of 125 kHz and select the Auto Trigger Source as Free Running. You can also enable an interrupt function if you require.

cvavr avr adc

Function for getting ADC value

Now when you generate and save the code, all the register values are set automatically along with a function:

unsigned char read_adc(unsigned char adc_input).

This function returns the digital value of analog input at that pin of PORTA whose number is passed as parameter, e.g., if you want to know the digital value of voltage applied at PA3, and then just call the function as,

read_adc(3);

If the ADC is 8 bit, it will return a value from 0 to 255. Most probably you will need to print it on LCD. So, the code would be somewhat like

int a; char c[10]; // declare in the section of global variables

a=read_adc(3);
itoa(a,c);
lcd_puts(c);

No comments:

Post a Comment