E &TC SUBJECT: MC
EXPT. NO.: 3 DATE:
PROBLEM STATEMENT:
A. Write a program in assembly language to toggle all the LED’s interfaced to port P0 of 8051 /
89C51 continuously with delay of 100 ms. Use looping for delay.
B. Write a program in assembly language for up/down counting of hex numbers up to two
digits. Display the result on LEDs connected to PORT P0.
C. Write a program in assembly language for up/down counting of BCD numbers up to two
digits. Display the result on LEDs connected to PORT P0.
D. Write a program in assembly language to display ASCII values from ‘A’ to ‘Z’ on LEDs
connected to PORT P0.
OBJECTIVE:
a. To understand the Keil IDE.
b. To study delay generation using loop
c. To study LED interfacing
d. To study up/down counter
e. To study hexadecimal, BCD, ASCII number representation
1. THEORY
The four 8-bit I/O ports P0, P1, P2 and P3 each use 8 pins. All the ports upon RESET are configured
as input, 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. To use any of these ports as an input port,
it must be programmed. Port 0 can be used for input or output; each pin must be connected
externally to a 10K ohm pull-up resistor. This is due to the fact that P0 is an open drain, unlike P1,
P2, and P3. Open drain is a term used for MOS chips in the same way that open collector is used for
TTL chips.
Base 16, the hexadecimal system, is used as a convenient representation of binary numbers.
ASCII Table:
The binary representation of the digits 0 to 9 is called BCD (Binary Coded Decimal)
Unpacked BCD
In unpacked BCD, the lower 4 bits of the number represent the BCD number, and the rest of the
bits are 0.
Ex. 00001001 and 00000101 are unpacked BCD for 9 and 5
Packed BCD
In packed BCD, a single byte has two BCD number in it, one in the lower 4 bits, and one in the
upper 4 bits.
Ex. 0101 1001 is packed BCD for 59H
MOV A, #17H
ADD A, #28H
The DA instruction is provided to correct the aforementioned problem associated with BCD
addition. The DA instruction will add 6 to the lower nibble or higher nibble if need.
The DA instruction adjusts the 8-bit value in the accumulator to correspond to binary-coded decimal
(BCD) format. This instruction begins by testing the low-order nibble of the accumulator. If the AC
flag is set or if the low 4 bits of the accumulator exceed a value of 9, the accumulator is incremented
Microcontroller and Application (T.E.E &TC) 2016_2017
by 6. The high-order nibble is then tested. If the carry flag is set or if the high 4 bits of the
accumulator exceed a value of 9, the value 60h is added to the accumulator. The “DA” instruction
works only on A. In other word, while the source can be an operand of any addressing mode, the
destination must be in register A in order for DA to work.
Syntax: DA A
Operation: DA
IF (A3-0 > 9) OR (AC = 1)
A=A+6
IF (A7-4 > 9) OR (C = 1)
A = A + 60h
Example DA A
The DJNZ instruction decrements the byte indicated by the first operand and, if the resulting value
is not zero, branches to the address specified in the second operand.
Operation: DJNZ
PC = PC + 2
Rn = Rn – 1
IF Rn <> 0
PC = PC + offset
Example : DJNZ R6, LABEL
The CJNE instruction compares the first two operands and branches to the specified destination if
their values are not equal. If the values are the same, execution continues with the next instruction.
Operation: CJNE
PC = PC + 3
IF Rn </> immedate
PC = PC + offset
IF Rn < immediate
C=1
ELSE
C=0
Example: CJNE R6, #12H, LABEL
The address of the subroutine is calculated by combining the 5 high-order bits of the incremented
PC (for A15-A11), the 3 high-order bits of the ACALL instruction opcode (for A10-A8), and the
second byte of the instruction (for A7-A0). The subroutine that is called must be located in the same
2KByte block of program memory as the opcode following the ACALL instruction.
The LCALL instruction calls a subroutine located at the specified address. This instruction first
adds 3 to the PC to generate the address of the next instruction. This result is pushed onto the stack
low-byte first and the stack pointer is incremented by 2. The high-order and low-order bytes of the
PC are loaded from the second and third bytes of the instruction respectively. Program execution is
transferred to the subroutine at this address. No flags are affected by this instruction.
Operation: LCALL
PC = PC + 3
SP = SP + 1
(SP) = PC[7-0]
SP = SP + 1
(SP) = PC[15-8]
PC = addr16
The RET instruction pops the high-order and low-order bytes of the PC from the stack (and
decrements the stack pointer by 2). Program execution resumes from the resulting address which is
typically the instruction following an ACALL or LCALL instruction. No flags are affected by this
instruction.
Operation: RET
PC15-8 = (SP)
SP = SP - 1
PC7-0 = (SP)
SP = SP - 1
Microcontroller and Application (T.E.E &TC) 2016_2017
Example: RET
2. Algorithm:
3. Interfacing Diagram:
5. References:
a. Mazidi, 8051 microcontroller & embedded system 3rd Edition ,Pearson
b. Datasheet of 8051 microcontroller
____________________________________________________________
____________________________________________________________
____________________________________________________________
____________________________________________________________