Analog to digital conversion
Analog to digital conversion is the process of converting the continuous time signal into the discrete signal above figure shows the continuous time analog sinusoidal signal and its equivalent binary (ie digital) number .
Since AVR is a digital microcontroller ,it only understand the digital value , hence for any analog signal before processing it need to be converted into the digital value . All the real world signal that come from sensors like temperature ,pressure ,voltage ,humidity etc are analog in nature . To present these signals into a meaningful information we need to convert them into digital signals .
AVR microcontroller have its internal ADC module so we do not need external circuitary for conversion .according to the datasheet of Atmega-16 ADC have following features.
- 0.5 LSB Integral Non-linearity
- ±2 LSB Absolute Accuracy
- 13 μs- 260 μs Conversion Time
- Up to 15 kSPS at Maximum Resolution
- 8 Multiplexed Single Ended Input Channels
- 7 Differential Input Channels
- 2 Differential Input Channels with Optional Gain of 10x and 200x
- Optional Left adjustment for ADC Result Readout
- 0 - VCC ADC Input Voltage Range
- Selectable 2.56V ADC Reference Voltage
- Free Running or Single Conversion Mode
- ADC Start Conversion by Auto Triggering on Interrupt Sources
- Interrupt on ADC Conversion Complete
- Sleep Mode Noise Canceler
The ATmega16 features a 10-bit successive approximation ADC. The ADC is connected to an
8-channel Analog Multiplexer which allows 8 single-ended voltage inputs constructed from the
pins of Port A. The single-ended voltage inputs refer to 0V (GND).
Summarizing the features we can say there are two main features
- 8 channelimplies that there are 8 ADC pins are multiplexed together. You can easily see that these pins are located across PORTA (PA0…PA7).
- 10 bit resolutionimplies that there are 2^10 = 1024 steps (as described below) we can represent any signal in terms of numbering 0 to 1023.
So PORTA also acts as channel representation of converted signal.
Suppose we use a 5V reference. In this case, any analog value in between 0 and 5V is converted into its equivalent ADC value as shown above. The 0-5V range is divided into 2^10 = 1024 steps. Thus, a 0V input will give an ADC output of 0 , 5V input will give an ADC output of 1023, whereas a 2.5V input will give an ADC output of around 512 and so on. This is the basic concept of ADC.
To those whom it might concern, the type of ADC implemented inside the AVR MCU is of Successive Approximation type..
Before starting the further explanation lets learn through example of ADC .
In this example we are going to interface a potentiometer which divides the continuous voltage between 0 to 5 to a discrete value 0-1024. Ok lets complete the circuitary in your hardware or simulator .
a potentiometer is connected to the ADC0 pin ie PINA0 and the output will be obtained from Port C .and the output is connected to a 4511 ic which is BCD to seven segment converter to observe the digital output on seven segment display (ie to drive the seven segment display)
Now write the code for the circuit .
//Program for ADC to read from channel 0 and show the 8 bit o/p on PORTB
To understand the code we have to understand the basic register that are use to convert analog signal into digital form inside the avr .(for more clear explanation refer data sheet of Atmega16 .)doc2569 inside the atmel website
download full verson .and refer page no.217
Well there are following register we have to consider ie we are interesting to set or reset the particular bit of there register to make operate it to give a desired output
- ADC Multiplexer Selection Register –ADMUX
- ADC control and status register A -ADCSRA
ADC Multiplexer Selection Register –ADMUX
Figure above shows ADMUX register
The bits that are highlighted are of interest to us. In any case, we will discuss all the bits one by one.
- Bits 7:6 – REFS1:0 – Reference Selection Bits– These bits are used to choose the reference voltage. The following combinations are used.
The ADC needs a reference voltage to work upon. For this we have a three pins AREF, AVCC and GND. We can supply our own reference voltage across AREF and GND. For this, choose the first option. Apart from this case, you can either connect a capacitor across AREF pin and ground it to prevent from noise, or you may choose to leave it unconnected. If you want to use the VCC (+5V), choose the second option. Or else, choose the last option for internal Vref.
Let’s choose the second option for Vcc = 5V.
- Bit 5 – ADLAR – ADC Left Adjust Result– Make it ‘1’ to Left Adjust the ADC Result. We will discuss about this a bit later.
- Bits 4:0 – MUX4:0 – Analog Channel and Gain Selection Bits– There are 8 ADC channels (PA0…PA7). Which one do we choose? Choose any one! It doesn’t matter. How to choose? You can choose it by setting these bits. Since there are 5 bits, it consists of 2^5 = 32 different conditions as follows. However, we are concerned only with the first 8 conditions. Initially, all the bits are set to zero.
Thus, to initialize ADMUX, we write
ADMUX = (1<<REFS0);
ADCSRA – ADC Control and Status Register A
The ADCSRA register is as follows.
- Bit 7 – ADEN – ADC Enable– As the name says, it enables the ADC feature. Unless this is enabled, ADC operations cannot take place across PORTA i.e. PORTA will behave as GPIO pins.
- Bit 6 – ADSC – ADC Start Conversion– Write this to ‘1’ before starting any conversion. This 1 is written as long as the conversion is in progress, after which it returns to zero. Normally it takes 13 ADC clock pulses for this operation. But when you call it for the first time, it takes 25 as it performs the initialization together with it.
- Bit 5 – ADATE – ADC Auto Trigger Enable– Setting it to ‘1’ enables auto-triggering of ADC. ADC is triggered automatically at every rising edge of clock pulse. View the SFIOR register for more details.
- Bit 4 – ADIF – ADC Interrupt Flag– Whenever a conversion is finished and the registers are updated, this bit is set to ‘1’ automatically. Thus, this is used to check whether the conversion is complete or not.
- Bit 3 – ADIE – ADC Interrupt Enable– When this bit is set to ‘1’, the ADC interrupt is enabled. This is used in the case of interrupt-driven ADC.
- Bits 2:0 – ADPS2:0 – ADC Prescaler Select Bits– The prescaler (division factor between XTAL frequency and the ADC clock frequency) is determined by selecting the proper combination from the following.
Assuming XTAL frequency of 16MHz and the frequency range of 50kHz-200kHz, we choose a prescaler of 128.
Thus, F_ADC = 16M/128 = 125kHz.
Thus, we initialize ADCSRA as follows.
// Enable ADC and set Prescaler division factor as 128
ADCL and ADCH – ADC Data Registers
The result of the ADC conversion is stored here. Since the ADC has a resolution of 10 bits, it requires 10 bits to store the result. Hence one single 8 bit register is not sufficient. We need two registers – ADCL and ADCH (ADC Low byte and ADC High byte) as follows. The two can be called together as ADC.
ADC Data Registers (ADLAR = 0)
ADC Data Registers (ADLAR = 1)
You can very well see the the effect of ADLAR bit (in ADMUX register). Upon setting ADLAR = 1, the conversion result is left adjusted.
Ok now believe me this much knowledge about register will be enough to understand the conversion process of the ADC .
Explanation of the code
First we have declared three function two of them take integer as an argument and one is void.
But one of them return unsigned integer value lets check who are they J
void digital_value(int );
unsigned int ADC_read(unsigned int );
among these three functions first function void digital_value(int );
is declared to send the actual digital data to the desired port (here PORTC ) . second function void ADC_initialization(void); is declared to initialize the ADC inside the AVR ie to set or reset some particular bit of some register to initialize the ADC process .the third function is declared to convert the analog data into digital value and then it returns a digital data of the analog value .
secondly we should define the function what we have declared above .lets one by one explain them.
Inside this function REFS0 bit of ADMUX register is set so that we can use the AVcc with external capacitor at AREF pin of the microcontroller for reference purpose .
And ADCSRA registers ADEN bit is set to enable the ADC ,ADPS2,ADPS1,ADPS0 bits are set so that we can use prescalar of 128 (later on we will discuss about the prescalar in timer and counter chapter ) .
This function is created for the actual conversion . it takes channel number as an argument , start conversion by setting ADSC bit of ADCSRA register
Wait for ADC interrupt flag (we will discuss about interrupt later ).
And the converted digital value is returned where it is called .
Based on the converted value of the ADC_read() function the , the converted digital value is passed into this function and according to the value the desired work is done . Here I have quantized all the digital value and mapped between 0 and 9 which is displayed on seven segment display using a display driver /decoder . You can modify it and make it happened differently .
That’s all for analog to digital conversion .