I/O PORT PROGRAMMING

on Tuesday, 17 September 2013
PORTS: 
  • 8051 have four input/output ports, they are P0, P1,P2 and P3. 
  • Every Port will have 8pins i.e Px.0 to Px.7.
  • Every port will have internal pull up connections except Port0, so pull up should be connected.

Before we go further in  this section we need to know few basic things in initializing Ports as inputs or outputs.
Rule of Thumb is that to initialize any Port as input then we need to write all one's to it ( i.e FFh) to that port and to initialize as output need to write all zeros (i.e 00h ) to that port.
As we have seen in our previous post that 8051 have 4 I/O ports i.e Port 0 , Port 1 , Port 2, and Port 3.
Let us say we want to make Port 0 as input then 
Mov P0,#0FFh

If port0 as output then
Mov P0,#00h

If port1 as input then Mov p1,#0ffh , if output then Mov P1,#00h

so on.... 

Example1: Create a square wave of 50% duty cycle on bit0 of port2
ORG 00h
Mov P2,#00h          ;its output
Back: Setb p2.0       ;p2.0=1
          Lcall Delay     ;delay
          clr p2.0          ;p2.0=0
          Lcall delay      ; delay
          SJMP Back    ; jump back
END
we can  write the same program using cpl (compliment) instruction.

ORG 00h
Mov P2,#00h          ;its output
CLR P2.0
Back: CPL P2.0       ;if 1 then 0 , if 0 then 1
          Lcall Delay     ;delay
          SJMP Back    ; jump back
END







Example2: Write a program to check p0.1 pin continuously, if its high(i.e1) then make p1.1 pin high if not 0.

ORG 00h
Setb P0.1                       ;i.e input
clr p1.1                          ;i.e output
Acall Dealy                    ; small delay
Back: Mov C, p0.1        ;Taking input from p0.1 into C
          Mov p1.1,C         ;p1.1=C
          Sjmp Back           ; jump back
END

Example3: Assume P1.3 represent the status of the oven,if the input goes high (i.e 1) then it mean the oven is hot, then send high to low pulse to p1.5 which is connected to buzzer.

Back: JNB P1.3, Back    ;check status of the oven, if its zero then stay here
         SETB P1.5           ;high p1.5
         CLR P1.5             ;low p1.5 i.e high to low pulse generated at p1.5
         SJMP Back          ;short jump back


Example4:
Two switches are connected to pin p2.1 and P2.2(one each).A buzzer is connected to P0.0 pin. Check both the switches, if status of both the switches are same then Buzzer should be OFF and if the status of both the switches are different then Buzzer should be ON.

 First we take input port2 into any register, and we will mask the lower bits and move to different registers and we will check condition using Xor instruction. If 0 the buzzer off, if 1 then buzzer ON. 

ORG 00h
Mov P2,#0ffh                     ;input
Mov P0,#00h                     ;output
Back: Mov A,P2                ;Taking input into A
          Mov R0,A                ;Save a copy of input into R0
          ANL A,#01h            ;we are extracting the first bit lsb i.e p2.0(example of masking)
          Mov R1,A                ;Moving result in R1(i.e status of p2.0)
          Mov A,R0                ;we are copying R0  into A(i.e original input)
          ANL A,#02h            ;extracting the 2nd bit of lsb i.e p2.1(example of masking)
          XRL A,R1                ;Xor both the inputs to check they are equal or not
          JNZ BuzzerOn          ;if equal then A=0 , if not equal A=1, jump if A=1 to buzzer on
          CLR p0.0                 ;if A=0 then p0.0=0 i.e buzzer off
          SJMP Back              ;sjmp to back to update again
BuzzerOn:   Setb p0.0         ;if A=1 then p0.0=1 i.e buzzer ON
                   Sjmp back       ; short jump back
End

Note, we can write the same program more effectively ,i wrote in this way because to show  as much as the usage of many instructions.

AUTHOR: YOUSTRON SIC




1 comments:

Anonymous said...

Example 4:
I think we need to rotate the A after the inst: ANL A,#02h. Please correct me if i am wrong.

Post a Comment