34/317
2 - How does a typical microcontroller work?
2.5.1.7 Interrupt service routine
When the processor has granted an interrupt request, and read the interrupt vector, it starts
executing the interrupt service routine. This routine is merely a segment of program, written
with exactly the same ease and constraints as the main program. It may be written using the
same language and tools, or in any other language.
The interrupt service routine is supposed to take appropriate action according to the source of
the interrupt. For example, if an input bit has changed its state, the service routine may change
the state of an output bit; if the interrupt was generated by the timer, this may produce the
transmission of a byte by the SPI, etc. according to the structure of the application as defined
by the programmer.
Eventually, the service routine is finished. Then the core may return to the main program. This
is done by executing the IRET instruction.
2.5.1.8 Interrupt Return instruction
As described above, an interrupt service routine looks a little bit like a subroutine. Like in a
subroutine, the return address is stored in the stack, and the execution of the RET instruction
returns to the calling program.
However, some more things have to be done before returning to the interrupted program. All
the core registers were pushed on the stack when the the interrupt request was granted (ex-
cept the Y register). They must now be restored, so that the execution of the service routine
will not leave any trace in the core. This is the role of the IRET instruction in the ST7.
The IRET instruction proceeds by popping all the data off the stack that had previously been
pushed, namely the Condition Code register (at this point the I bit is also restored), the Accu-
mulator, the X register and the Program Counter.
From this time on, execution of the interrupted program resumes.
2.5.2 Software precautions related to interrupt service routines
As described above, the interrupt mechanism is fairly simple to use, since it only consists of
setting the interrupt vectors to the address of the corresponding service routines, and writing
a piece of code that must end with a IRET instruction.
Actually, an interrupt service routine may do anything in the system, since it uses the regular
instruction set of the core and has access to the whole memory. It may thus affect the state of
the main program, even though the core registers have been preserved. The following para-
graphs deal with the precautions to take when using interrupts.
2.5.2.1 Saving the Y register
(This point is specific to the ST7.) If the service routine uses the Y register, one must re-
member that this register is not saved automatically by the interrupt granting mechanism.
Содержание ST7 Series
Страница 1: ...ST7 8 BIT MCU FAMILY USER GUIDE JANUARY 1999 1 ...
Страница 238: ...238 317 8 C Language and the C Compiler 08 Burn bmp Then use the EPROMer programmer software as described in Chapter 7 ...
Страница 289: ...289 317 10 Second Application a Sailing Computer 10 befor Bs Rw Vw VMG AlphaR AlphaV Before the wind ...