






Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!







As the name implies, an interrupt is some event which interrupts normal program execution.
As stated earlier, program flow is always sequential, being altered only by those instructions which expressly cause program flow to deviate in some way. However, interrupts give us a mechanism to "put on hold" the normal program flow, execute a subroutine, and then resume normal program flow as if we had never left it. This 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." The 8051 may be configured so that when any of these events occur the main program is temporarily suspended and control passed to a special section of code which presumably would execute some function related to the event that occurred. Once complete, control would be returned to the original program. The main program never even knows it was interrupted.
The ability to interrupt normal program execution when certain events occur makes it much easier and much more efficient to handle certain conditions. If it were not for interrupts we would have to manually check in our main program whether the timers had over flown, whether we had received another character via the serial port, or if some external event had occurred. Besides making the main program ugly and hard to read, such a situation would make our program inefficient since we’d be burning precious "instruction cycles" checking for events that usually don’t happen.
For example, let’s say we have a large 16k program executing many subroutines performing many tasks. Let’s also suppose that we want our program to automatically toggle the P3.0 port every time timer 0 overflows. The code to do this isn’t too difficult:
JNB TF0,SKIP_TOGGLE CPL P3. CLR TF SKIP_TOGGLE: ...
Since the TF0 flag is set whenever timer 0 overflows, the above code will toggle P3.0 every time timer 0 overflows. This accomplishes what we want, but is inefficient. The JNB instruction consumes 2 instruction cycles to determine that the flag is not set and jump over the unnecessary code. In the event that timer 0 overflows, the CPL and CLR instruction require 2 instruction cycles to execute. To make the math easy, let’s say the rest of the code in the program requires 98 instruction cycles. Thus, in total, our code consumes 100 instruction cycles ( instruction cycles plus the 2 that are executed every iteration to determine
We can configure the 8051 so that any of the following events will cause an interrupt:
? Timer 0 Overflow. ? Timer 1 Overflow. ? Reception/Transmission of Serial Character. ? External Event 0. ? External Event 1.
In other words, we can configure the 8051 so that when Timer 0 Overflows or when a character is sent/received, the appropriate interrupt handler routines are called.
Obviously we need to be able to distinguish between various interrupts and executing different code depending on what interrupt was triggered. This is accomplished by jumping to a fixed address when a given interrupt occurs.
Interrupt Flag Interrupt Handler Address External 0 IE0 0003h Timer 0 TF0 000Bh External 1 IE1 0013h Timer 1 TF1 001Bh Serial RI/TI 0023h
By consulting the above chart we see that whene ver Timer 0 overflows (i.e., the TF0 bit is set), the main program will be temporarily suspended and control will jump to 000BH. It is assumed that we have code at address 000BH that handles the situation of Timer 0 overflowing.
By default at powerup, all interrupts are disabled. This means that even if, for example, the TF0 bit is set, the 8051 will not execute the interrupt. Your program must specifically tell the 8051 that it wishes to enable interrupts and specifically which inte rrupts it wishes to enable.
Your program may enable and disable interrupts by modifying the IE SFR (A8h):
Bit Name Bit Address Explanation of Function 7 EA AFh Global Interrupt Enable/Disable 6 - AEh Undefined 5 - ADh Undefined 4 ES ACh Enable Serial Interrupt 3 ET1 ABh Enable Timer 1 Interrupt 2 EX1 AAh Enable External 1 Interrupt 1 ET0 A9h Enable Timer 0 Interrupt 0 EX0 A8h Enable External 0 Interrupt
As you can see, each of the 8051’s interrupts has its own bit in the IE SFR. You enable a given interrupt by setting the corresponding bit. For example, if you wish to enable Timer 1 Interrupt, you would execute either:
MOV IE,#08h or SETB ET Both of the above instructions set bit 3 of IE, thus enabling Timer 1 Interrupt. Once Timer 1 Interrupt is enabled, whenever the TF1 bit is set, the 8051 will automatically put "on hold" the main program and execute the Timer 1 Interrupt Handler at address 001Bh.
However, before Timer 1 Interrupt (or any other interrupt) is truly enabled, you must also set bit 7 of IE. Bit 7, the Global Interupt Enable/Disable, enables or disables all interrupts simultaneously. That is to say, if bit 7 is cleared then no interrupts will occur, even if all the other bits of IE are set. Setting bit 7 will enable all the interrupts that have been selected by setting other bits in IE. This is useful in program execution if you have time-critical code that needs to execute. In this case, you may need the code to execute from start to finish without any interrupt getting in the way. To accomplish this you can simply clear bit 7 of IE (CLR EA) and then set it after your time-criticial code is done.
So, to sum up what has been stated in this section, to enable the Timer 1 Interrupt the most common approach is to execute the following two instructions:
SETB ET SETB EA Thereafter, the Timer 1 Interrupt Handler at 01Bh will automatically be called whenever the TF1 bit is set (upon Timer 1 overflow).
Interrupt priorities are controlled by the IP SFR (B8h). The IP SFR has the following format:
Bit Name Bit Address Explanation of Function 7 - - Undefined 6 - - Undefined 5 - - Undefined 4 PS BCh Serial Interrupt Priority 3 PT1 BBh Timer 1 Interrupt Priority 2 PX1 BAh External 1 Interrupt Priority 1 PT0 B9h Timer 0 Interrupt Priority 0 PX0 B8h 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 following actions are taken automatically by the microcontroller:
? The current Program Counter 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.
Take special note of the third step: If the interrupt being handled is a Timer or External interrupt, the microcontroller automatically clears the interrupt flag before passing control to your interrupt handler routine. This means it is not necessary that you clear the bit in your code.
An interrupt ends when your program executes the RETI (Return from Interrupt) instruction. When the RETI instruction is executed the following actions are taken by the microcontroller:
? 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.
Serial Interrupts are slightly different than the rest of the interrupts. This is due to the fact that there are two interrupt flags: RI and TI. If either flag is set, a serial interrupt is triggered. As you will recall from the section on the serial port, the RI bit is set when a byte is received by the serial port and the TI bit is set when a byte has been sent.
This means that when your serial interrupt is executed, it may have been triggered because the RI flag was set or because the TI flag was set--or because both flags were set. Thus, your routine must check the status of these flags to determine what action is appropriate. Also, since the 8051 does not automatically clear the RI and TI flags you must clear these bits in your interrupt handler.
A brief code example is in order:
RI,CHECK_TI ;If the RI flag is not set, we jump to check TI
MOV A,SBUF ;If we got to this line, it’s because the RI bit was set CLR RI ;Clear the RI bit after we’ve processed it
CHECK_TI: JNB TI,EXIT_INT ;If the TI flag is not set, we jump to the exit point
CLR TI ;Clear^ the^ TI^ bit^ before^ we^ send^ another character MOV SBUF,#’A’ ;Send another character to the serial port
EXIT_INT: RETI
As you can see, our code checks the status of both interrupts flags. If both flags were set, both sections of code will be executed. Also note that each section of code clears its corresponding interrupt flag. If you forget to clear the interrupt bits, the serial interrupt will be executed over and over until you clear the bit. Thus it is very important that you always clear the interrupt flags in a serial interrupt.
bit to be set). Since an interrupt routine must guarantee that the registers remain unchanged by the routine, the routine pushes the original values onto the stack using the PUSH instruction. It is then free to use the registers it protected to its heart’s content. Once the interrupt has finished its task, it pops the original values back into the registers. When the interrupt exits, the main program will never know the difference because the registers are exactly the same as they were before the interrupt executed.
In general, your interrupt routine must protect the following registers:
? Registers R0-R
Remember that PSW consists of many individual bits that are set by various 8051 instructions. Unless you are absolutely sure of what you are doing and have a complete understanding of what instructions set what bits, it is generally a good idea to always protect PSW by pushing and popping it off the stack at the beginning and end of your interrupts.
Note also that most assemblers (in fact, ALL assemblers that I know of) will not allow you to execute the instruction:
PUSH R This is due to the fact that depending on which register bank is selected, R0 may refer to either internal ram address 00h, 08h, 10h, or 18h. R0, in and of itself, is not a valid memory address that the PUSH and POP instructions can use.
Thus, if you are using any "R" register in your interrupt routine, you will have to push that register’s absolute address onto the stack instead of just saying PUSH R0. For example, instead of PUSH R0 you would execute:
PUSH 00h Of course, this only works if you’ve selected the default register set. If you are using an alternate register set, you must PUSH the address which corresponds to the register you are using.
Interrupts are a very powerful tool available to the 8051 developer, but when used incorrectly they can be a source of a huge number of debugging hours. Errors in interrupt routines are often very difficult to diagnose and correct.
If you are using interrupts and your program is crashing or does not seem to be performing as you would expect, always review the following interrupt-related issues:
? Register Protection : Make sure you are protecting all your registers, as explained above. If you forget to protect a register that your main program is using, very strange results may occur. In our example above we saw how failure to protect registers caused the main program to apparently calculate that 25h + 10h = 51h. If you witness problems with registers changing values unexpectedly or operations producing "incorrect" values, it is very likely that you’ve forgotten to protect registers. ALWAYS PROTECT YOUR REGISTERS. ? Forgetting to restore protected values : Another common error is to push registers onto the stack to protect them, and then forget to pop them off the stack before exiting the interrupt. For example, you may push ACC, B, and PSW onto the stack in order to protect them and subsequently pop only ACC and PSW off the stack before exiting. In this case, since you forgot to restore the value of "B", an extra value remains on the stack. When you execute the RETI instruction the 8051 will use that value as the return address instead of the correct value. In this case, your program will almost certainly crash. ALWAYS MAKE SURE YOU POP THE SAME NUMBER OF VALUES OFF THE STACK AS YOU PUSHED ONTO IT. ? Using RET instead of RETI: Remember that interrupts are always terminated with the RETI instruction. It is easy to inadvertantly use the RET instruction instead. However, the RET instruction will not end your interrupt. Usually, using a RET instead of a RETI will cause the illusion of your main program running normally, but your interrupt will only be executed once. If it appears that your interrupt mysteriously stops executing, verify that you are exiting with RETI.