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 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 }
Previous article:AVR Development Arduino Method (I) Port Subsystem
Next article:AVR Development Arduino Method (Part 3) Timer/Counter Subsystem
- Popular Resources
- Popular amplifiers
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- Innolux's intelligent steer-by-wire solution makes cars smarter and safer
- 8051 MCU - Parity Check
- How to efficiently balance the sensitivity of tactile sensing interfaces
- What should I do if the servo motor shakes? What causes the servo motor to shake quickly?
- 【Brushless Motor】Analysis of three-phase BLDC motor and sharing of two popular development boards
- Midea Industrial Technology's subsidiaries Clou Electronics and Hekang New Energy jointly appeared at the Munich Battery Energy Storage Exhibition and Solar Energy Exhibition
- Guoxin Sichen | Application of ferroelectric memory PB85RS2MC in power battery management, with a capacity of 2M
- Analysis of common faults of frequency converter
- In a head-on competition with Qualcomm, what kind of cockpit products has Intel come up with?
- Dalian Rongke's all-vanadium liquid flow battery energy storage equipment industrialization project has entered the sprint stage before production
- Allegro MicroSystems Introduces Advanced Magnetic and Inductive Position Sensing Solutions at Electronica 2024
- Car key in the left hand, liveness detection radar in the right hand, UWB is imperative for cars!
- After a decade of rapid development, domestic CIS has entered the market
- Aegis Dagger Battery + Thor EM-i Super Hybrid, Geely New Energy has thrown out two "king bombs"
- A brief discussion on functional safety - fault, error, and failure
- In the smart car 2.0 cycle, these core industry chains are facing major opportunities!
- The United States and Japan are developing new batteries. CATL faces challenges? How should China's new energy battery industry respond?
- Murata launches high-precision 6-axis inertial sensor for automobiles
- Ford patents pre-charge alarm to help save costs and respond to emergencies
- New real-time microcontroller system from Texas Instruments enables smarter processing in automotive and industrial applications
- IAR IDE for MSP430, 8051, ARM and other platforms
- What does damping coefficient mean? What is the damping coefficient KD value?
- There is a chip shortage now. Are there any domestic chips that can replace this buck-boost chip from Lingte? LTC1517ES5-3.3, SOT...
- MSP430 MCU Development Record (7)
- EEWORLD University Hall ---- EMC pre-test environment construction
- Restraining the complexity of embedded system design
- Device Basics - The Impact of Capacitor Self-resonance Frequency on Selection
- [Zhongke Bluexun AB32VG1 RISC-V Evaluation Board] Try to use RTThread multitasking
- CC3200 wireless wifi processor full voice interactive control smart home system
- Pre-registration for the prize live broadcast | ON Semiconductor's advanced image sensor solutions for the Internet of Things