0% found this document useful (0 votes)
36 views37 pages

Module 3

Uploaded by

Sri Vani
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
36 views37 pages

Module 3

Uploaded by

Sri Vani
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 37

STACK AND I/0 PORT

• 8051 Stack, I/O Port Interfacing and


Programming: 8051 Stack, Stack and Subroutine
instructions. Assembly language program
examples on subroutine and involving loops -
Delay subroutine, Factorial of an 8 bit number
(result maximum 8 bit), Block move without
overlap, Addition of N 8 bit numbers, Picking
smallest/largest of N 8 bit numbers. Interfacing
simple switch and LED to I/O ports to switch
on/off LED with respect to switch status.
Stack in the 8051
7FH
• The register used to access Scratch pad RAM
the stack is called SP
(stack pointer) register. 30H
2FH
Bit-Addressable RAM
• The stack pointer in the 20H
8051 is only 8 bits wide,1FH Register Bank 3
which means that it can 18H
17H
take value 00 to FFH. 10H Register Bank 2

When 8051 powered up, 0FH 08H


(Stack) Register Bank 1

the SP register contains 07H


Register Bank 0
value 07. 00H
• How Stacks are Accessed
• As the stack is a section of a RAM, there are registers
inside the CPU to point to it. The register used to
access the stack is known as the stack pointer register.
The stack pointer in the 8051 is 8-bits wide, and it can
take a value of 00 to FFH. When the 8051 is initialized,
the SP register contains the value 07H. This means that
the RAM location 08 is the first location used for the
stack. The storing operation of a CPU register in the
stack is known as a PUSH, and getting the contents
from the stack back into a CPU register is called a POP.
• PUSH operation
• The ‘PUSH’ is used for taking the values from
any register and storing in the starting address
of the stack pointer, i.e., 00h by using ‘PUSH’
operation. And, for the next ‘PUSH’, it
increments +1, and stores the value in the
next address of the stack pointer, i.e., 01h.

• POP Operation
• It is used for placing the values from the stack pointer’s
maximum address to any other register’s address. If we
use this ‘POP’ again, then it decrements by 1, and the
value stored in any register is given as ‘POP’
I/O Port Pins
• 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
– When the first 0 is written to a port, it becomes an
output
– To reconfigure it as an input, a 1 must be sent to
the port
– To use any of these ports as an input port, it must
be programmed
Port 0
• It can be used for input or output
– Each pin must be connected externally to a
10K ohm pull-up resistor
• 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
Port 0 (cont.)
Port 0 as Input

• In order to make port 0 an input, the port


must be programmed by writing 1 to all the
bits
I/O Port Cell
Dual Role of Port 0

• Port 0 is also designated as AD0-AD7


– Allowing it to be used for both address and data
– When connecting an 8051/31 to an external
memory, port 0 provides both address and data
Port 1
• Port 1 can be used as input or output
– In contrast to port 0, this port does not need
any pull-up resistors since it already has pull-
up resistors internally
– Upon reset, port 1 is configured as an input
port
Port 1 as Input

• To make port 1 an input port, it must be


programmed as such by writing 1 to all its bits
Port 2
• Port 2 can be used as input or output
– Just like port 1, port 2 does not need any
pull-up resistors since it already has pull-up
resistors internally
– Upon reset, port 2 is configured as an input
port
Port 2 as Input or Dual Role

• To make port 2 an input port, it must be


programmed as such by writing 1 to all its bits
– In many 8051-based system, P2 is used as simple
I/O
– In 8031-based systems, port 2 must be used along
with P0 to provide the 16-bit address for the
external memory
• Port 2 is also designated as A8 – A15
• Port 0 provides the lower 8 bits via A0 – A7
Port 3
• Port 3 can be used as input or output
– Port 3 does not need any pull-up resistors
– Port 3 is configured as an input port upon
reset
• This is not the way it is most commonly used
• Port 3 has the additional function of
providing some extremely important
signals
Port 3 (cont.)
Different ways of Accessing Entire 8
Bits
I/O Ports and Bit Addressability

• Sometimes we need to access only 1 or 2 bits


of the port
I/O Ports and Bit Addressability (cont.)

• Instructions that are used for signal-bit


operations are as following:
Checking an Input Bit

• The JNB and JB instructions are widely used


single-bit operations
– They allow you to monitor a bit and make a
decision depending on whether it’s 0 or 1
– These two instructions can be used for any bits of
I/O ports 0, 1, 2, and 3
– Port 3 is typically not used for any I/O, either
single-bit or byte-wise
7
7
0 0
Reading Input Pins vs. Port Latch
• In reading a port
– Some instructions read the status of port
pins
– Others read the status of an internal port
latch
– When reading ports there are two
possibilities:
• Read the status of the input pin
• Read the internal latch of the output port
– Confusion between them is a major source of errors in
8051 programming
– Especially where external hardware is concerned
Reading Latch for Output Port
• Some instructions read the contents of an
internal port latch instead of reading the
status of an external pin
– For example, look at the ANL P1,A instruction
and the sequence of actions is:
• It reads the internal latch of the port and brings
that data into the CPU
• This data is ANDed with the contents of register A
• The result is rewritten back to the port latch
• The port pin data is changed and now has the
same value as port latch
Reading Latch for Output Port (cont.)
• Read-Modify-Write
– The instructions read the port latch normally
read a value, perform an operation then
rewrite it back to the port latch
Read-modify-write Feature

• The ports in 8051 can be accessed by the


Read-modify-write technique
– This feature saves many lines of code by
combining in a single instruction all three actions
• Reading the port
• Modifying it
• Writing to the port
References
1. “The 8051 Microcontroller and Embedded Systems – using assembly and C”,
Muhammad Ali Mazidi and Janice Gillespie Mazidi and Rollin D. McKinlay;
PHI, 2006 / Pearson, 2006.

2. “The 8051 Microcontroller”, Kenneth J. Ayala, 3rd Edition, Thomson/Cengage


Learning.

You might also like