Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Chapter 4: Input/Output Programming of 8051 CPU

Download as pdf or txt
Download as pdf or txt
You are on page 1of 7

Chapter 4: Input/Output Programming of 8051 CPU:

Hardware of 8051 family: - These MC’s are packaged in DIP (dual inline package) or QFP (quad flat package)
or LLC (leadless chip carrier). They have 40-pins, dedicated for various functions like: Address, Data, Control.
- The MC have four 8-bit input/output ports (P0, P1,P2 and P3) that uses a total of 32-pins.
- A few M.C versions have 20-pins with reduced I/O ports for less demanding applications

- The 6-pins for RST (reset), Vcc (5v supply), GND, XTAL1, XTAL2 and EA (external access) has to be
connected for the M.C to function. PSEN (program storage enable) and ALE (address latch enable) are only
used with 8031 (with no on-chip ROM)
- RST Pin (pin 9): It is an input and needs high for 2 machine cycles to activate RESET action (normally low).

1
- EA: For M.C’s with on-chip ROM, EAVcc. For M.C’s with no on-chip ROM, EA GNDed (enabled) to
indicate program code is stored externally. This pin along with PSEN & ALE access external memory (ROM)
- Input/Output Ports and their functions:
- Port 0: Can be used as input and output ports, but needs to connect with 10K pull-up resistor (only P0).
Port 0 is also designated as AD0-AD7, allowing it to be used for address and data access for external ROM
Example 1: The following code will continuously
send out to port 0 the alternating value 55H and AAH

BACK: MOVA,#55H
MOV P0, A
ACALL DELAY
MOV A, #0AAH
MOV P0,A  CPL A
ACALL DELAY
SJMPBACK

- In order to make port 0 an input, the port must be programmed by writing logic 1’s to all the bits.
Example 2: Port 0 is configured as an input by writing 1s to it and then P0  P1
MOV A, #0FFH ; A=FF hex
MOV P0, A ; make P0 an input port; by writing logic 1’s to all pins
BACK: MOV A, P0 ; get data through P0
MOV P1, A ; send it out through port 1
SJMP BACK ; keep doing it

- Improvement of the program == replace SJMP with DJNZ and see last part of pg 3
- input, the port must be programmed by writing logic 1’s to all the bits.
Example 3: Write a program to “Toggle bits at port 1 continuously” (similar program as example 1)
Example 3.5: Port 1 is configured first as an input port by writing logic 1’s to it, then data is received from that
port and saved in R7 and R5
MOV A, #0FFH ; A=FF hex
MOV P1, A ; make P1 an input port ;by writing it all logic 1’s
MOV A, P1 ; get data from P1
MOV R7, A ; save it to in register R7
ACALL DELAY ; wait
MOV A, P1 ; another data from P1
MOV R5, A ; save it to in rester R5
Port 2: can be used as input or output and like P1 has internal pull- up resistors. To make Port 2 an input, the port
must be programmed by writing logic 1’s to all the bits normally, P2 is used as simple I/O port.
- For M.C’s with no on-chip ROM, Port-0 and Port-2 is used to send the 16-bit address for external ROM (with the
help of ALE). Port-2 is used to send A8-A15 address of the external ROM. A0-A7 is send through Port-0.
Port 3: can be used as input or output and like P2 has internal pull- up resistors. To make Port 3 an input, it needs
to be reset,
SET although this port is not normally used as input.

2
The additional function of Port 3 is to provide some external important signals:

- Example: Write a program to repeatedly toggle all the bits of P0, P1, and P2 every 1/4 of a second
ORG0
BACK: MOV A, #55H
MOV P0, A
MOV P1, A  Note that MOV P2, P1 is Valid
MOV P2, A
ACALL QSDELAY ; Quarter of a second
MOV A, #0AAH
MOV P0, A
MOV P1, A
MOV P2, A
ACALL QSDELAY
SJMP BACK
QSDELAY:MOV R5, #11
H3:MOV R4, #248
H2:MOV R3, #255
H1:DJNZ R3, H1 ; 4 MC for DS89C4x0
DJNZ R4, H2
DJNZ R5, H3 ; Delay = 11 x 248 x 255 x 4M.Cycle x 90 ns 250430 s = 0.25 sec
RET
END

- Different Ways of accessing Entire 8-bits == improve the program:


Example1: The entire 8 bits of Port 1 are accessed  same as above
BACK:MOV A,#55H
MOV P1,A
ACALL DELAY
MOV A,#0AAH
MOV P1,A
ACALL DELAY
SJMP BACK
Example2: Rewrite the code in a more efficient manner by accessing the port directly without going
through the accumulator
BACK:MOV P1,#55H
ACALL DELAY
MOV P1,#0AAH
ACALL DELAY
SJMP BACK
3
Example3: Another way of doing the same thing
MOV A,#55H
BACK:MOV P1,A
ACALL DELAY
CPL A
SJMP BACK
- Sometime only 1 or 2 bits of the port needs to be accessed (I/O Bit Manipulation program):
BACK:CPL P1.2 ; complement P1.2
ACALL DELAY
SJMP BACK
; Another variation of the above program
AGAIN:SETB P1.2 ; set only P1.2
ACALL DELAY
CLR P1.2 ; clear only P1.2
ACALL DELAY
SJMP AGAIN

Note that P1.2 is the 2ND bit of P1, since the


1st bit is P1.0 and so on. Table 4-3 shows I/O ports.

Example 4: Write a program to create a square wave of 50%


duty cycle on bit 0 of port 1.
Solution: 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, we toggle P1.0 with a time delay in between each
state. Following program can do this:

HERE: SETB P1.0 ; set to high bit 0 of port 1


LCALL DELAY ; call the delay subroutine
CLR P1.0 ; P1.0=0
LCALL DELAY
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

-Instructions that are used for signal-bit operations are as following:


The JNB and JB instructions are widely used single-bit operations. They allow you to monitor a bit and make a
decision depending on whether it’s 0 or 1. These two instructions can be used for any bits of I/O ports 0, 1, 2, and
3. Port 3 is typically not used for any I/O, either single-bit or byte-wise

4
Example 4-3 Write a program to perform the following:
(a) Keep monitoring the P1.2 bit until it becomes high
(b) When P1.2 is high, write value 45H to port 0 and
(c) Send a high-to-low (H-to-L) pulse to P2.3
Solution:
SETB P1.2 ; make P1.2 an input
MOV A, #45H ; A=45H
AGAIN:JNB P1.2, AGAIN ; get out when P1.2=1=high
MOV P0, A ; issue A to P0
SETB P2.3 ; make P2.3 high
CLR P2.3 ; make P2.3 low for H-to-L

In above program instruction “AGAIN:JNB P1.2, AGAIN” (JNB  means jump if no bit) stays in the loop as
long as P1.2 is low. When P1.2 becomes hign, it gets out of the loop and writes 45H to port 0 and creates a H-to-L
pulse by sequence of instructions SETB and CLR

Example 4-4 Assume that P2.3 (port-2 pin-3) is an input pin and represents the condition of an oven. If it goes
high, it means that the oven is hot. Monitor the bit/pin continuously. Whenever it goes high, send a high-to-low
pulse to port P1.5 to turn on a buzzer.
Solution: SETB P2.3; make P2.3 an input
HERE:JNB P2.3,HERE ; keep monitoring for high
SETB P1.5 ; set bit P1.5=1
CLR P1.5 ; make high-to-low
SJMP HERE ; keep repeating
Example 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 (Aascii=41h, aascii=61h 0ascii=30h
Solution:
SETB P1.7 ; make P1.7 an input
AGAIN: JB P1.7, OVER ; jump if P1.7=1
MOV P2, #’N’ ; SW=0, issue ‘N’to P2
SJMP AGAIN ; keep monitoring
OVER: MOV P2, #’Y’ ; SW=1, issue ‘Y’to P2
SJMP AGAIN ; keep monitoring

Example 4-6 A switch is connected to pin P1.7. Write a program without using JB/JNB 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. Use the carry
flag to check the switch status.
Solution:
SETB P1.7 ; make P1.7 an input
AGAIN: MOV C, P1.7 ; read SW status into CF
JC OVER ; jump if SW=’1’  if CF=’1’
MOV P2, #’N’ ; SW=0, issue ‘N’to P2
SJMP AGAIN ; keep monitoring
OVER: MOV P2, #’Y’ ; SW=1, issue ‘Y’to P2
SJMP AGAIN ; keep monitoring
Example 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 ; make P1.0 an input
AGAIN: MOV C, P1.0 ; read SW status into CF  Note that MOV P2.7, P1.0 is invalid
MOV P2.7, C ; send SW status to LED  Note that MOV P2, P1 is Valid
SJMP AGAIN ; keep repeating
5
- In reading a port, some instructions read the status of port pins. Others read the status of an internal port
latch. Therefore, when reading ports there are two possibilities: (1) Read the status of the input pin, (2)
Read the internal latch of the output port
- Confusion between them is a major source of errors in 8051 programming. Especially where external
hardware is concerned
- Some instructions read the contents of an internal port latch instead of reading the status of an external pin

Internal pull=up
resistor of P1, P2, P3

Writing “0” to the port makes M1 (transistor)  ON. This provides GNC connection to both LOAD and
INPUT pin. So attempting to read from P1 will always give ‘0’. But the excessive current (if exist) from both
path may damage M1. So use a Pull-Up resistor in the path of P1 to limit the INPUT current.

6
ANL dest.-byte, source-byteLogical AND for byte  not flags affected

- For example, look at the ANL P1, A instruction and the sequence of actions is executed as follow
1. It reads the internal latch of the port and brings that data into the CPU
2. This data is ANDed with the contents of register A
3. The result is rewritten back to the port latch
4. The port pin data is changed and now has the same value as port latch  so outputted data

- Read-Modify-Write: The instructions read the


port latch normally read a value, perform an
operation then rewrite it back to the port latch:

- The ports in 8051 can be accessed by the


Read-modify-write technique. This feature saves
many lines of code by combining in a single
instruction all three actions
o 1. Reading the port
o 2. Modifying it
o 3. Writing to the port

MOVP1, #55H ; 1st send P1=01010101  read port


AGAIN:XRL P1, #0FFH ; ex-OR P1 with 1111 1111  P1=10101010  modify and write to port
ACALL DELAY ; see previous lecture for Delay subrouting  redo modify and write to port
SJMP AGAIN
7

You might also like