In the 8051 there are a total of four ports for I/O operations. A total of 32 pins are set aside for the four ports PO, P1 P2, and P3, where each port takes 8 pins.
The four ports PO, P1, P2, and P3 each use 8 pins, making them 8-bit ports. All the ports upon RESET 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 the port.
Toggle all the bits:
Get a byte from port 0 and send it to port 1:
Bit Addressability:
Sometimes we need to access only 1 or 2 bits of the port instead of the entire 8 bits. When accessing a port in single-bit manner, we use the syntax “SETB X. Y” where X is the port number 0, 1,2. or 3, and Y is the desired bit number from 0 to 7.
Toggle P0.1:
Single Bit Instruction:
Monitoring an IO pin:
Instruction for Reading an Input Port:
Instructions reading a Latch:
In reading a port, some instructions read the status of port pins while others read the status of an internal port latch.
The instructions that read the port latch normally read a value, perform an operation (and possibly change it), then rewrite it back to the port latch. This is often called “Read-Modify-Write”. A single instruction can be used to read the port, modify its value and write the result back to port.
The four ports PO, P1, P2, and P3 each use 8 pins, making them 8-bit ports. All the ports upon RESET 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 the port.
Toggle all the bits:
ORG 0H
MOV P0, #0H ; make P0 an output port
MOV P1, #0H ; make P1 an output port
MOV P2, #0H ; make P2 an output port
MOV P3, #0H ; make P3 an output port
BACK : MOV P0, #55H
MOV P1, #55H
MOV P2, #55H
MOV P3, #55H
ACALL DELAY
MOV P0, #0AAH
MOV P1, #0AAH
MOV P2, #0AAH
MOV P3, #0AAH
ACALL DELAY
SJMP BACK
; Delay Subroutine
ORG 500H ; put time delay subroutine at program memory location 500H
DELAY : MOV R3, #0FFH ; initialize the counter
LOOP : DJNZ R3, LOOP ; stay her until r5 = 0
RET ; return to caller
END
Get a byte from port 0 and send it to port 1:
ORG 0H
MOV P0, #0FFH ; make P0 an input port
MOV P1, #0H ; make P1 an output port
BACK : MOV A, P0
MOV P1, A
SJMP BACK
END
Bit Addressability:
Sometimes we need to access only 1 or 2 bits of the port instead of the entire 8 bits. When accessing a port in single-bit manner, we use the syntax “SETB X. Y” where X is the port number 0, 1,2. or 3, and Y is the desired bit number from 0 to 7.
Toggle P0.1:
ORG 0H
MOV P0, #0H ; make P0 an output port
AGAIN: SETB P0.1
ACALL DELAY
CLR P0.1
ACALL DELAY
SJMP AGAIN
; Delay Subroutine
ORG 500H ; put time delay subroutine at program memory location 500H
DELAY : MOV R3, #0FFH ; initialize the counter
LOOP : DJNZ R3, LOOP ; stay her until r5 = 0
RET ; return to caller
END
Single Bit Instruction:
Instruction | 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 |
JNB bit, target | Jump to target if bit = 0 |
JBC bit, target | Jump to target if bit = 1, Clear bit |
Monitoring an IO pin:
ORG 0H
SETB P0.1 ; make P0.1 an input pin
AGAIN : JNB P0.1, AGAIN
MOV P0, #55H
END
Instruction for Reading an Input Port:
Mnemonic | Example | Description |
---|---|---|
MOV A, PX | MOV A, P0 | Bring in to A the data at P0 pins |
JNB PX.Y, target | JNB P0.1, LABEL | Jump if P0.1 = 0 |
JB PX.Y, target | JB P0.1, LABEL | Jump if p0.1 = 1 |
MOV C, PX.Y | MOV C, P0.1 | Copy the status of P0.1 to Carry flag |
Instructions reading a Latch:
In reading a port, some instructions read the status of port pins while others read the status of an internal port latch.
Mnemonic | Example |
---|---|
ANL PX, A | ANL P0, A |
ORL PX, A | ORL P0, A |
XRL PX, A | XRL P0, A |
JBC bit, target | JBC P0.1, LABEL |
CPL PX.Y | CPL P0.1 |
INC PX | INC P0 |
DEC PX | DEC P0 |
DJNZ PX.Y, target | DJNZ P0.1. LABEL |
MOV PX.Y, C | MOV P0.1, C |
SETB PX.Y | SETB P0.1 |
CLR PX.Y | CLR P0.1 |
The instructions that read the port latch normally read a value, perform an operation (and possibly change it), then rewrite it back to the port latch. This is often called “Read-Modify-Write”. A single instruction can be used to read the port, modify its value and write the result back to port.
ORG 0H
MOV P0, #55H
AGAIN: XRL P0, #0FFH ; read P0, XOR P0 with FFH and write the result back to P0
ACALL DELAY
SJMP AGAIN
; Delay Subroutine
ORG 500H ; put time delay subroutine at program memory location 500H
DELAY : MOV R3, #0FFH ; initialize the counter
LOOP : DJNZ R3, LOOP ; stay her until r5 = 0
RET ; return to caller
END
Related topics:
8051 Loop | 8051 Delay | 8051 Timer Programming | 8051 Counter | 8051 Serial Port Programming | 8051 Programming Timer Interrupt | 8051 Programming Serial Interrupt | 8051 Programming External Interrupt
List of topics: 8051
No comments:
Post a Comment