0% found this document useful (0 votes)
10 views20 pages

ATmega 328P Interrupts Explained

Uploaded by

manojsardeshmukh
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views20 pages

ATmega 328P Interrupts Explained

Uploaded by

manojsardeshmukh
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Vishwakarma Institute of Technology

FY - Department of Engineering, Sciences and Humanities

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.

Methods to read/ monitor the


inputs
1. Polling
2. Interrupt
3
Interrupts:-----
Polling method:
Vishwakarma Institute of Technology

Repeatedly reading a data port and testing the input data value.
FY - Department of Engineering, Sciences and Humanities

The processor takes a measurement from a port/sensor at regular time intervals.


This happens, even if there has been no change in the connected device.
Guaranteed or predictable time slices are allocated to each device that is polled.
Polling is ...
•Simple to code.
• Not very efficient because the polling happens even if the sensor reading has not changed
Disadvantages of polling include:
•Resource Intensive: Constantly checking for data or events consumes
system resources, including CPU cycles, leading to inefficiency and
wasted power.
•Latency: There can be a delay between the occurrence of an event and
the system detecting it, especially if the polling interval is long, leading to
higher latency in event processing.
•Complexity in Real-Time Systems: In real-time applications where 4
Interrupts:-----
Vishwakarma Institute of Technology

Interrupts allow program to respond to events when they occur and


FY - Department of Engineering, Sciences and Humanities

allow program to ignore events until the occur

External events e.g.--


• UART ready with/for next character
• Signal change on pin
• Action depends on context
• level/edges arrived on pin

Internal events e.g.--


• Power failure
• Arithmetic exception
• Timer “tick”

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

• It only occurs when needed.


• Interrupts are more efficient than polling but the timing is less predictable.
• More difficult to code than polling.
When interrupt occurs 🡪
1. The original process stops/paused and the return-address is saved onto the stack.
2. The interrupt code ISR is executed.
3. The original process is re-started using the return-address retrieved from the stack.
Timer Interrupts
Most processors have built-in timers which can be used to trigger processes when the timer
interval ends. (Timer overflow)
6
A single microcontroller can serve several devices by two ways a) interrupt and b)
polling

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

We might use interrupts in following cases


1. To detect pin changes (eg. rotary encoders, button presses)
2. Watchdog timer (e.g. if nothing happens after 8 seconds, interrupt )
3. Timer interrupts – used for comparing/overflowing timers
4. SPI data transfers
The next section
5. I2C data transfers discusses pin change
6. USART data transfers type of interrupt i.e.
To detect pin changes
7. ADC conversions (analog to digital)
8. EEPROM ready for use
9. Flash memory ready
9
Interrupts:-----
Vishwakarma Institute of Technology

Interrupts are useful for making things happen automatically in microcontroller


FY - Department of Engineering, Sciences and Humanities

programs, and can help solve timing problems as in Polling method.

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

1. Reading the state of an input pin is


necessary in microcontroller
programs .
2. A specific input pin changing state
can be notified. This is done by
using Pin Change INTerrupts.
3. Each of the digital I/O pins can be
configured as PCINT by writing into
interrupt registers in software.

11
Pin Change Interrupt Registers
In Atmega 328 there are three special registers associated with PCINT

1. PCICR🡪 Pin Change Interrupt Control Register

2. PCIFR🡪 Pin Change Interrupt Flag Register

3. PCMSKx🡪 Pin Change Mask Register.


X- indicates 0, 1 and 2. These are PCINT groups 0, 1 and 2

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

PCIE2 PCIE1 PCIE0


Bit 7 Bit 6 Bit 5 Bit 4 Bit 3 Bit 2 Bit 1 Bit 0

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

PCIE1 PCINT8 to PCINT14 Port C0 to C6 6 pins --A0 to A5


(7 pins) 7 pins. mapping excludes Reset pin

PCIE2 PCINT16 to Port D0 to D7 8 pins – Digital pins 0 to 7


PCINT23 (8 pins) 8 pins Mapping all 8

13
Pin Change Interrupt Flag Register
When interrupt is triggered by a pin, then corresponding group flag bits will be
Vishwakarma Institute of Technology

set. These flag bits are found at register PCIFR:


FY - Department of Engineering, Sciences and Humanities

Bits in PCIFR are PCIF0, PCIF1 and PCIF2

PCIF2 PCIF1 PCIF0


Bit 7 Bit 6 Bit 5 Bit 4 Bit 3 Bit 2 Bit 1 Bit 0

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

PCMSK0 is used to enable each PCINT pins 0 to 7

PCMSK1 is used to enable each PCINT pins 8 to 14

PCMSK2 is used to enable each PCINT pins 16 to 23

15
PCINT - Pin Change Interrupt
Vishwakarma Institute of Technology

SN INT - Interrupt PCINT - Pin Change INTerrupt


FY - Department of Engineering, Sciences and Humanities

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.

ISR is a loop. The code to be executed is written in that loop.


The code is executed when interrupt from that group occurs and the interrupt flag is set.

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

Interrupt definition Vector name


Number Address
RESET External Pin, Power-on
1 0x0000 Reset, Brown-out Reset and RESET
Watchdog System Reset

2 0x0002 External Interrupt Request 0 INT0_vect

3 0x0004 External Interrupt Request 1 INT1_vect

4 0x0006 Pin Change Interrupt Request 0 PCINT0_vect

5 0x0008 Pin Change Interrupt Request 1 PCINT1_vect

6 0x000A Pin Change Interrupt Request 2 PCINT2_vect

7 0x000C Watchdog Time-out Interrupt WDT_vect

8 0x000E Timer/Counter2 Compare Match A TIMER2_COMPA_vect

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

10 0x0012 Timer/Counter2 Overflow TIMER2_OVF_vect

11 0x0014 Timer/Counter1 Capture Event TIMER1_CAPT_vect

12 0x0016 Timer/Counter1 Compare Match A TIMER1_COMPA_vect

13 0x0018 Timer/Counter1 Compare Match B TIMER1_COMPB_vect

14 0x001A Timer/Counter1 Overflow TIMER1_OVF_vect

15 0x001C Timer/Counter0 Compare Match A TIMER0_COMPA_vect

16 0x001E Timer/Counter0 Compare Match B TIMER0_COMPB_vect

17 0x0020 Timer/Counter0 Overflow TIMER0_OVF_vect

19
Interrupt Vector table
Vishwakarma Institute of Technology

Vector Program
FY - Department of Engineering, Sciences and Humanities

Interrupt definition Vector name


Number Address

18 0x0022 SPI Serial Transfer Complete SPI_STC_vect

19 0x0024 USART Rx Complete USART_RX_vect

20 0x0026 USART Data Register Empty USART_UDRE_vect

21 0x0028 USART Tx Complete USART_TX_vect

22 0x002A ADC Conversion Complete ADC_vect

23 0x002C EEPROM Ready EE_READY_vect

24 0x002E Analog Comparator ANALOG_COMP_vect

25 0x0030 Two-wire Serial Interface TWI_vect

26 0x003A Store Program Memory Read SPM_READY_vect

20

You might also like