0% found this document useful (0 votes)
5 views

chapter 4

Chapter Four discusses the basic input/output operations of 8051 microcontrollers, detailing the structure and configuration of its four I/O ports (P0, P1, P2, and P3). Each port has specific characteristics for input and output operations, including the need for pull-up resistors and internal configurations. Additionally, the chapter includes examples of simple input/output projects, such as LED interfacing and basic arithmetic operations in C programming.

Uploaded by

shimels chekole
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

chapter 4

Chapter Four discusses the basic input/output operations of 8051 microcontrollers, detailing the structure and configuration of its four I/O ports (P0, P1, P2, and P3). Each port has specific characteristics for input and output operations, including the need for pull-up resistors and internal configurations. Additionally, the chapter includes examples of simple input/output projects, such as LED interfacing and basic arithmetic operations in C programming.

Uploaded by

shimels chekole
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 25

Chapter Four

Basic Input/output Operations


Contents
• I/O port detailed structure
• Simple Input/output project (Different Programs for LED Interfacing)
4.1. I/O port detailed structure
• All 8051 microcontrollers have 4 I/O ports each comprising 8 bits which
can be configured as inputs or outputs. Accordingly, in total of 32
input/output pins enabling the microcontroller to be connected to
peripheral devices are available for use.
• Pin configuration, i.e. whether it is to be configured as an input (1) or an
output (0), depends on its logic state. In order to configure a
microcontroller pin as an input, it is necessary to apply a logic zero (0) to
appropriate I/O port bit. In this case, voltage level on appropriate pin will
be 0.
• Similarly, in order to configure a microcontroller pin as an input, it is
necessary to apply a logic one (1) to appropriate port. In this case, voltage
level on appropriate pin will be 5V (as is the case with any TTL input).
This may seem confusing but don't loose your patience. It all becomes
clear after studying simple electronic circuits connected to an I/O pin.
• 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
• The structure of a Port-0 pin is shown in fig .It has 8 pins (P0.0-P0.7).
• Port-0 can be used as a normal bidirectional I/O port or it can be used for address/data
interfacing for accessing external memory. When control is '1', the port is used for
address/data interfacing. When the control is '0‘, the port can be used as a
bidirectional I/O port.
PORT 0 as an Input Port
• Let us assume that control is '0'. When the port is used as an input port, '1' is written to
the latch. In this situation both the output MOSFETs are 'off'. Hence the output pin
have floats hence whatever data written on pin is directly read by read pin.
PORT 0 as an Output Port
• Suppose we want to write 1 on pin of Port 0, a '1' written to the latch which turns 'off' the
lower FET while due to '0' control signal upper FET also turns off as shown in fig. above.
Here we wants logic '1' on pin but we getting floating value so to convert that floating
value into logic '1' we need to connect the pull up resistor parallel to upper FET . This is
the reason why we needed to connect pull up resistor to port 0 when we want to initialize
port 0 as an output port .
PORT 1:
• The structure of a port-1 pin is shown in fig below. It has 8 pins (P1.0-
P1.7)
• Port-1 dedicated only for I/O interfacing. When used as output port, not needed to
connect additional pull-up resistor like port 0. It have provided internally pull-up
resistor as shown in fig. above. The pin is pulled up or down through internal pull-
up when we want to initialize as an output port. To use port-1 as input port, '1' has to
be written to the latch. In this input mode when '1' is written to the pin by the
external device then it read fine. But when '0' is written to the pin by the external
device then the external source must sink current due to internal pull-up. If the
external device is not able to sink the current the pin voltage may rise, leading to a
possible wrong reading.
• 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
• To make port 1 an input port, it must be programmed as such by writing 1 to all its
bits
PORT 2:
• The structure of a port-2 pin is shown in fig. below. It has 8-pins (P2.0-P2.7)
• Just like P1, 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 we use for higher external address byte or a normal input/output port. The I/O
operation is similar to Port-1. Port-2 latch remains stable when Port-2 pin are used
for external memory access. Here again due to internal pull-up there is limited
current driving capability.
• 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, indicating its dual function
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 (P3.0-P3.7) having alternate functions to each pin, The internal
structure of a port-3 pin is shown in fig below.
• Each pin of Port-3 can be individually programmed for I/O operation or
for alternate function. The alternate function can be activated only if
the corresponding latch has been written to '1'. To use the port as input
port, '1' should be written to the latch. This port also has internal pull-
up and limited current driving capability.
• Port 3 has the additional function of providing some extremely
important signals

• It work as an IO port same like Port 2. only alternate function of port


3 makes its architecture different than other ports.
4.2. Simple Input/output project (Different Programs for
LED Interfacing)
 How to create a new project??
• First start KIEL IDE software
• Select project and click new project
• Give name of the project(do not add any .asm or .c extension)
• Now the save the project after that select your microcontroller, here I select
atmel at89c51
• Now go to file and select new file
• Now save the editor file in the same folder where you have saved your project
• Now write your program code here and save it
• Now add this file (save it with extension .c) in the project i.e in source group
1.
• Now right click on source group and click on build target.
• How to create ***.hex file?
1. After finish the program build
2. Move your mouse on target 1. Right click  options for target 
output  create hex file  Ok
3. Re build your program and see the .hex file
• How to run the program with MCU circuit?
After creating ***.hex file from your program
1. Open your MCU circuit design.
2. Move your mouse on the MCU chip Right click  Edit properties 
click program file folder to choose the address of your .hex file 
click your ***.hex file Ok click play button to check the circuit
simulation result.
Write & execute c language program to addition, subtraction, multiplication and division two
integer numbers.
A. The algorithm (using char) is
#include<reg51.h>
void main(void)
{
unsigned char x,y,z; // declare three variables here
x=0x34;
y=0xa9; // assign some value to two integers
P1=0x00; //now declare port 1 as an output port to show the result.
Write 0 value to port 1
z=x+y; // now perform addition and store the result in to
third variable z.
P1=z; // now giving value of z i.e addition of numbers to port 1.
}
• Now go to debug and click on start/stop.
• Go to peripherals and select port 1.
• Now run the project and you will get addition of two numbers in port 1.
The result will be 0xDD
B. The algorithm (using int) is C. The algorithm (using long) is
#include<reg51.h> #include<reg51.h>
void main(void) void main(void)
{ {
unsigned int x,y,z,s,w,v; unsigned long x,y,z,s,w,v;
x=0x34; x=0x3445;
y=0xa9; y=0xa953;
P1=0x00; P1=0x00;
P2=0x00; P2=0x00;
P3=0x00; P3=0x00;
P0=0x00; P0=0x00;
z=x+y; z=x+y;
w=x*y; w=x*y;
v=y/x; v=y/x;
s=x-y; s=x-y;
P0=v; P0=v;
P1=z; P1=z;
P2=w; P2=w;
P3=s; P3=s;
} }
Example 1. Create one-led blinking using keil and protuse
#include<reg51.h>
sbit led=P1^0;
void delay(void);
void main(void)
{
while(1) //infinite loop
{
led=0;
delay();
led=1;
delay();
}
}
void delay(void)
{
int i;
int j;
for(i=0;i<10;i++)
{
for(j=0;j<1000;j++);
}
Example 2. Seven segment display
#include<reg51.h>
int i;
int j;
char
nums[10]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0xf
f,0x6f};
void main(void)
{
while(1) //infinite loop
{

for(i=0;i<10;i++)
{
P1=nums[i];
for(j=0;j<30000;j++);
}
}
}
Example 3. Simple traffic light
#include<reg51.h> red=0;
sbit red=P1^0; yellow=1;
sbit yellow=P1^1; for(i=0;i<60000;i++);
sbit green=P1^2; yellow=0;
green=1;
void main(void)
for(i=0;i<60000;i++);
{
for(i=0;i<60000;i++);
unsigned int i;
for(i=0;i<60000;i++);
red=yellow=green=0;
green=0;
while(1)
yellow=1;
{
red=1; for(i=0;i<60000;i++);
for(i=0;i<60000;i++); yellow=0;
for(i=0;i<60000;i++); }
for(i=0;i<60000;i++); }
Example 4. Write an 8051 C program to toggle bits of P1 ports continuously
with a 250 ms.
#include <reg51.h>
void MSDelay(unsigned int); U1
void main(void) 19
XTAL1 P0.0/AD0
39
38
P0.1/AD1
{ 18
XTAL2
P0.2/AD2
P0.3/AD3
37
36
35
while (1) //repeat forever P0.4/AD4
P0.5/AD5
34
33
R8 P0.6/AD6
{ 220R
9
RST P0.7/AD7
32

21
P1=0x55; R7 P2.0/A8
22
P2.1/A9
220R 23
P2.2/A10
MSDelay(250); R6 29
30
PSEN P2.3/A11
24
25
ALE P2.4/A12
P1=0xAA; 220R
R4
31
EA P2.5/A13
P2.6/A14
26
27
28
MSDelay(250); 220R
P2.7/A15
1 10
R3 P1.0 P3.0/RXD
} 2
3
P1.1 P3.1/TXD
11
12
220R P1.2 P3.2/INT0
4 13
} R5 5
P1.3 P3.3/INT1
14
P1.4 P3.4/T0
220R 6 15
P1.5 P3.5/T1
void MSDelay(unsigned int itime) R1 7
8
P1.6 P3.6/WR
16
17
P1.7 P3.7/RD
{ R2
220R
AT89C51

unsigned int i,j; 220R

for (i=0;i<itime;i++)
for (j=0;j<1275;j++);
}
Example 5. Counter up to 99
#include<reg51.h>
void delay(unsigned int ms)
{
unsigned int i,j;
for(i=0;i<ms;i++) U1
19 39
XTAL1 P0.0/AD0
for(j=0;j<1275;j++); P0.1/AD1
P0.2/AD2
38
37
18 36
} XTAL2 P0.3/AD3
P0.4/AD4
35
34
P0.5/AD5
void main(void) 9
P0.6/AD6
33
32
RST P0.7/AD7
{ P2.0/A8
21
22
char number[10]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0xff,0x6f}; P2.1/A9
P2.2/A10
23
29 24
PSEN P2.3/A11
int i,j; 30
31
ALE P2.4/A12
25
26
EA P2.5/A13
P1=0x00; P2.6/A14
27
28
P2.7/A15
P2=0x00; 1
P1.0 P3.0/RXD
10
2 11
P1.1 P3.1/TXD
while(1) 3
4
P1.2 P3.2/INT0
12
13
P1.3 P3.3/INT1
{ 5
6
P1.4 P3.4/T0
14
15
P1.5 P3.5/T1
7 16
for(i=0;i<10;i++) 8
P1.6
P1.7
P3.6/WR
P3.7/RD
17

{ AT89C51

P1=number[i];
for(j=0;j<10;j++)
{
P2=number[j];
delay(50);
}
}

You might also like