Thursday, March 17, 2016

8051 Interrupts

  • 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
Steps in Executing an Interrupt:
  • 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.
Interrupt Vector Table:
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.
InterruptInterrupt Handler Address (ROM Location)PinFlag Clearing
Reset0000h9Auto
External event 00003hP3.2 (12)Auto
Timer 0 overflow event000BhAuto
External event 10013hP3.3 (13)Auto
Timer 1 overflow event001BhAuto
Serial Port: Reception/Transmission of a byte.0023hProgrammer 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)
BitNameFunction
7EAGlobal Interrupt Enable/Disable bit.
6--Undefined
5--Undefined
4ESEnable Serial Interrupt
3ET1Enable Timer 1 Interrupt
2EX1Enable External 1 Interrupt
1ET0Enable Timer 0 Interrupt
0EX0Enable 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
Program can set priority to interrupt. There are two levels of interrupt priority: high and low. Interrupt priorities are controlled by IP SFR (B8h).
BitNameFunction
BitNameFunction
7--Undefined
6--Undefined
5--Undefined
4PSSerial Interrupt Priority
3PT1Timer 1 Interrupt Priority
2PX1External 1 Interrupt Priority
1PT0Timer 0 Interrupt Priority
0PX0External 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.
Interrupt and Program Flow:
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.
When an interrupt ends, microcontroller does the following automatically.
  • 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.
Important Considerations:
Microcontroller protects Program Counter only. It does not protect other registers like,
  • PSW
  • DPTR (DPH/DPL)
  • SP
  • ACC
  • B
  • Registers R0-R7
In general, interrupt routine must protect the above registers. Interrupt routine must pushes the register values onto the stack using the PUSH instruction before executing event handling related code. Once the execution is done, it pops the original values back into the registers. It is generally a good idea to always protect registers by pushing and popping it off the stack at the beginning and end of your interrupts.



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