Interrupts
Interrupts
timers overflowing
receiving character via the serial port
transmitting character via the serial port
one of two "external events
Example : in a large program, automatically toggle P3.0 port on
each overflow of timer 0 (defined as 16 bit wide counter/timer)
Example : using code within main program
SKIP_TOGGLE:
; remaining 98 program instructions
SJMP startLoop
Assume entire program code consumes 100 instruction cycles (98 instruction cycle
s plus the
2 that are executed every iteration to determine whether or not timer 0 has over
flowed).
Code executed 65536/100 = 655 times between each timer 0 overflow
JNB instruction is performed a total of 2 x 655 (1310) instruction cycles betwee
n timer 0
overflows
Also add another 2 instruction cycles to perform the P3.0 toggling code
So, 1312/65536 = 2.002% of the processor time is being spent just checking wheth
er to
toggle P3.0 !
. and the code is ugly
Example : using interrupts
ISR subroutine would be nothing more than
CPL P3.0
RETI
Every 65536 instruction cycles the program executes the CPL instruction
and the RETI instruction
Require 3 instruction cycles rather than 1312 instruction cycles.
Code is more efficient and easier to read & understand.
So, setup the interrupt and forget about it, secure in the knowledge that
the 8051 will execute the code whenever necessary
Interrupts and the serial (Rx) port