AVR Development Arduino Method (Part 2) Interrupt Subsystem

Publisher:星光闪耀Latest update time:2019-12-05 Source: elecfans Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

Before understanding the interrupt subsystem, we must first understand the concept of interrupts. You are reading a book and the phone rings. What will you do? I believe most people will do this: mark the position you are reading first, and then continue reading after answering the phone. This is an example of an interrupt in real life. We call "the phone ringing" an interrupt source. The main processor ATMega328P of Arduino UNO R3 has 26 interrupt sources, as shown in the following table:

Vector Number

Program address

Interrupt Sources

Interrupt Definition

Interrupt service routine name

1

0x0000

RESET

External level reset, power-on reset, power-off detection reset, watchdog reset


2

0x0002

INT0

External interrupt request 0

INT0_vect

3

0x0004

INT1

External interrupt request 1

INT1_vect

4

0x0006

PCINT0

Pin level change interrupt request 0

PCINT0_vect

5

0x0008

PCINT1

Pin level change interrupt request 1

PCINT1_vect

6

0x000A

PCINT2

Pin level change interrupt request 2

PCINT2_vect

7

0x000C

WDT

Watchdog overflow interrupt

WDT_vect

8

0x000E

TIMER2 COMPA

Timer/Event Counter 2 Compare Match A

TIMER2_COMPA_vect

9

0x0010

TIMER2 COMPB

Timer/Event Counter 2 Compare Match B

TIMER2_COMPB_vect

10

0x0012

HOUR2 OVF

Timer/Event Counter 2 Overflow

TIMER2_OVF_vect

11

0x0014

TIMER1 CAPT

Timer/Event Counter 1 Event Capture

TIMER1_CAPT_vect

12

0x0016

TIMER1 COMPA

Timer/Event Counter 1 Compare Match A

TIMER1_COMPA_vect

13

0x0018

TIMER1 COMPB

Timer/Event Counter 1 Compare Match B

TIMER1_COMPB_vect

14

0x001A

HOUR1 OVF

Timer/Event Counter 1 Overflow

TIMER1_OVF_vect

15

0x001C

TIMER0 COMPA

Timer/Event Counter 0 Compare Match A

TIMER0_COMPA_vect

16

0x001E

TIMER0 COMPB

Timer/Event Counter 0 Compare Match B

TIMER0_COMPB_vect

17

0x0020

TIMER0 OVF

Timer/Event Counter 0 overflow

TIMER0_OVF_vect

18

0x0022

SPI STC

SPI serial transfer ends

SPI_STC_vect

19

0x0024

USART RX

USART reception ends

USART_RX_vect

20

0x0026

USART UDRE

USART data register empty

USART_UDRE_vect

21

0x0028

USART TX

USART, end of transmission

USART_TX_vect

22

0x002A

ADC

Analog-to-digital conversion completed

ADC_vect

23

0x002C

EE READY

EEPROM ready

EE_READY_vect

24

0x002E

ANALOG COMP

Analog Comparator

ANALOG_COMP_vect

25

0x0030

TWI

Two-wire serial interface

TWI_vect

26

0x0032

SPM READY

Save program memory contents ready

SPM_ready_vect

Here we take external interrupt 0 as an example to understand the programming of the interrupt subsystem. Using the circuit used for the digital input example in the previous chapter, this example reverses the state of the LED when the key is pressed:


 1 // Interrupt.ino

 2 const byte ledPin = 13;

 3 const byte interruptPin = 2;

 4 volatile byte state = LOW;

 5 

 6 void setup() {

 7   pinMode(ledPin, OUTPUT);

 8   pinMode(interruptPin, INPUT_PULLUP);

 9   attachInterrupt(digitalPinToInterrupt(interruptPin), blink, CHANGE);

10 }

11 

12 void loop() {

13   digitalWrite(ledPin, state);

14 }

15 

16 void blink() {

17   state = !state;

18 }


The Arduino library functions related to external interrupts are:

 

  attachInterrupt(digitalPinToInterrupt(pin), ISR, mode): Enables external interrupt for the specified pin and connects it to the specified interrupt service routine

  pin: specifies the pin for the external interrupt

  ISR: Specifies the name of the interrupt service routine

  mode: LOW (low level trigger interrupt), CHANG (logic level change trigger interrupt), RISING (rising edge trigger interrupt) or FALLING (falling edge trigger interrupt)

  detachInterrupt(digitalPinToInterrupt(pin)): disable the specified interrupt

  pin: specifies the pin to cancel the external interrupt

  interrupts(): Enable general interrupts

  noInterrupts(): disable general interrupts

 

The external interrupt of ATMega328P is controlled by 2 related registers. The structure of the external interrupt control register EICRA is shown in the figure below:




INT1

INT0




ISC11

ISC10

ISC01

ISC00

The ISCx[1:0] (x = 0, 1) bits are used to set the trigger mode of the external interrupt, as shown in the following table:

ISCx[1:0]

(x = 0, 1)

External interrupt trigger mode

00

Low level

01

Logic level changes

10

Falling edge

11

Rising edge

The external interrupt mask register EIMSK is used to set whether to mask external interrupts. Its structure is shown in the figure below:






INT1

INT0

If you write 1 to a bit, the external interrupt controlled by that bit is enabled; if you write 0, it is disabled.


Rewrite the above program by directly accessing the registers:


 1 // Interrupt_reg.ino

 2 volatile byte state = LOW;

 3 

 4 void setup() {

 5   DDRB |= (1 << PB5);

 6   

 7   DDRD &= ~(1 << PD2);

 8   PORTD |= (1 << PD2);

 9   EICRA &= ~(1 << ISC01) & ~(1 << ISC00);

10   EIMSK |= (1 << INT0);

11 sei(); // Enable global interrupt

12 }

13 

14 void loop() {

15   if (state == HIGH) {

16     PORTB |= (1 << PB5);

17   } else {

18     PORTB &= ~(1 << PB5);

19   }

20 }

21 

22 // External interrupt 0 interrupt processing function

23 ISR(INT0_vect) {

24   state = !state;

25 }


Reference address:AVR Development Arduino Method (Part 2) Interrupt Subsystem

Previous article:AVR Development Arduino Method (I) Port Subsystem
Next article:AVR Development Arduino Method (Part 3) Timer/Counter Subsystem

Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号