Exp8 ADC in ATMEGA32
Exp8 ADC in ATMEGA32
(ATMEGA32)
Submitted by
Ronit Dutta, MS in IOT and Signal Processing
Department of Electrical Engineering, IIT Kharagpur
Introduction to ADC
Analog-to-digital converters are among the most widely used devices for data
acquisition. Digital computers use binary (discrete) values, but in the physical world
everything is analog (continuous).
Temperature, pressure (wind or liquid), humidity and velocity are a few examples of
physical quantities that we deal with every day. A physical quantity is converted to
electrical (voltage, current) signals using a device called a transducer. Transducers are
also referred to as sensors. Sensors for temperature, velocity, pressure, light, and
many other natural quantities produce an output that is voltage (or current).
Therefore, we need an analog-to-digital converter to translate the analog signals to
digital numbers so that the microcontroller can read and process them.
2
Embedded System on AVR ATMEGA32:Exp8
The above figure is the ideal conversion with step size for ADC. Here, Vref=FS
and resolution is FS/(2n-1). For 3-bit ADC, the ideal resolution is FS/7. We all
know that we cannot apply analog voltage more than reference voltage of the
ADC. So, here if we apply exactly FS as analog voltage then only we can achieve
111 as digital conversion bits.
To overcome this situation, the resolution of n-bit ADC is defined as Vref/2n.
The below figure represents the practical 3-bit ADC resolution with
corresponding analog voltage with converted digital bits.
The ADC has n-bit resolution, where n can be 8, 10, 12, 16, or even 24 bits.
Higher-resolution ADCs provide a smaller step size, where step size is the
smallest change that can be discerned by an ADC. Some widely used
resolutions for ADCs are shown in below Table.
3
Embedded System on AVR ATMEGA32:Exp8
IV. Digital Data Output: In an 8-bit ADC we have an 8-bit digital data output of
D0–D7, while in the 10-bit ADC the data output is D0–D9. To calculate the
output voltage, we use the following formula:
Where Dout= digital data output (in decimal), Vin= analog input voltage, and
step size (resolution) is the smallest change, which is Vref/1024 for an 10-bit
ADC.
ADC Programming in the ATMEGA32
Because the ADC is widely used in data acquisition, in recent years an increasing
number of microcontrollers have had an on-chip ADC peripheral, just like timers and
USART. An on-chip ADC eliminates the need for an external ADC connection, which
leaves more pins for other I/O activities. The vast majority of the AVR chips come with
ADC. In this section we discuss the ADC feature of the ATmega32 and show how it is
programmed in Assembly.
ATmega32 ADC features:
The ADC peripheral of the ATmega32 has the following characteristics:
a. It is a 10-bit ADC.
4
Embedded System on AVR ATMEGA32:Exp8
5
Embedded System on AVR ATMEGA32:Exp8
Above figure shows the block diagram of internal circuitry of Vref selection. As
we can see that we have three options: (a) AREF pin, (b) AVCC pin, or (c)
internal 2.56V. The below table shows how REFS1 and REFS0 bits of the
ADMUX register can be used to select the Vref source.
6
Embedded System on AVR ATMEGA32:Exp8
The ADCSRA register is the status and control register of ADC. Bits of this
register control or monitor the operation of the ADC.
ADEN (Bit 7): ADC Enable
This bit enables or disables the ADC. Setting this bit to one will enable the ADC,
and clearing this bit to zero will disable it even while a conversion is in progress.
7
Embedded System on AVR ATMEGA32:Exp8
A timing factor that we should know about is the acquisition time. After an ADC
channel is selected, the ADC allows some time for the sample-and-hold
capacitor (C hold) to charge fully to the input voltage level present at the
channel.
In the AVR, the first conversion takes 25 ADC clock cycles in order to initialize
the analog circuitry and pass the sample-and-hold time. Then each consecutive
conversion takes 13 ADC clock cycles.
Below table lists the conversion times for some different conditions. Notice
that sample-and-hold time is the first part of each conversion.
8
Embedded System on AVR ATMEGA32:Exp8
As we mentioned before, the ADLAR bit of the ADMUX is used for making it
right-justified or left-justified because we need only 10 of the 16 bits.
Reserved Bit (Bit4): This bit is reserved for future use in the ATmega32. For
ensuring compatibility with future devices, this bit must be written zero when
SFIOR is written.
The remaining SFIOR bits are not used in ADC experiment.
Since the General Purpose GPIO is covered, so to display the digital conversion the LED bar
graph is used or 10 LEDs can be used instead of the LED bar graph.
How to know which sides pins are anode and cathode respectively?
9
Embedded System on AVR ATMEGA32:Exp8
.INCLUDE "M32DEF.INC"
.ORG 0x0000
//stack declaration
LDI R16,HIGH(RAMEND)
OUT SPH,R16
LDI R16,LOW(RAMEND)
OUT SPL,R16
// ADC Initialization
LDI R16,0x00
OUT ADMUX,R16
LDI R16,0xC3
OUT ADCSRA,R16
10
Embedded System on AVR ATMEGA32:Exp8
Verify the digital conversion with corresponding analog voltage by hand calculation.
Vref=5Volt, ADC Bits=10bit, Analog Applied Voltage=1.835Volt
ADC Value= (1024/5)*1.835=375.808
Since the ADC follows the floor converted digital value. Hence,
ADC Value= int(375.808)=375
The corresponding 10-bit binary value is 0101110111
Note: The resolution of te ADC is 4.88mV. To show this minimum analog input change the
4-decimal Voltmeter will be required. Since here 3-decimal voltmeter is present so we
cannot verify the LSB value of the analog input. After hand conversion there may be 1-bit
error due to this.
Class Assignment 1: Write Assembly Code for Analog to Digital conversion with Left Justified
and display the digital conversion on LED bar graph as shown in the below figure. Verify the
digital conversion with corresponding analog voltage by hand calculation.
After simulation, make the above two circuits on Hardware and verify these.
11
Embedded System on AVR ATMEGA32:Exp8
After learning LCD and UART, you can verify the ADC conversion through these.
Experiment2: /* Write a program for the AVR for ADC and transfer the ADC Value
serially through UART at 9600 baud, continuously. Oscillator Frequency=8MHz, U2X=0,
StopBit=1, No Parity Bit. The data will be printed on realterm horizontally. */
.INCLUDE "M32DEF.INC"
.ORG 0x0000
LDI R16,HIGH(RAMEND)
OUT SPH,R16
LDI R16,LOW(RAMEND)
OUT SPL,R16
// ADC Initialization
LDI R16,0x00
OUT ADMUX,R16
LDI R16,0xC3
OUT ADCSRA,R16
//UART Initialization
//UART Initialization
LDI R16,0x08// Enabling Tx Rx
OUT UCSRB,R16
LDI R17,0x86// 8 bit data mode
OUT UCSRC,R17
//9600bps Baud-Rate Settings for 8MHz Oscillator
LDI R16,51
OUT UBRRL,R16
12
Embedded System on AVR ATMEGA32:Exp8
Make the above hardware and UART hardware connection to display the result on realterm
as shown in below figure. Set the display as unsigned int16 and verify the 10-bit ADC value
ranging from 0 to 1023.
13
Embedded System on AVR ATMEGA32:Exp8
Experiment3: /* Write a program for the AVR for ADC and transfer the ADC Value
serially through UART at 9600 baud, continuously. Oscillator Frequency=8MHz, U2X=0,
StopBit=1, No Parity Bit. The data will be printed on realterm vertically i.e. new
line program. */
.INCLUDE "M32DEF.INC"
.ORG 0x0000
LDI R16,HIGH(RAMEND)
OUT SPH,R16
LDI R16,LOW(RAMEND)
OUT SPL,R16
// ADC Initialization
LDI R16,0x00
OUT ADMUX,R16
LDI R16,0xC3
OUT ADCSRA,R16
//UART Initialization
//UART Initialization
LDI R16,0x08// Enabling Tx Rx
OUT UCSRB,R16
LDI R17,0x86// 8 bit data mode
OUT UCSRC,R17
//9600bps Baud-Rate Settings for 8MHz Oscillator
LDI R16,51
OUT UBRRL,R16
14
Embedded System on AVR ATMEGA32:Exp8
Set the display as unsigned int16, then select new line mode and select binary sync characters
to verify the 10-bit ADC value ranging from 0 to 1023 vertically.
.INCLUDE "M32DEF.INC"
.ORG 0x0000
LDI R16,HIGH(RAMEND)
OUT SPH,R16
LDI R16,LOW(RAMEND)
OUT SPL,R16
// LCD Initialization
CBI PORTD,PIND5 // Command Register Enable
LDI R16,0x38 //2 lines and 5x7 matrix
OUT PORTB,R16
CALL ENABLE
LDI R16,0x02 // Return Home
OUT PORTB,R16
CALL ENABLE
LDI R16,0x01 // Clear display screen
OUT PORTB,R16
CALL ENABLE
LDI R16,0x0C //Display on, cursor off
OUT PORTB,R16
CALL ENABLE
LDI R16,0x06 // Shift Cursor to right after print on LCD
OUT PORTB,R16
CALL ENABLE
15
Embedded System on AVR ATMEGA32:Exp8
//ADC Initialization
LDI R16,0x00
OUT ADMUX,R16
LDI R16,0xC3
OUT ADCSRA,R16
LDI R20,0
LOOP_Digit1: INC R20
SBIW R25:R24,10
BRPL LOOP_Digit1
DEC R20
ADIW R25:R24,10
16
Embedded System on AVR ATMEGA32:Exp8
CALL ENABLE
OUT PORTB,R19
CALL ENABLE
OUT PORTB,R20
CALL ENABLE
OUT PORTB,R24
CALL ENABLE
//Wait for few times
CALL DELAY
JMP Infinite_Loop
Make the below circuit and simulate it then make the hardware to realize practically.
17