AVR has different interrupt sources. Each interrupt and reset has a separate interrupt vector in the program space. All interrupt events have their own enable bit. When the enable bit is set and the global interrupt enable bit I in the status register is also set, an interrupt can occur. Depending on the value of the program counter PC, interrupts may be automatically disabled when the boot lock bit BLB02 or BLB12 is programmed. This feature improves software security. For details, please refer to P 267 "Memory Programming".
The lowest address of the program memory space is defined by default as the reset and interrupt vectors. For a complete list of vectors, see P 55 "Interrupts". The list also determines the priority of the different interrupts. The lower the address of the vector, the higher the priority. RESET has the highest priority, and the next is INT0 - External Interrupt Request 0. By setting IVSEL in the MCU Control Register (MCUCR), the interrupt vector can be moved to the beginning of the boot Flash. Detailed description is given in P 55 "Interrupts". Programming the fuse bit BOOTRST can also move the reset vector to the beginning of the boot Flash. For details, see P 255 "Supporting Boot Loaders - Self-Programming Capability that Can Read While Writing (RWW, Read-While-Write)".
When an interrupt occurs, the global interrupt enable bit I is cleared and all interrupts are disabled. User software can enable interrupt nesting by setting bit I. At this time, all interrupts can interrupt the current interrupt. I is automatically set after executing the RETI instruction.
Basically there are two types of interrupts. The first is triggered by an event and sets the interrupt flag. For these interrupts, the program counter jumps to the actual interrupt vector to execute the interrupt handling routine, and the hardware will clear the corresponding interrupt flag. The interrupt flag can also be cleared by writing "1" to it. When an interrupt occurs, if the corresponding interrupt enable bit is "0", the interrupt flag is set and remains set until the interrupt is executed or cleared by software. Similarly, if the global interrupt flag is cleared, all interrupts that have occurred will not be executed until I is set. The pending interrupts are then executed in order of interrupt priority.
The second type of interrupt is always triggered as long as the interrupt condition is met. These interrupts do not require an interrupt flag. If the interrupt condition disappears before the interrupt is enabled, the interrupt will not be triggered.
After exiting an interrupt, the AVR always returns to the main program and executes one instruction before it can execute other suspended interrupts.
The status register is not automatically saved when entering the interrupt routine, nor is it automatically restored when the interrupt returns. These tasks must be completed by software.
When you use the CLI instruction to disable interrupts, interrupts are disabled immediately. No interrupt can occur after executing the CLI instruction, even if it occurs while executing CLI. The following example shows how to use this instruction to prevent interrupts from occurring when writing to the EEPROM.
Assembly code examples |
in r16, SREG ; save SREG cli ; disable interrupt sbi EECR, EEMWE ; start EEPROM write operation sbi EECR, EEWE out SREG, r16 ; restore SREG (I-bit) |
C code example |
char cSREG; cSREG = SREG; /* save SREG */ /* disable interrupt */ _CLI(); EECR |= (1< |
When interrupts are enabled using the SEI instruction, the first instruction that follows is executed before any interrupts are executed.
Assembly code examples |
sei ; Set the global interrupt enable flag sleep ; Enter sleep mode and wait for an interrupt to occur ; Note: Enter sleep mode before executing any pending interrupts |
C code example |
_SEI(); /* Set the global interrupt enable flag */ _SLEEP(); /* Enter sleep mode and wait for an interrupt to occur */ /* Note: Enter sleep mode first before executing any pending interrupts */ |
Interrupt response time
The AVR interrupt response time is at least 4 clock cycles. After 4 clock cycles, the program jumps to the actual interrupt handling routine. During these 4 clock cycles, the PC is automatically pushed to the stack. Under normal circumstances, the interrupt vector is a jump instruction, and this jump takes 3 clock cycles. If the interrupt occurs during the execution of a multi-cycle instruction, the MCU will not execute the interrupt program until the multi-cycle instruction is executed. If the MCU is in sleep mode when the interrupt occurs, the interrupt response time increases to 8 clock cycles. The additional clock cycle is introduced due to the wake-up startup time.
The interrupt return requires 4 clocks, during which the PC (two bytes) will be popped off the stack, the stack pointer will be incremented by two, and the I bit of the status register SREG will be set.
Previous article:ATmega128 Flash Program Memory
Next article:ATmega128 instruction execution timing
- Popular Resources
- Popular amplifiers
- Learn ARM development(16)
- Learn ARM development(17)
- Learn ARM development(18)
- Embedded system debugging simulation tool
- A small question that has been bothering me recently has finally been solved~~
- Learn ARM development (1)
- Learn ARM development (2)
- Learn ARM development (4)
- Learn ARM development (6)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- LED chemical incompatibility test to see which chemicals LEDs can be used with
- Application of ARM9 hardware coprocessor on WinCE embedded motherboard
- What are the key points for selecting rotor flowmeter?
- LM317 high power charger circuit
- A brief analysis of Embest's application and development of embedded medical devices
- Single-phase RC protection circuit
- stm32 PVD programmable voltage monitor
- Introduction and measurement of edge trigger and level trigger of 51 single chip microcomputer
- Improved design of Linux system software shell protection technology
- What to do if the ABB robot protection device stops
- Microchip Accelerates Real-Time Edge AI Deployment with NVIDIA Holoscan Platform
- Microchip Accelerates Real-Time Edge AI Deployment with NVIDIA Holoscan Platform
- Melexis launches ultra-low power automotive contactless micro-power switch chip
- Melexis launches ultra-low power automotive contactless micro-power switch chip
- Molex leverages SAP solutions to drive smart supply chain collaboration
- Pickering Launches New Future-Proof PXIe Single-Slot Controller for High-Performance Test and Measurement Applications
- Apple faces class action lawsuit from 40 million UK iCloud users, faces $27.6 billion in claims
- Apple faces class action lawsuit from 40 million UK iCloud users, faces $27.6 billion in claims
- The US asked TSMC to restrict the export of high-end chips, and the Ministry of Commerce responded
- The US asked TSMC to restrict the export of high-end chips, and the Ministry of Commerce responded
- AD16.0 Copy Room Formats Problems
- The world's smallest gingerbread house
- I have a problem with using the eclipse that comes with altera. Can anyone tell me what I have used?
- The Three Realms of Oscilloscopes
- Problems with the snubber circuit
- Hardware Emulation
- [RVB2601 Creative Application Development] Introduction and use of cJSON for RVB2601
- I bought an ST-LINK emulator for SensorTile.box for 19 RMB
- How to set the clearance of ALTIUM without affecting the copper coating
- Common RF Certification Systems