Module3 - 8051 Stack, IO Port Interfacing and Programming - Updated
Module3 - 8051 Stack, IO Port Interfacing and Programming - Updated
Module – 3
8051 Stack, I/O Port Interfacing and Programming
8051 Stack
The stack is a section of RAM used by the CPU to store information temporarily.
o This information could be data or an address.
The register used to access the stack is called the SP (stack pointer) register.
o The stack pointer in the 8051 is only 8 bit wide, which means that it can take value of 00
to FFH.
o When the 8051 is powered up, the SP register contains value 07.
o RAM location 08 is the first location begin used for the stack by the 8051.
The storing of a CPU register in the stack is called a PUSH
o SP is pointing to the last used location of the stack
o As we push data onto the stack, the SP is incremented by one
This is different from many microprocessors
Loading the contents of the stack back into a CPU register is called a POP
o With every pop, the top byte of the stack is copied to the register specified by the
instruction and the stack pointer is decremented once
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:
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:
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
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
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
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 counts
AGAIN: DJNZ R5, AGAIN ; Stay here until R5 becomes 0
RET ; Return to Called program
END
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 AAh
SJMP BACK ; 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
END
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 DELAY
CPL A ; Complement A to toggle
MOV P1,A ; Send AAh to port1
MOV R0, #0FFH ; required count value for generating delay
ACALL 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
END
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 buzzer.
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
Example 3-15
A switch is connected to pin P1.0 and an LED to pin P2.7. Write a program to get the
status of switch and send it the LED.
***************************************************************************************