CHAPTER #3- I/O PORT PROGRAMMING IN 8051

CHAPTER #3- I/O PORT PROGRAMMING IN 8051

Upon Completion of this chapter, you will be able to:

  • Explain the purpose of each pin of the 8051 microcontroller
  • List the 4 ports of the 8051
  • Describe the dual role of Port 0 in providing both data and addresses
  • Code Assembly Language to use the ports for input and output
  • Explain the use of Port 3 for interrupt signals
  • Code 8051 instructions for I/O handling
  • Code bit-manipulation instructions in the 8051

SECTION I - PIN DESCRIPTION THE 8051:

8051 Pin Diagram:

P1.01PDIP [DUAL IN LINE PACKAGE]40VCC
P1.12805139P0.0 [AD0]
P1.23 38P0.1 [AD1]
P1.34 37P0.2 [AD2]
P1.45 36P0.3 [AD3]
P1.56 35P0.4 [AD4]
P1.67 34P0.5 [AD5]
P1.78 33P0.6 [AD6]
RESET9 32P0.7 [AD7]
[RXD] P3.010 31EA / VPP
[TXD] P3.111 30ALE / PROG
[INT0] P3.212 29PSEN
[INT1] P3.313 28P2.7 [A15]
[T0] P3.414 27P2.6 [A14]
[T1] P3.515 26P2.5 [A13]
[WR] P3.616 25P2.4 [A12]
[RD] P3.717 24P2.3 [A11]
XTAL218 23P2.2 [A10]
XTAL119 22P2.1 [A9]
GROUND20 21P2.0 [A8]

The 8051 family members come in packages such as PDIP [Dual in-line package], QFP [Quad flat Package] and LLC [Leadless Chip Carrier]. All packages have 40 pins that are dedicated for various functions such as I/O, RD, WR, address, data, and interrupts. 20 pin version of 8051 also provided by some companies with a reduced number of I/O ports.

The above diagram is of 40 pins. The total of 32 pins are set aside for the four ports P0, P1, P2 and P3. Each port has 8 pins. The rest of the pins are designated as Vcc, GND, XTAL1, XTAL2, RST, and EA. These pins must be connected in order for the system to work. Function of each pin is described below.

  • Vcc:

Pin 40 provides supply voltage to the chip. The voltage supply is +5V.

  • GND:

Pin 20 is the DC ground.

  • XTAL1 and XTAL2:

The 8051 has an on-chip oscillator but requires an external clock to run it. The quartz crystal oscillator connected to XTAL1 and XTAL2 also needs two capacitors of 30pF value. One side of each capacitor is connected to the ground. The maximum oscillator frequency connected to XTAL1 and XTAL2 is referred as speed of the 8051. There are various crystal oscillators available used to connect to pins XTAL1 and XTAL2. e.g. 12 MHz, 20 MHz, 11.0592 MHz etc. The 12 MHz chip must be connected to a crystal with 12 MHz frequency or less. 20 MHz microcontroller requires a crystal frequency of no more than 20 MHz. When the 8051 is connected to a crystal oscillator and is powered up, the frequency on the XTAL2 pin using the oscilloscope can be observed

  • RST:

RESET Value of Some 8051 Registers:

REGISTERRESET VALUE
Program Counter [PC]0000
Accumulator [ACC]0000
B0000
Program Status Word [PSW]0000
Stack Pointer [SP]0007
Data Pointer [DPTR]0000

RESET is the pin # 9. This is an input pin and is active high [normally low]. When a high pulse is applied to this pin, the microcontroller will reset and terminate all activities. This is called power-on reset. All values given in the above table register will be lost upon power-on reset.

The value of the program counter [PC] is 0 upon reset, forcing the CPU to fetch the first opcode from ROM memory location 0000. This means that we must place the first line of source code in ROM location 0 because that is where the CPU wakes up and expects to find the first instruction.

The two ways of connecting the RST pin to the power-on reset circuitry is diagrammed below.

The RESET input must have a minimum duration of 2 machine cycles in order to make the RESET input effective. The high pulse must be high for a minimum of 2 machine cycles before it is allowed to go low.

  • EA [External Access]:

The EA pin is connected to Vcc for the 8051 family members. EA pin is pin # 31. This pin must be connected to to either Vcc or ground. EA pin cannot be left unconnected. In case of 8031 and 8032 microcontrollers, this pin is connected to GND. In 8031 and 8032 family members there in no on-chip ROM, code is stored on an external ROM. Connected EA pin to ground indicates that the code is stores externally.

  • PSEN [Program Store Enable]:

PSEN is a output pin. This pin number of this pin is 29. In an 8031 based system, where an external ROM holds the program code, this pin is connected to the OE [Output Enable] pin of the ROM.

  • ALE [Address Latch Enable]:

This is an active high output pin. This pin number of this pin is 30. When connecting an 8031 to an external ROM or memory, Port 0 provides both the address and data. The 8031 multiplexes address and data through Port 0 to save pins. The ALE pin is used for de-multiplexing the address and data by connecting to the G pin of the 74LS373 chip.

Functions of Input / Output I/O Port Pins:

There are four ports in 8051 microcontroller named as P0, P1, P2 and P3. Each port has 8 pins. For Port 0 8 pins names as P0.0, P0.1, P0.2, P0.3, P0.4, P0.5, P0.6, and P0.7. All the ports upon RESET are ready to be configured or used as output. To use all these ports as an input port, it must be programmed.

  • PORT 0:

Port 0 has 8 pins P0.0, P0.1, P0.2, P0.3, P0.4, P0.5, P0.6, and P0.7, pins 39, 38, 37, 36, 35, 34, 33, 32. This port can be used for input or output. Each pin of the port 0 must be connected externally to a 10K Ohm pull-up resistor, to use Port 0 as both input or output ports. This is because the Port 0 [P0] is an open drain. This is not the case in other ports P1, P2 and P3. 'Open drain' is a term used for MOS chips, as 'open collector' is used for TTL chips. To use Port 0 for both input and output, have to connect Port 0 to pull-up resistors. When external pull-up resistors connected upon reset, Port 0 is configures as output port.

Role of Port 0 as Input Port:

To make Port 0 as input port, the pull up resistors are connected to port 0. The port must be programmed by writing 1 to all the bits. Lets examine the code example below in which the Port 0 is configured first as an input port by writing 1's to it and then data is received from that port and send to Port 1 [P1].

MOV A,#0FFH;Load accumulator with value FFH in hex or 255 in decimal
MOV P0,A;Make Port 0 as an input port by writing all 1's to it
BACK: MOV A,P0;Get data from Port 0
MOV P1,A;Send it to Port 1
SJMP BACK;Keep doing it repeatedly

The Dual Role of Port 0:

Port 0 can be used to configures for both data and address. The Port 0 is also designated as AD0 - AD7. When connecting an 8051 to an external memory, port 0 provides both address and data. The 8051 multiplexes address and data through Port 0 to save pins. Address latch enable [ALE] indicates if Port 0 has address or data. When ALE=0, it provides data D0 - D7, but when ALE = 1, it has address A0 - A7. With the help of 74LS373 latch, ALE is used for demultiplexing address and data.

  • PORT 1:

​Port 1 has a total of 8 pins. P1.0, P1.1, P1.2, P.13, P1.4, P1.5, P1.6, and P1.7. Port 1 can be use as an input or output. As compares to Port 0, this port does not need any pull-up resistors. Port 1 already has internally connected pull-up resistors. Port 1 is configured as a output port when reset.

Example # 1:

Lets examine the code, which will continuously send Output to Port 1 the alternating values 55H and AAH

MOV A,#55H
BACK MOV P1,A
ACALL DELAY
CPL A
SJMP BACK

The Role of Port 1 as Input Port:

The Port 1 must be programmed by writing 1's to all thr bits in order to make Port 1 as an input port.

Example # 2:

MOV A,#0FFH; Load Accumulator with FFH in hex
MOV P1,A;Make P1 an input Port, by writing all 1's to Port 1
MOV A,P1;Get data from Port 1
MOV R3,A;Save data in register R3
ACALL DELAY;Wait
MOV A,P1;Get another data from Port 1
MOV R4,A;Save data in register R4
ACALL DELAY;Wait
MOV A,P1;Get another data from Port 1
MOV R5,A;Save data in register R5

In Example # 2 above, the Port 1 is configures as input port by writing 1's to it, then data is received from that port and saved in R3, R4 and R5.

  • PORT 2:

​Port 2 also has total of 8 pins. P2.0, P2.1, P2.2, P2.3, P2.4, P2.5, P2.6, and P2.7. Port 2 can be used for input or output port. To make Port 2 as an input, the Port 2 must be programmed by writing 1's to all bits.

Example # 3:

MOV A,#55H
BACK: MOV P2,A
ACALL DELAY
CPL A
SJMP BACK

In Example # 3 above, the code will send out continuously to Port 2 an alternating values 55H and AAH to toggle the bits or Port 2 continuously.

The Role of Port 2 as an Input Port:

The Port 2 must be programmed by writing 1's to all the port 2 bits to make Port 2 as an  input port.

Example #4:

MOV A,#0FFH;Load accumulator with value 0FFH in hex
MOV P2,A;Make Port 2 an input port by writing 1's to all bits of port 2
BACK: MOV A,P2;Get data from Port 2
MOV P1,A;Send data to Port 1
SJMP BACK;Keep doing it repeatedly

In Example # 4 above, the Port 2 is configures as input port by writing all 1's, then data is received from that port and is sent to Port 1 continuously.

The Dual Role or Port 2:

Port 2 has dual role. Port 2 is also designated as A8 - A15. This indicates that Port 2 has a dual function. An 8031 microcontroller is capable of accessing 64K bytes of external memory, it needs a path for the 16 bits of the address. P0 provides the lower 8 bits via A0 - A7 while Port 2 provides bits A8 - A15 of the address. When the 8031 is connected to external memory, Port 2 is used for the upper 8 bits of the 16-bit address, and it cannot be used for input / output operations.

  • Port 3:

​Port 3 has total 8 Pins. P3.0, P3.1, P3.2, P3.3, P3.4, P3.5, P3.6,  and P3.7. Pins 10, 11, 12, 13, 14, 15, 16, and 17. Port 3 can be sued as an input port or output port. Port 3 does need any pull-up resisters, just like the Port 1 and Port 2 does not require pull-up resistors. Only Port 0 require Pull-up resistors. Port 3 configured as an output port upon system reset. Port 3 has additional function of providing signals of interrupts.

Alternate Functions of Port 3:

PORT 3 BITFUNCTIONPIN
P3.0RxD [Receive Serial Communications Signals]10
P3.1TxD [Transmit Serial Communications Signals]11
P3.2INT0 [External Interrupt 0]12
P3.3INT1 [External Interrupt 1]13
P3.4T0 [Timer 0]14
P3.5T1 [Timer 1]15
P3.6WR [Write Signals of External Memory]16
P3.7RD [Read Signals of External Memory]17

 

More From Iamtechnical.com

Advertisement: