Texas Instruments MSC1210 User Manual

Page 108

Advertising
background image

Description

10-2

10.1 Description

As the name implies, an interrupt is some event that interrupts normal program
execution. As stated previously, program flow is always sequential, being al-
tered only by those instructions that expressly cause program flow to deviate
in some way. However, interrupts give us a mechanism to put on hold the nor-
mal program flow, execute a subroutine, and then resume normal program
flow as if we had never left it. This subroutine, called an interrupt handler or
interrupt service routine (ISR), is only executed when a certain event (inter-
rupt) occurs. The event may be one of 21 interrupt sources such as the timers
overflowing, receiving a character via the serial port, transmitting a character
via the serial port, or external events. The MSC1210 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 exe-
cute 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 more efficient to handle certain conditions. If it were not for inter-
rupts, the program would have to be manually checked as to whether the timers
have overflowed, whether the serial port has received another character, or if some
external event has occurred. Besides making the main program ugly and hard to
read, such a situation makes the program inefficient because precious instruction
cycles are wasted checking for events that happen infrequently.

For example, say a large 16k program is executing many subroutines and per-
forming many tasks. Additionaly, suppose that the program is to automatically
toggle the P3.0 port every time Timer 0 overflows. The code to do this is not
very difficult:

JNB TF0,SKIP_TOGGLE

CPL P3.0

CLR TF0

SKIP_TOGGLE: ...

The above code toggles P3.0 every time Timer 0 overflows because the TF0
flag is set whenever Timer 0 overflows. This accomplishes what is needed, but
is inefficient.

Luckily, this is not necessary. Interrupts allow you to forget about checking for the
condition. The microcontroller itself will check for the condition automatically, and
when the condition is met, will jump to a subroutine (the interrupt handler), execute
the code, and then return. In this case, the subroutine would be nothing more than:

CPL P3.0

;Toggle P3.0

RETI

;Return from the interrupt

First, notice that the CLR TF0 command has disappeared. That is because
when the MSC1210 executes the Timer 0 interrupt routine, it automatically
clears the TF0 flag. Also notice that instead of a normal RET instruction, there
is a RETI instruction. The RETI instruction does the same thing as a RET in-
struction, but tells the 8051 that an interrupt routine has finished. Interrupt han-
dlers must always end with RETI.

Advertising