Download as pdf or txt
Download as pdf or txt
You are on page 1of 7

Module – 3

8051 Stack, I/O Port Interfacing and Programming

CALL and Subroutines:


Call instruction is used to call subroutine
 Subroutines are often used to perform tasks that need to be performed frequently
 This makes a program more structured in addition to saving memory space

Instructions for calling subroutine are listed below,


LCALL (long call)
 3-byte instruction
 First byte is the opcode
 Second and third bytes are used for address of target subroutine
 Subroutine can be located anywhere within 64K byte address space

ACALL (absolute call)


 2-byte instruction
 11 bits are used for address within 2K-byte range

Process of executing subroutine:


 LCALL and ACALL instructions are used to call subroutine from the main
program.
 CALL instruction transfers the execution control to a subroutine with the
intention of coming back to the main program.
 Thus when a subroutine is called, control is transferred to that subroutine, 8051
processor
 Saves the address of next instruction immediately below the LCALL or
ACALL onto to the stack before branching to subroutine.
 Begins to fetch instructions form the new location
 After finishing execution of the subroutine.
 The instruction RET transfers control back to the caller (main program) using
the return address from the stack.
 Every subroutine needs RET as the last instruction.
Following program will continuously send out to port 0 the alternating value 55H and
AAH, with a delay in between. Delay code is written as a subroutine and it is called
from the main program using LCALL instruction.

Difference between jump and call operations:


 Time Delay Subroutines:

CPU executing an instruction takes a certain number of clock cycles. These are
referred as to as machine cycles.
The length of machine cycle depends on the frequency of the crystal oscillator
connected to 8051.
In original 8051, one machine cycle lasts 12 oscillator periods.

 Find the period of the machine cycle for 11.0592 MHz crystal frequency.
Solution:
Machine cycle frequency = 11.0592 MHz / 12 = 921.6 kHz
Period of one machine cycle = 1/921.6 kHz = 1.085μs
Large Delay using nested loops: g
N
 Write an ALP to generate delay of 1mS.
Solution:
One machine cycle =1.085 μS with crystal frequency of 11.0592MHz.
Total number of machine cycles required for generating 1mS delay = 1mS/1.085 μS =
= 921.6 nearly 922.
Initializing counter and RET instruction takes 3 machine cycles.
So remaining machine cycles are (922-3) = 919
Number of times the loop with 4 machine cycles should be repeated is = 919/4 =
229.75 nearly 230.
So initialize the R2 with 230 as a counter.

Instructions Machine
Cycles
DELAY:MOV R2, #230 1
HERE: NOP 1
NOP 1
DJNZ R2, HERE 2
RET 2

Delay = 4 x 230 x 1.085 μS + (1+2) x 1.085 μS


= 1001.45 μS
= 1.00145 mS
Nearly equal to 1mS.

 Write an ALP to generate a delay of 1 second.

Instructions Machine
Cycles
DELAY: MOV R3, #04 1
L3 :MOV R2,#250 1
L2 : MOV R1,#230 1
L1 : NOP 1
NOP 1
DJNZ R1,L1 2
DJNZ R2,L2 2
DJNZ R3, L3 2
RET 2

Below are approximate calculations:


Loop1 generates approximately 1mS delay.
Loop2 repeats loop1, 250 time, hence loop2 generates 250 ms approximately.
Loop3 repeats loop2, 4 times, hence loop3 generates 1 Second delay approximately.

You might also like