8051 IO Port Programming
Dr. Lokesh M
Asst. Professor
Dept. of EEE
NIE IT, Mysuru-570018
Agenda
01 IO port pins and their functions
02 Port 0
Dual Role : As input or output and to access address and data
03 Port 1: As input/output
Get a modern PowerPoint Presentation that is beautifully designed
04 Port 2:
Dual Role : As input or output and to access address along with P0
05 Port 3:
Dual Role : As input or output and as special additional functions
32 pins for four ports, Vcc,
GND,XTAL1, XTAL2, RST,
EA,ALE/PROG and PSEN.
8051 IO Port Programming
The four 8 bit I/O ports P0,P1,P2 and P3
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.
It can be used for input or output.
To use the pins of P0 as both input and output ports,
PORT 0 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
As Input/output P1, P2, and P3.
As address/data Open drain is a term used form MOS Chips in the same
way that open collector is used for TTL chips.
Port 0 as
Input
In order to make it an input, the port must be
programmed by writing 1 to all the bits
Dual Role of Port 0
Port 0 is also designated as AD0-AD7, allowing
Toggle all Bits of P0 it to be used for both address and data.
When connecting 8051/31 to an external
Back: MOV A, #55H memory, P0 provides both address and data.
MOV P0,A ALE (Pin no 31) indicates if P0 has address or
ACALL Delay data.
MOV A,#0AAH When ALE = 0, it provides data D0-D7, but
MOV P0,A when ALE = 1, it has address A0-A7.
ACALL Delay The 8051 multiplexes address and data through
SJMP Back port 0 to save pins.
Port 1
Toggle all Bits of P1
It can be used as input or output.
MOV A, #55H It doesn't require pull-up resistors because
Back: MOV P1,A they are already connected internally.
ACALL Delay Upon reset, Port 1 is configured as an
CPL A input port
SJMP Back
PORT 1 AS INPUT
MOV A ,#0FFH ;A = FF hex
MOV P1,A ;Make P1 an input port
MOV A,P1 ;get data from P1 If Port 1 is configured to be used as an output port, then
MOV R7,A ;save it in Reg R7
ACALL DELAY ;wait
to use it as an input port again, program it by writing 1
MOV A,P1 ;get another data from P1 to all of its bits as in the following code:
MOV R6,A ;save it in R6
ACALL DELAY ;wait
MOV A,P1 ;get another data from P1
MOV R5,A ;save it in R5
Port1 is configured first as input
port by writing 1s to it, then data
is received from that port and
saved in R7,R6,and R5.
PORT 2
Port 2 occupies a total of 8 pins (pins 21 through 28)
Toggle all Bits of P2 and can be used for both input and output operations.
Just as P1 (Port 1), P2 also doesn't require external
Pull-up resistors because they are already connected
internally.
MOV A, #55H
It must be used along with P0 to provide the 16-bit
Back: MOV P2,A
address for the external memory. So it is also
ACALL Delay
designated as (A0–A7).
CPL A
When the 8051 is connected to an external memory, it
SJMP Back
provides path for upper 8-bits of 16-bits address, and it
cannot be used as I/O.
Upon reset, Port 2 is configured as an input port.
PORT 2 as Input
Get a byte from P2
and send it to P1
If Port 2 is configured to be used as an output port, then
MOV A, #0FFH to use it as an input port again, program it by writing 1s
MOV P2,A to all of its bits
Back: MOV A,P2
MOV P1,A
SJMP Back
Dual Role of Port 2
Port 2 must be used along with P0 to provide the 16-bit ad-
dress of external memory.
P2 is also designated as A8-A15, indicating its dual function.
Since an 8051/31 is capable of accessing 64K bytes of
external memory, it needs a path for the 16-bits of the
address.
P0 provides lower byte A0-A7.
P2 provides higher byte A8-A15 of the address.
Port 3
It is also of 8 bits and can be used as Input/Output.
This port provides some extremely important signals.
P3 Bit Function Pin P3.0 and P3.1 are RxD (Receiver) and TxD (Transmitter)
P3.0 RxD 10 respectively and are collectively used for Serial
P3.1 < TxD 11 Communication.
P3.2 < INT0 12 P3.2 and P3.3 pins are used for external interrupts.
P3.3 < INT1 13 P3.4 and P3.5 are used for timers T0 and T1 respectively.
P3.4 < T0 14 P3.6 and P3.7 are Write (WR) and Read (RD) pins.
P3.5 < T1 15
These are active low pins, means they will be active when 0 is
P3.6 < WR 16
P3.7 < RD 17 given to them and these are used to provide Read and Write
operations to External ROM in 8031 based systems.
Upon reset all ports have value FFH on them*
I/O BIT Manipulation Programming
Sometimes we need to access only 1 or 2 bits of the port in-
stead of the entire 8 bits.
To access single bit use syntax :
“SETB X.Y”
Where X is the port number 0,1,2 and 3.
Y is the desired bit from 0 to 7.
Section Break
Insert the title of your subtitle Here
Welcome!!
Insert the title of your subtitle Here
This PowerPoint Template has clean and neu-
tral design that can be adapted to any content
and meets various market segments.
Our Services
Insert the title of your subtitle Here
Your Text Here Your Text Here Your Text Here Your Text Here
Easy to change Easy to change Easy to change Easy to change
colors, photos colors, photos colors, photos colors, photos
and Text. and Text. and Text. and Text.
You can simply impress your audience and add a unique zing and appeal to your Presenta-
tions. Easy to change colors, photos and Text. I hope and I believe that this Template will
your Time, Money and Reputation. Easy to change colors, photos and Text. Get a modern
PowerPoint Presentation that is beautifully designed
Our Team Layout
Insert the title of your subtitle Here
Your Picture Here Your Picture Here Your Picture Here Your Picture Here Your Picture Here
Name Here Name Here Name Here Name Here Name Here
Programmer Programmer Programmer Programmer Programmer
Easy to change
colors, photos
and Text.
Timeline Style
Insert the title of your subtitle Here
Your Text Here Your Text Here
Get a modern Power- Get a modern Power-
Point Presentation that Point Presentation that
is beautifully designed. is beautifully designed.
2015 2016 2017 2018 2019
Your Text Here Your Text Here Your Text Here
Get a modern Power- Get a modern Power- Get a modern Power-
Point Presentation that Point Presentation that Point Presentation that
is beautifully designed. is beautifully designed. is beautifully designed.
Portfolio Presentation
Insert the title of your subtitle Here
Your Picture Here
Get a modern PowerPoint Presentation that is beautifully designed. Easy to change colors, photos
and Text. You can simply impress your audience and add a unique zing and appeal to your Presenta -
tions. Get a modern PowerPoint Presentation that is beautifully designed.
Infographic Style
Insert the title of your subtitle Here
Your Text Here
You can simply impress your audi-
ence and add a unique zing and
appeal to your Presentations.
Your Text Here
Your Text Here You can simply impress your audi-
ence and add a unique zing and
You can simply impress your audi- appeal to your Presentations.
ence and add a unique zing and
appeal to your Presentations.
Your Text Here
Your Text Here You can simply impress your audi-
ence and add a unique zing and
You can simply impress your audi- appeal to your Presentations.
ence and add a unique zing and
appeal to your Presentations.
80 Chart Style
Insert the title of your subtitle Here
70
Your Text Here
60
You can simply impress your audience and add a unique
50 zing and appeal to your Presentations. Get a modern
PowerPoint Presentation that is beautifully designed.
40 Get a modern PowerPoint Presentation that is beautifully
designed. Easy to change colors, photos and Text.
30
Your Text Here
20 You can simply impress your audience
and add a unique zing
10
Your Text Here
You can simply impress your audience
0
Text Text Text Text Text and add a unique zing
Infographic Style
Insert the title of your subtitle Here
Your Text Here
01 You can simply impress your audience
and add a unique zing.
Your Text Here
02 You can simply impress your audience
and add a unique zing.
Your Text Here
03 You can simply impress your audience
and add a unique zing.
Your Text Here
Infographic Style
Insert the title of your subtitle Here
Your Text Here
You can simply impress your audi-
ence and add a unique zing and
Your Text Here
appeal to your Presentations.
You can simply impress your audi-
ence and add a unique zing and
Your Text Here
appeal to your Presentations.
You can simply impress your audi-
ence and add a unique zing and
Your Text Here
appeal to your Presentations.
You can simply impress your audi-
ence and add a unique zing and
Your Text Here
appeal to your Presentations.
You can simply impress your audi-
ence and add a unique zing and
appeal to your Presentations.
Mobile Style
Insert the title of your subtitle Here
Simple Portfolio Simple Portfolio
Designed Designed
Easy to change colors, photos and Text. Easy to change colors, photos and Text.
Easy to change colors, photos and Text. Your Picture Here Easy to change colors, photos and Text.
Easy to change colors, photos and Text. Easy to change colors, photos and Text.
Your Text Here Your Text Here
You can simply impress You can simply impress
your audience and add a your audience and add a
unique zing. unique zing.
Infographic Style
Insert the title of your subtitle Here
Your Text Here
You can simply impress your audi-
ence and add a unique zing and
appeal to your Presentations. PowerP
o int
Design
ed Your Text Here
Your Text Here You can simply impress your audi-
You can simply impress your audi- ence and add a unique zing and
appeal to your Presentations.
ence and add a unique zing and Presenta
appeal to your Presentations. tio n
Your Picture Here
Your Text Here
You can simply impress your
audience and add a unique zing
and appeal to your Presenta-
tions.
Portfolio Presentation
Designed
Get a modern PowerPoint Presentation that is beautifully designed. Easy to
change colors, photos and Text.
Infographic Style
Insert the title of your subtitle Here
Your Text Here
You can simply impress your audi-
ence and add a unique zing and
appeal to your Presentations.
Your Text Here
You can simply impress your audi-
ence and add a unique zing and
Your Text Here
appeal to your Presentations.
You can simply impress your audi-
ence and add a unique zing and
appeal to your Presentations.
Your Text Here
You can simply impress your audi-
ence and add a unique zing and
appeal to your Presentations.
Portfolio Presentation
Insert the title of your subtitle Here
Get a modern PowerPoint
Presentation that is
beautifully designed.
Your Picture Here Your Picture Here Easy to change colors, Your Picture Here Your Picture Here
photos and Text. You can
simply impress your au-
dience and add a unique
zing and appeal to your
Presentations. Get a
modern PowerPoint Pre-
sentation that is beauti-
fully designed. Easy to
change colors, photos
Your Picture Here Your Picture Here and Text. Get a modern Your Picture Here Your Picture Here
PowerPoint Presentation
that is beautifully de-
signed.
Worldmap Style
Insert the title of your subtitle Here
01 02 03
Your Text Here Your Text Here Your Text Here
You can simply im- You can simply im- You can simply im-
press your audience press your audience press your audience
and add a unique and add a unique and add a unique
zing and appeal to zing and appeal to zing and appeal to
your Presentations. your Presentations. your Presentations.
Table Style
Insert the title of your subtitle Here
PowerPoint PowerPoint PowerPoint PowerPoint
Get a modern Get a modern Get a modern Get a modern
PowerPoint Pre- PowerPoint Pre- PowerPoint Pre- PowerPoint Pre-
sentation that is sentation that is sentation that is sentation that is
beautifully de- beautifully de- beautifully de- beautifully de-
signed. You can signed. You can signed. You can signed. You can
simply impress simply impress simply impress simply impress
your audience your audience your audience your audience
and add a unique and add a unique and add a unique and add a unique
zing and appeal zing and appeal zing and appeal zing and appeal
to your Presenta- to your Presenta- to your Presenta- to your Presenta-
tions tions tions tions
Worldmap Style
Insert the title of your subtitle Here
60%
You can simply impress your audi-
ence and add a unique zing and 60%
appeal to your Presentations.
70%
You can simply impress your audi-
ence and add a unique zing and
55%
appeal to your Presentations.
70%
50%
You can simply impress your audi-
ence and add a unique zing and
appeal to your Presentations.
Portfolio
Presentation
Your Picture Here
Designed
Get a modern PowerPoint Presen-
tation that is beautifully designed.
Easy to change colors, photos and
Text. You can simply impress your
audience and add a unique zing and
appeal to your Presentations. Get a Your Picture Here Your Picture Here
modern PowerPoint Presentation
that is beautifully designed. Easy to
change colors, photos and Text. Get
a modern PowerPoint Presentation
that is beautifully designed.
Your Picture Here
Infographic Style
Insert the title of your subtitle Here
Your Text Here Your Text Here Your Text Here
You can simply impress You can simply impress You can simply impress
your audience and add a your audience and add a your audience and add a
unique zing. unique zing. unique zing.
Your Text Here Your Text Here
You can simply impress You can simply impress
your audience and add a your audience and add a
unique zing. unique zing.
NetBook Style
Insert the title of your subtitle Here
Your Text Here
You can simply impress your audience and
add a unique zing and appeal to your Pre-
sentations.
Your Text Here
You can simply impress your audience and
Your Picture Here
add a unique zing and appeal to your Pre-
sentations.
Your Text Here
You can simply impress your audience and
add a unique zing and appeal to your Pre-
sentations.
Get a modern PowerPoint Presentation that is beautifully designed. Easy to change colors, photos and Text. You
can simply impress your audience and add a unique zing and appeal to your Presentations.
Table Style
Insert the title of your subtitle Here
Content Content Content Content
$150 $180 $130 $100
Get a modern Power- Get a modern Power- Get a modern Power- Get a modern Power-
Point Presentation that Point Presentation that Point Presentation that Point Presentation that
is beautifully designed. is beautifully designed. is beautifully designed. is beautifully designed.
Easy to change colors, Easy to change colors, Easy to change colors, Easy to change colors,
photos and Text photos and Text photos and Text photos and Text
Chart Style
Insert the title of your subtitle Here
46% 63% 78%
Your Text Here Your Text Here Your Text Here
You can simply impress You can simply impress You can simply impress
your audience and add your audience and add your audience and add
a unique zing. a unique zing. a unique zing.
Portfolio
Presentation Your Picture Here
Your Text Here
You can simply impress
your audience and add a
unique zing
Your Text Here
You can simply impress Your Picture Here
your audience and add a
unique zing
Your Text Here
You can simply impress
your audience and add a Your Picture Here Your Picture Here
unique zing
Your Text Here
You can simply impress
your audience and add a
unique zing
Infographic Style
Insert the title of your subtitle Here
Your Text Here Your Text Here
You can simply impress your audi- You can simply impress your audi-
ence and add a unique zing and ence and add a unique zing and
appeal to your Presentations. appeal to your Presentations.
Your Text Here
You can simply impress your audi-
ence and add a unique zing and
appeal to your Presentations.
Your Text Here
You can simply impress your audience and
Your Picture Here
add a unique zing and appeal to your Pre-
Portfolio sentations.
Presentation
Your Text Here
Designed You can simply
impress your
audience and Your Picture Here
add a unique
Get a modern PowerPoint Presen- zing and appeal
tation that is beautifully designed. to your Presen-
Easy to change colors, photos and tations.
Text.
You can simply impress your au-
dience and add a unique zing. Your Text Here
You can simply impress your audience and Your Picture Here
add a unique zing and appeal to your Pre-
sentations.
Chart Style
Insert the title of your subtitle Here
Your Text Here Your Text Here
You can simply impress your You can simply impress your
audience and add a unique zing 25% audience and add a unique zing
and appeal to your Presenta- and appeal to your Presenta-
tions. Get a modern PowerPoint
Presentation that is beautifully
45% tions. Get a modern PowerPoint
Presentation that is beautifully
designed. designed. Easy to change col-
ors, photos and Text.
Your Text Here
Get a modern PowerPoint Pre-
You can simply impress your
sentation that is beautifully de-
audience and add a unique zing
signed. Easy to change colors,
and appeal to your Presenta- 30% photos and Text.
tions. Get a modern PowerPoint
Presentation that is beautifully
designed.
Columns
Style
Your Text Here Your Text Here
You can simply impress your You can simply impress your
audience and add a unique audience and add a unique
Insert the title of your sub- zing and appeal to your Pre- zing and appeal to your Pre-
title Here sentations. Easy to change sentations. Easy to change
colors, photos and Text. You colors, photos and Text. You
can simply impress your audi- can simply impress your audi-
ence and add a unique zing ence and add a unique zing
and appeal to your Presenta- and appeal to your Presenta-
tions. tions.
You can simply impress your You can simply impress your
audience and add a unique audience and add a unique
zing and appeal to your Pre- zing and appeal to your Pre-
sentations. sentations.
Thank you
Insert the title of your subtitle Here
Fully Editable Shapes
Fully Editable Icon Sets : A
You can Resize without los-
ing quality
You can Change Fill Color &
Line Color
FREE
PPT TEM-
PLATES
www.allppt.com
Fully Editable Icon Sets : B
You can Resize without los-
ing quality
You can Change Fill Color &
Line Color
FREE
PPT TEM-
PLATES
www.allppt.com
Fully Editable Icon Sets : C
You can Resize without los-
ing quality
You can Change Fill Color &
Line Color
FREE
PPT TEM-
PLATES
www.allppt.com