37/317
2 - How does a typical microcontroller work?
The following example shows:
What happens when the data are desynchronized.
Let us assume the main program wants to increment the word variable
reg
(16-bit register in
page zero), that currently contains
42FF
hex. The following code will be used:
; word variable reg contains 42FF hex
; increment word variable reg by 1
inc reg+1
; add 1 to the low byte; low
byte is incremented. reg = 4200
hex
jrne endinc
; increment high byte if carry
endinc : ...
; here the program continues
; both bytes are incremented. reg = 4300 hex
If an interrupt service routines uses the value of
reg
, and if the interrupt request occurs for ex-
ample at the first line of the code above, the interrupt service routine will see that
reg
is
4200
while it is actually either
42FF
or
4300
. This error may have serious consequences.
How to make the handling atomic.
To avoid this situation, it is sufficient to mask out all the interrupts, by changing the code as fol-
lows:
; increment word variable X by 1
sim
; prevent interrupts from
occurring
inc reg+1
; add 1 to the low byte
jrne endinc
; increment high byte if carry
endinc
rim
; allow interrupts to occur
...
; here the program continues
; both bytes are incremented. reg = 4300 hex, now the interrupt can be
performed
All interrupt requests that occur between the
SIM
and the
RIM
instructions are delayed for the
duration of that piece of code. If this would cause an excessive latency for one particular inter-
rupt that does not use that data, it is possible to mask out the specific interrupt source whose
service routine actually uses this value.
This example mentions the case where the data is written by the main program, and read by
the interrupt service routine. Actually, the reverse case is also a source of problem: if the main
program reads the data, and the interrupt service routine writes it, the main program may start
Содержание 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 ...