AVR Interrupt and External Interrupt : Arduino / ATmega328p

Introduction

This section describes the specifics of the interrupt handling as performed in ATmega328P. It also discusses the interrupt vectors available in the chip and how they are used.


What You Will Learn

  • How Iterrupts work in Arduino?
  • How CPU Interrupts of AVR ATmega328p chip works?
  • What are the different sources of Interrupts in ATmega328p?
  • What are the difference between internal and external Interrupts in ATmega328p?
  • How Interrupts are enabled or disabled in Arduino/ATmega328p?

ATmega328P Interrupt Vectors

The AVR provides several different interrupt sources. These interrupts and the separate Reset Vector each have a separate program vector in the program memory space. All interrupts are assigned individual enable bits which must be written logic one together with the Global Interrupt Enable bit in the Status Register in order to enable the interrupt.

Each Interrupt Vector occupies two instruction words inATmega328/328P. This list determines the priority levels of the different interrupts. The lower the address the higher is the priority level. The lowest addresses in the program memory space are by default defined as the Reset and Interrupt Vectors.

Vector No.Program
Address
SourceInterrupt Definition
10x0000RESETExternal Pin, Power-on,
Brown-out, Watchdog System Reset
20x0002INT0External Interrupt Request 0
30x0004INT1External Interrupt Request 1
40x0006PCINT0Pin Change Interrupt Request 0
50x0008PCINT1Pin Change Interrupt Request 1
60x000APCINT2Pin Change Interrupt Request 2
70x000CWDTWatchdog Time-out Interrupt
80x000ETIMER2 COMPATimer/Counter2 Compare Match A
90x0010TIMER2 COMPBTimer/Counter2 Compare Match B
100x0012TIMER2 OVFTimer/Counter2 Overflow
110x0014TIMER1 CAPTTimer/Counter1 Capture Event
120x0016TIMER1 COMPATimer/Counter1 Compare Match A
130x0018TIMER1 COMPBTimer/Coutner1 Compare Match B
140x001ATIMER1 OVFTimer/Counter1 Overflow
150x001CTIMER0 COMPATimer/Counter0 Compare Match A
160x001ETIMER0 COMPBTimer/Counter0 Compare Match B
170x0020TIMER0 OVFTimer/Counter0 Overflow
180x0022SPI, STCSPI Serial Transfer Complete
190x0024USART, RXUSART Rx Complete
200x0026USART, UDREUSART, Data Register Empty
210x0028USART, TXUSART, Tx Complete
220x002AADCADC Conversion Complete
230x002CEE READYEEPROM Ready
240x002EANALOG COMPAnalog Comparator
250x0030TWI2-wire Serial Interface
260x0032SPM READYStore Program Memory Ready

When an interrupt occurs, the Global Interrupt Enable I-bit is cleared and all interrupts are disabled. The user software can write logic one to the I-bit to enable nested interrupts. All enabled interrupts can then interrupt the current interrupt routine. The I-bit is automatically set when a Return from Interrupt instruction – RETI – is executed. When the AVR exits from an interrupt, it will always return to the main program and execute one more instruction before any pending interrupt is served.

The interrupt execution response for all the enabled AVR interrupts is four clock cycles minimum. After four clock cycles, the program vector address for the actual interrupt handling routine is executed. During this four clock cycle period, the Program Counter is pushed onto the Stack. The vector is normally a jump to the interrupt routine, and this jump takes three clock cycles. If an interrupt occurs during the execution of a multi-cycle instruction, this instruction is completed before the interrupt is served. If an interrupt occurs when the MCU is in sleep mode, the interrupt execution response time is increased by four clock cycles. This increase comes in addition to the start-up time from the selected sleep mode.

A return from an interrupt handling routine takes four clock cycles. During these four clock cycles, the Program Counter (two bytes) is popped back from the Stack, the Stack Pointer is incremented by two, and the I-bit in SREG is set.

If the program never enables an interrupt source, the Interrupt Vectors are not used, and regular program codes can be placed at these locations. This is also the case if the Reset Vector is in the Application section while the Interrupt Vectors are in the Boot section or vice versa.

BOOTRSTIVSELReset AddressInterrupt Vectors Start Address
100x0000x002
110x000Boot Reset Address + 0x0002
00Boot Reset Address0x002
01Boot Reset AddressBoot Reset Address + 0x0002

MCUCR – MCU Control Register

The MCU Control Register controls the placement of the Interrupt Vector table.

Bit 76543210
Read/Write
Initial Value 
R
R/W
R/W
R/W
R
R
R/W
R/W
0
0x35 (0x55)BODSBODSEPUDIVSELIVCE
  • Bit 1 – IVSEL: Interrupt Vector Select
    When the IVSEL bit is cleared (zero), the Interrupt Vectors are placed at the start of the Flash memory. When this bit is set (one), the Interrupt Vectors are moved to the beginning of the Boot Loader section of the Flash.
  • Bit 0 – IVCE: Interrupt Vector Change Enable
    The IVCE bit must be written to logic one to enable change of the IVSEL bit. IVCE is cleared by hardware four cycles after it is written or when IVSEL is written.

ATmega328P External Interrupts


Atmega328p DIP Dual Inline Package Pin Out

The External Interrupts are triggered by the INT0 and INT1 pins or any of the PCINT23…0 pins. The interrupts will trigger
even if the INT0 and INT1 or PCINT23…0 pins are configured as outputs. This feature provides a way of generating a software interrupt. The pin change interrupt PCI2 will trigger if any enabled PCINT[23:16] pin toggles. The pin change interrupt PCI1 will trigger if any enabled PCINT[14:8] pin toggles. The pin change interrupt PCI0 will trigger if any enabled PCINT[7:0] pin toggles. The PCMSK2, PCMSK1, and PCMSK0 Registers control which pins contribute to the pin change interrupts. Pin change interrupts on PCINT23…0 are detected asynchronously. This implies that these interrupts can be used for waking the part also from sleep modes other than Idle mode.

The INT0 and INT1 interrupts can be triggered by a falling or rising edge or a low level. This is set up as indicated in the specification for the External Interrupt Control Register A – EICRA. When the INT0 or INT1 interrupts are enabled and are configured as level triggered, the interrupts will trigger as long as the pin is held low. Note that recognition of falling or rising edge interrupts on INT0 or INT1 requires the presence of an I/O clock.

Timing of Pin Change Interrupts in AVR
Timing of Pin Change Interrupts in AVR

EICRA – External Interrupt Control Register A

The External Interrupt Control Register A contains control bits for interrupt sense control.

Bit 76543210
Read/Write
Initial Value 
R
R
R
R
R/W
R/W
R/W
R/W
0
0x35 (0x55)ISC11ISC10ISC01ISC00
  • Bit 7:4 – Reserved
    Unused and will always reads as zero
  • Bit 3, 2 – ISC11, ISC10: Interrupt Sense Control 1 Bit 1 and Bit 0
    The External Interrupt 1 is activated by the external pin INT1 if the SREG I-flag and the corresponding interrupt mask are set. The value on the INT1 pin is sampled before detecting edges. If edge or toggle interrupt is selected, pulses that last longer than one clock period will generate an interrupt. Shorter pulses are not guaranteed to generate an interrupt. If low level interrupt is selected, the low level must be held until the completion of the currently executing instruction to generate an interrupt.
  • Bit 1, 0 – ISC01, ISC00: Interrupt Sense Control 0 Bit 1 and Bit 0
    The External Interrupt 0 is activated by the external pin INT0 if the SREG I-flag and the corresponding interrupt mask are set. The value on the INT0 pin is sampled before detecting edges. If edge or toggle interrupt is selected, pulses that last longer than one clock period will generate an interrupt. Shorter pulses are not guaranteed to generate an interrupt. If low level interrupt is selected, the low level must be held until the completion of the currently executing instruction to generate an interrupt.
ISC11ISC10Description
00The low level of INT1 generates an interrupt request.
01Any logical change on INT1 generates an interrupt request.
10The falling edge of INT1 generates an interrupt request.
11The rising edge of INT1 generates an interrupt request
ISC01ISC00Description
00The low level of INT0 generates an interrupt request.
01Any logical change on INT0 generates an interrupt request.
10The falling edge of INT0 generates an interrupt request.
11The rising edge of INT0 generates an interrupt request

EIMSK – External Interrupt Mask Register

The External Interrupt Mask Register contains enables or disables the INT1 and INT0 interrupts.

Bit 76543210
Read/Write
Initial Value 
R
R
R
R
R
R
R/W
R/W
0
0x1D (0x3D)INT1INT0
  • Bit 7:2 – Reserved
    These bits are unused bits and will always read as zero.
  • Bit 1 – INT1: External Interrupt Request 1 Enable
    When the INT1 bit is set (one) and the I-bit in the Status Register (SREG) is set (one), the external pin interrupt is enabled.
  • Bit 0 – INT0: External Interrupt Request 0 Enable
    When the INT0 bit is set (one) and the I-bit in the Status Register (SREG) is set (one), the external pin interrupt is enabled.

EIFR – External Interrupt Flag Register

The External Interrupt Flag Register contains the INTF1 and INTF0 flag.

Bit 76543210
Read/Write
Initial Value 
R
R
R
R
R
R
R/W
R/W
0
0x1C (0x3C)INTF1INTF0
  • Bit 7:2 – Reserved
    These bits are unused bits and will always read as zero.
  • Bit 1 – INTF1: External Interrupt Flag 1
    When an edge or logic change on the INT1 pin triggers an interrupt request, INTF1 becomes set (one). If the I-bit in SREG and the INT1 bit in EIMSK are set (one), the MCU will jump to the corresponding Interrupt Vector. The flag is cleared when the interrupt routine is executed. Alternatively, the flag can be cleared by writing a logical one to it. This flag is always cleared when INT1 is configured as a level interrupt.
  • Bit 0 – INTF0: External Interrupt Flag 0
    When an edge or logic change on the INT0 pin triggers an interrupt request, INTF0 becomes set (one). If the I-bit in SREG and the INT0 bit in EIMSK are set (one), the MCU will jump to the corresponding Interrupt Vector. The flag is cleared when the interrupt routine is executed. Alternatively, the flag can be cleared by writing a logical one to it. This flag is always cleared when INT0 is configured as a level interrupt.

PCICR – Pin Change Interrupt Control Register

The Pin Change Interrupt Control Register contains enables or disables the PCI1, PCI2, PCI3.

Bit 76543210
Read/Write
Initial Value 
R
R
R
R
R
R/W
R/W
R/W
0
(0x68)PCIE2PCIE1PCIE0
  • Bit 7:3 – Reserved
    These bits are unused bits and will always read as zero.
  • Bit 2 – PCIE2: Pin Change Interrupt Enable 2
    When the PCIE2 bit is set (one) and the I-bit in the Status Register (SREG) is set (one), pin change interrupt 2 is enabled. Any change on any enabled PCINT[23:16] pin will cause an interrupt. The corresponding interrupt of Pin Change Interrupt Request is executed from the PCI2 Interrupt Vector. PCINT[23:16] pins are enabled individually by the PCMSK2 Register.
  • Bit 1 – PCIE1: Pin Change Interrupt Enable 1
    When the PCIE0 bit is set (one) and the I-bit in the Status Register (SREG) is set (one), pin change interrupt 0 is enabled. Any change on any enabled PCINT[7:0] pin will cause an interrupt. The corresponding interrupt of Pin Change Interrupt Request is executed from the PCI0 Interrupt Vector. PCINT[7:0] pins are enabled individually by the PCMSK0 Register.

PCIFR – Pin Change Interrupt Flag Register

The Pin Change Interrupt Flag Register contains the PCIF1, PCIF2, PCIF3 flags.

Bit 76543210
Read/Write
Initial Value 
R
R
R
R
R
R/W
R/W
R/W
0
0x1B (0x3B)PCIE2PCIE1PCIE0
  • Bit 7:3 – Reserved
    These bits are unused bits and will always read as zero.
  • Bit 2 – PCIF2: Pin Change Interrupt Flag 2
    When a logic change on any PCINT[23:16] pin triggers an interrupt request, PCIF2 becomes set (one). If the I-bit in SREG and the PCIE2 bit in PCICR are set (one), the MCU will jump to the corresponding Interrupt Vector. The flag is cleared when the interrupt routine is executed. Alternatively, the flag can be cleared by writing a logical one to it.
  • Bit 1 – PCIF1: Pin Change Interrupt Flag 1
    When a logic change on any PCINT[14:8] pin triggers an interrupt request, PCIF1 becomes set (one). If the I-bit in SREG and the PCIE1 bit in PCICR are set (one), the MCU will jump to the corresponding Interrupt Vector. The flag is cleared when the interrupt routine is executed. Alternatively, the flag can be cleared by writing a logical one to it.

PCMSK2 – Pin Change Mask Register 2

Bit 76543210
Read/Write
Initial Value 
R/W
R/W
R/W
R/W
R/W
R/W
R/W
R/W
0
(0x6D)PCINT23PCINT22PCINT21PCINT20PCINT19PCINT18PCINT17PCINT16
  • Bit 7:0 – PCINT[23:16]: Pin Change Enable Mask 23…16
    Each PCINT[23:16]-bit selects whether pin change interrupt is enabled on the corresponding I/O pin. If PCINT[23:16] is set and the PCIE2 bit in PCICR is set, pin change interrupt is enabled on the corresponding I/O pin. If PCINT[23:16] is cleared, pin change interrupt on the corresponding I/O pin is disabled.

PCMSK1 – Pin Change Mask Register 1

Bit 76543210
Read/Write
Initial Value 
R/W
R/W
R/W
R/W
R/W
R/W
R/W
R/W
0
(0x6C)PCINT14PCINT13PCINT12PCINT11PCINT10PCINT9PCINT8
  • Bit 6:0 – PCINT[14:8]: Pin Change Enable Mask 14…8
    Each PCINT[14:8]-bit selects whether pin change interrupt is enabled on the corresponding I/O pin. If PCINT[14:8] is set and the PCIE1 bit in PCICR is set, pin change interrupt is enabled on the corresponding I/O pin. If PCINT[14:8] is cleared, pin change interrupt on the corresponding I/O pin is disabled.

PCMSK0 – Pin Change Mask Register 0

Bit 76543210
Read/Write
Initial Value 
R/W
R/W
R/W
R/W
R/W
R/W
R/W
R/W
0
(0x6B)PCINT7PCINT6PCINT5PCINT4PCINT3PCINT2PCINT1PCINT0
  • Bit 6:0 – PCINT[7:0]: Pin Change Enable Mask 7…0
    Each PCINT[7:0] bit selects whether pin change interrupt is enabled on the corresponding I/O pin. If PCINT[7:0] is set and the PCIE0 bit in PCICR is set, pin change interrupt is enabled on the corresponding I/O pin. If PCINT[7:0] is cleared, pin change interrupt on the corresponding I/O pin is disabled.

إرسال تعليق

Post a Comment (0)

أحدث أقدم