Anda di halaman 1dari 8

CLASS: T.E.

E &TC SUBJECT: MC
EXPT. NO.: 3 DATE:

TITLE: Interfacing LED’s with 89C51

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

S/W PACKAGES USED:


Keil IDE, Windows 7

1. THEORY

1.1 Ports in 8051:

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.

Fig.: Port P0 of 8051

Sometimes we need to access only 1 or 2 bits of the port


Example:
CLR P1.2
Microcontroller and Application (T.E.E &TC) 2016_2017
SETB P1.2

1.2 Hexadecimal number :

Base 16, the hexadecimal system, is used as a convenient representation of binary numbers.

To represent a binary number as its equivalent


hexadecimal number, start from the right and
group 4 bits at a time, replacing each 4-bit
binary number with its hex equivalent

Ex. Represent binary 100111110101 in hex


1001 1111 0101
= 9 F 5

To convert from hex to binary, each hex digit


is replaced with its 4-bit binary equivalent.

Ex. Convert hex 29B to binary


2 9 B
= 0010 1001 1011

1.3 ASCII number :


Character data is represented in a computer by using standardized numeric codes which have been
developed. The most widely accepted code is called the American Standard Code for
Information Interchange (ASCII). The ASCII (pronounced “ask-E”) code assigns binary patterns
for numbers 0 to 9, all the letters of English alphabet, uppercase and lowercase, many control
codes and punctuation marks. The ASCII system uses 7 bits to represent each code. The ASCII
table has 128 characters, with values from 0 through 127. Thus, 7 bits are sufficient to represent a
character in ASCII; however, most computers typically reserve 1 byte, (8 bits), for an ASCII
character. One byte allows a numeric range from 0 through 255 which leaves room for growth in
the size of the character set, or for a sign bit. The ASCII characters and their decimal code values
are shown in Table

ASCII Table:

Microcontroller and Application (T.E.E &TC) 2016_2017


1.4 BCD Number :

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

Adding two BCD numbers must give a BCD


result.

MOV A, #17H
ADD A, #28H

The result above should have been 17 + 28 = 45


(0100 0101).
To correct this problem, the programmer must
add 6 (0110) to the
lower digit: 3F + 06 = 45H.

1.5 DA (Decimal Adjust for Addition) Instruction:

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

1.6 DJNZ Instruction:

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.

Syntax: DJNZ Rn, offset

Operation: DJNZ
PC = PC + 2
Rn = Rn – 1
IF Rn <> 0
PC = PC + offset
Example : DJNZ R6, LABEL

1.7 CJNE Instruction:

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.

Syntax: CJNE Rn, #immediate, offset

Operation: CJNE
PC = PC + 3
IF Rn </> immedate
PC = PC + offset
IF Rn < immediate
C=1
ELSE
C=0
Example: CJNE R6, #12H, LABEL

1.8 ACALL Instruction:


Microcontroller and Application (T.E.E &TC) 2016_2017
The ACALL instruction calls a subroutine located at the specified address. The PC is incremented
twice to obtain the address of the following instruction. The 16-bit PC is then stored on the stack
(low-order byte first) and the stack pointer is incremented twice. No flags are affected.

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.

Syntax: ACALL addr11


Operation PC = PC + 2
SP = SP + 1
(SP) = PC7-0
SP = SP + 1
(SP) = PC15-8
PC10-0 = A10-0
Example: ACALL LABEL

1.9 LCALL 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

Example: LCALL delay

1.10 RET Instruction:

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

1.11 Delay calculation:

For XTAL=11.0592 MHz:


11.0592/12 = 921.6 KHz
Machine cycle (MC) = 1/921.6 KHz = 1.085us [us=microsecond]

2. Algorithm:

2.1 Toggle all the LED’s interfaced to port P0


1. Start
2. Use MOV instruction to put high logic data on port pins.
3. Call delay subroutine of 100ms
4. Use MOV instruction to put low logic data on port pins.
5. Call delay subroutine of 100ms
6. Stop

2.2 Up/down counting of hex numbers up to two digits


1. Start
2. Set the counter register value
3. Use MOV instruction to put hexadecimal number on port pins.
4. Call delay subroutine of 100ms
5. Increment/Decrement the hexadecimal number
6. Decrement counter register value and jump to step 3 if not zero
7. Stop

2.3 Up counting of BCD numbers up to two digits


1. Start
2. Set the counter register value
3. Use MOV instruction to put hexadecimal number on port pins.
4. Call delay subroutine of 100ms
5. Increment using ADD instruction
6. Use DA A instruction to adjust result as BCD number
Microcontroller and Application (T.E.E &TC) 2016_2017
7. Decrement counter register value and jump to step 3 if not zero
8. Stop

2.4 Display ASCII values from ‘A’ to ‘Z’


1. Start
2. Set the counter register value
3. Use MOV instruction to put ASCII number on port pins.
4. Call delay subroutine of 100ms
5. Increment ASCII number
6. Decrement counter register value and jump to step 3 if not zero
7. Stop

3. Interfacing Diagram:

4. Source code ( Attach separate Sheet)

5. References:
a. Mazidi, 8051 microcontroller & embedded system 3rd Edition ,Pearson
b. Datasheet of 8051 microcontroller

6. Result & Conclusion

____________________________________________________________
____________________________________________________________
____________________________________________________________
____________________________________________________________

Microcontroller and Application (T.E.E &TC) 2016_2017


Microcontroller and Application (T.E.E &TC) 2016_2017 4.5

Microcontroller and Application (T.E.E &TC) 2016_2017

Anda mungkin juga menyukai