MC - Module 3 Notes
MC - Module 3 Notes
Example 3-1
Show the stack and stack pointer from the following. Assume the default stack area.
MOV R6, #25H
MOV R1, #12H
MOV R4, #0F3H
PUSH 6
PUSH 1
PUSH 4
Solution:
1
Example 3-2
Examining the stack, show the contents of the register and SP after execution of the
following instructions. All value are in hex.
POP 3 ; POP stack into R3
POP 5 ; POP stack into R5
POP 2 ; POP stack into R2
Solution:
The CPU also uses the stack to save the address of the instruction just below the CALL
instruction
o This is how the CPU knows where to resume when it returns from the called
subroutine.
The reason of incrementing SP after push is
o Make sure that the stack is growing toward RAM location 7FH, from lower to upper
addresses.
If the stack pointer were decremented after push
o We would be using RAM locations 7, 6, 5, etc. which belong to R7 to R0 of bank 0,
the default register bank.
When 8051 is powered up, register bank 1 and the stack are using the same memory space
o We can reallocate another section of RAM to the stack.
Example 3-3
Examining the stack, show the contents of the register and SP after execution of the
following instructions. All value are in hex.
MOV SP, #5FH ;make RAM location 60H, first stack location
MOV R2, #25H
MOV R1, #12H
MOV R4, #0F3H
PUSH 2
PUSH 1
PUSH 4
Solution:
Stack and Subroutine instructions
CALLs and Subroutines
Call instruction is used to call subroutine
o Subroutines are often used to perform tasks that need to be performed frequently
o This makes a program more structured in addition to saving memory space
LCALL (long call)
o 3-byte instruction
o First byte is the opcode
o Second and third bytes are used for address of target subroutine
• Subroutine is located anywhere within 64K byte address space
ACALL (absolute call)
o 2-byte instruction
o 11 bits are used for address within 2K-byte range
When a subroutine is called, control is transferred to that subroutine, the processor
o Saves on the stack the address of the instruction immediately below the LCALL
o Begins to fetch instructions from the new location
After finishing execution of the subroutine
o The instruction RET transfers control back to the caller
• Every subroutine needs RET as the last instruction
3
CALL Instructions- ACALL
The only difference between ACALL and LCALL is
o The target address for LCALL can be anywhere within the 64K byte address
o The target address of ACALL must be within a 2K-byte range
The use of ACALL instead of LCALL can save a number of bytes of program ROM space.
RETI
Pops two bytes from the stack into the program counter (PC) and reset the interrupt enable Flip-
Flops.
The only difference between the RET and RETI instructions is the enabling of the interrupt logic
when RETI is used.
RET is used at the ends of subroutines called by an opcode. RETI is used by subroutines
called by an interrupt
I/O Port Interfacing and Programming
The four 8-bit I/O ports P0, P1, P2 and P3 each uses 8 pins
All the ports upon RESET are configured as input, ready to be used as input ports
o When the first 0 is written to a port, it becomes an output
o To reconfigure it as an input, a 1 must be sent to the port
o To use any of these ports as an input port, it must be programmed
Port 0
Port 0 can be used for input or output, each pin must
be connected externally to a 10K ohm pull-up resistor
o This is due to the fact that P0 is an open
drain, unlike P1, P2, and P3
• Open drain is a term used for MOS
chips in the same way that open
collector is used for TTL chips
Example 3-5
The following code will continuously send out to port 0 the alternating value 55H and
AAH
BACK: MOV A,#55H
MOV P0,A
ACALL DELAY
MOV A,#0AAH
MOV P0,A
ACALL DELAY
SJMP BACK
5
In order to make port 0 an input, the port must be programmed by writing 1 to all the bits
Example 3-6
Port 0 is configured first as an input port by writing 1s to it, and then data is received from
that port and sent to P1
MOV A,#0FFH ;A=FF hex
MOV P0,A ;make P0 an i/p port by writing it all 1s
BACK:MOV A,P0 ;get data from P0
MOV P1,A ;send it to port 1
SJMP BACK ;keep doing it
Port 1
Port 1 can be used as input or output
o In contrast to port 0, this port does not need any pull-up resistors since it already has pull-
up resistors internally
o Upon reset, port 1 is configured as an input port
o To make port 1 an input port, it must be programmed as such by writing 1 to all its bits
Example 3-7
The following code will continuously send out to port 1 the alternating value 55H and
AAH
MOV A,#55H
BACK: MOV P1,A
ACALL DELAY
CPL A
SJMP BACK
A= 01010101(55h) =>Complementing gives 10101010(AAh)
Example 3-8
Port 1 is configured first as an input port by writing 1s to it, then data is received from
that port and saved in R7 and R5
MOV A,#0FFH ;A=FF hex
MOV P1,A ;make P1 an input port by writing it all 1s
MOV A,P1 ;get data from P1
MOV R7,A ;save it to in reg R7
ACALL DELAY ;wait
MOV A,P1 ;another data from P1
MOV R5,A ;save it to in reg R5
Port 2 and Port 3
Port 2 can be used as input or output
o Just like P1, port 2 does not need any pull-up resistors since it already has pull-up
resistors internally
o Upon reset, port 2 is configured as an input port
To make port 2 an input port, it must be programmed as such by writing 1 to all its bits.
Port 3 can be used as input or output
o Port 3 does not need any pull-up resistors
o Port 3 is configured as an input port upon reset.
Example 3-9
Write a program to toggle all the bits of port1 by sending to it the values 55h and AAh
continuously. Put a time delay in between each issuing of data to port1.
ORG 0
BACK: MOV A,#55H ; Load A with 55h
MOV P1,A ; Send 55h to port1
ACALL DELAY ; Call the subroutine DELAY
MOV A,#0AAH ; Load A with AAh
MOV P1,A ; Send AAh to port1
ACALL DELAY ; Call the subroutine DELAY
SJMP BACK ; Keep doing this indefinitely
Delay Subroutine
ORG 300H
DELAY: MOV R5, #0FFH ; R5 works as counter with 256
AGAIN: DJNZ R5,countsAGAIN ; Stay here until R5 becomes 0
RET ; Return to Called
program
7
Example 3-10
Rewrite the previous program efficiently.
ORG 0
MOV A,#55H ; Load A with 55h
BACK: MOV P1,A ; Send 55h to port1
ACALL DELAY ; Call the subroutine DELAY
CPL A ; Complements A, so that A becomes
SJMP BACK ;AAh
Keep doing this indefinitely
------ Delay Subroutine---------
ORG 300H
DELAY: MOV R5, #0FFH ; R5 works as counter with 256 counts
AGAIN: DJNZ R5, AGAIN ; Stay here until R5 becomes 0
RET ; Return to Called
program
Example 3-11
Write a program to toggle the bits of port1 delay which depends on the value of a
number in R0.
ORG 0
BACK: MOV A,#0H
MOV P1,A ; Send 0h to port P1
MOV R0, #30H ; Required count value for generating
delay
ACALL DELAY; Call the subroutine
CPL A DELAY
MOV P1,A ; Complement
Send AAh toA to toggle
MOV R0, #0FFH port1 ; required count value for generating
ACALLdelay
DELAY ; Call the subroutine DELAY
SJMP BACK ; Keep doing this indefinitely
------ Delay Subroutine---------
ORG 300H
DELAY: NOP ; Do nothing
AGAIN: DJNZ R0, AGAIN ; Stay here until R0 becomes 0
RET ; Return to the Called
program
Example 3-12
Write a program to perform the following:
(a) Keep monitoring the P1.2 bit until it becomes high
(b) When P1.2 becomes high, write value 45H to port 0
(c) Send a high-to-low (H-to-L) pulse to P2.3
SETB P1.2 ;make P1.2 an input
MOV A,#45H ;A=45H
AGAIN: JNB P1.2,AGAIN ;get out when P1.2=1
MOV P0,A ;issue A to P0
SETB P2.3 ;make P2.3 high
CLR P2.3 ;make P2.3 low for H-to-
L
Example 3-13
Assume that bit P2.3 is an input and represents the condition of an
oven. If it goes high, it means that the oven is hot. Monitor the bit
continuously.
Whenever it goes high, send a high-to-low pulse to port P1.5 to turn on a
HERE: JNB P2.3,HERE ;keep monitoring for high
SETB P1.5 ;set bit P1.5=1
CLR P1.5 ;make high-to-low
SJMP HERE ;keep repeating
Example 3-14
A switch is connected to pin P1.7. Write a program to check the status of SW and
perform the following:
(a) If SW=0, send letter ‘N’ to P2
(b) If SW=1, send letter ‘Y’ to P2