0% found this document useful (0 votes)
13 views29 pages

ADC Programming for ATMega32

The document provides an overview of the Analog to Digital Converter (ADC) in the ATMega32 microcontroller, detailing its specifications, programming, and usage. It explains the ADC's 10-bit resolution, multiple input channels, and necessary registers for operation, along with example code for reading analog inputs and interfacing with devices like temperature sensors. Additionally, it outlines tasks for using the ADC to control DC motor speed and create a voltmeter that measures up to +5 V.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views29 pages

ADC Programming for ATMega32

The document provides an overview of the Analog to Digital Converter (ADC) in the ATMega32 microcontroller, detailing its specifications, programming, and usage. It explains the ADC's 10-bit resolution, multiple input channels, and necessary registers for operation, along with example code for reading analog inputs and interfacing with devices like temperature sensors. Additionally, it outlines tasks for using the ADC to control DC motor speed and create a voltmeter that measures up to +5 V.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd

ADC(Analog to Digital Converter) of ATMega32

ADC Programming with ATmega32


ADC(Analog to Digital Converter) of ATMega32

ATMega32 uses a successive approximation ADC type a


device analog inputs. It's 10-bit wise and has up to 8 input
channels. In addition, it can handle up to 16 differential
voltage input combinations.

Eight analog channels are multiplexed with PortA digital


pins. By default PortA is digital I/O. AVCC is supply
voltage for ADC module. AREF pin is a positive analog
voltage reference. But the voltage reference could be
programmed to internally connected to a 2.56 V reference
voltage.

The ADC clock source is driven from the MCU clock with
the division factor from 2 to 128. For the 10-bit ADC
reading result, the clock frequency work 50 kHz to 200
kHz. But only an 8-bit result is needed the clock frequency
could take higher than 200 kHz.
ADC Registers
Basically, to use ADC we have to deal with some registers.
•ADMUX

• Reference selection register REFS[1:0], used for selecting the reference voltage. By default it's AREF pin. For
more reference voltage selections, check the device datasheet.

• ADC left adjust result ADLR, used for adjusting the 10-bit ADC result between right and left. By default it's right
justified ('0').

• Analog channel and gain selection bits MUX[3:0], used for selecting between any single ended input,
differential input and gain setting. By default, it's single ended ADC0 input channel.
•ADCSRA

• ADC enable ADEN, turn on the ADC module when it's set to '1'.
• ADC start conversion ADSC, start the ADC conversion when it's set to '1'. This bit is also used for testing the
progress of the conversion. When it's read '0', the conversion is completed.
• ADC auto trigger enable ADATE, make the ADC conversion automatically start by any options. ADC interrupt flag
ADIF, is set to '1' when the ADC conversion is completed.
• ADC interrupt enable ADIE, when set to '1' the interrupt for ADC is enabled.
• ADC prescaler select bits ADPS[2:0], used for select the clock source for ADC. it's driven from MCU clock with
the scale factor between 2 to 128. By default it has a division factor of 2.
•ADCL and ADCH

The data register is a 10-bit pair of two 8-bit registers ADCL and ADCH. It's readable and writable. By default
ADLAR = '0', the result is right-justified.
Using the ADC
Reading the analog input from ADC0 with example.
In this simple introduction of using the ADC, I use ADC0 an analog input channel with single end mode.
The ADC clock is driven from the MCU clock with the division of 2. PortC displays the ADCH while
PortD displays the ADCL result.
Source code:
#include <avr/io.h>
int main(void)
{
//PortC as output
DDRC=0xFF;
//PortD as output
DDRD=0xFF;
//PA0 or ADC0 as an analog input
DDRA=0;
//Turn on the ADC module
ADCSRA|=(1<<ADEN);
while (1)
{
//Start the conversion
ADCSRA|=(1<<ADSC);
//Wait for the completion
while((ADCSRA&(1<<ADSC))==1);
//Read the result
PORTD=ADCL;
PORTC=ADCH;
}
}
Using ADC of ATMega32 to adjust PWM duty cycle

In this ADC module, we have an easy example of interfacing with device ADC. LM35 is analog temperature
sensor capable of measuring the temperature between -55 to 150 degree Celsius. It outputs an analog voltage
value that is 10 mV per degree Celsius proportionally.
In this example the system could only read the positive temperature value. Because I have some
difficulty of working with negative voltage reading at this time.
Source Code
while (1)
{
#include <avr/io.h> //Start the conversion
ADCSRA|=(1<<ADSC);
#define F_CPU 4000000UL //Wait for the completion
#include <util/delay.h> while((ADCSRA&(1<<ADSC))==1);
/*Convert ADC to voltage*/
int main(void) voltage=(ADCL+(ADCH<<8))*5.0/1024;
{ float voltage; /*Convert to temperature*/
temperature=voltage*100;
float temperature; /*make dot point*/
int _temperature; temperature*=10;
_temperature=(int)temperature;
char ssd[16]={0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D, /*Displaying the Data*/
0x07,0x7F,0x6F,0x77,0x7C,0x39,0x5E,0x79,0x71}; PORTD=0x00;
PORTC=ssd[_temperature/1000];
//PortC as output if(_temperature>=1000) PORTD=0x01;
DDRC=0xFF; _delay_ms(10);
PORTD=0x00;
//PortD as output PORTC=ssd[(_temperature%1000)/100];
DDRD=0xFF; PORTD=0x02;
_delay_ms(10);
//PA0 or ADC0 as an analog input PORTD=0x00;
DDRA=0; PORTC=ssd[(_temperature%100)/10]|0x80;
PORTD=0x04;
//Turn on the ADC module _delay_ms(10);
ADCSRA|=(1<<ADEN); PORTD=0x00;
PORTC=ssd[_temperature%10];
PORTD=0x08;
_delay_ms(10);
}
}
Making a simple +5 V voltmeter with ATMega32 ADC module
we have introduce about the ADC module of ATMega32. Actually the maximum reference voltage of the
ADC is +5 V in usual. From this concept we can measure an input analog voltage input to any ADC
channel at +5 V full scale reading.
The reading ADC value in the single ended conversion could be obtained from:
Task
Task-1
• Using ADC module of ATMega32 to adjust
DC motor speed

Task-2

• Making a simple +5 V voltmeter with ATMega32


ADC module

HINT:
use ADC0 to read an analog input voltage fed from a POT.
The POT varies an analog output voltage from 0 V to +5 V
DC. The MCU displays the reading result of the voltage
from 0.0 V to 5.0 V.

You might also like