ATmega 328P Interrupts Explained
ATmega 328P Interrupts Explained
Microcontroller
2. ATmega 328P
Interrupts -PCINT
1
Interrupts:-----
Interrupts are signals that tell the microcontroller to stop
what it is doing and execute a specific function, called
an interrupt service routine (ISR). Interrupts can be
triggered by various sources, such as buttons, sensors,
serial communication, or other peripherals.
2
Types of Interrupts in AVR
•External Interrupt.
•Timer Interrupt.
•USART Receive and Transmit Interrupt.
•EEPROM Ready Interrupt.
•ADC conversion complete.
Repeatedly reading a data port and testing the input data value.
FY - Department of Engineering, Sciences and Humanities
5
Interrupts:-----
Interrupt is a signal generated by hardware or software that triggers the interrupt service
Vishwakarma Institute of Technology
routine-ISR to run.
FY - Department of Engineering, Sciences and Humanities
Interrupts
o Whenever any device needs its service, the device notifies the
microcontroller by sending it an interrupt signal
o Upon receiving an interrupt signal, the microcontroller interrupts whatever it
is doing and serves the device
o The program which is associated with the interrupt is called the interrupt
service routine (ISR) or interrupt handler
o For every interrupt, there must be an interrupt service routine (ISR), or
interrupt handler
7
Advantages of interrupts include:
•Efficiency: Systems that employ interrupts are generally more efficient as the CPU
can focus on other tasks while waiting for external events, rather than constantly
polling for them.
•Reduced Latency: Interrupt-driven communication can reduce latency. Devices can
be serviced immediately when they have data to transfer, leading to faster
response times.
•Real-time Responsiveness: Interrupts enable immediate responses to external
events, making them essential for real-time applications where timely reactions are
critical.
Disadvantages of interrupts include:
•Prioritization: Interrupts can be prioritized, allowing the system to respond to the
•Complexity: Implementing and managing interrupts can be complex, involving
most important events first, ensuring essential tasks are handled promptly.
intricate synchronization and handling mechanisms, especially in multi-device
environments.
•Task Interference: In some cases, interrupts can restrict the simultaneous
execution of tasks as the CPU's attention is frequently diverted to handle
interrupt requests.
8
Interrupts:-----
The types of interrupts in AVR
Vishwakarma Institute of Technology
FY - Department of Engineering, Sciences and Humanities
Simple tasks for using an interrupt include --(To detect pin changes )
• Reading a rotary encoder
• Read a sound sensor that is trying to catch a sound of click
• Read an infrared sensor (photo-interrupter)
• Read key press in 4 key keypad
• Read key press in 16 key keypad etc
In all of these situations, using an interrupt can free the microcontroller to get
some other work done and still not missing the input.
10
PCINT - Pin Change Interrupt
Vishwakarma Institute of Technology
The names and locations of each of the pin change interrupt pins.
FY - Department of Engineering, Sciences and Humanities
11
Pin Change Interrupt Registers
In Atmega 328 there are three special registers associated with PCINT
12
Pin Change Interrupt Control
Register
To enable pin change interrupt on a pin, we need to manipulate the PCICR register
Vishwakarma Institute of Technology
FY - Department of Engineering, Sciences and Humanities
Group control Bit PCINT group pins Pin numbers Pin numbers
Set – Enable group On ATmega 328 On Arduino board
Clear– Disable group
PCIE0 PCINT0 to PCINT7 Port B0 to B7 6 pins -- pin Digital pins 8 to13
(8 pins) 8 pins mapping excludes crystal pins
13
Pin Change Interrupt Flag Register
When interrupt is triggered by a pin, then corresponding group flag bits will be
Vishwakarma Institute of Technology
Once the PCINT group is activated in PCICR, next step is to select which pins of that
group can trigger the interrupt.
14
To enable specific pin(s) within a group, PCMSK –(Pin Change Mask register) is used
Three PCMSK registers for each group. Each bit in the PCMSK register corresponds to a PCINT pin
Vishwakarma Institute of Technology
FY - Department of Engineering, Sciences and Humanities
15
PCINT - Pin Change Interrupt
Vishwakarma Institute of Technology
1 INT refers to the dedicated PCINT refers to the interrupts that can be
hardware interrupt pins generated by almost any of the I/O pins.
PCINT0-PCINT23
2 Each interrupt has a dedicated Group of pins share the same PCINT vector
Vector address. This ROM address.
address is not shared within There are 3 PCINT groups.
interrupts.
3 The INT pin is linked to a Programmer needs to determine which pin
dedicated interrupt vector so change causes the interrupt within the ISR
you always know which pin before acting on it.
caused the interrupt when in the
ISR.
16
Pin Change Interrupt –ISR Vectors
What is the interrupt vector?
When the interrupt is triggered, the main code is paused and control jumps to the interrupt
Vishwakarma Institute of Technology
vector.
FY - Department of Engineering, Sciences and Humanities
The code for that ISR is executed. The ISR is located at vector address in ROM.
Each of the 3 groups of pins has a ISR or Interrupt Service Routines at three possible vectors.
There are three vectors, PCINTx_vect for pins from group x. (x is 0, 1, 2 respectively)
• ISR (PCINT0_vect) // for pin group Port D
• ISR (PCINT1_vect) // for pin group Port C
• ISR (PCINT2_vect) // for pin group Port B
Example syntax-----
ISR (PCINT2_vect)
{ // For PCINT of pins --, code here
}
17
Interrupt Vector table
Vishwakarma Institute of Technology
Vector Program
FY - Department of Engineering, Sciences and Humanities
18
Interrupt Vector table
Vishwakarma Institute of Technology
Vector Program
Interrupt definition Vector name
FY - Department of Engineering, Sciences and Humanities
Number Address
9 0x0010 Timer/Counter2 Compare Match B TIMER2_COMPB_vect
19
Interrupt Vector table
Vishwakarma Institute of Technology
Vector Program
FY - Department of Engineering, Sciences and Humanities
20