Engineers Garage

  • Electronics Projects and Tutorials
    • Electronic Projects
      • Arduino Projects
      • AVR
      • Raspberry pi
      • ESP8266
      • BeagleBone
      • 8051 Microcontroller
      • ARM
      • PIC Microcontroller
      • STM32
    • Tutorials
      • Audio Electronics
      • Battery Management
      • Brainwave
      • Electric Vehicles
      • EMI/EMC/RFI
      • Hardware Filters
      • IoT tutorials
      • Power Tutorials
      • Python
      • Sensors
      • USB
      • VHDL
    • Circuit Design
    • Project Videos
    • Components
  • Articles
    • Tech Articles
    • Insight
    • Invention Stories
    • How to
    • What Is
  • News
    • Electronic Product News
    • Business News
    • Company/Start-up News
    • DIY Reviews
    • Guest Post
  • Forums
    • EDABoard.com
    • Electro-Tech-Online
    • EG Forum Archive
  • DigiKey Store
    • Cables, Wires
    • Connectors, Interconnect
    • Discrete
    • Electromechanical
    • Embedded Computers
    • Enclosures, Hardware, Office
    • Integrated Circuits (ICs)
    • Isolators
    • LED/Optoelectronics
    • Passive
    • Power, Circuit Protection
    • Programmers
    • RF, Wireless
    • Semiconductors
    • Sensors, Transducers
    • Test Products
    • Tools
  • Learn
    • eBooks/Tech Tips
    • Design Guides
    • Learning Center
    • Tech Toolboxes
    • Webinars & Digital Events
  • Resources
    • Digital Issues
    • EE Training Days
    • LEAP Awards
    • Podcasts
    • Webinars / Digital Events
    • White Papers
    • Engineering Diversity & Inclusion
    • DesignFast
  • Guest Post Guidelines
  • Advertise
  • Subscribe

How to use internal ADC of AVR microcontroller using interrupts- (Part 28/46)

By Ashutosh Bhatt July 5, 2010

This article is in continuation to AVR interrupts. There are two types of interrupts external and internal in AVR microcontroller. The aforesaid article covers external interrupts. AVR microcontrollers have seventeen internal interrupts. These internal interrupts are generated by the internal peripherals of Microcontroller like Timer, ADC etc. The internal interrupts are used for efficient operation of the internal peripherals. This article explains the internal interrupts using the example of an ADC interrupt.

 


 

Each internal peripheral system consists of one IE (interrupt Enable) bit which activates the internal interrupts of that peripheral. For example, in-built ADC of AVR consists of ADIE (ADC interrupt Enable) bit in ADCSRA register.
In addition, the I-bit of SREG is also activated to activate interrupts. SREG is a status register of AVR microcontroller which contains information about the result of most recently executed arithmetic instructions.
 
SREG (Status Register):
 
Bit Configuration of SREG Register of AVR microcontroller to activate interrupts
 
Fig. 2: Bit Configuration of SREG Register of AVR microcontroller to activate interrupts
 
Bit 7-I: (Global interrupt Enable):
To activate Global Interrupts this bit must be set to high. If this bit is not enabled, none of the interrupts will work. “sei ()” command is used to enable the Global Interrupt, and “cli()” command is used to disable global interrupt.
 
For better clarification of internal interrupts, ADC interrupts is explained below:
 
ADC interrupts:
In the article of AVR ADC, polling method is used to receive converted value. During the polling of a signal, microcontroller cannot perform another task. Hence, it is better to use interrupt method. ADC system consists of ADIE bit in ADCSRA register. ADIE bit is enabled to use ADC interrupts.
 
ADC in auto-triggering mode:
In A/D conversion ADSC bit remains high till the conversion is not completed. As soon as the, conversion gets completed, ADSC automatically gets cleared by hardware. Before starting the next conversion, ADSC must be set high again. Alternatively, auto triggering can be used to enable the ADSC bit after each conversion. The ADATE (ADC Auto Triggering Enable) bit in ADCSRA register is used to activate auto-triggering mode. There are various triggering options available in AVR ADC, which can be selected by configuring ADTS (ADC Triggering Select) bits in SFIOR register.
 
SFIOR (Special Function I/O register):
 
Bt Configuration of ADTS in SFIOR register in AVR
 
Fig. 3: Bit Configuration of ADTS in SFIOR register in AVR
 
The following table shows the combination of ADTS [2:0] bits to select triggering source:

Bit combination of ADTS [2:0] to select triggering source

Fig. 4: Bit combination of ADTS [2:0] to select triggering source
 
Objective: A/D conversion using interrupt method and display 10-bit digital output on LCD as integer form.
 
Programming steps:
To initiate ADC:
1. Set the value in ADMUX register according to the ADC channel and the reference voltage.
2. Set the Prescaler bits accordingly in ADCSRA register.
3. Set the ADEN bit to enable the ADC.
4. Set ADIE bit to enable ADC interrupt.
5. Set ADATE bit to enable auto triggering.
6. Set ADSC bit to start conversion.
7. Configure SFIOR register to select trigger source. 
 

void adc_init()// ADC configuration
{
ADMUX=(1<<REFS0);//Vref=AVcc
// ADSC=1 ADC Enable
// ADPS[2:0]=111, prescaler=128
// ADIE=1, ADC interrupt Enable
//ADATE=1, ADC Auto Triggering Enable
ADCSRA=(1<<ADEN)|(7<<ADPS0)|(1<<ADSC)|(1<<ADIE)|(1<<ADATE);
 
//ADTS[2:0]= 100 , Timer0 overflow select as trigger source
SFIOR=(4<<ADTS0);
}

 

To read converted value:
1. Start Trigger Source for auto-triggering of ADC.  
2. Enable I-bit of SREG register to enable global Interrupt.
3. Store 10-bit converted data into 16-bit variable.
4. Convert the digital value to its corresponding ASCII value.
5. Display the converted value on LCD.

 

Project Source Code

###


//Program to use internal ADC of AVR microcontroller using interrupts
#include<avr/io.h>
#include<util/delay.h>
#include<avr/interrupt.h>
 
#define lcd PORTB
#define rs PD0
#define rw PD1
#define en PD2
 
void adc_init(void);
unsigned int adc_read(void);
void adc_conversion(uint16_t);
void lcd_init(void);
void lcdcmd(unsigned char);
void lcddata(unsigned char);
 
uint16_t adc_out;
 
int main()
{
unsigned char data[12]= "ADC OUTPUT:";
 
int i=0;
DDRD=0x07;
DDRB=0xFF;
DDRC=0xFF;
lcd_init();
 
while(data[i]!='')
{
lcddata(data[i]);
_delay_ms(5);
i++;
}
 
adc_init();
 
/* Configure timer0*/
 
TCCR0=(5<<CS00);
TIMSK=(1<<TOIE0);
 
sei();
while(1);
 
}
 
void adc_init() // ADC configuration
{
ADMUX=(1<<REFS0); //Vref=AVcc
// ADSC=1 ADC Enable
// ADPS[2:0]=111, prescaler=128
// ADIE=1, ADC interrupt Enable
//ADATE=1, ADC Auto Triggering Enable
ADCSRA=(1<<ADEN)|(7<<ADPS0)|(1<<ADSC)|(1<<ADIE)|(1<<ADATE);
 
//ADTS[2:0]= 100 , Timer0 overflow select as trigger source
SFIOR=(4<<ADTS0);
}
 
ISR(ADC_vect)
{
adc_conversion((ADC));
 
}
 
ISR(TIMER0_OVF_vect)
{
 
}
 
 
/* this function is written to convert interger value to their corresponding ASCII value*/
void adc_conversion(uint16_t adc_out)  
{
unsigned int adc_out1;
int i=0;
char position=0xC3;
 
for(i=0;i<=3;i++)
{
adc_out1=adc_out%10;
adc_out=adc_out/10;
lcdcmd(position);
lcddata(48+adc_out1);
position--;
}
}
 
void lcd_init() // fuction for LCD initialization
{
lcdcmd(0x38);
lcdcmd(0x0C);
lcdcmd(0x01);
lcdcmd(0x06);
lcdcmd(0x80);
}
 
void lcdcmd(unsigned char cmdout)
{
lcd=cmdout;
PORTD=(0<<rs)|(0<<rw)|(1<<en);
_delay_ms(10);
PORTD=(0<<rs)|(0<<rw)|(0<<en);
 
}
 
 
void lcddata(unsigned char dataout)
{
lcd=dataout;
PORTD=(1<<rs)|(0<<rw)|(1<<en);
_delay_ms(10);
PORTD=(1<<rs)|(0<<rw)|(0<<en);
}
 
 

###

 


Circuit Diagrams

Circuit-Diagram-of-How-to-use-internal-ADC-of-AVR-microcontroller-using-interrupts

Project Components

  • ATmega16
  • LCD

Project Video


Filed Under: AVR
Tagged With: adc, atmega16, avr, interrupt, microcontroller
 

Next Article

← Previous Article
Next Article →

Questions related to this article?
👉Ask and discuss on Electro-Tech-Online.com and EDAboard.com forums.



Tell Us What You Think!! Cancel reply

You must be logged in to post a comment.

EE TECH TOOLBOX

“ee
Tech Toolbox: Internet of Things
Explore practical strategies for minimizing attack surfaces, managing memory efficiently, and securing firmware. Download now to ensure your IoT implementations remain secure, efficient, and future-ready.

EE Learning Center

EE Learning Center
“engineers
EXPAND YOUR KNOWLEDGE AND STAY CONNECTED
Get the latest info on technologies, tools and strategies for EE professionals.

HAVE A QUESTION?

Have a technical question about an article or other engineering questions? Check out our engineering forums EDABoard.com and Electro-Tech-Online.com where you can get those questions asked and answered by your peers!


RSS EDABOARD.com Discussions

  • Grounding
  • FSM for an UART TX
  • Audio Switching
  • Transistor circuit in audio
  • Parametric Analysis in Cadence Virtuoso IC23.1

RSS Electro-Tech-Online.com Discussions

  • stud mount Schottky diodes
  • LED circuit for 1/6 scale diorama
  • Can I use this charger in every country?
  • Electronic board faulty?!?
  • using a RTC in SF basic

Featured – Designing of Audio Amplifiers part 9 series

  • Basics of Audio Amplifier – 1/9
  • Designing 250 Milli Watt Audio Power Amplifier – 2/9
  • Designing 1 Watt Audio Power Amplifier – 3/9
  • Designing a Bass Boost Amplifier – 4/9
  • Designing a 6 Watt Car Audio Amplifier – 5/9
  • Design a low power amplifier for headphones- 6/9

Recent Articles

  • Fischer connector system adds ratchet locking system designed for 300g shock resistance
  • Littelfuse introduces tactile switch with enhanced bracket peg design for mounting strength
  • Infineon releases GaN switch with monolithic bidirectional design
  • Sienna Semiconductor data converters feature sample rates from 20 to 250 Msps
  • Delta’s 5,500 W power supplies achieve 97.5% energy efficiency for AI servers

EE ENGINEERING TRAINING DAYS

engineering

Submit a Guest Post

submit a guest post
Engineers Garage
  • Analog IC TIps
  • Connector Tips
  • Battery Power Tips
  • DesignFast
  • EDABoard Forums
  • EE World Online
  • Electro-Tech-Online Forums
  • EV Engineering
  • Microcontroller Tips
  • Power Electronic Tips
  • Sensor Tips
  • Test and Measurement Tips
  • 5G Technology World
  • Subscribe to our newsletter
  • About Us
  • Contact Us
  • Advertise

Copyright © 2025 WTWH Media LLC. All Rights Reserved. The material on this site may not be reproduced, distributed, transmitted, cached or otherwise used, except with the prior written permission of WTWH Media
Privacy Policy

Search Engineers Garage

  • Electronics Projects and Tutorials
    • Electronic Projects
      • Arduino Projects
      • AVR
      • Raspberry pi
      • ESP8266
      • BeagleBone
      • 8051 Microcontroller
      • ARM
      • PIC Microcontroller
      • STM32
    • Tutorials
      • Audio Electronics
      • Battery Management
      • Brainwave
      • Electric Vehicles
      • EMI/EMC/RFI
      • Hardware Filters
      • IoT tutorials
      • Power Tutorials
      • Python
      • Sensors
      • USB
      • VHDL
    • Circuit Design
    • Project Videos
    • Components
  • Articles
    • Tech Articles
    • Insight
    • Invention Stories
    • How to
    • What Is
  • News
    • Electronic Product News
    • Business News
    • Company/Start-up News
    • DIY Reviews
    • Guest Post
  • Forums
    • EDABoard.com
    • Electro-Tech-Online
    • EG Forum Archive
  • DigiKey Store
    • Cables, Wires
    • Connectors, Interconnect
    • Discrete
    • Electromechanical
    • Embedded Computers
    • Enclosures, Hardware, Office
    • Integrated Circuits (ICs)
    • Isolators
    • LED/Optoelectronics
    • Passive
    • Power, Circuit Protection
    • Programmers
    • RF, Wireless
    • Semiconductors
    • Sensors, Transducers
    • Test Products
    • Tools
  • Learn
    • eBooks/Tech Tips
    • Design Guides
    • Learning Center
    • Tech Toolboxes
    • Webinars & Digital Events
  • Resources
    • Digital Issues
    • EE Training Days
    • LEAP Awards
    • Podcasts
    • Webinars / Digital Events
    • White Papers
    • Engineering Diversity & Inclusion
    • DesignFast
  • Guest Post Guidelines
  • Advertise
  • Subscribe