Embedded Systems 简明教程

Embedded Systems - I/O Programming

在 8051 中,I/O 操作使用四个端口和 40 个引脚完成。以下引脚图显示了 40 个引脚的详细信息。I/O 操作端口预留 32 个引脚,每个端口有 8 个引脚。其他 8 个引脚指定为 Vcc、GND、XTAL1、XTAL2、RST、EA(条形)、ALE/PROG(条形)和 PSEN(条形)。

In 8051, I/O operations are done using four ports and 40 pins. The following pin diagram shows the details of the 40 pins. I/O operation port reserves 32 pins where each port has 8 pins. The other 8 pins are designated as Vcc, GND, XTAL1, XTAL2, RST, EA (bar), ALE/PROG (bar), and PSEN (bar).

它是 40 针 PDIP(塑料双列直插式封装)

It is a 40 Pin PDIP (Plastic Dual Inline Package)

pin diagram

Note - 在 DIP 封装中,你可以通过 IC 中间的切口识别第一个引脚和最后一个引脚。第一个引脚位于此切口标记的左侧,最后一个引脚(即本例中的第 40 个引脚)位于切口标记的右侧。

Note − In a DIP package, you can recognize the first pin and the last pin by the cut at the middle of the IC. The first pin is on the left of this cut mark and the last pin (i.e. the 40th pin in this case) is to the right of the cut mark.

I/O Ports and their Functions

四个端口 P0、P1、P2 和 P3 各使用 8 个引脚,使其成为 8 位端口。复位后,所有端口都配置为输入,可立即用作输入端口。当第一个 0 写入端口时,它将变为输出。要重新配置它为输入,必须向端口发送 1。

The four ports P0, P1, P2, and P3, each use 8 pins, making them 8-bit ports. Upon RESET, all the ports are configured as inputs, 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 a port.

Port 0 (Pin No 32 – Pin No 39)

它有 8 个引脚(32 到 39)。它可以用于输入或输出。与 P1、P2 和 P3 端口不同,我们通常将 P0 连接到 10K-ohm 上拉电阻,以便用作输入或输出端口,因为它是一个开漏输出。

It has 8 pins (32 to 39). It can be used for input or output. Unlike P1, P2, and P3 ports, we normally connect P0 to 10K-ohm pull-up resistors to use it as an input or output port being an open drain.

它还指定为 AD0-AD7,允许它同时用作地址和数据。对于 8031(即无 ROM 芯片),当我们需要访问外部 ROM 时,P0 将同时用于地址和数据总线。ALE(引脚号 31)指示 P0 具有地址或数据。当 ALE = 0 时,它提供数据 D0-D7,但当 ALE = 1 时,它具有地址 A0-A7。如果没有外部存储器连接,则必须将 P0 外部连接到 10K-ohm 上拉电阻。

It is also designated as AD0-AD7, allowing it to be used as both address and data. In case of 8031 (i.e. ROMless Chip), when we need to access the external ROM, then P0 will be used for both Address and Data Bus. ALE (Pin no 31) indicates if P0 has address or data. When ALE = 0, it provides data D0-D7, but when ALE = 1, it has address A0-A7. In case no external memory connection is available, P0 must be connected externally to a 10K-ohm pull-up resistor.

port0
MOV A,#0FFH  ;(comments: A=FFH(Hexadecimal  i.e. A=1111 1111)

MOV P0,A     ;(Port0 have 1's on every pin so that it works as Input)

Port 1 (Pin 1 through 8)

它是一个 8 位端口(第 1 至 8 个引脚),可以作为输入或输出使用。它不需要上拉电阻,因为它们已经内部连接。复位后,端口 1 配置为输入端口。以下代码可用于向端口 1 发送 55H 和 AAH 的交替值。

It is an 8-bit port (pin 1 through 8) and can be used either as input or output. It doesn’t require pull-up resistors because they are already connected internally. Upon reset, Port 1 is configured as an input port. The following code can be used to send alternating values of 55H and AAH to Port 1.

;Toggle all bits of continuously
MOV     A,#55
BACK:

MOV     P2,A
ACALL   DELAY
CPL     A      ;complement(invert) reg. A
SJMP    BACK

如果端口 1 配置为用作输出端口,那么要再次用作输入端口,请对它进行编程,方法是像以下代码一样向它的所有位写入 1。

If Port 1 is configured to be used as an output port, then to use it as an input port again, program it by writing 1 to all of its bits as in the following code.

;Toggle all bits of continuously

MOV     A ,#0FFH    ;A = FF hex
MOV     P1,A        ;Make P1 an input port
MOV     A,P1        ;get data from P1
MOV     R7,A        ;save it in Reg R7
ACALL   DELAY       ;wait

MOV     A,P1        ;get another data from P1
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

Port 2 (Pins 21 through 28)

端口 2 共占用 8 个引脚(第 21 至 28 个引脚),可用于输入和输出操作。就像 P1(端口 1)一样,P2 也不需要外部上拉电阻,因为它们已经内部连接。它必须与 P0 一起使用,以便为外部存储器提供 16 位地址。因此,它也指定为 (A0–A7),如引脚图所示。当 8051 连接到外部存储器时,它为 16 位地址的高 8 位提供路径,并且不能用作 I/O。复位后,端口 2 配置为输入端口。以下代码可用于向端口 2 发送 55H 和 AAH 的交替值。

Port 2 occupies a total of 8 pins (pins 21 through 28) 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. It must be used along with P0 to provide the 16-bit address for the external memory. So it is also designated as (A0–A7), as shown in the pin diagram. When the 8051 is connected to an external memory, it 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. The following code can be used to send alternating values of 55H and AAH to port 2.

;Toggle all bits of continuously
MOV     A,#55
BACK:
MOV     P2,A
ACALL   DELAY
CPL     A         ; complement(invert) reg. A
SJMP    BACK

如果端口 2 配置为用作输出端口,那么要再次用作输入端口,请对它进行编程,方法是像以下代码一样向它的所有位写入 1。

If Port 2 is configured to be used as an output port, then to use it as an input port again, program it by writing 1 to all of its bits as in the following code.

;Get a byte from P2 and send it to P1
MOV    A,#0FFH    ;A = FF hex
MOV    P2,A       ;make P2 an input port
BACK:
MOV    A,P2       ;get data from P2
MOV    P1,A       ;send it to Port 1
SJMP   BACK       ;keep doing that

Port 3 (Pins 10 through 17)

它也是 8 位的,可以用作输入/输出。此端口提供一些非常重要的信号。P3.0 和 P3.1 分别是 RxD(接收器)和 TxD(发送器),并共同用于串行通信。P3.2 和 P3.3 引脚用于外部中断。P3.4 和 P3.5 分别用于计时器 T0 和 T1。P3.6 和 P3.7 是写(WR)和读(RD)引脚。这些是低电平有效引脚,这意味着当向它们提供 0 时它们将变为活动状态,并且它们用于向基于 8031 的系统中的外部 ROM 提供读写操作。

It is also of 8 bits and can be used as Input/Output. This port provides some extremely important signals. P3.0 and P3.1 are RxD (Receiver) and TxD (Transmitter) respectively and are collectively used for Serial Communication. P3.2 and P3.3 pins are used for external interrupts. P3.4 and P3.5 are used for timers T0 and T1 respectively. P3.6 and P3.7 are Write (WR) and Read (RD) pins. These are active low pins, means they will be active when 0 is given to them and these are used to provide Read and Write operations to External ROM in 8031 based systems.

P3 Bit

Function

Pin

P3.0

RxD

10

P3.1

TxD

11

P3.2

Complement of INT0

12

P3.3

INT1

13

P3.4

T0

14

P3.5

T1

15

P3.6

WR

16

P3.7

Complement of RD

17

Dual Role of Port 0 and Port 2

  1. Dual role of Port 0 − Port 0 is also designated as AD0–AD7, as it can be used for both data and address handling. While connecting an 8051 to external memory, Port 0 can provide both address and data. The 8051 microcontroller then multiplexes the input as address or data in order to save pins.

  2. Dual role of Port 2 − Besides working as I/O, Port P2 is also used to provide 16-bit address bus for external memory along with Port 0. Port P2 is also designated as (A8– A15), while Port 0 provides the lower 8-bits via A0–A7. In other words, we can say that when an 8051 is connected to an external memory (ROM) which can be maximum up to 64KB and this is possible by 16 bit address bus because we know 216 = 64KB. Port2 is used for the upper 8-bit of the 16 bits address, and it cannot be used for I/O and this is the way any Program code of external ROM is addressed.

Hardware Connection of Pins

  1. Vcc − Pin 40 provides supply to the Chip and it is +5 V.

  2. Gnd − Pin 20 provides ground for the Reference.

  3. XTAL1, XTAL2 (Pin no 18 & Pin no 19) − 8051 has on-chip oscillator but requires external clock to run it. A quartz crystal is connected between the XTAL1 & XTAL2 pin of the chip. This crystal also needs two capacitors of 30pF for generating a signal of desired frequency. One side of each capacitor is connected to ground. 8051 IC is available in various speeds and it all depends on this Quartz crystal, for example, a 20 MHz microcontroller requires a crystal with a frequency no more than 20 MHz.

xtal
  1. RST (Pin No. 9) − It is an Input pin and active High pin. Upon applying a high pulse on this pin, that is 1, the microcontroller will reset and terminate all activities. This process is known as Power-On Reset. Activating a power-on reset will cause all values in the register to be lost. It will set a program counter to all 0’s. To ensure a valid input of Reset, the high pulse must be high for a minimum of two machine cycles before it is allowed to go low, which depends on the capacitor value and the rate at which it charges. (Machine Cycle is the minimum amount of frequency a single instruction requires in execution).

  2. EA or External Access (Pin No. 31) − It is an input pin. This pin is an active low pin; upon applying a low pulse, it gets activated. In case of microcontroller (8051/52) having on-chip ROM, the EA (bar) pin is connected to Vcc. But in an 8031 microcontroller which does not have an on-chip ROM, the code is stored in an external ROM and then fetched by the microcontroller. In this case, we must connect the (pin no 31) EA to Gnd to indicate that the program code is stored externally.

rst
  1. PSEN or Program store Enable (Pin No 29) − This is also an active low pin, i.e., it gets activated after applying a low pulse. It is an output pin and used along with the EA pin in 8031 based (i.e. ROMLESS) Systems to allow storage of program code in external ROM.

  2. ALE or (Address Latch Enable) − This is an Output Pin and is active high. It is especially used for 8031 IC to connect it to the external memory. It can be used while deciding whether P0 pins will be used as Address bus or Data bus. When ALE = 1, then the P0 pins work as Data bus and when ALE = 0, then the P0 pins act as Address bus.

I/O Ports and Bit Addressability

这是在编写 8051 代码时对 8051 使用最广泛的功能。有时我们只需要访问端口的 1 或 2 位,而不是整个 8 位。8051 提供了访问端口的各个位的能力。

It is a most widely used feature of 8051 while writing code for 8051. Sometimes we need to access only 1 or 2 bits of the port instead of the entire 8-bits. 8051 provides the capability to access individual bits of the ports.

在以单比特方式访问端口时,我们使用语法“SETB X. Y”,其中 X 是端口号 (0 到 3),Y 是数据位 D0-D7 的位号 (0 到 7),其中 D0 是 LSB,而 D7 是 MSB。例如,“SETB P1.5”设置端口 1 的高电平位 5。

While accessing a port in a single-bit manner, we use the syntax "SETB X. Y" where X is the port number (0 to 3), and Y is a bit number (0 to 7) for data bits D0-D7 where D0 is the LSB and D7 is the MSB. For example, "SETB P1.5" sets high bit 5 of port 1.

以下代码展示了我们如何连续切换位 P1.2。

The following code shows how we can toggle the bit P1.2 continuously.

AGAIN:
SETB    P1.2
ACALL   DELAY
CLR     P1.2
ACALL   DELAY
SJMP    AGAIN

Single-Bit Instructions

Instructions

Function

SETB bit

Set the bit (bit = 1)

CLR bit

clear the bit (bit = 0)

CPL bit

complement the bit (bit = NOT bit)

JB bit, target

jump to target if bit = 1 (jump if bit)

JNB bit, target

jump to target if bit = 0 (jump if no bit)

JBC bit, target

jump to target if bit = 1, clear bit (jump if bit, then clear)