- An interrupt is some event which interrupts normal program execution
- Interrupts give us a mechanism to "put on hold" the normal program flow, execute a event specific subroutine, and then resume normal program flow as if we had never left it.
- The subroutine, called an interrupt handler, is only executed when a certain event (interrupt) occurs.
- The event may be one of the timers "overflowing," receiving a character via the serial port, transmitting a character via the serial port, or one of two "external events".
- When any of the above events occur the main program is temporarily suspended and control is passed to a special section of code (interrupt handler) to execute some function related to the event that occurred.
- Once the execution is complete, control would be returned to the main program
- It finishes the instruction it is executing and saves the address of the next instruction (PC) on the stack.
- It also saves the current status of all the interrupts internally (i.e., not on the stack).
- It jumps to a fixed location in memory called the interrupt vector table that holds the address of the interrupt service routine.
- The microcontroller gets the address of the ISR from the interrupt vector table and jumps to it. It starts to execute the interrupt service subroutine until it reaches the last instruction of the subroutine, which is RETI (return from interrupt).
- Upon executing the RETI instruction, the microcontroller returns to the place where it was interrupted. First, it gets the program counter (PC) address from the stack by popping the top two bytes of the stack into the PC. Then it starts to execute from that address.
This defines which section of code should execute when an interrupt occurs. The code (interrupt handler) location is fixed or predefined. When an interrupt occurs, the main program will be temporarily suspended and control will jump to the fixed address.
Interrupt | Interrupt Handler Address (ROM Location) | Pin | Flag Clearing |
---|---|---|---|
Reset | 0000h | 9 | Auto |
External event 0 | 0003h | P3.2 (12) | Auto |
Timer 0 overflow event | 000Bh | Auto | |
External event 1 | 0013h | P3.3 (13) | Auto |
Timer 1 overflow event | 001Bh | Auto | |
Serial Port: Reception/Transmission of a byte. | 0023h | Programmer Clears it |
A limited number of bytes is set aside for each interrupt. A total of 8 bytes from location 0003 to 000A is set aside for INT0, external hardware interrupt 0. If the service routine for a given interrupt is short enough to fit in the memory space allocated to it, it is placed in the vector table; otherwise, an LJMP instruction is placed in the vector table to point to the address of the ISR. In that case, the rest of the bytes allocated to that interrupt are unused.
ORG 0H ; wake-up ROM reset location
LJMP MAIN ; bypass interrupt vector table
; ----------wake up the main program
ORG 30H
MAIN:
....
END
Setting Interrupts:
- By default at power up, all interrupts are disabled
- Program may enable and disable interrupts by modifying the IE SFR (A8h).
- EA bit must be set to trigger interrupt(s)
Bit | Name | Function |
---|---|---|
7 | EA | Global Interrupt Enable/Disable bit. |
6 | -- | Undefined |
5 | -- | Undefined |
4 | ES | Enable Serial Interrupt |
3 | ET1 | Enable Timer 1 Interrupt |
2 | EX1 | Enable External 1 Interrupt |
1 | ET0 | Enable Timer 0 Interrupt |
0 | EX0 | Enable External 0 Interrupt |
Interrupt Priority:
By default, 8051 checks interrupt conditions in the following order
- External 0 Interrupt
- Timer 0 Interrupt
- External 1 Interrupt
- Timer 1 Interrupt
- Serial Interrupt
Bit | Name | Function |
---|---|---|
Bit | Name | Function |
7 | -- | Undefined |
6 | -- | Undefined |
5 | -- | Undefined |
4 | PS | Serial Interrupt Priority |
3 | PT1 | Timer 1 Interrupt Priority |
2 | PX1 | External 1 Interrupt Priority |
1 | PT0 | Timer 0 Interrupt Priority |
0 | PX0 | External 0 Interrupt Priority |
When considering interrupt priorities, the following rules apply.
- Nothing can interrupt a high-priority interrupt--not even another high priority interrupt.
- A high-priority interrupt may interrupt a low-priority interrupt.
- A low-priority interrupt may only occur if no other interrupt is already executing.
- If two interrupts occur at the same time, the interrupt with higher priority will execute first. If both interrupts are of the same priority the interrupt which is serviced first by polling sequence will be executed first.
When an interrupt is triggered, the microcontroller does the following automatically:
- The current Program Counter (PC) is saved on the stack, low-byte first.
- Interrupts of the same and lower priority are blocked.
- In the case of Timer and External interrupts, the corresponding interrupt flag is cleared.
- Program execution transfers to the corresponding interrupt handler vector address.
- The Interrupt Handler Routine executes.
- Two bytes are popped off the stack into the Program Counter to restore normal program execution.
- Interrupt status is restored to its pre-interrupt status.
Microcontroller protects Program Counter only. It does not protect other registers like,
- PSW
- DPTR (DPH/DPL)
- SP
- ACC
- B
- Registers R0-R7
Related topics:
8051 Register Bank and Stack | 8051 Flags Bits | 8051 Program Flow | 8051 Timers | 8051 Serial Port
List of topics: 8051
No comments:
Post a Comment