Module4 4
Module4 4
Atria Institute of Technology All ports of 8051 are Bidirectional. They all have:
• D-Latch
Bengaluru-560024 • Output Driver
Department of Electronics and Communication Engineering • Input Buffer
Module-4 I/O Programming Port Programming Module-4 I/O Programming Port Programming
Ex:Port 0 is configured first as an input port by writing 1s to it, and Ex: Port 1 is configured first as an input port by writing 1s to it,
then data is received from that port and sent to P1 then data is received from that port and saved in R7 and R5
MOV A,#0FFH ;A=FF hex MOV A,#0FFH ;A=FF hex
MOV P0,A ;make P0 an i/p port MOV P1,A ;make P1 an input port
;by writing it all 1s ;by writing it all 1s
BACK: MOV A,P0 ;get data from P0 MOV A,P1 ;get data from P1
MOV P1,A ;send it to port 1 MOV R7,A ;save it to in reg R7
SJMP BACK ;keep doing it ACALL DELAY ;wait
MOV A,P1 ;another data from P1
MOV R5,A ;save it to in reg R5
Atria Institute of Technology NATARAJA N, Dept. of ECE, AIT Atria Institute of Technology NATARAJA N, Dept. of ECE, AIT
Module-4 I/O Programming Port Programming Module-4 I/O Programming Port Programming
Example Example
Write the following programs.
Assume that bit P2.3 is an input and represents the condition of an
Create a square wave of 50% duty cycle on bit 0 of port 1. oven. If it goes high, it means that the oven is hot. Monitor the bit
Solution: continuously. Whenever it goes high, send a high-to-low pulse to port
P1.5 to turn on a buzzer.
The 50% duty cycle means that the “on” and “off” state (or the high
and low portion of the pulse) have the same length. Therefore, Solution:
we toggle P1.0 with a time delay in between each state. HERE: JNB P2.3,HERE ;keep monitoring for high
HERE: SETB P1.0 ;set to high bit 0 of port 1 SETB P1.5 ;set bit P1.5=1
LCALL DELAY ;call the delay subroutine CLR P1.5 ;make high-to-low
CLR P1.0 ;P1.0=0
LCALL DELAY SJMP HERE ;keep repeating
SJMP HERE ;keep doing it
Another way to write the above program is:
HERE: CPL P1.0 ;set to high bit 0 of port 1
LCALL DELAY ;call the delay subroutine
SJMP HERE ;keep doing it
8051
P1.0
Atria Institute of Technology NATARAJA N, Dept. of ECE, AIT Atria Institute of Technology NATARAJA N, Dept. of ECE, AIT
Module-4 I/O Programming Port Programming Module-4 I/O Programming Port Programming
Example 4-7
Example
A switch is connected to pin P1.0 and an LED to pin P2.7. Write a
A switch is connected to pin P1.7. Write a program to check the status program to get the status of the switch and send it to the LED
of SW and perform the following:
(a) If SW=0, send letter ‘N’ to P2
(b) If SW=1, send letter ‘Y’ to P2 Solution:
Use the carry flag to check the switch status. SETB P1.0 ;make P1.0 an input
AGAIN: MOV C,P1.0 ;read SW status into CF
Solution: MOV P2.7,C ;send SW status to LED
SETB P1.7 ;make P1.7 an input SJMP AGAIN ;keep repeating
AGAIN: MOV C,P1.7 ;read SW status into CF
JC OVER ;jump if SW=1
MOV P2,#’N’ ;SW=0, issue ‘N’ to P2
The instruction
SJMP AGAIN ;keep monitoring MOV P2.7,P1.0
OVER: MOV P2,#’Y’ ;SW=1, issue ‘Y’ to P2 is wrong , since
SJMP AGAIN ;keep monitoring such an instruction
However does not exist
‘MOV P2,P1’ is a
valid instruction
Atria Institute of Technology NATARAJA N, Dept. of ECE, AIT Atria Institute of Technology NATARAJA N, Dept. of ECE, AIT
Module-4 I/O Programming Port Programming Atria Institute of Technology
Bengaluru-560024
Department of Electronics and Communication Engineering
Example: ALP to Read switch. If S is
closed turn ON the LED, Else turn it OFF