Вы находитесь на странице: 1из 33

Chap 4 I/O PORT Programming

The 8051 Microcontroller and Embedded Systems, 2e By Muhammad Ali Mazidi, Janice Gillispie Mazidi, and Rolin D. McKinlay

2006 Pearson Education, Inc. Pearson Prentice Hall Upper Saddle River, NJ 07458

4.1 8051 I/O Programming


4 I/O ports : P0, P1, P2, P3
Each takes one-byte

All are input ports upon RESET


Write 0 to a port , it becomes an output Write 1 to a port, it becomes an input

The 8051 Microcontroller and Embedded Systems, 2e By Muhammad Ali Mazidi, Janice Gillispie Mazidi, and Rolin D. McKinlay

2006 Pearson Education, Inc. Pearson Prentice Hall Upper Saddle River, NJ 07458

Figure 41

8051 Pin Diagram

The 8051 Microcontroller and Embedded Systems, 2e By Muhammad Ali Mazidi, Janice Gillispie Mazidi, and Rolin D. McKinlay

2006 Pearson Education, Inc. Pearson Prentice Hall Upper Saddle River, NJ 07458

PORT 0
Pin 32-39 Each pin must be connected to a 10K resistor
Due to the nature of open-drain for MOS chips

The 8051 Microcontroller and Embedded Systems, 2e By Muhammad Ali Mazidi, Janice Gillispie Mazidi, and Rolin D. McKinlay

2006 Pearson Education, Inc. Pearson Prentice Hall Upper Saddle River, NJ 07458

Port 0 as Input
;Get a byte from P0 and send it to P1 MOV A,#0FFH MOV P0,A BACK: MOV A,P0 MOV P1,A SJMP BACK
The 8051 Microcontroller and Embedded Systems, 2e By Muhammad Ali Mazidi, Janice Gillispie Mazidi, and Rolin D. McKinlay 2006 Pearson Education, Inc. Pearson Prentice Hall Upper Saddle River, NJ 07458

Dual role of Port 0


Also can be AD0-AD7
For both address and data of extern memory

The 8051 Microcontroller and Embedded Systems, 2e By Muhammad Ali Mazidi, Janice Gillispie Mazidi, and Rolin D. McKinlay

2006 Pearson Education, Inc. Pearson Prentice Hall Upper Saddle River, NJ 07458

Port 1
Pin 1 to 8 Do not need pull-up resistros Set as input upon RESET

The 8051 Microcontroller and Embedded Systems, 2e By Muhammad Ali Mazidi, Janice Gillispie Mazidi, and Rolin D. McKinlay

2006 Pearson Education, Inc. Pearson Prentice Hall Upper Saddle River, NJ 07458

MOV MOV MOV MOV ACALL MOV MOV ACALL MOV MOV

A,#0FFH P1,A A,P1 R7,A DELAY A,P1 R6,A DELAY A,P1 R5,A
2006 Pearson Education, Inc. Pearson Prentice Hall Upper Saddle River, NJ 07458

The 8051 Microcontroller and Embedded Systems, 2e By Muhammad Ali Mazidi, Janice Gillispie Mazidi, and Rolin D. McKinlay

Port 2
Pin 21-28 Set as input upon RESET Also as A8-A15 : addresses for external memory

The 8051 Microcontroller and Embedded Systems, 2e By Muhammad Ali Mazidi, Janice Gillispie Mazidi, and Rolin D. McKinlay

2006 Pearson Education, Inc. Pearson Prentice Hall Upper Saddle River, NJ 07458

Port 3
Pin 10 to 17

The 8051 Microcontroller and Embedded Systems, 2e By Muhammad Ali Mazidi, Janice Gillispie Mazidi, and Rolin D. McKinlay

2006 Pearson Education, Inc. Pearson Prentice Hall Upper Saddle River, NJ 07458

Ex. 4-1
Write a test program for a 8051 chip to toggle all the bits of P0, P1, and P2 every of a second. Assume a crystal frequency of 11.0592 MHz

The 8051 Microcontroller and Embedded Systems, 2e By Muhammad Ali Mazidi, Janice Gillispie Mazidi, and Rolin D. McKinlay

2006 Pearson Education, Inc. Pearson Prentice Hall Upper Saddle River, NJ 07458

Solution ( part of )
ORG 0 BACK: MOV A,#55H MOV P0,A MOV P1,A MOV P2,A CALL QSDELAY
The 8051 Microcontroller and Embedded Systems, 2e By Muhammad Ali Mazidi, Janice Gillispie Mazidi, and Rolin D. McKinlay

MOV A,#0AAH MOV P0,A MOV P1,A MOV P2,A CALL QSDELAY SJMP BACK

2006 Pearson Education, Inc. Pearson Prentice Hall Upper Saddle River, NJ 07458

Different Ways of Toggle I/O Ports


BACK: MOV P1,#55H CALL DELAY MOV P1,#0AAH CALL DELAY SJMP BACK MOV A,#55H BACK: MOV P1,A CALL DELAY CPL A SJMP BACK

The 8051 Microcontroller and Embedded Systems, 2e By Muhammad Ali Mazidi, Janice Gillispie Mazidi, and Rolin D. McKinlay

2006 Pearson Education, Inc. Pearson Prentice Hall Upper Saddle River, NJ 07458

Table 42

Reset Value of Some 8051 Ports

The 8051 Microcontroller and Embedded Systems, 2e By Muhammad Ali Mazidi, Janice Gillispie Mazidi, and Rolin D. McKinlay

2006 Pearson Education, Inc. Pearson Prentice Hall Upper Saddle River, NJ 07458

I/O Ports and Bit-addressability


I/O ports bits can be set/cleared individually!!
SETB P1.2 CLR P1.2 CPL P1.2

One-bit toggle
BACK: CPL P1.2 CALL DELAY SJMP BACK
2006 Pearson Education, Inc. Pearson Prentice Hall Upper Saddle River, NJ 07458

The 8051 Microcontroller and Embedded Systems, 2e By Muhammad Ali Mazidi, Janice Gillispie Mazidi, and Rolin D. McKinlay

Table 43

Single-Bit Addressability of Ports

The 8051 Microcontroller and Embedded Systems, 2e By Muhammad Ali Mazidi, Janice Gillispie Mazidi, and Rolin D. McKinlay

2006 Pearson Education, Inc. Pearson Prentice Hall Upper Saddle River, NJ 07458

Ex. 4-2
Create a square wave of 50% duty cycle on bit 0 of port 1 Create a square wave of 66% duty cycle on bit 3 of port 1 The 66% duty cycle means :
H : 66%, L : 33%

The 8051 Microcontroller and Embedded Systems, 2e By Muhammad Ali Mazidi, Janice Gillispie Mazidi, and Rolin D. McKinlay

2006 Pearson Education, Inc. Pearson Prentice Hall Upper Saddle River, NJ 07458

50% Duty Cycle


HERE: SETB P1.0 CALL DELAY CLR P1.0 CALL DELAY SJMP HERE HERE: CPL P1.0 CALL DELAY SJMP HERE

The 8051 Microcontroller and Embedded Systems, 2e By Muhammad Ali Mazidi, Janice Gillispie Mazidi, and Rolin D. McKinlay

2006 Pearson Education, Inc. Pearson Prentice Hall Upper Saddle River, NJ 07458

66% Duty cycle


BACK: SETB CALL CALL CLR CALL SJMP P1.3 DELAY DELAY P1.3 DELAY BACK
2006 Pearson Education, Inc. Pearson Prentice Hall Upper Saddle River, NJ 07458

The 8051 Microcontroller and Embedded Systems, 2e By Muhammad Ali Mazidi, Janice Gillispie Mazidi, and Rolin D. McKinlay

Table 44

Single-Bit Instructions

The 8051 Microcontroller and Embedded Systems, 2e By Muhammad Ali Mazidi, Janice Gillispie Mazidi, and Rolin D. McKinlay

2006 Pearson Education, Inc. Pearson Prentice Hall Upper Saddle River, NJ 07458

Checking An Input-Bit
JNB ( jump if bit=0 ) and JB ( jump if bit=1 )

The 8051 Microcontroller and Embedded Systems, 2e By Muhammad Ali Mazidi, Janice Gillispie Mazidi, and Rolin D. McKinlay

2006 Pearson Education, Inc. Pearson Prentice Hall Upper Saddle River, NJ 07458

Ex. 4-3
Write a program to
(a) keep monitoring the p1.2 bit until it becomes high (b) when P1.2 becomes high, write value 45H to port 0 (c) send high-to-low (H-to-L) pule to P2.3

The 8051 Microcontroller and Embedded Systems, 2e By Muhammad Ali Mazidi, Janice Gillispie Mazidi, and Rolin D. McKinlay

2006 Pearson Education, Inc. Pearson Prentice Hall Upper Saddle River, NJ 07458

Solution
SETB MOV JNB MOV SETB CLR P1.2 A,#45H P1.2,AGAIN P0,A P2.3 P2.3
2006 Pearson Education, Inc. Pearson Prentice Hall Upper Saddle River, NJ 07458

AGAIN:

The 8051 Microcontroller and Embedded Systems, 2e By Muhammad Ali Mazidi, Janice Gillispie Mazidi, and Rolin D. McKinlay

Ex. 4-4
Assume that bit P2.3 is an input and represents the condition of an oven. If it goes high, it means that the oven is hot. Monitor the bit continuously. Whenever it goes high, send a high-to-low pulse to port P1.5 to turn on a buzzer.

The 8051 Microcontroller and Embedded Systems, 2e By Muhammad Ali Mazidi, Janice Gillispie Mazidi, and Rolin D. McKinlay

2006 Pearson Education, Inc. Pearson Prentice Hall Upper Saddle River, NJ 07458

Solution
HERE: JNB SETB CLR SJMP P2.3, HERE P1.5 P1.5 HERE

The 8051 Microcontroller and Embedded Systems, 2e By Muhammad Ali Mazidi, Janice Gillispie Mazidi, and Rolin D. McKinlay

2006 Pearson Education, Inc. Pearson Prentice Hall Upper Saddle River, NJ 07458

Ex. 4-5
A switch is connected to pin P1.7. Write a program to check the status of SW and perform the following
(a) If SW=0, send letter N to P2. (b) If SW=1, send letter Y to P2

The 8051 Microcontroller and Embedded Systems, 2e By Muhammad Ali Mazidi, Janice Gillispie Mazidi, and Rolin D. McKinlay

2006 Pearson Education, Inc. Pearson Prentice Hall Upper Saddle River, NJ 07458

Solution
AGAIN:

OVER:

SETB JB MOV SJMP MOV SJMP

P1.7 P1.2, OVER P2,#N AGAIN P2,#Y AGAIN


2006 Pearson Education, Inc. Pearson Prentice Hall Upper Saddle River, NJ 07458

The 8051 Microcontroller and Embedded Systems, 2e By Muhammad Ali Mazidi, Janice Gillispie Mazidi, and Rolin D. McKinlay

Reading a single bit into the Carrry flag


Ex. 4-6
AGAIN: SETB P1.7 MOV C, P1.2 JC OVER MOV P2,#N SJMP AGAIN MOV P2,#Y SJMP AGAIN
2006 Pearson Education, Inc. Pearson Prentice Hall Upper Saddle River, NJ 07458

OVER:

The 8051 Microcontroller and Embedded Systems, 2e By Muhammad Ali Mazidi, Janice Gillispie Mazidi, and Rolin D. McKinlay

Ex. 4-7
A switch is connected to pin P1.0 and an LED to pin P2.7. Write a program to get the status of the switch and send it to the LED. Solution : SETB P1.0 AGAIN: MOV C,P1.0 MOV P2.7,C SJMP AGAIN
The 8051 Microcontroller and Embedded Systems, 2e By Muhammad Ali Mazidi, Janice Gillispie Mazidi, and Rolin D. McKinlay 2006 Pearson Education, Inc. Pearson Prentice Hall Upper Saddle River, NJ 07458

Reading input pins VS. port latch


Some instructions will read the status of an internal port latch
Read the status of the input pin Read the internal latch of the output port

This arises when we want to read the external pin AFTER we configure the port as input
The 8051 Microcontroller and Embedded Systems, 2e By Muhammad Ali Mazidi, Janice Gillispie Mazidi, and Rolin D. McKinlay 2006 Pearson Education, Inc. Pearson Prentice Hall Upper Saddle River, NJ 07458

ANL

P1,A

Get the internal latch of the port P1 and bring it into the CPU This data is ANDed with data in A The result is rewritten back to the port latch The port pin data is changed and now has the same value as the port latch
2006 Pearson Education, Inc. Pearson Prentice Hall Upper Saddle River, NJ 07458

The 8051 Microcontroller and Embedded Systems, 2e By Muhammad Ali Mazidi, Janice Gillispie Mazidi, and Rolin D. McKinlay

Table 46

Instructions Reading a Latch (Read-Modify-Write)

The 8051 Microcontroller and Embedded Systems, 2e By Muhammad Ali Mazidi, Janice Gillispie Mazidi, and Rolin D. McKinlay

2006 Pearson Education, Inc. Pearson Prentice Hall Upper Saddle River, NJ 07458

Read-Modify-Write
MOV AGAIN: XLR ACALL SJMP P1,#55H P1,#0FFH DELAY AGAIN

The 8051 Microcontroller and Embedded Systems, 2e By Muhammad Ali Mazidi, Janice Gillispie Mazidi, and Rolin D. McKinlay

2006 Pearson Education, Inc. Pearson Prentice Hall Upper Saddle River, NJ 07458

Вам также может понравиться