L16 17 Combined Lecture 16 17 Stack Interrupts Timers
L16 17 Combined Lecture 16 17 Stack Interrupts Timers
April 6, 2022
Objectives
• Understand and use the 8051 stack.
• Understand and use the ‘lcall’ and ‘ret’
instructions.
• Understand and use the ‘push’ and ‘pop’
instructions.
• Setup and use Interrupts.
• Understand and use Timers/Counters.
1
The 8051 stack ii it
any
e
• We need the stack to use the lcall instruction as well as
interrupts.
s
men
I
• The stack is an area of memory where variables can be
stacked. It is a LIFO memory: the last variable you put in
is the first variable that comes out.
• Special Function Register SP (stack pointer) points to
the beginning of the stack. SP in the 8051 is
incremented before it is used (for push), or used and
i.ciii Iii
them decremented (for pop).
• After reset, SP is set to 07H. If you have variables in
internal RAM, any usage of the stack is likely to corrupt
them. Solution: at the beginning of your program set the
SP special function register so it points to free memory:
mov SP, #7FH ; Set the stack pointer to idata start
push
pun
2
DSO upto F
iiiiimn
i.fif
x Ds6
i
Push and Pop
it
microprocessors!) have push and pop
o
er instructions.
1.0 itTo Lecture 16-17: Stack, Interrupts, Timers
Copyright © 2009-2022, Jesus Calvino-Fraga. Not to be copied, used, or
revised without explicit written permission from the copyright owner.
5
c.ini
i iiiii
a
lcall example
; Blinky.asm: blinks an LED connected to LEDR0
using2 bond 2no R $MODDE0CV
org 0000H
ljmp myprogram
3
lcall example
myprogram:
mov SP, #7FH
; Turn off all LEDs...
mov LEDRA, #0
mov LEDRB, #0 Never jump into
M0:
cpl LEDRA.0
a subroutine!
lcall WaitHalfSec
sjmp M0
END
4
Push and Pop Example
WasteTime:
push Acc
push B
push dpl
mov Acc, #100
L3: mov B, #100
L2: mov dpl, #100
L1: djnz dpl, L1
djnz B, L2
djnz Acc, L3
pop dpl
pop B
pop Acc
ret
Lecture 16-17: Stack, Interrupts, Timers 9
Copyright © 2009-2022, Jesus Calvino-Fraga. Not to be copied, used, or
revised without explicit written permission from the copyright owner.
Common bug!
WasteTime:
push B
push Acc
push dpl
mov Acc, #100
L3: mov B, #100
L2: mov dpl, #100
L1: djnz dpl, L1 ; 3 bytes, 2 machine cycles
djnz B, L2
djnz Acc, L3
pop dpl
pop B
pop Acc Where is it?
ret pops are in the
wrong order!
5
Push/Pop for R0 to R7
WaitHalfSec:
push AR0
push AR1
push AR2
mov R2, #20
L3: mov R1, #250
L2: mov R0, #184
L1: djnz R0, L1 ; 2 machine cycles-> 2*0.27126us*184=100us
djnz R1, L2 ; 100us*250=0.025s
djnz R2, L3 ; 0.025s*20=0.5s
pop AR2
pop AR1
pop AR0 The extra ‘A’ is for ‘direct address’ of
ret register R0. This is only required for
registers R0 to R7
Interrupts
work along
a Icall
progan to
a predecine
• ‘Interrupts’ are a means of executing subroutines
on automatically without using the ‘lcall’ instruction. The
memory in only difference is that the subroutine that is automatically
called must end with ‘reti’ instead of ‘ret’.
ammechangewaofinter.pl
• Associated with external logic that requires CPU
attention on command.
• Interrupt uses:
– Handshake I/O thus preventing CPU from being tied up.
– Providing a way to handle some errors: illegal opcodes, dividing
by 0, power failure, etc.
– Getting the CPU to perform periodic tasks: generate square
waves, keep time of day, measure frequency, etc.
– Waking up the processor when in low power mode.
6
Interrupts
I “borrowed” this from an old 68HC11
textbook!
Interrupts
• Most processors provide a way of enabling /
disabling all maskable interrupts. For the 8051:
clr EA ;Disable interrupts
setb EA ;Enable interrupts
• Some other interrupts are non-maskable and
they MUST be serviced. For example, the X86
has the “Non-Maskable Interrupt” NMI.
• Maskable interrupts can be enabled/disabled
individually. For the 8051 use register IE:
7
IE: INTERRUPT ENABLE REGISTER.
(Address A8H)
EA EC ET2 ES ET1 EX1 ET0 EX0
Bit Name Description
7 EA Interrupt Enable Bit: EA = 1 interrupt(s) can be
serviced, EA = 0 interrupt servicing disabled.
6 BPE Breakpoint Enable bit. (CV-8052)
5 ET2 Timer 2 Interrupt Enable. (8052)
4 ES Serial Port Interrupt Enable
3 ET1 Timer 1 Overflow Interrupt Enable.
2 EX1 External Interrupt 1 Enable.
1 ET0 Timer 0 Overflow Interrupt Enable.
0 EX0 External Interrupt 0 Enable.
8
Interrupt Service Routines (ISR)
Vectors
• Notice that there are only 8 bytes available between
vectors. Not enough for a decent ISR, but more than
enough for a ljmp instruction!
• IF you enable a particular interrupt, there MUST be an
ISR, or your program WILL crash. A fool proof code
technique is to setup all the ISR vectors and place a reti
(return from interrupt) instruction for those that are not
used (next example).
• In assembly language you can use the “org” directive to
set an ISR vector.
• To return from an ISR use the reti instruction. To return
from a normal routine use the ret instruction.
Example 1
; Basic interrupt setup
org 0h
ljmp myprogram
; External interrupt 0
org 3h
reti
9
Example 1 (cont.)
; External interrupt 1
org 13h
reti
; Timer 1 interrupt
org 1bh
reti
; Timer 2 interrupt
org 2bh
reti
forever:
[...other code here...]
jmp forever
10
Example 2: (cont.) the ISR.
; Timer 0 interrupt
org 0bh
cpl P1.1 ; Check this pin with the scope!
reti
11
Saving and Restoring Registers in
the Stack
• If your ISR routine uses a register, you
must make sure that it will remain
unmodified before returning to the
interrupted program.
• As mentioned before you use the
instructions push/pop to save/restore
registers to/from the stack.
• Additionally, you could use one of four
available register banks in your ISR.
Lecture 16-17: Stack, Interrupts, Timers 23
Copyright © 2009-2022, Jesus Calvino-Fraga. Not to be copied, used, or
revised without explicit written permission from the copyright owner.
8051’s Timers/Counters
• The original 8051 has only two
timers/counters: 0 and 1.
• Newer 8051 microcontrollers usually have:
1. The 8051 timers/counters: timers 0 and 1
2. The 8052 timer/counter: timer 2
3. Additional timers (3, 4, 5, etc.) Not available in
the CV-8052.
4. The Programmable Counter Array (PCA). Not
available in the CV-8052, but very common in
many other processors.
• Let us begin with timers 0 and 1:
Lecture 16-17: Stack, Interrupts, Timers 24
Copyright © 2009-2022, Jesus Calvino-Fraga. Not to be copied, used, or
revised without explicit written permission from the copyright owner.
12
Timer 0 and Timer 1 Operation Modes
(Section 3-10 of MCS-51 manual)
• Timer 0 and 1 have four modes of operation:
• Mode 0: 13-bit timer/counter (compatible with the 8048
microcontroller, the predecessor of the 8051). Do not
use this mode; use mode 1 instead!
• Mode 1: 16-bit timer/counter.
• Mode 2: 8-bit auto reload timer counter.
• Mode 3: Special mode 8-bit timer/counter (timer 0
only). (I have never used it!)
• Timer 1 can be used as baud rate generator for
the serial port. Some 8051/8052 microcontrollers
have a dedicated baud rate generator.
Lecture 16-17: Stack, Interrupts, Timers 25
Copyright © 2009-2022, Jesus Calvino-Fraga. Not to be copied, used, or
revised without explicit written permission from the copyright owner.
Timer 1 Timer 0
GATE C/T* M1 M0 GATE C/T* M1 M0
13
TCON: timer/counter control register.
(Address 88H)
TF1 TR1 TF0 TR0 IE1 IT1 IE0 IT0
Bit Name Description
7 TF1 Timer 1 overflow flag.
6 TR1 Timer 1 run control.
5 TF0 Timer 0 overflow flag.
4 TR0 Timer 0 run control.
3 IE1 Interrupt 1 flag.
2 IT1 Interrupt 1 type control bit.
1 IE0 Interrupt 0 flag.
0 IT0 Interrupt 0 type control bit.
Timer/Counter 0 or 1 in Mode 0
C/T*=0
Interrupt
THx TLx
TFx
(8 bits) (5 bits)
C/T*=1
Tx PIN Overflow
Control
GATE
Do not use this
mode! Use mode
INTx* pin
1 instead.
TRx
Lecture 16-17: Stack, Interrupts, Timers 28
Copyright © 2009-2022, Jesus Calvino-Fraga. Not to be copied, used, or
revised without explicit written permission from the copyright owner.
14
Timer/Counter 0 or 1 in Mode 1
C/T*=0
Interrupt
THx TLx
TFx
(8 bits) (8 bits)
C/T*=1
Tx PIN Overflow
Control
GATE
Most useful!
INTx* pin
TRx
Lecture 16-17: Stack, Interrupts, Timers 29
Copyright © 2009-2022, Jesus Calvino-Fraga. Not to be copied, used, or
revised without explicit written permission from the copyright owner.
Timer/Counter 0 or 1 in Mode 2
C/T*=0
Overflow Interrupt
TLx
TFx
(8 bits)
C/T*=1
Tx PIN Control
THx
GATE
(8 bits)
INTx* pin
TRx
Lecture 16-17: Stack, Interrupts, Timers 30
Copyright © 2009-2022, Jesus Calvino-Fraga. Not to be copied, used, or
revised without explicit written permission from the copyright owner.
15
Timer/Counter 0 in Mode 2
myprogram:
; After reset, the stack pointer register is set to 07h
; We may need space for variables, so move the SP
mov SP, #7fH
; Enable timer 0
mov a, TMOD
anl a, #0f0H
orl a, #00000010B ; GATE=0, C/T*=0, M1=1, M0=0: 8-bit auto reload timer
mov TMOD, a
mov TH0, #080H ; Set the interrupt rate
setb TR0 ; Enable timer 0
setb ET0 ; Enable timer 0 interrupt
setb EA
forever:
.
.
. 12
.
Rate = × (100 H − TH 0)
OSC
.
. 12
Rate = × (100 H − 80 H ) = 46.08µ s
jmp forever 33.3333MHz
16
T2CON: timer/counter 2 control register.
(Address C8H)
TF2 EXF2 RCLK TCLK EXEN2 TR2 C/T2* CP/RL2*
TH2 TL2
TF2
(8 bits) (8 bits)
T2 PIN C/T2*=1
Control
Interrupt
TR2
RCAP2H RCAP2L
T2EX PIN
EXF2
EXEN2
Lecture 16-17: Stack, Interrupts, Timers 34
Copyright © 2009-2022, Jesus Calvino-Fraga. Not to be copied, used, or
revised without explicit written permission from the copyright owner.
17
Timer/Counter 2 in auto-reload
mode
OSC ÷12 Overflow
C/T2*=0
TH2 TL2
TF2
(8 bits) (8 bits)
T2 PIN C/T2*=1
Control
TR2 Interrupt
RCAP2H RCAP2L
T2EX PIN
EXF2
EXEN2
Lecture 16-17: Stack, Interrupts, Timers 35
Copyright © 2009-2022, Jesus Calvino-Fraga. Not to be copied, used, or
revised without explicit written permission from the copyright owner.
18
Time Delay Using a Timer
• Implement a 10 ms delay subroutine using
timer 0. Assume the routine will be
running in a CV-8052 soft processor.
Timer 0 in Mode 1
C/T*=0
Interrupt
TH0 TL0
TF0
(8 bits) (8 bits)
C/T*=1
T0 PIN Overflow
Control
TRx
Lecture 16-17: Stack, Interrupts, Timers 38
Copyright © 2009-2022, Jesus Calvino-Fraga. Not to be copied, used, or
revised without explicit written permission from the copyright owner.
19
Calculating TH0 and TL0
CLK 33.3333MHz
Rate= 12 = 12
216 − [THn,TLn] 65536 − [THn,TLn]
2.77777MHz 2.77777MHz
[THn,TLn]=65536 − = 65536 − = 37758
Rate (1/10ms)
20
Time Delay Using Timer 0
; Let the Assembler do the calculation for us!
XTAL equ 33333333
FREQ equ 100 ; 1/100Hz=10ms
RELOAD_TIMER0_10ms equ 65536-(XTAL/(12*FREQ))
Wait10ms:
; Initialize the timer
mov a, TMOD
anl a, #11110000B ; Clear bits for timer 0, keep bits for timer 1
orl a, #00000001B ; GATE=0, C/T*=0, M1=0, M0=1: 16-bit timer
mov TMOD, a
clr TR0 ; Disable timer 0
mov TH0, #high(RELOAD_TIMER0_10ms )
mov TL0, #low(RELOAD_TIMER0_10ms )
clr TF0 ;Clear the timer flag
setb TR0 ; Enable timer 0
Wait10ms_L0:
jnb TF0, Wait10ms_L0 ; Wait for overflow
ret
21
I/O ports in the 8051/CV-8052
pomon
• The Input/Output (I/O) pins are accessed using
pimon
SFRs P0, P1, P2, P3. They are all bit
pzmon addressable.
• To use the I/O as output pins configure them
with the PxMOD register (not bit addressable).
‘1’ makes the pin an output. For example to set
P0.1 as output: have a use Andor
not bit
addressable
Not the
Same
22
masks
bin
GPIOA->MODER |= 0x00000001;
profiling
Exercises
• A common way of passing parameters to a function is
via the stack. Modify the function WaitHalfSec so that it
receives the number of half-seconds to wait in the stack.
(Note: this problem is not as trivial as it sounds. You
may need to increment and/or decrement register SP to
solve this problem)
• Most C programs pass parameters to functions via the
stack. Also C programs use the stack to allocate
automatic variables (local variables defined within the
function). This works fine most of the time, but
sometimes a condition commonly known as “stack
overflow” occurs. Explain what causes “stack overflow”.
23
Exercises
• Write an Interrupt service routine for timer 0 that
generates a 1 kHz square wave in pin P0.0 of
the CV-8052 processor.
• Write an interrupt service routine for timer 2 that
increments a two digit BCD counter displayed in
the 7-segment displays HEX1 and HEX0 of the
CV-8052 every second. Make sure that the ISR
for this question and the ISR from the previous
question can run concurrently in the same
processor.
Lecture 16-17: Stack, Interrupts, Timers 47
Copyright © 2009-2022, Jesus Calvino-Fraga. Not to be copied, used, or
revised without explicit written permission from the copyright owner.
Exercises
• Write a one second delay function using
timer 1. This function will run in a CV-
8052 with a 33.33MHz clock.
• Program profiling is used to find the usage
of resources by a piece of code (a
subroutine, for example). A profile value
often needed is execution time. Show
how to use timer 0 to find out the
execution time of a subroutine.
24
ex
P18am
Searl ISR example in leave
1
140052 MoDDEocv
1
change
Usetime0
1 1 1 and Time0
HEXI HEXO limp TimeroISR
pan lean L
timero done
popseen in reverse order
Cjne
changes
lover
p over
Exercises
• From the examples given in this lecture,
explain how to use the timer overflow flag
to measure frequencies higher than 65535
Hz while using a 1-second time interval.
25