Page 1
2002/2/252002/2/25 Microcomputers and MicroprocessorsMicrocomputers and Microprocessors 11
Suranaree UniversitySuranaree University
Of TechnologyOf Technologyมทสมทส
2002 Anant Oonsivilai2002 Anant Oonsivilai
Chapter 4Chapter 4I/O Port ProgrammingI/O Port Programming
Page 2
222002/2/252002/2/25 Microcomputers and MicroprocessorsMicrocomputers and Microprocessors2002 Anant Oonsivilai2002 Anant Oonsivilai
OutlinesOutlines
8051 pin functions8051 pin functions
4 ports of 80514 ports of 8051
Dual role of port 0 for data & addressDual role of port 0 for data & address
Code assembly language to use portsCode assembly language to use ports
Use of port 3 for interrupt signalsUse of port 3 for interrupt signals
Code 8051 instructions for I/O handlingCode 8051 instructions for I/O handling
Code bit-manipulation instructionsCode bit-manipulation instructions
Page 3
332002/2/252002/2/25 Microcomputers and MicroprocessorsMicrocomputers and Microprocessors2002 Anant Oonsivilai2002 Anant Oonsivilai
8051 Pin Diagram8051 Pin Diagram
Page 4
442002/2/252002/2/25 Microcomputers and MicroprocessorsMicrocomputers and Microprocessors2002 Anant Oonsivilai2002 Anant Oonsivilai
Clock GenerationClock Generation
Page 5
552002/2/252002/2/25 Microcomputers and MicroprocessorsMicrocomputers and Microprocessors2002 Anant Oonsivilai2002 Anant Oonsivilai
Clock GenerationClock Generation
Page 6
662002/2/252002/2/25 Microcomputers and MicroprocessorsMicrocomputers and Microprocessors2002 Anant Oonsivilai2002 Anant Oonsivilai
Machine & Clock CyclesMachine & Clock Cycles
Page 7
772002/2/252002/2/25 Microcomputers and MicroprocessorsMicrocomputers and Microprocessors2002 Anant Oonsivilai2002 Anant Oonsivilai
RESET Value of RegistersRESET Value of Registers
Page 8
882002/2/252002/2/25 Microcomputers and MicroprocessorsMicrocomputers and Microprocessors2002 Anant Oonsivilai2002 Anant Oonsivilai
RESET CircuitsRESET Circuits
Page 9
992002/2/252002/2/25 Microcomputers and MicroprocessorsMicrocomputers and Microprocessors2002 Anant Oonsivilai2002 Anant Oonsivilai
RESET CircuitsRESET Circuits
Page 10
10102002/2/252002/2/25 Microcomputers and MicroprocessorsMicrocomputers and Microprocessors2002 Anant Oonsivilai2002 Anant Oonsivilai
Pins for External ROM/RAMPins for External ROM/RAM
EA (external access)EA (external access) VVCCCC: on-chip ROM: on-chip ROM GND: external ROMGND: external ROM
PSEN (program store enable)PSEN (program store enable) to OE (output enable) pin of ROMto OE (output enable) pin of ROM
ALE (address latch enable)ALE (address latch enable) to G (enable) pin of 74LS373to G (enable) pin of 74LS373
See See Chapter 14Chapter 14
Page 11
11112002/2/252002/2/25 Microcomputers and MicroprocessorsMicrocomputers and Microprocessors2002 Anant Oonsivilai2002 Anant Oonsivilai
Port 0: I/O, Open DrainPort 0: I/O, Open Drain
Initially configured as outputInitially configured as output
MOVMOV A,#55H A,#55H
BACK:BACK: MOV MOV P0,A P0,A
ACALL DELAYACALL DELAY
CPLCPL A A
SJMPSJMP BACK BACK
Page 12
12122002/2/252002/2/25 Microcomputers and MicroprocessorsMicrocomputers and Microprocessors2002 Anant Oonsivilai2002 Anant Oonsivilai
Port 0: I/O, Open DrainPort 0: I/O, Open Drain
Page 13
13132002/2/252002/2/25 Microcomputers and MicroprocessorsMicrocomputers and Microprocessors2002 Anant Oonsivilai2002 Anant Oonsivilai
Port 0 as Input: Write all 1sPort 0 as Input: Write all 1s
Dual role of port 0Dual role of port 0 Can be configured as AD0 – AD7Can be configured as AD0 – AD7
MOV A,#0FFH ;A = FF hex
MOV P0,A ;make P0 an input port
;by writing all 1s to it
BACK: MOV A,P0 ;get data from P0
MOV P1,A ;send it to port 1
SJMP BACK ;keep doing it
Page 14
14142002/2/252002/2/25 Microcomputers and MicroprocessorsMicrocomputers and Microprocessors2002 Anant Oonsivilai2002 Anant Oonsivilai
Port 1: I/O, No Pull-up ResistorsPort 1: I/O, No Pull-up Resistors
Initially configured as outputInitially configured as output
MOV A,#55H
BACK: MOV P1,A
ACALL DELAY
CPL A
SJMP BACK
MOV A,#0FFH ;A=FF hex
MOV P1,A ;make P1 an input port
;by writing all 1s to it
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 reg R6
ACALL DELAY ;wait
MOV A,P1 ;get another data from P1
MOV R5,A ;save it in reg R5
Page 15
15152002/2/252002/2/25 Microcomputers and MicroprocessorsMicrocomputers and Microprocessors2002 Anant Oonsivilai2002 Anant Oonsivilai
Port 2: I/O, No Pull-up ResistorsPort 2: I/O, No Pull-up Resistors
Initially configured as outputInitially configured as outputMOV A,#55H
BACK: MOV P2,AACALL DELAYCPL ASJMP BACK
MOV A,#0FFH ;A=FF hexMOV P2,A ;make P2 an input port by
;writing all 1s to itBACK: MOV A,P2 ;get data from P2
MOV P1,A ;send it to Port 1SJMP BACK ;keep doing that
Dual role of port 2: A8-A15Dual role of port 2: A8-A15
Page 16
16162002/2/252002/2/25 Microcomputers and MicroprocessorsMicrocomputers and Microprocessors2002 Anant Oonsivilai2002 Anant Oonsivilai
Port 3: I/O, No Pull-up ResistorsPort 3: I/O, No Pull-up Resistors
Page 17
17172002/2/252002/2/25 Microcomputers and MicroprocessorsMicrocomputers and Microprocessors2002 Anant Oonsivilai2002 Anant Oonsivilai
Different Ways of Accessing 8 bitsDifferent Ways of Accessing 8 bits
BACK:BACK: MOVMOV A,#55HA,#55H
MOVMOV P1,AP1,A
ACALLACALL DELAYDELAY
MOVMOV A,#0AAHA,#0AAH
MOVMOV P1,AP1,A
ACALLACALL DELAYDELAY
SJMPSJMP BACKBACK
BACK:BACK: MOVMOV P1,#55HP1,#55H
ACALLACALL DELAYDELAY
MOVMOV P1,#0AAHP1,#0AAH
ACALLACALL DELAYDELAY
SJMPSJMP BACKBACK
MOV P1,#55H ;P1=01010101AGAIN: XLR P1,#0FFH ;EX-OR
;P1 with 1111 1111 ACALL DELAY SJMP AGAIN
Page 18
18182002/2/252002/2/25 Microcomputers and MicroprocessorsMicrocomputers and Microprocessors2002 Anant Oonsivilai2002 Anant Oonsivilai
Single-bit Addressability of Single-bit Addressability of PortsPorts
BACK:BACK: CPLCPL P1.2P1.2 ;complement P1.2 only;complement P1.2 only
ACALLACALL DELAYDELAY
SJMPSJMP BACKBACK
;another variation of the above program follows;another variation of the above program follows
AGAIN:AGAIN: SETBSETB P1.2P1.2 ;change only P1.2=high;change only P1.2=high
ACALLACALL DELAYDELAY
CLRCLR P1.2P1.2 ;change only P1.2=low;change only P1.2=low
ACALLACALL DELAYDELAY
SJMPSJMP AGAINAGAIN
Page 19
19192002/2/252002/2/25 Microcomputers and MicroprocessorsMicrocomputers and Microprocessors2002 Anant Oonsivilai2002 Anant Oonsivilai
Single-bit Addressability of Single-bit Addressability of PortsPorts
Page 20
20202002/2/252002/2/25 Microcomputers and MicroprocessorsMicrocomputers and Microprocessors2002 Anant Oonsivilai2002 Anant Oonsivilai
Example 4-2Example 4-2