A child can first learn multiplication examples of one-digit numbers and can then learn the effects of multiplications. Then he or she can be taught multiplication tables of the other numbers and the multiplication process for 2, 3 and more digit numbers. Another alternative is to memorize the multiplication tables of the numbers and then learn the multiplication of single-digit, two-digit and three-digit numbers.
The first approach is being followed in the book. Chapter 4 describes individual instructions in the 8051 instruction set. This chapter gives examples of instructions, C statements, simple programs and interrupt service routines (ISRs) related to interrupts. A programming framework for programming using assemblers and C compilers will be described in Chapter 9.
We have already studied the following:
A precise and thorough understanding of the interrupt structure in 8051 and other MCU families has, however, not been dealt so far, although it is a matter of great importance for smart use and for the realization of full potential of any MCU.
The interrupt structure allows us to employ an MCU for real-time control of multiple tasks. It lets us achieve the real-time use and facilitates synchronization of the devices and resources.
This chapter enables a reader to understand the interrupt structure of different MCUs. It gives examples of how to write the programs in many important applications and of real-time interrupt latency control applications.
When a mobile phone rings and interrupts us, we immediately respond to it by setting aside whatever work we are involved in. There could be an important message awaiting us.
We respond to the phone call by pressing its key. This interrupts the presently executing instructions in the mobile. A service routine executes in the mobile on interruption by pressing the key. Similarly a service routine executes on interruption from another source.
A device such as a timer interrupts the processor’s presently running program. When a timer overflows, there is an interrupt. When a timer reaches a pre-fixed count or time, there may be an interrupt. When the timer receives an input in order to capture the instance of occurrence of an external event, there may be an interrupt. There may be periodic clock interrupts from a timer. The serial interface on serial input causes an interrupt. Serial interface on serial output interrupts when the transmit register becomes empty.
An error that occurs when a program runs or an input from an external event also causes an interrupt. A number of sources can interrupt the central processing unit’s (CPU) current program.
There are a number of sources of interrupt of a running program. Just as the interruption from the phone is important to us, the interrupts from the events from various sources are important to any MCU-based system.
Interrupt-handling structures in the CPUs and MCUs are used for a number of applications. A few examples of applications are as follows:
A call is a program instruction for diversion from the program after executing the current instruction.
Let us assume that a program or routine is executing and running a set of instructions. The running instructions are called foreground program. A foreground program instruction may call another routine at some point of time using instruction (for example, ACALL or LCALL instruction of 8051). The call instruction executes as follows:
Return from a Routine
The return instruction (last instruction RET at the called routine) executes as follows: It restores the PC, both higher and lower bytes, at PCH and PCL from the stack top and re-adjusts the SP to SP = SP′ − 2.
Note that the CPU registers other than PCH and PCL also saves in certain MCUs on the call to a routine and restores on return. For example, 68HC11 saves the registers also. Figure 5.1(a) shows the steps in the call to a routine and return from the routine.
Nested Calls
A call after executing an instruction is a user-programmed diversion from the program’s current sequence of instructions in foreground program A to another sequence of instructions in another routine B. The B can also call another routine C at a certain point of time and that can call the third routine D and so on. The return from D is to that instruction of routine C, which needs execution on return from the called routine D. The return from C is to that instruction of routine B, which needs execution on return from the called routine C. The return from B is to that instruction of foreground program A, which needs execution on return from the called routine B. The calls from A to B, B to C and C to D are also referred to as the nested calls in this case. Nesting of calls means that a routine can also call another routine from within. Figure 5.1(b) shows the steps in the nested calls.
Interrupt
A foreground program instruction may be interrupted for calling the service of another routine at an unknown instance of time. A call to interrupt service routine (ISR) on hardware interrupt is not a programmed (not from the user instruction at the program) diversion. It occurs after executing the current instruction on an event from an interrupt source.
Figure 5.1 (a) Steps in the call to a routine and return from the routine and (b) steps in nested calls
Interrupt Source
An interrupt source is a source of an event at a device or circuit that causes the interrupt. For example, the ringing of a phone is an interrupt source for the mobile. The key that is pressed on the phone is the interrupt source for the mobile. A timer timeout is an interrupt source. It causes from event of setting timer overflow flag.
An interrupt source may also be a software interrupt instruction (SWI) for the interrupt. The SWI executes when some exceptional condition is encountered during the running of a program.
Interrupt Service Routine
Suppose, a certain value for the number of ticks required for the timeout is loaded into a timer, and the timer is started using certain instruction. On starting, it starts accepting the clock inputs at regular intervals. An overflow of the timer represents its timeout. On a timeout-event, some action is required. The action on that event is called the timer overflow interrupt. The foreground program currently executed by the CPU can get the timer overflow interrupt at any instruction (at any point of time). On the timer overflow, interrupt of the ongoing program occurs after completing the instruction being executed. On the interrupt, the execution of instructions starts for a service routine. The new routine is called interrupt service routine (ISR).
An ISR is a routine that executes because of the interrupt (service demand) from a source.
An ISR executes on a hardware signal or software interrupt (also called exception or trap). The foreground program A diversion is from the current sequence of instructions in A to another sequence of instructions B, which services the interrupt. The sequence of these codes B is called interrupt service routine.
The ISR B may be required to first push the parameters of A in case the ISR is likely to change these during execution. A byte at an addressed location or register (other than the bytes at the PC) may also be saved by push. A push is on to the stack. Push can be of the Processor Status Word, accumulator, other special function registers or memory addresses that are likely to be affected by ISR instructions.
There are certain features in 8051, which most often makes it possible to have a short ISR and does not need the PUSH and POP instructions at the ISR start and before return from the interrupt.
Note that in certain MCUs on the interrupt call to a routine, the CPU registers other than PCH and PCL also save and therefore they also restore on the return. For example, 68HC11 restores the registers also.
Before the Return Instruction in ISR
All saved parameters and any byte at an addressed location or register (other than the bytes at the PC) pushed at the beginning must be popped in reverse order (last in first out; LIFO) before the return. ISR B must restore in the LIFO way, the saved parameters by the pops from the stack. The byte of an addressed location or register (other than the bytes at the PC) that was saved on the stack within the ISR must be restored by the ISR before the RETI instruction. These actions restore all the previous states of the variables and flags in the registers so that the foreground program functioning is not affected.
Return Instruction in ISR
The return instruction RETI (last instruction at the ISR) at the ISR B is executed as follows:
Suppose the PCL was saved first on the stack top, then the PCH. On return, first the PCH retrieves from the stack top and then the PCL. It means LIFO. Figure 5.2 shows the steps for an interrupt to a service routine and return from that.
The RETI is an instruction in 8051 for return from the ISR. It restores PC of the foreground program. It is similar to the RET instruction in a called routine. The additional difference is that the interrupt-handling structure resets and gets ready for the next ISR and next interrupt.
Figure 5.2 Steps for an interrupt to run a service routine and steps for return
The following steps are adopted for handling an interrupt in 8051 MCU:
Figure 5.3 shows different elements of an MCU interrupt-handling structure.
Interrupt-handling structure provides for the following:
Figure 5.3 Different elements of an MCU interrupt-handling structure
A source activates the interrupt-handling (servicing) structure on identification. A flag facilitates the identification of an event in the processor when an internal interrupt source is activated. A flag can be either at a register called interrupt status register or at the interrupt pending register or may coexist with the control bits in the control register. Examples of co-existence are in TCON and SCON registers in 8051. Table 5.1 gives the example of identification flags for the interrupt sources in 8051, instances of their setting to 1 and instance of their resetting to 0. Setting to 1 indicates the need for the interrupt handling, and then the interrupt-handling structure activates in case that interrupt is not masked. INT0 or INT1 level-activated interrupts do not change (latch 1) in IE0 or IE1 and thus do not have identification flags.
Table 5.1 Example of identification flags in 8051 and their setting and resetting
Table 5.1 also gives the instance of its resetting to prevent repetition of interrupt service from the same event for the occurrence of interrupt in 8051. Resetting for timer overflow and edge-triggered interrupts is by the hardware automatically on execution of ISRs. Resetting for TI and RI interrupts are by software (instructions in the ISR for servicing those) and are not automatically on start of execution of ISR.
Example 5.1
Figure 5.4(a) shows the example of occurrences of timer overflow TF0 and serial receive receiver interrupt (RI) at two instances. Timer overflow TF0 flag resets on start of ISR for it. However, for RI and TI, resetting must be done by an instruction before RETI and after the start of ISR to enable the next interrupt from the serial port.
Figure 5.4 (a) Example of occurrences of timer overflow TF0 and serial receive RI interrupt at two instances, (b) example of occurrences of repeated serial receive RI interrupts from the serial bits arriving at the RxD and (c) example of occurrences of repeated 1 to 0 edge transitions at INT0 input pin when counting the number of rotations of a wheel. The identification flag whether automatic or not on the ISR start settings is also shown in these figures
Figure 5.4(b) shows the example of occurrence of repeated serial RIs from the serial bits arriving at the R×D.
Example 5.3
Assume that a wheel is rotating. There is a photo-transistor PT and LED pair. The LED passes light through the wheel when the wheel completes one rotation. Thus, a PT starts conducting when the rotating wheel passes through 360°−2° angle of rotation and the LED is able to send light to it. It stops conducting when the rotating wheel passes +2° past one rotation. A pulse is thus generated at INT0 if this pin connects the PT. Figure 5.4(c) shows an example of occurrences of repeated INT0 inputs, for example, when counting the number of rotations of a wheel. Each wheel rotation brings a photo-transistor into a conducting state from the light. The identification of flag settings is also shown in these figures.
Example 5.4
Assume that a key sets 1 when released and when pressed resets to 0 the INT pin INT0 or INT1. When IT0 or IT1 = 0, then the interrupt is of a level-activated type. The pin level-0 activated interrupts do not change (latch 1) the IE0 or IE1. Figure 5.5 shows the timing diagram for the example of occurrences of repeated level-activated case ISR execution when INT0 input is kept 0 for long periods. Long means time interval greater than execution interval of the ISR. ISR repeat execution is prevented when either enable flag for INT0 or INT1 is made 0 or the level is made 1 from 0 at the INT0 or INT1 pin.
Consider that a CPU has been enabled to respond to an interrupt from a source. A call is then to be made on an ISR. Let the first instruction address of the ISR be ISR_ADDR. How does the CPU find or evaluate the ISR_ADDR? That is, how is step 4 (Figure 5.2) of the four steps pointed out above implemented before the diversion to the ISR? Table 5.2 gives three methods for an interrupt source or source group:
Figure 5.5 Example of occurrences of repeated ISR run when the INT0 input is kept at level 0 for long periods using a switch
Figure 5.6 (a) Direct address access method for an interrupt, (b) example of vector address for the ISR address for an interrupt source or source group and (c) example for vector address for the ISR address pointer for an interrupt source or source group
Figures 5.6(a), (b) and (c) show the steps in three methods.
Vector address ISR_VECTADDR
ISR vector address is a memory address where the CPU PC vectors (program flow changes the direction) automatically for the execution of ISR.
An action on interrupt from a source can occur only whenever the CPU has been previously enabled by the program (or internally by the CPU itself) to respond to that interrupt.
Table 5.2 Method of finding ISR address for an interrupting source or source group
a In 8086, an ISR_VECTADDR_Pointer is indirectly calculated. The external device just needs to put a byte for the type (level) of the interrupt in the second interrupt acknowledgement cycle. [Level means priority level in order of decreasing priority of 8086 CPU for interrupt service.]
Example 5.5
Advantage ofvector address for the ISR address itself: The vector addresses of the multiple interrupts can separate by 8 bytes or more. The 8 bytes can suffice for a short ISR piece of code. For example, when the successive characters of a string are to be sent on the TxD line from SBUF in 8051, the 8-byte space suffices (Program Example in Section 5.9).
Example 5.6
The 8051-interrupt structure generates a vector address for the corresponding source ISR. The ISR can be a maximum of 8 bytes, which can be located at the vector address or from where a call can be made to the longer piece of codes from there.
Advantage ofvector address for the ISR address pointer: The vector addresses of the multiple interrupts can be very near each other, separated by 2 or 4 bytes.
Example 5.7
8086, the vector addresses start from 0x0000 and each one is separated by 4 bytes only. The vector addresses for the ISR pointers are 0x0000, 0x0004, 0x0008, 0x000C, …, 0x00FC for type 0, type 1, type 2, type 3, …, type 255 interrupts. There can be 256 interrupt types (levels) of the interrupts. Two bytes at each address give 16 bits for the IP (instruction pointer 16-bits) and the next 2 bytes give 16 bits for the CS (code segment 16-bits) needed for calculating the 20-bit ISR address in 8086. Instruction physical address = (CS) × 10000H + (IP).
The 8086-interrupt structure generates a vector address for the corresponding source ISR pointer. The ISR can be at a physical address, pointed by the segment and instruction pointer defined at the 4 bytes located at the vector address. Successive interrupt levels and, therefore, vector addresses are separated by 4 bytes and there are a total of 256 levels of interrupts.
Example 5.8
The 68HC11/12 family interrupt structure generates a vector address for the corresponding source ISR pointer. Successive vector addresses are separated by 2 bytes.
The UART serial port interrupt for the receiver and the transmitter has a vector address of ISR_ VECTADDR_Pointer = 0xFFD6-0xFFD7 for finding the higher and lower bytes of ISR. The PC loads these bytes for executing the ISR for the serial port interrupt.
Interrupt latency can be defined as the interval between the interrupt event and the start of the interrupt service for that event. For example, if a timer has overflowed at an instance t0 and its service routine initiates at time t1, then the latency interval for it equals Tlat = (t1−t0) (refer Sections 5.6 and 6.3.1 for further description). Let the timer overflow interrupt be serviced latest by t2, then the deadline Td = (t2 − t0). Interrupt deadline means interrupt service deadline. It is defined as maximum permissible interrupt latency plus execution interval of the ISR.
Example 5.9
Example 5.10
Figure 5.4(a) shows the example of occurrences of timer overflow TF0 and serial receive RI at two instances. If the time difference between these two instances is 100 μs.
Solution:
Example 5.11
Assume that the mode 3 (11-bit UART) serial interrupt is receiving the bits at 9,600 baud/s. The repeated interrupts (Figure 5.4(b)) are therefore occurring at every 11/9,600 s. Therefore, the deadline for servicing each interrupt, Td = 11/9,600 s. Therefore, the latency for the serial port SI interrupt should be less than 11/9,600 s. Td < Tlat is the requirement, and priority assignments have to be according to this, else a service to an event is missed. If ISR for serial buffer takes 8 μs, the latency = 11/9,600 second − 8 μs = 1,145.83 − 8 μs = 1,137.83 μs.
Example 5.12
Assume that 80196 MCU provides for a FIFO in place of a serial receive-buffer for just 1 byte. It provides three interrupts, I1 when the first byte is in the buffer, I2 when FIFO is half full and I3 when FIFO is full. Assume that bytes are received every 11/9600s.
When I2 is used, 4 bytes make FIFO half full before the next interrupt. Time is still there for 4 more bytes to be received. Hence, the deadline = 44/9,600 s.
When I1 is used, I1 must be serviced before the next serial byte get into the buffer. The deadline = 11/9,600 s.
When I3 is used, 8 bytes make FIFO full before the next interrupt. Time is still there for 8 more bytes to be received after the first byte is received. Hence, the deadline = 88/9,600 s.
The use of I3 provides high deadline period for servicing serial interrupts.
An MCU has a number of devices and a device may have multiple functions. There are, thus, multiple sources of the interrupts to a program being executed by the CPU of an MCU. Figure 3.18 shows the multiple sources of the interrupt. For example, refer timer interrupt and SI (Serial Port Interface) 8051 interrupt sources.
A source can be either internal or external. For example, INT0 is an external interrupt and T0 overflow is an internal interrupt source in 8051. Each of the interrupt sources demands a temporary transfer of control from the presently executed program to an ISR corresponding to the source (or source group). The internal sources differ in different CPUs or microcontrollers. Ways of external sources interrupt could also differ. The important internal and external interrupt sources are described as follows:
Hardware interrupts related to external sources or peripherals: At an external pin, on either occurrence of a level 1 or 0, or a positive or negative edge transition, an interrupt may be caused. It depends on the CPU. Table 5.3 gives the examples of hardware-related external interrupts.
Table 5.3 Examples of external pin interrupts and methods of finding their vector addresses
Method of Finding the Address | Example |
---|---|
Direct address access |
There is an interrupt pin, for example, in 80×86. It is INTR. The external circuit, which interrupts, also sends the interrupt type or the level on the data bus. It does so in the second INTA cycle at the INTA pin for the interrupt acknowledgement to the circuit. (This pin is not present in 8051 family MCUs.) |
Vector address as the ISR address |
When the external circuit interrupts at this pin, the CPU hardware generates the vector address internally. The examples of these pins are RST5.5, RST6.5 and RST7.5 pins in 8085, where the vector address generated internally = (5.5 or 6.5 or 7.5)×8. Note that this calculation of address formula uses the decimal numbers. INT0 and INT1 pin external interrupts are the examples in 8051 MCUs. |
Vector address for the ISR address pointer |
NMI pin in 80x86 is an example. The vector address, which generates, points to the 4 bytes for the bits to be loaded into the IP and CS registers, which then define the ISR_ADDR. The program then executes the ISR from the 20-bit address (IP) + (CS)×10000H. |
Software-related interrupts: Table 5.4 gives the examples of the software error-related interrupt.
Table 5.4 Examples of software-related interrupts
Interrupt | Example |
---|---|
Software instruction to enforce an interrupt |
a. ‘SWI’ instruction in 68HC11/12 family (refer Section 14.2.2(6)) or in ARM |
The software error-related interrupts in 80×86. Examples are the division by zero (type 0), illegal opcode (type 6) and overflow (type 4). |
aRefer Table 7.13 in Daniel Tabak, Advanced Microprocessors, McGraw-Hill, International Students Edition, 1995, pp. 145–147.
NON-MASKABLE INTERRUPT SOURCES
Certain interrupt sources cannot be masked (corresponding ISR cannot be disabled from execution) and these must be serviced (8051 has all interrupts that are maskable). Table 5.5 gives examples of non-maskable interrupts.
Table 5.5 Examples of non-maskable interrupts
Processor Family | Example |
---|---|
80×86 |
An example, NMI pin interrupt in 80x86. It usually connects to DRAM parity error detection circuit. A few of exceptions (traps) also need to be always serviced. |
68HC11/12 |
a. Clock monitor failure. The slow down in the clock must interrupt and execute the required instructions. |
There are a number of devices and therefore multiple sources of interrupts. There are certain interrupts that can be masked, so those events are not responded by the interrupt-handling structure. Figure 3.18 shows the different maskable interrupts in 8051. Table 3.19 gives the interrupt enabling-disabling bits, IE.0 to IE.7 for these.
An MCU-based system may require the following during the execution of a foreground program or routine:
Example 5.13
CPU of MCU hardware may have one of the following two features:
Nested Interrupts
Nested interrupts become feasible in case 1. An interrupt after executing an instruction is an event-triggered diversion from the program’s current sequence of instructions in foreground programA to another sequence of instructions in another ISR B. Assume that interrupt priorities are highest at D, highest but one C and lowest is B. B can therefore also interrupt to run the ISR C at a certain point of time and that can interrupt to run ISR D and so on. The return from D is to that instruction of ISR C, which needs execution on return from the D. The return from C is to that instruction of ISR B, which needs execution on return from the C. The return from B is to that instruction of foreground program A, which needs execution on return from the B. The interrupts from A to B, to C and to D are also referred to as the nested interrupts in this case. Nesting of interrupt means that a routine can also interrupt to another routine from within. Interrupt from in-between is also called preempting. Preemption is by a higher priority interrupt.
Figure 5.7(a) shows steps in the interrupt call to a routine (ISR) and return from the ISR in an interrupt-handling structure (hardware and software). Figure 5.7(b) shows nesting of ISRs in cases of pre-emption.
Critical Section Instructions
The operations using a global variable or shared variable or memory buffer are done by instructions called critical instructions. Critical instructions are the instructions which must execute first before program flow to other task. (Program flow changes by context switch to other task.)
Example 5.14
Handling critical section codes: All maskable interrupt sources can also be masked by a single enabling/disabling bit. This helps in the execution of a critical section in current routine.
Figure 5.7 (a) Steps in the interrupt call to a routine (ISR) and return from the ISR in an interrupt handling structure when in-between diversion is not permitted and (b) nesting of ISRs in cases of preemption (in-between diversion) permitted
One method of handling critical codes for the operations on shared or global variables is using the instruction for disabling interrupts before the start and for enabling interrupts before the end of the critical section codes.
Polling
Several interrupt events can occur in quick succession before an interrupt is serviced by the ISR. Sometimes a source group is handled by a common ISR. An interrupt-handling structure in the system may provide for a polling mechanism. For example, serial port input and output interrupts are serviced by a common ISR.
Polling means to check each interrupt source one by one using instructions and service source which interrupted. The service is by calling the corresponding interrupt service routine. Polling is done one by one using instructions. It is done by successively checking for each interrupt which one occured. The checking continues by polling for the occurrences of different interrupts. It is done by finding whether the identification flag for an interrupt is set or reset. Figure 5.8 shows the sequence of instructions for polling and finding the pending interrupts.
Alternative to Polling
Polling to check each interrupt source one by one and service that interrupts by calling the service routine can take time when there are many interrupts. Therefore, there are two alternatives.
The Alternative to polling is CPU using vectored interrupts mechanism. Vector address sequencing is as per the priority. When an interrupt occurs, if it is not masked, then the interrupt-handling structure hardware causes the processor to vector to an address to enable the servicing of that interrupt by executing the service routine for that interrupt.
When the alternative is vectored priority interrupt, if a higher priority interrupt occurs, and if it is not masked, then the interrupt-handling structure hardware causes the processor to vector to ISR address of higher priority interrupt. This enables the servicing of that interrupt by executing the service routine for that interrupt. After the return from the higher priority ISR, the preempted ISR starts and executes the remaining instructions.
Polling among Common Interrupts in a Source Group Service by a Common ISR
Sometimes a source group is handled by common ISR. For example, serial port input and output interrupts are serviced by a common ISR. Polling then means to check each interrupt source in the group one by one using instructions and service, which interrupt by calling the set of service routine instructions. Polling one by one using instructions is done successively only for the interrupts in the group. For example, finding which of the identification flags for the interrupts are set or which reset in the group. Figure 5.9 shows the sequences of instructions for polling and finding pending interrupts in a group of interrupts TI and RI.
Figure 5.9 Sequences of instructions for polling and finding pending interrupts in a group of interrupts TI and RI
Instances of Vectoring to Higher Precedence Interrupt
An interrupt is under servicing by a temporary diversion to an ISR, and at that instant, another interrupt occurs which should also be serviced as per precedence assigned by CPU for vectoring to that interrupt. Two possibilities exist—either the second is serviced only after the completion of the ISR in service of the first interrupt, or the second interrupt is to be serviced immediately (after finishing the current instruction), giving it precedence over the first. First case example is 68HC11/12 and second case example is 8051.
Figure 5.10 shows the vectoring instances in two distinct family MCUs. When and how does the vector priority service take place? Table 5.6 lists the various vector priority service methods in the different interrupt structures in the different family MCUs.
Table 5.6 Various vectored priority interrupt procedure methods in different interrupt structures in the different families of MCUs
a The critical set ofinstructions, interrupt structure disabled at the set start, for example, by IE.7 bit in 8051 reset to 0 and enabled at the set end. For example, by IE.7 bit in 8051 set to 1.
b The priority is default priority, in case an IP register bit is not assigned ≡ 1. When IP register bits of two sources are equal, then the default priority prevails.
c Except for a specific interrupt source group special case XIRQ, which can be set to be polled at the end of each instruction in the presently executed ISR.
d 68HC11/12 has the fixed priorities only with no user-assigned priority values. Only the XIRQ non-maskable interrupt has the highest priority and is polled for its occurrence at the end of each ISR or foreground program instruction.
Figure 5.10 Two vectored priority interrupt procedures for the interrupt source: (a) 8051 and (b) 68HC11/12
A higher priority ISR cannot pre-empt a lower priority interrupt under execution in this case. We can define the priorities according to the earliest deadline, and a higher priority ISR can pre-empt a lower priority interrupt provided that interrupt source is not masked at the interrupt instance. The latency for the higher priority interrupt is minimized. However, the current service will have increased servicing interval and can miss the deadline. Therefore, ISRs should be made short.
Example 5.15
Assume that there are four interrupts, 1, 2, 3 and 4 in priority order (highest priority assignment is for 1 and lowest to interrupt 4). Let interrupt 3 be under service and have an execution time of 2 ms still left for return at an instant t. Interrupt 4 is pending at t since 1 ms. Now, a higher priority interrupt 1 pre-empts interrupt 3 at t. Let its execution time be 5 ms. Now, the latency for interrupt 4 becomes 1 + 5 + 2 = 8 ms. The latency for interrupt 1 is 0 ms as it gets services just after t. (Neglecting the time for context switching like saving the PC and registers.) Figure 5.11 shows the latency interval extension because of preemption. Interrupt 3 will now restart the service after 5 ms and will take 7 ms more to finish. If we have a user-assigned priority provision, whenever the pre-emption by the higher priority task has an adverse effect on the current ISR, it can be controlled.
A higher priority ISR cannot preempt a lower priority interrupt under service in this case.
If the interrupt source is not masked at the interrupt instance at the end of the ISR, then the higher priority interrupt source will be executed. The latency for the higher priority interrupt thus equals the time left for finishing the current ISR. The lower priority interrupt services will have an increase in the latency interval by an interval equal to the execution time of the interrupt service. We can define the masks keeping in view the default priorities so that ISR execution is according to the first earliest deadline in case we have to lower the latency of the low priority interrupt.
Figure 5.11 (a)Assumed priority order and (b) the execution interval extension of the current ISR due to preemption at t the end of an instruction in the ISR (example of MCU 8051) and the effect of masking ISR-1 on interrupt 4 latency
Example 5.16
There are four interrupts, 1, 2, 3 and 4 in priority order (the highest priority assignment is for 1 and the lowest to interrupt 4). Let interrupt 3 be under service for 8 ms and have an execution time of 2 ms still left for return at an instant t. Interrupt 4 is pending at t since 1 ms. Now, once ISR 3 starts a higher priority interrupt 1 cannot pre-empt interrupt 3 at t, but only after the return from interrupt 3. Assume its execution time is 5 ms. Now, once ISR-3 starts the latency for interrupt 1 becomes 10 ms (neglecting the time for context switching by saving PC and registers). Figure 5.12 shows the latency interval extension because of pre-emption only at the end of an ISR. A started ISR is executed until the end (return instruction RETI), and the maximum latency interval of each service is therefore predictable. Note that if interrupt source 1 is masked, then the latency for interrupt 4 will become 3 ms only. Interrupt 3 will take only 2 ms to finish, which is the same as without the occurrence of interrupt 1 in between. Before the return from interrupt 4, interrupt 1 is unmasked. Interrupt 1 minimum latency is now 10 ms plus time taking in execution of ISR-4.
We can get an equivalency between the above two approaches using appropriate SWIs so that the earlier deadline ISR starts within the permitted latency.
Example 5.17
The first case (Section 5.6.3) becomes identical to the case (Section 5.6.4) if (i) all interrupts are disabled at the start of an ISR by DI instruction and (ii) enabled before RETI by another EI instruction. Alternatively, the user temporarily assigns the current ISR as priority 1 and others 0 before the start of an ISR.
Figure 5.12 (a) Assumed priority order and (b) the extension of latency interval of higher priority ISR due to preemption permission only at the end of the ISR (example of MCU 68HC11/12)
Let an MCU interrupt structure have the following features:
Each of the multiple-interrupt sources (including traps or exceptions) has a default-assigned priority on reset of MCU. Default MCU-assigned priorities at the interrupt structure set a structure in which the priority can be as per the requirement in most application needs in real time. For example, Table 3.20 shows that INT0 has the highest priority and UART the lower ones. The reason why serial interrupts are assigned low priority by default in the hardware is that these take place at a specific rate, for example, at every 11/9,600 s or 11/1,99,200 s for mode 3 SI interrupt and 9600 baud rate. The external interrupt at INT0 assigned the highest priority as it may be from some source, which demands immediate attention.
Example 5.18
8051 has default priorities assigned for seven or nine sources (Figure 3.19). The default internal assignments on reset in 8051 from Table 3.20 are as follows:
Default Priority | Interrupt |
---|---|
1 (Highest) |
INT0 |
4 |
TF0 |
5 |
INT1 |
6 |
TF1 |
7 |
TI and RI |
Certain MCUs provide for a user assigning the priorities by overriding the default-assigned priorities. Two notable features of user-assignable priorities are as follows—user-assigned priorities always override the default priorities and user-assigned priorities give a structure in which the priority can be increased or lowered as per the need in real time.
Example 5.20
Example 5.21
Give examples of when we assign the priorities.
Let the repeated serial buffer interrupts (Figure 5.4(b)) occur at every 11/9,600 s (assuming that the UART setting in SCON is in mode 3 and 9,600 baud.) If the byte is not received by servicing the RI interrupt every 11/9,600 s, the byte will be missed when the MCU starts servicing another program of more than 11/9,600 s. An A/D conversion interrupt can be assigned a lower priority in case only one measurement per second suffices. Let the signal from ECG (Electro-cardiogram) be recorded using the A/D conversion. If the converted byte is not put in the memory and the timer count is not put in the buffer on the highest priority, the MCU will not be recording the ECG signal faithfully. A/D conversion interrupt has to then be assigned highest priority and other interrupt sources can wait.
Example 5.22
The classic version of 8051 family MCU has the interrupt structure for two external interrupts, INT0 and INT1 (also INT2 in 8052) and internal interrupts from the timing and serial devices.
Timer Overflow (TF) Interrupts
Refer Table 3.11. TF1 and TF0 are flags for interrupt identification at TCON.7 and TCON.5, respectively, for the timer 1 and timer 0 overflows. TF1 or TF0 auto resets to 0 on executing the ISR instructions (Figure 5.4(a)). Resetting the flag to 0 enables the next instance execution of the ISR on the next overflow.
RI (Receiver Interrupts and TI (Transmitter Interrupts)
Refer Table 3.11. TF1 and TF0 are flags for interrupt identification at SCON.1 and SCON.0, respectively, for the serial port output and input. Flag TI or RI becomes 1 on completion of the serial output or serial input, respectively. They need resetting to 0 by the ISR instructions as they do not auto reset to 0 on executing ISRs. Resetting the flag to 0 enables the next execution of the ISR on the next serial output or input.
Edge-triggered Interrupts INT0 and INT1
Refer Table 3.11. IE1 or IE0 at TCON.3 and TCON.1, respectively, is INT1 or INT0 interrupt negative edge event identification flag when IT1 or IT0 = 1, respectively. The IE0 or IE 1 flag is latched (becomes 1) on occurrence of edge-triggered interrupt. RETI restores the interrupt event flag. The flag therefore auto resets to 0 on executing the ISR (Figure 5.4(c)). Resetting the flag to 0 enables the next execution of the ISR on the next negative edge at INT 1 or INT0.
Level-0 activated Interrupts INT0 and INT1
The IE0 or IE 1 flags are not latched (do not become 1) on occurrences of level-activated interrupts. RETI does not affect interrupt event flags. If we wish to avoid repeat execution of the ISR then before the RTI, either interrupt INT 0 or INT1 causing source should make the interrupting pin = 1 or DI disable interrupt should disable the corresponding IE bit for EX0 or EX1 bit in the IE register in case of a level-triggered interrupt on INT0 or INT1 pin.
Synchronous Serial RI (Receiver Interrupts) and TI (Transmitter Interrupts) Separate in Certain Versions
Serial port mode 0 interrupt flags TI and RI and becomes 1 on completion of the shift of the last bit out of 8-bits serial output and serial input, respectively. They need resetting to 0 by the ISR instructions. This prevents a repeat execution of the ISR. The next serial input or output can then be serviced by the ISR execution again.
We find that most groups have one interrupt source, and for each group there is a vectored priority interrupt structure in 8051.
The default-assigned priority-wise source groups are as follows (higher priority ones are described first):
Just as we ignore the ringing of our mobile phone by keeping the phone in the silent mode, there is an interrupt-handling mechanism for the unmasking and masking of an interrupt source. Meanings of unmasking and masking are the enabling and disabling of recognition of an interrupt from a source for a service. Service means the execution of a set of instructions called the ISR (refer Table 3.19). The mechanism uses the bits at the IE register at the SFR space within 8051 at the following addresses for the byte (Column 3) and addresses for the bits (Column 4).
Refer Table 3.20. The default priority assignments are as follows:
Default Priority | ISR |
---|---|
1 (Highest) |
External interrupt at pin INT0 |
2 |
Timer T0 overflow interrupt |
3 |
External interrupt at pin INT1 |
4 |
Timer T1 overflow interrupt |
5 |
Serial UART interface interrupt and serial synchronous SI mode interrupt (available in classic variants) |
6 |
Interrupt vector address timer T2 overflow cum T2EX pin negative transition for the time capture or reload at T2 |
7 |
Serial synchronous SI mode interrupt (available in a few family variants) |
Just as we select priority in responding when there are multiple pending actions, the interrupt-handling mechanism also provides for the priority assignments for service in the case of multiple interrupts from the sources. The mechanism uses the bits at the IP register. The register is at the SFR space. The programmer can also use the default priority assignments, which the system assigns at the start (reset). Refer Table 3.19. The mechanism uses the bits at the IP register at the SFR space within 8051 at the following addresses for the byte (Column 3) and addresses for the bits (Column 4).
The interrupt-handling mechanism also uses a vector address for the ISR codes. There is an interrupt vector address corresponding to each interrupt source and group of sources. For example, 8051 has vector address = 0x0003 and ISR addresses between 0x0003 and 0x005A (refer Figure 3.18 and Table 3.20). It is reproduced below for ready reference.
Addresses for ISR | Explanation of the Function |
---|---|
0x0003-0A |
Interrupt vector address for external interrupt at pin INT0 |
0x000B-12 |
Timer T0 overflow interrupt vector address |
0x0013-1A |
Interrupt vector address for external interrupt at pin INT1 |
0x001B-22 |
Timer T1 overflow interrupt vector address |
0x0023-2A |
Serial UART interface interrupt vector address (common for TxD serial outputs and RxD serial input frames) |
0x002B-32 |
Interrupt vector address timer T2 overflow cum T2EX pin negative transition for the time capture or reload at T2 |
0x0053-5A |
Serial synchronous SI mode interrupt (available in a few family variants) |
Default Conditions in 8051 on Reset
A noise-free input 1 for two machine cycles at the reset input causes the program to start from PC ≡ 0000H, (PCL ≡ 00H and PCL ≡ 00H). The special function registers, A, B, DPH, DPL, IE, PSW, SCON, TCON, TMOD, TH0, TL0, TH1, TL1, T2CON, TH2, TL2, RCAP2H, RCAP2L and T3 ≡ 00H. The IP, except its 7th bit, loads all bits to 0. User-assignable priorities are thus initially set to low, PCON bits except its 4th to 6th bit, as 0. The default SP ≡ 07H, WDCON ≡ A5H to disable the watchdog timer interrupts from T3 at the reset.
Interrupt Handling after Reset
A program in 8051 can be interrupted from the seven (or more in certain versions) permissible sources. This enables the uninterrupted execution of a set of instructions by disabling at the first instruction of the set and enabling at the last instruction of the set. When an interrupt occurs, the CPU in Intel 8051 does the following actions:
Step 1: The CPU pushes two CPU registers PCH and PCL on to the internal stack.
Step 2: Reset the EA = 0 momentarily (until the execution of the first instruction).
Step 3: The CPU sets the PC bits as per the corresponding ISR_VECTADDR, one among the seven vector addresses. ISR_VECTADDR is a vector address (Table 3.20). It is an address where the CPU PC vectors (program flow changes the direction) for the execution of the ISR after Step 2. A service program can either execute the routine or jump or call the ISR’s first instruction address if the ISR’s length is longer than 8 bytes and the routine executes from there.
Step 4: Set EA again to = 1 automatically at the ISR on execution of the first instruction. This enables the interrupt service from interrupts of priority higher than the present one.
After the first instruction at the ISR, servicing of any further interrupts from any lower priority interrupt are the only ones disabled until RETI executes and returns from the ISR occur. Vectored priority interrupt service occurs at the end of each instruction. (An ISR instruction must not have reset EA ≡ 0.)
Example 5.23
Timer 0 overflow interrupt in 8051 is enabled for interrupt service when both EA = 1 and ET0 = 1. (Refer Table 3.19, IE.7 bit EA and IE.1 bit ET0) EA = 1 unmasks the interrupt service structure for all sources, and ET0 = 1 unmasks T0 overflow interrupt.
Example 5.24
Timer 0 overflow interrupt in 8051 is assigned lower priority than serial UART interrupt when PT0 = 1 and PS = 1 as default priority (Refer Figure 3.18) on reset is that T0 IP is higher than the serial UART interrupt (refer Table 3.19, IP. 1 bit PT0 and IP.4 bit PS).
A reader who is not familiar with the use of an assembler, C compilers for 8051 and C language must first read the relevant sections in Chapter 9.
Default Settings on MCU Reset
When we write a program related to interrupt service, default settings on the MCU reset are remembered for the special function registers. The settings in the SFRs may require redefining for the interrupt handling by the ISRs. Table 5.7 gives these settings.
Table 5.7 Default settings on reset, which may require redefining for interrupt handling by the ISRs
SFRs | Default Setting on Reset | Action |
---|---|---|
A and B |
b00000000 |
Accumulator and B register are 0 to start with |
b00000000 |
Both timers T0 and T1 are in mode 0 |
|
TH2, TL2, RCAP2H and RCAP2L |
0X00 in each |
Timer T2 registers have all bits = 0 |
TH0, TL0, TH1 and TL1 |
0X00 in each |
Both timers are initially loaded with all bits = 0 |
IP |
bX000 0000 |
Default priorities of vectored priority interrupts will be used in case IP bits are not redefi ned |
IE |
b00000000 |
All interrupts get disabled and all interrupt source bits masked. [The interrupts should not occur before the foreground program initial instructions (booting up instructions) execute] |
PSW |
b0000 0000 |
Default use of Bank 0 for registers R0 to R7 |
SP |
0x07 |
Default use of 08H as stack top where the first pushed byte is saved |
WDCON |
0x0A5 |
Default disable watchdog timer interrupts from T3 |
DPH–DPL |
b0000 0000 – b0000 0000 |
Default eternal data memory address pointer at 0000H |
TCON, SCON and T2CON |
0x00 in each |
All interrupt flags reset |
X means 1 or 0.
Foreground Program Basic Instructions/C Statements
The following are the steps:
There are many instructions in 8051, which most often makes it possible to have a short ISR by using the pointers R0 and R1 and not use the PUSH and POP instructions at the ISR start and before the RTI instruction (return from interrupt). 8051 PSW does not have a zero flag. Therefore, there may not be any need to push PSW in many ISR programs in 8051.
Programming Approach for Getting Ready for the Interrupts in the Foreground Program
A foreground program has the following instructions after the seven steps described above:
Programming Approach for the ISR Instructions
The following approach is adapted when programming the ISRs:
The following are the programming examples of interrupt-related programs and routines.
Program Example P5.1
INT0 Negative Edge-triggered Interrupts
Assume that on an interrupt by negative edge, the ISR instruction calls a routine at address 1000H.
Getting Ready for the Interrupts in the Foreground Program
A foreground program needs the following programming approach:
Assembly instructions will be as follows:
CLR 89H; |
IE0 = TCON^1 = 0 INT0 interrupt flag reset |
SETB 0x88; |
IT0 = TCON^1 = 1 INT0 interrupt type is set for edge-triggered inputs |
SETB 0x0A8; |
EX0 = IE^0 = 1 |
SETB 0x0AF; |
EA = IEA7 = 1 |
|
or |
ORL IE, 0x81; |
Set EA (enable all) and EX0 (enable external interrupt) without modifying; other source mask bit |
The C statements will be as follows:
TCON^1= 0;
TCON^0 = 1;
IE^0 = 1;
IE^7 = 1; or
TCON = TCON | 0x01;
IE = IE | 0x81;
ISR Instructions
ISR instructions at INT0 vector address 0003H is as follows:
The C statements will be as follows:
void isr_func (void) interrupt 0 () { void routine1000 (void);
routine1000 () ;
}
Routine Instructions
Org: 1000H Instructions for routine1000
. .
.
.
RET; Return from the routine
The C statements will be as follows:
void routine1000 (void)
{
}
Examples P5.2 and P5.3 of codes demonstrate very important features of 8051 architecture.
There is no zero flag and there is no need of it because the DJNZ instruction itself takes care of the content of the counter zero or not zero. The register or directly addressed RAM is used as counter in the DJNZ. Had a zero flag been present to save the original state, PUSH PSW would have had to be executed at the start and POP PSW before the return. It may not have become possible to fit the code within 8 bytes space. Four important features are as follows:
Program Example P5.2
Interrupt-driven Data Transfer Through the Port by an External Peripheral
This example shows how the ISR addresses between 0x0003 and 0x000A are used for INT0. The ISR uses the same bank 3 for the service routine addresses. The foreground program also uses bank 3. There is a short service routine without PUSH or POP. There is an external device, which interrupts the 8051 after sending its output to the input at port P1. Assume that on each interrupt at the negative edge at INT0 pin, the ISR instruction transfers a byte at port P1 to RAM 64 byte memory buffer at internal RAM addresses 40H between 7FH.
Foreground program instruction: PSW is to be defined for bank 3
Getting Ready for the Interrupts in the Foreground Program
A foreground program needs the following programming approach:
Assembly instructions will be as follows:
SETB 0D3H; |
RS0 at PSW.3 = 1 |
SETB 0D4H; |
RS1 at PSW. 4 = 1 |
CLR 89H; IE0 = TCON^1 = 0; |
INT0 interrupt flag reset |
SETB 88H; IT0 = TCON^l = 1; |
INT0 interrupt type is set for edge-triggered inputs |
MOV R1, 40H; |
Let R1 be the destination block address for 16 bytes |
SETB 0A8H; EX0= IE^0 = 1; |
|
SETB 0AFH; EA= IE^7 = 1; |
|
|
or |
ORL IE, 0x81; |
Set EA (enable all) and EX0 (enable external interrupt) without modifying; other source mask bit. Using ORL in place of two SETB instructions |
The C statements will be as follows:
unsigned char [] data dest_block |
/* Declare destination block of bytes for dest_block */ |
unsigned char data buffer_end |
/* Declare bufffer_end (an address after top of buffer) */ |
dest_block = 0x40 |
/* Assign present dest_block address available = 40H in interna RAM */ |
buffer_top = 0x80 |
/* Assign present buffer_end + 1 address = 80H in internal RAM */ |
/* RS1 = 1 */ |
|
PSW^3 = 1; |
/* RS0 = 0 */ |
TCON^1 = 0; |
/* TCON.1 flag for INT0 edge interrupt reset */ |
TCON^0 = 1; |
/* TCON.0 set for INT0 as negative edge interrupt */ |
IE^0 = 1; |
/* EX0 at IE.0 = 1 */ |
IE^7 = 1; |
/* EA at IE.7 = 1 */ |
|
or |
TCON = TCON | 0x01;
IE = IE | 0x81;
ISR Instructions
ISR instructions at INT0 vector address 0003H will be as follows:
|
Org: 0003H Instructions for INT0 ISR at vector address 0003H-000A |
CJNE R1, 0x80, + I; |
Compare R1 with 80H, i.e. with the top of the memory buffer +1, if not equal; jump to ; transfer from port P1 else return in the next instruction |
RETI; |
Return from interrupt |
MOV@R1, PI; |
Source byte at P1 transfers to destination block at 40H |
INC R1; |
Increment R1 to point to next location of buffer |
RETI; |
Return from the routine |
The C statements will be as follows:
void isr_func { void interrupt 0 ( unsigned char[ ] data dest_block, unsigned char data buffer_end, unsigned char byte-count) {
if (R1 >= 0x40 && R1 < buffer_end) { dest_block [ byte_count-1] = P1; byte_count++; R1++; };
/* P1 transfers to the array at destination block and byte count increments by 1)
}
Program Example P5.3
Enabling Interrupts from Two Sources INT0 and INT1 and Application of Level-triggered Interrupts
Example of a coding sheet for enabling INT0 and INT1 and masking the remaining ones
Mnemonic | Comments | Flag(s) Affected |
---|---|---|
SETB IT0 |
Set interrupt INT0 type as falling edge-triggered interrupt |
TC0N.0 |
CLR IT1 |
Clear interrupt INT1 type as level 0 activated interrupt |
TCON.2 |
CLR IE0 |
Clear interrupt INT0 type as falling edge-triggered interrupt |
TC0N.0 |
CLR IE1 |
Clear interrupt INT1 type as level 0 activated interrupt IE1 remains 0 in the level-activated mode |
TCON.2 |
MOV IE, # 85H |
Set and enable interrupt structure, enable (unmask) INT0, set and enable (unmask) INT1 [Refer Table 3.19] |
IE.7, IE.2, IE.0 bit |
The C statements will be as follows:
TCON^1= 0; |
/* TCON.1 flag for INT0 edge interrupt reset */ |
TCON^0 = 1; |
/* TCON.0 set for INT0 as negative edge interrupt */ |
TCON^3 = 0; |
/* TCON.1 flag for INT1 edge interrupt reset */ |
TCON^2 = 0; |
/* TCON.0 set for INT1 as level-triggered interrupt */ |
IE^0 = 1; |
/* EX0 at IE.0 = 1 */ |
IE^7 = 1; |
/* EA at IE.7 = 1 */ |
The C statements will be as follows:
TCON = TCON | 0x01;
IE = IE | 0x85;
Program Example P5.4
Transmission of Characters from the Serial Interface
The program transfers the bytes starting from 60H to downwards up to 01H to the serial buffer.
Getting Ready for the Interrupts in the Foreground Program
The foreground program needs the following programming approach:
Assembly instructions will be as follows:
MOV SCON #b11000 0000; |
|
MOVR0, 0x60: |
Let R0 be the internal RAM pointer address for addresses between 60H and 01H |
SETB 0x0AC; |
ES at IE^4=1 |
|
= 1 |
SETB 0x0AF; |
EA at IE^7 = 1 |
|
or |
ORL IE, 0x90; |
Set EA (enable all) and ES (enable external interrupt) without modifying ; other source mask ;bit. Using ORL in place of two SETB instructions |
The C statements will be as follows:
SCON = 0xC0; |
|
source_block = 0x60; |
/ * Assign present source_block address available = 60H in internal RAM */ |
byte_count = 0x00; |
/* Initialize byte counts */ |
IE^4 = 1; |
/* IE.4 ES = 1 */ |
IE^7 = 1; |
/* IE.7 EA = 1 */ |
|
or |
IE = IE | 0x81; |
/* IE.4 ES = 1 and IE.7 EA = 1 */ |
ISR Instructions
The serial interface interrupt vector address is 0x0023 (Table 3.20). Therefore, the ISR has to be located at 0x0023. Program Example P5.4 shows that the 8-byte address space between 0x0023 and 0x002A can be used to locate the 7-byte ISR. Program Example P5.1 shows that if ISR is longer than 8 bytes then only another set of addresses needs to be used as the extension of the ISR codes. One can, however, write the code for the present case within the 8 bytes as given in Program Example P5.4. This is because of the use of R0 as a loop counter as well as a data pointer. ISR instructions at INT0 vector address 0x0023 using a coding sheet are as follows:
Example of codes of interrupt service routine using a coding sheet
a Machine codes are given above to show how DJNZ with Rel = 4 is coded, how TI and SBUF are used and how CLR is used. When an assembler is used, the machine codes are automatically generated from the mnemonics.
The C statements will be as follows:
void isr_func (void) interrupt 0 (unsigned char [ ] data source_block, unsigned char byte_count ) {
If (R0 <= 0x5F && R0 > 0x00) {
SBUF = source_block [byte_count−1];
R0 − −;
byte_count ++;
SCON^1 =0; }; /* SBUF transfers from the array at the source block, TI flag clears for the next interrupt and the byte count increments by 1)
}
Call: A program diversion to another routine, for example, by LCALL or ACALL instruction in 8051 or function call in C. The PC is saved onto the stack on the call and new routing address retrieves in the PC.
Catching an exception: Execution of ISR on throwing an exception by a software instruction on the occurrence of an event due to some exceptional condition encountered during a program execution. [C provides a catch function, which is called on throwing an exception.]
Control register: A register, which associates with a device to program its actions by setting the bits. Status bits (interrupt flags) can also coexist in the register. The device may be a controller.
Deadline: A time interval during which an interrupt must be serviced on its occurrence. For example, the timer overflow interrupt in an auto-reload timer must be serviced before the next overflow. Any timer overflow must be serviced by executing the ISR, and the return from that routine must reset the timer overflow flag so that the timer is ready for the next interrupt service.
Device internal interrupts: An internal device (for example, a timer or serial in-out device) generates interrupts internally in an MCU.
Exception: An interrupt or signal on the occurrence of an event due to some exceptional condition encountered during a program execution. For example, division by zero, overflow and some exception condition defined in a program (for example, percentage value exceeding 100 or becoming negative or the square root of a negative number).
External interrupt: An interrupt from an external peripheral or device or source.
Foreground program: A running program, which is interrupted by an interrupt source or sources.
Input capture interrupt: An interrupt on occurrence of an input after which the timer counts gets captured into a register. Capturing means copying at specific instance.
Internal interrupt: An interrupt in a controller or MCU from an internal on-chip peripheral or device or source.
Interrupt enable register: A register in which (i) primary-level and (ii) secondary-level bits are set for (a) enabling the common interrupt-handling structure for the maskable sources and (b) masking the sources selectively, respectively.
Interrupt flag: A bit in a register to indicate the occurrence of an event such as a serial receiver receiving a byte or a serial transmitting register emptying after transmitting a byte or the timer overflow.
Interrupt latency: An interval between the occurrence of an interrupt event from a source and its service by saving the program counter and start of the ISR after loading new address in the PC. Latency can be more for the lower priority interrupts and is least of the highest priority interrupt.
Interrupt masking: Disabling the recognition and service from a specific interrupt source.
Interrupt pending register: A register, the bits of which identify the interrupt sources (or source groups) pending the service. All pending register bits have to be reset by the program instruction because in a certain source group the service to one of the sources may still be pending to start.
Interrupt priority: When there are multiple events, arising from multiple sources, pending for the service, there is a priority mechanism internally built in the interrupt-handling structure, and default priorities are assigned at the MCU on resetting the processor.
Interrupt priority register: Certain MCUs have priority register(s) for user-assignable priorities, which are set or reset or given a level by the write instructions for the register(s). Priorities defined at the register override the default priorities which were defined on the processor reset.
Interrupt service routine (ISR): A routine, which runs on recognition of the event from a source. ISR for maskable sources run if interrupt structures for these sources are enabled by setting the primary-level bit, and each source (or source group) is enabled at the secondary-level bit. The ISR for non-maskable ones always runs. The last instruction RETI (return from interrupt) restores the saved PC (and other registers also in some processors) for the foreground program. RETI in 8051 also resets the interrupting source so that it can cause the next interrupt service.
Interrupt source: An event-creating device or input or action after which the processor responds by executing a service routine. For example, a source is interrupted by a timer device on the time-out (timer overflow) or an input at INTO pin in 8051. Programming should be such that the latency interval is less such that there is no miss of the deadline for any source.
Interrupt source group: Two or more sources can be said to belong to a group when they have a distinct identification flag for its occurrence but either a common vector address at which the processor vectors on occurrence of either of them or a common mask bit or a common priority assignment bit. TI and TI interrupts of a serial line are the examples of the source group, which has a common priority bit, enabling bit and vector address.
Interrupt structure: A mechanism by which the processor handles the events from the sources.
ISR address: Address where a routine for a source or source group is located at the memory. Maskable interrupt: An interrupt, the service of which can be enabled or disabled by setting or resetting a bit.
Nested calls: Assume that routine B can also call another routine C at a certain point of time and that can call the third routine D and so on. The calls to B, then to C and then to D are also referred to as nested calls.
Nested ISR calls: Assume that ISR B diverts to another ISR C at a certain point of time as B is pre-empted by higher priority ISR C and that can call the third ISR D as C is pre-empted by higher priority ISR D and so on. The calls to B, C and D are also referred to as the nested interrupt service calls.
Non-maskable interrupt: An interrupt for which the service is must. The user program has no instruction or control bit for masking it.
Out-compare interrupt: An interrupt, the occurrence of which occurs when the timer counts equal the preset count in an out-compare register. On interrupt, a service routine is executed and also an output may occur at an MCU pin.
Polling an interrupt: A search method to find one by one, using instructions. Polling selects an interrupt, which is to be serviced next among the pending interrupts
RETI: An instruction in 8051 ISR to return the control to the foreground program. The last instruction RETI (return from interrupt) restores the saved PC (and other registers also in some processors). RETI also resets the interrupting source so that it can cause the next interrupt service.
Return: An instruction in a called a routine to return the control to the foreground program back or another pending interrupt service.
Software interrupt: An interrupt by a software instruction.
Software-error interrupt: An interrupt, for example, by division by zero or by an overflow on arithmetic operation or by an illegal opcode.
Status register: A register to reflect the status of the interrupts and other device functions. Status bits (interrupt flags) can also coexist in a control register. The device may be a controller.
Throwing an exception: An interrupt call by software instruction on occurrence of an event due to some exceptional condition encountered during a program execution. [C provides a catch function, which is called on throwing an exception.]
Trap: An interrupt in 80×86, which when enabled, traps the processor at the end of each instruction. It is useful for debugging a program.
Vector address: An address, where the CPU vectors on occurrence of an interrupt event if the service to that is enabled (not-masked). At the vector address, there may be the short ISR or call or jump to the ISR. In certain MCUs, there are bytes for the pointer of the ISR.
Vectored interrupt: Vectoring for a service on an interrupt from an interrupt source.
Vectored priority interrupt: Vectoring for a service on an interrupt as per the priority of the interrupt source in certain MCUs is at the end of the ISR and in others at the end of each instruction cycle as well as at the end of the ISR. The end of ISR vectoring is among all the enable sources according to the highest to lowest priority. The end of the ISR instruction vectoring is among all the enable sources and is in the priority order highest to the current ISR priority.
0023 CLR TI
0025 MOV SBUF, @ R0
0027 RETI
Bytes transmitted by successive run of the ISR are as follows: