0% found this document useful (0 votes)
10 views60 pages

Microcontroller LAB Updated

The document is a laboratory manual for the BEE403 Microcontrollers course at AMC Engineering College for the academic year 2025-26. It includes the syllabus, a cycle of experiments covering arithmetic instructions, data transfer, and interfacing with various components, along with detailed procedures for each experiment. The manual is designed for students in the IV semester of the Electrical & Electronics Engineering department, providing guidelines for practical applications using microcontrollers.

Uploaded by

nishp7053
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views60 pages

Microcontroller LAB Updated

The document is a laboratory manual for the BEE403 Microcontrollers course at AMC Engineering College for the academic year 2025-26. It includes the syllabus, a cycle of experiments covering arithmetic instructions, data transfer, and interfacing with various components, along with detailed procedures for each experiment. The manual is designed for students in the IV semester of the Electrical & Electronics Engineering department, providing guidelines for practical applications using microcontrollers.

Uploaded by

nishp7053
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

BEE403 Microcontrollers 2025

AMC ENGINEERING COLLEGE


18th K.M.,Bannerghatta Road Bangalore-560083
(Affiliated to VTU,Belgaum and recognized by AICTE,New Delhi)

DEPARTMENT OF ELECTRICAL & ELECTRONICS


ENGINEERING

IV SEMESTER B.E.
LABORATORY MANUAL

BEE403-MICROCONTROLLERS
Academic year: 2025-26

Staff in charge:

Ms. Dilsha purushothaman


Asst. Professor

NAME OF THE STUDENT :

BRANCH :

UNIVERSITY SEAT NO. :

SEMESTER & SECTION :

Dept. of EEE, AMCEC, Bangalore-83 Page 1


BEE403 Microcontrollers 2025

Dept. of EEE, AMCEC, Bangalore-83 Page 2


BEE403 Microcontrollers 2025
TABLE OF CONTENTS
Sl. Contents
No
1 Syllabus

2
Cycle of Experiments
Arithmetic instructions: Addition, subtraction, multiplication and division. Square using
3 MATLAB/simulink.
Data transfer – Program for block data movement, sorting, exchanging, finding largest
4
element in an array.
5 Up/Down BCD/ Binary Counters
6 Boolean and logical instructions (bit manipulation).
Code conversion programs – BCD to ASCII, ASCII to BCD, ASCII to decimal, Decimal to
7
ASCII, Hexa.
Programs to generate delay, Programs using serial port and on-chip
8
timer/counters.
9 Simulate and test a PWM controlled DC motor using Simscape.

10 Stepper motor interface for direction and speed control.

11 Alphanumerical LCD panel interface.

12 Generate different waveforms: Sine, Square, Triangular, Ramp using DAC interface.

13 Appendix

14 Viva Questions

Dept. of EEE, AMCEC, Bangalore-83 Page 3


BEE403 Microcontrollers 2025

Dept. of EEE, AMCEC, Bangalore-83 Page 4


BEE403 Microcontrollers 2025

MICROCONTROLLERS

SYLLABUS

Sub Code: BEE403 CIE Marks: 50


Hrs/ Week (L: T: P:S) : 3:0:2:0 SEE Marks: 50
Credits : 04 Exam Hours : 03

1. Arithmetic instructions: Addition, subtraction, multiplication and division. Square


using MATLAB/simulink.
2. Data transfer – Program for block data movement, sorting, exchanging, finding largest
element in an array.
3. Up/Down BCD/ Binary Counters
4. Boolean and logical instructions (bit manipulation).
5. Code conversion programs – BCD to ASCII, ASCII to BCD, ASCII to decimal, Decimal
to ASCII, Hexa.
6. Programs to generate delay, Programs using serial port and on-chip timer/counters.
Note: Single chip solution for interfacing 8051 is to be with C Programs for the following
experiments.
7. Simulate and test a PWM controlled DC motor using Simscape.
8. Stepper motor interface for direction and speed control.
9. Alphanumerical LCD panel interface.
10. Generate different waveforms: Sine, Square, Triangular, Ramp using DAC interface.

Dept. of EEE, AMCEC, Bangalore-83 Page 5


BEE403 Microcontrollers 2025

Dept. of EEE, AMCEC, Bangalore-83 Page 6


BEE403 Microcontrollers 2025

CONTENTS

CYCLE - I
1. Arithmetic instructions: Addition, subtraction, multiplication and division.
Square using MATLAB/simulink.
2. Data transfer – Program for block data movement, sorting, exchanging, finding
largest element in an array.
3. Up/Down BCD/ Binary Counters

CYCLE – II

4. Boolean and logical instructions (bit manipulation).


5. Code conversion programs – BCD to ASCII, ASCII to BCD, ASCII to decimal,
Decimal to ASCII, Hexa.
6. Programs to generate delay, Programs using serial port and on-chip
timer/counters.

CYCLE - III
7. Simulate and test a PWM controlled DC motor using Simscape.
8. Stepper motor interface for direction and speed control.
9. Alphanumerical LCD panel interface.
10. Generate different waveforms: Sine, Square, Triangular, Ramp using DAC
interface.

Dept. of EEE, AMCEC, Bangalore-83 Page 7


BEE403 Microcontrollers 2025

Dept. of EEE, AMCEC, Bangalore-83 Page 8


BEE403 Microcontrollers 2025

Date of Experiment: Experiment No: 1

ARITHMETIC INSTRUCTIONS: ADDITION, SUBTRACTION, MULTIPLICATION


AND DIVISION. SQUARE USING MATLAB/ SIMULINK.

a) ARITHMETIC OPERATIONS

Aim
Write an ALP to perform the following:
If x=0 perform addition(a+b);
else if x=1 perform subtraction (a-b);
else if x=2 perform multiplication (a*b);
else if x=3 perform division (a/b),
where a & b are eight-bit numbers.
Algorithm
1. Store the condition x in r1.
2. Load the first and second numbers to A and B registers respectively
3. Compare the contents of r1 and perform the operations add, sub, etc accordingly.
4. Store the result present in A and B registers to the appropriate memory locations
Label Mnemonics Comments
ORG 0000H Start from 0000h location
SJMP 30H Short jump to 30h address
ORG 30H Start from 30h location
MOV R0,#40H Move immediate data to R0
MOVX A,@R0 Move ext R0 to A
MOV R1,A Move A to R1
INC R0 Increment R0
MOVX A,@R0 Move R0 to A
MOV B,A Move A to B
INC R0 Increment R0
MOVX A,@R0 Move ext RAM R0 to A
CJNE R1,#00,CKSUB
ADD A,B Add B to A
MOV B,#00 Move imm data to B
JNC SKIP Jump
MOV B,#01H Move imm data to B
SKIP SJMP LAST Short jump
CKSUB CJNE R1,#01,CKMUL
CLR C Clear carry
SUBB A,B Subtract borrow
MOV B,#00 Move imm data to B
JNC SKIP1
MOV B,#0FFH
SKIP1 SJMP LAST

Dept. of EEE, AMCEC, Bangalore-83 Page 9


BEE403 Microcontrollers 2025

CKMUL CJNE R1,#02,CKDIV


MUL AB Multiply AB
SJMP LAST
CKDIV CJNE R1,#03,OTHER
DIV AB Divide AB
SJMP LAST
OTHER MOV A,#00
MOV B,#00 Move imm data to B
LAST INC R0 Increment R0
MOVX @R0,A Move A to ext RAM
INC R0 Increment R0
MOV A,B Move B to A
MOVX @R0,A Move A to ext RAM
HERE SJMP HERE
END

PROCEDURE:
1. Create the project and save it.
2. Select target device – Atmel 89C51ED2.
3. Create a new file / develop the source code.
4. Save the created file with .asm extension.
5. Add the file to the project.
6. Build the target.
7. Debug to enable the output.

TRIAL I: TRIAL II:


Before execution – ADD
0040H 0041H 0042H 0043H
00 10 40 00

After execution
0040H 0041H 0042H 0043H
00 10 40 50

Before execution – SUBTRACT


0040H 0041H 0042H 0043H
01 10 40 00

After execution
0040H 0041H 0042H 0043H
01 10 40 30

Before execution – MULTIPLY


0040H 0041H 0042H 0043H
02 02 22 00

Dept. of EEE, AMCEC, Bangalore-83 Page 10


BEE403 Microcontrollers 2025

After execution
0040H 0041H 0042H 0043H
02 02 22 44

Before execution – DIVIDE


0040H 0041H 0042H 0043H
03 02 22 00

After execution
0040H 0041H 0042H 0043H
03 02 22 11

b) SQUARE & CUBE OF A NUMBER

Aim
An 8-bit number x is stored in external memory location 9000H.
Write an ALP to compute:
(a) Square of the number x if LSB of data RAM 20H is set.
(b) Cube of the number x if LSB of data RAM 20H is reset.
Store the result at locations 9001H, 9002H, 9003H.

Program Table
Label Mnemonics Comments
ORG 0000H Start from 0000h location
SJMP 30H Short jump to 30h address
ORG 30H Start from 30h location
MOV Move DPTR = 9000H
DPTR,#9000H
MOVX A,@DPTR Move DPTR to A
MOV R0,A Move A to R0
MOV B,A Move A to B
MUL AB Multiply A and B
CLR C Clear carry
JB 01,LAST
PUSH B
MOV B,A Move A to B
MOV A,R0 Move R0 to A
MUL AB
INC DPTR
MOVX @DPTR,A
MOV A,B
MOV R2,A
POP B
MOV A,R0
MUL AB
ADD A,R2

Dept. of EEE, AMCEC, Bangalore-83 Page 11


BEE403 Microcontrollers 2025

LAST INC DPTR


MOVX @DPTR,A
MOV A,B
ADDC A,#00
INC DPTR
MOVX @DPTR,A
HERE SJMP HERE
END

PROCEDURE:
1. Create the project and save it.
2. Select target device – Atmel 89C51ED2.
3. Create a new file / develop the source code.
4. Save the created file with .asm extension.
5. Add the file to the project.
6. Build the target.
7. Debug to enable the output.

RESULT
Trial I
Before execution: D:020H = FFH (square)
After execution: X:9000H
9000H 9001H 9002H 9003H
02 04 00 00

Before execution: D:020H = 00H (cube)


After execution: X:9000H
9000H 9001H
02 08

Trial II

Dept. of EEE, AMCEC, Bangalore-83 Page 12


BEE403 Microcontrollers 2025

Date of Experiment: Experiment No: 2

DATA TRANSFER – PROGRAM FOR BLOCK DATA MOVEMENT, SORTING,


EXCHANGING, FINDING LARGEST ELEMENT IN AN ARRAY.

a) DATA TRANSFER – BLOCK MOVE

Aim

Write an ALP to transfer 10 bytes of data from memory location 8035H to 8062H.
Program algorithm:
1. Initialize register to hold source, destination addresses, data and count values.
2. Obtain data from source location into accumulator and from accumulator into the
destination location.
3. Decrement the count of register and repeat step 2 till the count value reads zero

Program
Label Mnemonics Comments
ORG 0000H Start from 0000h location
SJMP 30H Short jump to 30h address
ORG 30H Start from 30h location
MOV DPH,#80H Load the DPH with 8 bit constant
MOV R0,#35H Move immediate data to register
MOV R1,#62H Move immediate data to register
MOV R3,#05H Move immediate data to register
BACK MOV DPL,R0 Load DPL with R0
MOVX A,@DPTR Move data from [Link] to acc
MOV DPL,R1 Load DPL with R1
MOVX @DPTR,A Move acc data to [Link]
INC R0 Increment register
INC R1 Increment register
DJNZ R3,BACK Decrement register & jump if not zero
HERE SJMP HERE Short jump – relative address
END End

PROCEDURE:
1. Create the project and save it.
2. Select target device – Atmel 89C51ED2.
3. Create a new file / develop the source code.
4. Save the created file with .asm extension.
5. Add the file to the project.
6. Build the target.
7. Debug to enable the output.
8. Enter 5 bytes of data in the source memory starting from address 8035H.

Dept. of EEE, AMCEC, Bangalore-83 Page 13


BEE403 Microcontrollers 2025

9. Run the program to view the data in the destination address 8062H.
10. Exit debug mode.

Result: TRIAL I
Source:
X:8035H 8036H 8037H 8038H 8039H
2 4 6 8 9
Destination:
X:8062H 8063H 8064H 8065H 8066H
2 4 6 8 9

b) DATA TRANSFER – EXCHANGE

Aim

Write an ALP to exchange 5 bytes of data from memory location 32H to 62H.

Program algorithm:
1. Initialize register to hold source, destination addresses, data and count values.
2. Obtain data from source location into accumulator and save in a register.
3. Obtain data from destination location into accumulator.
4. Exchange the data at the memory location.
5. Decrement the count of register and repeat from step 2 to 4 till the count value reads zero.

Program

LabelMnemonics Comments
ORG 0000H Start from 0000h location
SJMP 30H Short jump to 30h address
ORG 30H Start from 30h location
MOV DPH,#32H Load the DPH with 8 bit constant
MOV R0,#32H Move immediate data to register
MOV R1,#62H Move immediate data to register
MOV R3,#05H Move immediate data to register
BACK MOVX A,@R0 Move ext. RAM to A
MOV R2,A Move acc to reg
MOVX A,@R1 Move ext. RAM R1 to A
MOVX @R0,A Move A to [Link]
MOV A,R2 Move R2 to A
MOVX @R1,A Move A to [Link]
INC R0 Increment register
INC R1 Increment register
DJNZ R3,BACK Decrement register & jump if not zero
HERE SJMP HERE Short jump – relative address
END End

Dept. of EEE, AMCEC, Bangalore-83 Page 14


BEE403 Microcontrollers 2025

PROCEDURE:
1. Create the project and save it.
2. Select target device – Atmel 89C51ED2.
3. Create a new file/ develop the source code.
4. Save the created file with .asm extension.
5. Add the file to the project.
6. Build the target.
7. Debug to enable the output.
8. Enter 5 bytes of data A at 32H and data B at 62H.
9. Run the program to view the data A at 62H & data B at 32H.
10. Exit debug mode.

Result: TRIAL I:
Before execution
Source:
X:32H 33H 34H 35H 36H
2 4 6 8 9
Destination:
X:62H 63H 64H 65H 66H
4 7 8 9 E

After execution
Source:
X:32H 33H 34H 35H 36H
4 7 8 9 E
Destination:
X:62H 63H 64H 65H 66H
2 4 6 8 9

c) DATA TRANSFER – BUBBLE SORTING


Aim
Write an ALP to sort 5 bytes of data in descending order stored at memory location 9000H

Program algorithm:
1. Store the elements of the array from the address 9000H
2. Initialize a pass counter with array size -1 count.
3. Load compare counter with pass counter contents and initialize DPTR to point to the start
address
4. Store the current and the next array elements pointed by DPTR in reg B & R2
5. Subtract the next element from the current element.
6. If the carry flag is not set then exchange the 2 no’s in the array.
7. Decrement the compare counter and repeat through step 4 until the counter becomes zero.
8. Decrement the pass counter and repeat through step 3 until the counter becomes zero.
Assembly program
Label Mnemonics Comments
ORG 0000H Start from 0000h location

Dept. of EEE, AMCEC, Bangalore-83 Page 15


BEE403 Microcontrollers 2025

SJMP 30H Short jump to 30h address


ORG 30H Start from 30h location
MOV R0,#05H Mov R0 = 05H
L1: MOV DPTR,#9000H DPTR=9000H
MOV A,R0 Move R0 to A
MOV R1,A Move A to R1
L2: MOVX A,@DPTR Move [Link] to A
MOV B,A Move A to B
INC DPTR Increment DPTR
MOVX A,@DPTR Move DPTR to A
CLR C Clear carry flag
MOV R2,A Move A to R2
SUBB A,B Subtract borrow
JNC for JC noexchg Jump if carry to label
ascending
MOV A,B Move B to A
MOVX @DPTR,A Move A to DPTR
DEC DPL Decrement DPL
MOV A,R2 Move R2 to A
MOVX @DPTR,A Move A to DPTR
INC DPTR Increment DPTR
noexchg: DJNZ R1,L2 Decrement jump if non zero
DJNZ R0,L1 Decrement jump if non zero
HERE: SJMP HERE Short jump – relative address
END End

PROCEDURE:
1. Create the project and save it.
2. Select target device – Atmel 89C51ED2.
3. Create a new file/ develop the source code.
4. Save the created file with .asm extension.
5. Add the file to the project.
6. Build the target.
7. Debug to enable the output.
8. Load 5 bytes of data in random at address 9000H.
9. Run the program to view stored data in descending at 9000H.
10. Exit debug mode.

Result: TRIAL 1:
before execution
X:9000H 9001H 9002H 9003H 9004H
12 34 08 FA 0B
After execution:
X:9000H 9001H 9002H 9003H 9004H
FA 34 12 0B 08

Dept. of EEE, AMCEC, Bangalore-83 Page 16


BEE403 Microcontrollers 2025

d) DATA TRANSFER – LARGEST


Aim
Write an ALP to find the largest number in an array of 6 bytes of data at location 5000H. Store
the largest element at location 5052H.
Program algorithm:
1. Store the elements of the array from the address 5000H
2. Store the length of the array in R3 and set it as counter.
3. DPTR is loaded with starting address of the array.
4. Store the first number of the array in R1 where R1 is assigned to hold the largest number.
5. Increment DPTR.
6. Subtract the number pointed by DPTR from the contents of R1 for comparison if the next
array element is larger than the one in R1.
7. If the element pointed by DPTR is larger then load the larger number into R1.
8. Decrement the counter and repeat steps through 5 until the counter becomes zero.
9. Store the largest number in R1 in address 5052H
Label Mnemonics Comments
ORG 0000H Start from 0000h location
SJMP 30H Short jump to 30h address
ORG 30H Start from 30h location
MOV R3,#06H Mov R3 = 06H
MOV DPTR,#5000H DPTR=5000H
MOVX A,@DPTR Move DPTR to A
MOV R1,A Move A to R1
ABOVE: INC DPTR Increment DPTR
MOVX A,@DPTR Move DPTR to A
CLR C Clear carry
MOV R2,A Move A to R2
SUBB A,R1
JNC for JC SKIP
smallest
MOV A,R2
MOV R1,A
SKIP: DJNZ R3,ABOVE
MOV DPL,#52H
MOV A,R1
MOVX @DPTR,A
HERE: SJMP HERE Short jump – relative address
END End

PROCEDURE:
1. Create the project and save it.
2. Select target device – Atmel 89C51ED2.
3. Create a new file/ develop the source code.

Dept. of EEE, AMCEC, Bangalore-83 Page 17


BEE403 Microcontrollers 2025

4. Save the created file with .asm extension.


5. Add the file to the project.
6. Build the target.
7. Debug to enable the output.
8. Load 5 bytes of data in random at address 5000H.
9. Run the program to view the largest data stored at 5052H.
10. Exit debug mode.

Result:
TRIAL I: before execution
X:5000H 5001H 5002H 5003H 5004H
34 23 FA 08 11
After execution:
5052H
FA

Dept. of EEE, AMCEC, Bangalore-83 Page 18


BEE403 Microcontrollers 2025

Date of Experiment: Experiment No: 3

UP/DOWN BCD/ BINARY COUNTERS


a) HEXA UP/DOWN COUNTER
Aim
Write an ALP to implement an 8 bit up/down hexa counter
Program algorithm:
1. Move 00 to A register.
2. Call the delay subroutine for 1Sec. Move FFH to registers R1, R2 & R3.
3. Decrement the value of registers nested one within the other such that they create a delay
of 1Sec.
4. Increment value of A
5. Stop the counter after it reaches a maximum value of FFH
Label Mnemonics Comments
ORG 0000H Start from 0000h location
SJMP 30H Short jump to 30h address
ORG 30H Start from 30h location
MOV A,#00 Move 00 to A
BACK: ACALL DELAY Absolute subroutine call
INC A (DEC A) Increment A
JNZ BACK Jump if A is not zero
HERE: SJMP HERE Short jump
DELAY: MOV R1,#0FFH Move 0FFH to R1
DEC1: MOV R2,#0FFH Move 0FFH to R2
DEC0: MOV R3,#0FFH Move 0FFH to R3
DEC2: DJNZ R3,DEC2 Decrement R3 and jump if not zero
DJNZ R2,DEC0 Decrement R2 and jump if not zero
DJNZ R1,DEC1 Decrement R1 and jump if not zero
RET Return from subroutine
END End

PROCEDURE:
1. Create the project and save it.
2. Select target device – Atmel 89C51ED2.
3. Create a new file/ develop the source code.
4. Save the created file with .asm extension.
5. Add the file to the project.
6. Build the target.
7. Debug to enable the output.
8. Enable the watch window.
9. Type 'A' in the watch window with an initial value of 00.
10. Run the program & watching the counter value varying from 00H to FFH and back to
00H.
RESULT:
Accumulator A is incremented in binary form 00, 01, 02.........0A , 0B......FF

Dept. of EEE, AMCEC, Bangalore-83 Page 19


BEE403 Microcontrollers 2025

b) BCD UP/DOWN COUNTER


Aim
Write an ALP to implement an 8 bit BCD up/down counter

Program algorithm:

1. Move 00 to A register.
2. Call the delay subroutine for 1Sec. Move FFH to registers R1, R2 & R3.
3. Decrement the value of registers nested one within the other such that they create a delay
of 1Sec.
4. Increment value of A
5. Stop the counter after it reaches a maximum value of FFH and rolls back to 00H.

Label Mnemonics Comments


ORG 0000H Start from 0000h location
SJMP 30H Short jump to 30h address
ORG 30H Start from 30h location
MOV A,#00 Move 00 to A
BACK: ACALL DELAY Absolute subroutine call
INC A Increment A
#01h for down counter ADD A,#99H
DA A
JNZ BACK Jump back if it is not zero
HERE: SJMP HERE Short jump
DELAY: MOV R1,#0FFH Move 0FFH to R1
DEC1: MOV R2,#0FFH Move 0FFH to R2
DEC0: MOV R3,#0FFH Move 0FFH to R3
DEC2: DJNZ R3,DEC2 Decrement R3 and jump if not zero
DJNZ R2,DEC0 Decrement R2 and jump if not zero
DJNZ R1,DEC1 Decrement R1 and jump if not zero
RET Return from subroutine
END End
PROCEDURE:
1. Create the project and save it.
2. Select target device – Atmel 89C51ED2.
3. Create a new file/ develop the source code.
4. Save the created file with .asm extension.
5. Add the file to the project.
6. Build the target.
7. Debug to enable the output.
8. Enable the watch window.
9. Type ‘A’ in the watch window with an initial value of 00.
10. Run the program & watching the counter value varying from 00H to 99 and back to 00H.

RESULT: Accumulator A is incremented in BCD form 00, 01, 02……..99.

Dept. of EEE, AMCEC, Bangalore-83 Page 20


BEE403 Microcontrollers 2025

Date of Experiment: Experiment No: 4

BOOLEAN AND LOGICAL INSTRUCTIONS (BIT MANIPULATION).

a) PROGRAM ILLUSTRATING BIT MANIPULATIONS

AIM

Two eight bit numbers NUM1 & NUM2 are stored in external memory locations 8000h &
80001h respectively.
Write an ALP to compare the 2 nos. Reflect your result as:

if NUM1<NUM2, SET LSB of data RAM 2F (bit address 78H)


If NUM1>NUM2, SET MSB of 2F(7FH).
if NUM1 = NUM2-Clear both LSB & MSB of bit addressable memory location 2Fh

ALGORITHM:

1. Store the elements of the array from the address 4000h


2. Move the first number in r0 and the second number in register A respectively
3. Clear carry flag and subtract the two numbers, if the carry flag is 0(if the nos are equal),
Clear both LSB & MSB of bit addressable memory location 2Fh
4. If the carry bit is set then Set MSB of 2F(7FH), else LSB of data RAM 2F (bit address
78H).

PROCEDURE:

1. Create the project and save it.


2. Select target device – Atmel 89C51ED2.
3. Create a new file/ develop the source code.
4. Save the created file with .asm extension.
5. Add the file to the project.
6. Build the target.
7. Debug to enable the output.

Label Mnemonics Comments


ORG 0000H Start from 0000h location
SJMP 30H Short jump to 30h address
ORG 30H Start from 30h location
MOV DPTR,#8000H
MOVX A,@DPTR
MOV R0,A
INC DPTR
MOVX A,@DPTR
CLR C

Dept. of EEE, AMCEC, Bangalore-83 Page 21


BEE403 Microcontrollers 2025

SUBB A,R0
JZ EQUAL
JNC BIG
SETB 78H
SJMP END1
BIG: SETB 7FH
SJMP END1
EQUAL: CLR 78H
CLR 7FH
END1: SJMP END1
END

RESULT:

1. Before Execution: X:08000h = 45 & X:8001 = 35 After Executuion: D:02FH =01


2. Before Execution: X:08000h = 25 & X:8001 = 35 After Executuion: D:02FH =80
3. Before Execution: X:08000h = 45 & X:8001 = 45 After Executuion: D:02FH =00

b) LOGICAL INSTRUCTIONS

Aim

Assembly program illustrating logical instructions (BYTE LEVEL)


Write an ALP to compute the following.
Three eight bit numbers X, NUM1 & NUM2 are stored in internal data RAM locations 20h, 21h
& 22H
IF X=0; THEN NUM1 (AND) NUM2,
IF X=1; THEN NUM1 (OR) NUM2,
IF X=2; THEN NUM1 (XOR) NUM2,
ELSE RES =00,
RES IS 23H LOCATION

Algorithm:

1. Point to the data RAM register 20h and store the condition x.
2. Point to 21h and 22h and move the first number to A register.
3. Compare the contents of r1 and perform the operations accordingly.
4. The result will be stored in 23H register.

Label Mnemonics Comments


ORG 0000H
SJMP 30H
ORG 30H
MOV A, 20h donot use #, as data ram 20h is to be accessed
MOV R1,A X IN R1
MOV A,21h A -NUM1

Dept. of EEE, AMCEC, Bangalore-83 Page 22


BEE403 Microcontrollers 2025

CJNE R1,#0,CKOR
ANL A, 22H
SJMP END1
CKOR: CJNE R1,#01,CKXOR
ORL A, 22H
SJMP END1
CKXOR: CJNE R1,#02,OTHER
XRL A, 22H
SJMP END1
OTHER: CLR A
END1: MOV 23H,A STORE RESULT
HERE: SJMP HERE
END

RESULT:

1. Before Execution: D:020H =00, 21=0f, 22 = 12 After Execution: D:023H = 02


2. Before Execution: D:020H =01, 21=0f, 22 = 12 After Execution: D:023H = 1F
3. Before Execution: D:020H =02, 21=0f, 22 = 12 After Execution: D:023H = 1D
4. Before Execution: D:020H =03, 21=0f, 22 = 12 After Execution: D:023H = 00

Dept. of EEE, AMCEC, Bangalore-83 Page 23


BEE403 Microcontrollers 2025

Dept. of EEE, AMCEC, Bangalore-83 Page 24


BEE403 Microcontrollers 2025

Date of Experiment: Experiment No: 5

CODE CONVERSION PROGRAMS – BCD TO ASCII, ASCII TO BCD, ASCII TO


DECIMAL, DECIMAL TO ASCII, HEXA
a) BCD to ASCII
Aim
ALP program to convert BCD – ASCII
Algorithm:
1. Move the BCD byte into 2 ASCII characters.
2. Get the lower nibble (BCD digit) and ORL (add) with 30H.
3. Store the converted ASCII value.
4. Get the higher nibble (10’s BCD digit) and ORL (add) with 30H.
5. Store the converted ASCII value in the specified memory locations.
Program Code:
Label Mnemonics Comments
ORG 0000H Start from 0000h location
SJMP 30H Short jump to 30h address
ORG 30H Start from 30h location
MOV R1,#50H Move #50H to R1
MOV A,@R1 Move the value in R1 to A
MOV R2,A Move A to R2
ANL A,#0FH AND #0FH to A
ORL A,#30H ORL #30H to A
INC R1 Increment R1
MOV @R1,A Move A to indirect RAM
MOV A,R2 Move R2 to A
SWAP A Swap nibbles within A
ANL A,#0FH AND #0FH to A
ORL A,#30H ORL #30H to A
INC R1 Increment R1

Dept. of EEE, AMCEC, Bangalore-83 Page 25


BEE403 Microcontrollers 2025

MOV @R1,A Move A to indirect RAM


HERE: SJMP HERE Short jump
END End

Result:
TRIAL I:
D: 050H

050H 051H 052H


44 34 34

b) ASCII TO DECIMAL

Aim

Write an ALP to implement ASCII to decimal conversion.

Algorithm:
1. Get the ASCII code in the range 30-39 as an input.
2. Clear carry bit.
3. Subtract with borrow 30h from the input & display the result.

Label Mnemonics Comments


ORG 00H
ORG 00H
MOVX A,@DPTR
CLR C Clear carry flag
SUBB A,#30H
MOV R7,A
END

RESULT:
Before execution x:9000h – 45
After execution
Project window
R7 – 0x15
A – 0x15

c) DECIMAL TO ASCII
Aim
Write an ALP program to implement decimal to ASCII conversion.
Algorithm:
1. Get the decimal number in the range 0-99 as an input.
2. Separate the higher and lower nibble of the two digit number.

Dept. of EEE, AMCEC, Bangalore-83 Page 26


BEE403 Microcontrollers 2025

3. Add 30h to the lower nibble & store the result.


4. Bring the higher nibble to the ones position, add 30h to it & display the result.

Label Mnemonics Comments


ORG 00H
LJMP 9000H
ORG 9000H
MOV DPTR,#9000H
MOVX A,@DPTR
ANL A,#0FH Mask the upper nibble
ADD A,#30H
MOV R6,A
MOVX A,@DPTR
ANL A,#0F0H Mask the lower nibble
SWAP A Bring it to the ones place
ADD A,#30H
MOV R7,A
END

RESULT:
Before execution x:9000h – 39
After execution
Project window
R6 – 0x39
R7 – 0x33

d) HEX – DECIMAL
Aim
Write an ALP program to convert HEX – DECIMAL
Algorithm:
1. Move the hexa data to be converted to A.
2. Move 10 to B register and divide with A register to ASCII value.
3. Exchange contents of quotient and remainder and transfer the quotient to one memory
location.
4. Increment DPTR and exchange.
5. Once again divide value by 10 and exchange.
6. Store the quotient in 2nd memory location.
7. Exchange number and once again divide by a value of 10.
8. Store the final remainder in the 3rd memory location which in total with all other memory
location gives the complete decimal number.
PROCEDURE:
1. Create the project and save it.
2. Select target device – Atmel 89C51ED2.
3. Create a new file/ develop the source code.
4. Save the created file with .asm extension.
5. Add the file to the project.

Dept. of EEE, AMCEC, Bangalore-83 Page 27


BEE403 Microcontrollers 2025

6. Build the target.


7. Debug to enable the output.
8. Load the hexa decimal value in external RAM location 9000H.
9. See the output in hexadecimal in memory locations 9001H, 9002H & 9003H.
10. Exit debug mode.
Label Mnemonics Comments
ORG 0000H Start from 0000h location
SJMP 30H Short jump to 30h address
ORG 30H Start from 30h location
MOV Move 9000H to DPTR
DPTR,#9000H
MOVX A,@DPTR Move the value in DPTR to A
MOV B,#10 Move #10 to B
DIV AB Divide A by B
INC DPTR Increment DPTR
XCH A,B Exchange B with A
MOVX @DPTR,A Move A to external RAM
XCH A,B Exchange B with A
MOV B,#10 Move #10 to B
DIV AB Divide A by B
INC DPTR Increment DPTR
XCH A,B Exchange B with A
MOVX @DPTR,A Move A to external RAM
XCH A,B Exchange B with A
INC DPTR Increment DPTR
MOVX @DPTR,A Move A to external RAM
HERE: SJMP HERE Short jump
END End

TRIAL I:
X:9000H
9000H 9001H 9002H 9003H
FF 05 05 02

e) DECIMAL – HEX
Aim
Write an ALP program to convert DECIMAL - HEX
Algorithm:
1. Move the decimal no. to be converted from ext mem loc 40H to A.
2. 'AND' A register with 0F0H and obtain the upper MSB of the decimal digit and swap the
LSB and MSB of A to bring the same to unit's place.
3. Move 0AH to B reg and multiply with A reg to convert to Hex value and store the
converted 10's value in R1.
4. Get LSB of the decimal no and add it to the converted 10's value.
5. Point to the next mem location and store the result in hexadecimal.
Dept. of EEE, AMCEC, Bangalore-83 Page 28
BEE403 Microcontrollers 2025

PROCEDURE:
1. Create the project and save it.
2. Select target device – Atmel 89C51ED2.
3. Create a new file/ develop the source code.
4. Save the created file with .asm extension.
5. Add the file to the project.
6. Build the target.
7. Debug to enable the output.
8. Load the decimal value in external RAM location 40H.
9. See the output in hexadecimal in memory locations 41H.
10. Exit debug mode.
Label Mnemonics Comments
ORG 0000H Start from 0000h location
SJMP 30H Short jump to 30h address
ORG 30H Start from 30h location
MOV DPTR,#40H Move 40H to DPTR
MOVX A,@DPTR Move the value in DPTR to A
ANL A,#0F0H Add operation between A & 0F0H
SWAP A Swap
MOV B,#0AH Move 0AH to B
MUL AB Multiply A & B
MOV R1,A Move A to R1
MOVX A,@DPTR Move ext DPTR to A
ANL A,#0FH AND between A & 0FH
ADD A,R1 Add R1 & A
INC DPTR Increment DPTR
MOVX @DPTR,A Move the value A to DPTR
HERE: SJMP HERE Short jump
END End

RESULT:
Trial 1:
Before execution
X: 040H : 45
After execution
X: 041H : 2D

Dept. of EEE, AMCEC, Bangalore-83 Page 29


BEE403 Microcontrollers 2025

Dept. of EEE, AMCEC, Bangalore-83 Page 30


BEE403 Microcontrollers 2025

Date of Experiment: Experiment No: 6

PROGRAMS TO GENERATE DELAY, PROGRAMS USING SERIAL PORT AND ON-


CHIP TIMER/COUNTERS
(a)
Aim
Write an ALP using TIMER0 to create 10 kHz square wave on P1.0.

Algorithm:
1) Load TMOD with 02h i.e timer0 mode2 8-bit auto reload.
2) Load TH0 with -3 to set the baud rate to 9600.
3) Start timer0 by setting TR0 to 1.
4) Monitor the value of TF0 flag with the use of the jump instruction.
5) Clear flag TF0 & compliment the 1st bit of the port1.
6) This makes the last bit of port1 to toggle.

Label Mnemonics Comments


MOV TMOD,#02h
MOV TH0,-3
SETB TR0
LOOP: JNB TF0,LOOP
CLR TF0
CPL P1.0
SJMP LOOP
END

RESULT:

Before execution: P1 = 0Xff

After execution: P1.0 toggles


P1 = 0xFE

(b) ALP FOR SERIAL DATA TRANSMISSION

Aim
Write an ALP for serial data transmission
Algorithm:

1) The TMOD register is loaded with the value 20h, indicating the use of timer1 in mode2 to set
baud rate.

Dept. of EEE, AMCEC, Bangalore-83 Page 31


BEE403 Microcontrollers 2025

2) TH1 is loaded with one of the value -3 to set the baud rate.
3) The SCON register is loaded with the value 50h, indicating serial mode1, where 8-bit data is
framed with start & stop bit.
4) TR1 is set to start timer1.
5) The character byte to be transferred serially is written into the SBUFF register.
6) The TI flag is monitored with the use of the jump instructions, to see if the character has been
transferred completely.
7) Clear TI flag to assure the transmition of the next byte.
8) To transfer the next byte, go to step5.

Label Mnemonics Comments


MOV TMOD,#20h
MOV TH1,#-3
MOV SCON,#50h
SETB TR1
MOV A.#’H’
ACALL TRANS
MOV A,#’I’
ACALL TRANS
AGAIN: SJMP AGAIN
TRANS: MOV SBUF,A
HERE: JNB TI,HERE
CLR TI
RET
END

RESULT:

UART#1

HI

Dept. of EEE, AMCEC, Bangalore-83 Page 32


BEE403 Microcontrollers 2025

Hardware Interfacing

Features of Embedded C:

• C is a simple programming language and so very easy to code.


• Embedded C has most features of C-language with more stress on certain bit manipulative
instructions.
• This feature makes it easy to write program for μC and μP.
• Keil is a versatile software with a cross compiler that will convert the C program to assembly
language and thus the program can be executed on the desired target (say 8051).

Some of the bit manipulative instructions used are

Symbol Operation
& Bitwise AND
| Bitwise OR
~ Bitwise NOT
>> Shift Right
<< Shift Left
^ P0.0

Dept. of EEE, AMCEC, Bangalore-83 Page 33


BEE403 Microcontrollers 2025

Dept. of EEE, AMCEC, Bangalore-83 Page 34


BEE403 Microcontrollers 2025

Date of Experiment: Experiment No: 7

SIMULATE AND TEST A PWM CONTROLLED DC MOTOR USING SIMSCAPE

Aim
Write a C program to control the speed of rotation of DC Motor.
Algorithm:

1. Configure P0,P1 as output port and P3 as input port.


2. Let initially the motor rotate with half speed count 7fh.
3. If “INR” button is pressed reduce the count because the speed is inversely proportional to count.
4. If “DEC” button is pressed increase the count.

Fig 3. DC motor interface to 8051 Microcontroller

Program:

#include <REG51xD2.H>
sbit inr= P3^2; /*speed increment switch*/
sbit dcr= P3^3 /*speed decrement switch*/
main()
{
unsigned char i=0x80;
P0 = 0x7f; /*Run the motor at half speed.*/
while (1)
{
if (!inr)
{
while (!inr);
if(i>10)
i=i-10; /*increase the DC motor speed*/
}
if(!dcr)

Dept. of EEE, AMCEC, Bangalore-83 Page 35


BEE403 Microcontrollers 2025

{
while(!dcr);
if(i<0xf0)
i=i+10; /*decrease the DC motor speed*/
}
P0=i;
}
}

Dept. of EEE, AMCEC, Bangalore-83 Page 36


BEE403 Microcontrollers 2025

Date of Experiment: Experiment No: 8

STEPPER MOTOR INTERFACE FOR DIRECTION AND SPEED CONTROL

Aim
Write a C program to control the speed of rotation and direction of rotation of a Stepper Motor.

Theory:
A stepper motor, unlike a DC motor, rotates in steps. It has 4 coils which form the stator and a
central rotor. Rotation depends on excitation of stator coils.

Excitation Sequence:
Step Coil A Coil B Coil C Coil D
1 0 0 0 1
2 1 0 0 0
3 0 1 0 0
4 0 0 0 1

To get 360° revolution, 200 steps are required.


360°
Step angle = 200 = 1.8°(difference between 2 teeth).
Algorithm:
1. Configure P0 as output.
2. Apply the initial excitation of 11 to motor coils through P0.
3. For clockwise motion - Rotate right once the excitation and repeat step 2.
4. For anticlockwise motion - Rotate left once the excitation and repeat step 2.

Dept. of EEE, AMCEC, Bangalore-83 Page 37


BEE403 Microcontrollers 2025

C Program Code:

#include <REG51xD2.H>

/* Delay Routine */
void delay (unsigned int x)
{
for(;x>0;x--);
return;
}

main ( )
{
unsigned char Val, i;
P0=0x00;
Val = 0x11;
for (i=0;i<4;i++)
{
P0 = Val;
Val = Val<<1; /* Val= Val>>1; for clockwise direction */
delay (500);
}
}

Dept. of EEE, AMCEC, Bangalore-83 Page 38


BEE403 Microcontrollers 2025

Date of Experiment: Experiment No: 9

ALPHANUMERICAL LCD PANEL INTERFACE

Aim:

To interface an alphanumeric LCD panel and to write a C Program to send a message to the
LCD

LCD details:

The LCD used is an alphanumeric 16*1 LCD. P0.0-P0.7 are 8 bit data lines. P2.4, P2.5 & P2.6
are RS, R/W and Enable pins Character construction is 5*7dots.

LCD Pin Diagram

void lcdcmd (unsigned char value)


{
P0=value;
rs=0; //select instruction register
rw=0; //write
en=1;
tdelay(1);
en=0;
}
void lcddata (void)
{

Dept. of EEE, AMCEC, Bangalore-83 Page 39


BEE403 Microcontrollers 2025

P0=temp;
rs=1; //select data register
rw=0; //write
en=1;
tdelay(1);
en=0;
}

void tdelay(unsigned int item)


{
unsigned int i,j;
for (i=0;i<item;i++)
for(j=0;j<900;j++);
}
LCD Interfacing with 8051

Result:

Dept. of EEE, AMCEC, Bangalore-83 Page 40


BEE403 Microcontrollers 2025

Date of Experiment: Experiment No: 10

GENERATE DIFFERENT WAVEFORMS: SINE, SQUARE, TRIANGULAR, RAMP


USING DAC INTERFACE
(a) GENERATING SQUARE WAVE
Aim
Write a C program to generate square wave

Fig 4: 8051 Microcontroller interfaced to Dual DAC 0808


Algorithm:

1. Let initial, amplitude of the square wave be 2.5v (7F) and frequency count 100.
2. Output the values 00h (0ff) and 7fh (on) Values through P0.
3. If amplitude key is pressed then increase the voltage in steps of 0.15v (8).

Dept. of EEE, AMCEC, Bangalore-83 Page 41


BEE403 Microcontrollers 2025

4. If the frequency key is pressed increment the count in steps of 50. If the count exceeds 1000
reset it back to 100.
5. Every time amplitude and frequency changes output the value through P0 and note the
waveform on CRO.

Program:

#include <REG51xD2.H>
void delay(unsigned int x) /* delay routine */
{
for (;x>0;x--);
}
main()
{
unsigned char on = 0xFF,Off=0x00;
P0=on;
delay(100);
P0 = Off; /* clear port */
delay(100);
}

Output Waveform at Port-0 (Logic Analyzer Window):

(b) GENERATING TRIANGULAR WAVE.


Aim
Write a C program to generate Triangular wave.
Algorithm:

1. Output the initial value 00 through P0.


2. Increment it in steps of 1 until a count value of FFh (5V) is reached. Every time repeat step 1.

Dept. of EEE, AMCEC, Bangalore-83 Page 42


BEE403 Microcontrollers 2025

3. Decrement it in steps of 1 until a zero value is reached and repeat step 1.

Program:

#include <REG51xD2.H>
main()
{
unsigned char i=0;
while(1)
{
for(i=0;i<0xff;i++)
{ /* Generate ON pulse */
P1 = i;
}
for(i=0xfe;i>0x00;i--) /* Generate OFF pulse */
{
P1 = i;}
}
}

Output Waveform at Port-1 (Logic Analyzer Window):

(c) GENERATING RAMP WAVE.


Aim
Write a C program to generate Ramp wave.
Algorithm:

1. Output the initial value 00 through P0.


2. Increment it in steps of 1 until a count value of FFh (5V) is reached. Every time repeat step
1.
3. Repeat step 1 & 2 continuously.

Dept. of EEE, AMCEC, Bangalore-83 Page 43


BEE403 Microcontrollers 2025

Program:

#include <REG51xD2.H>
main ()
{
Unsigned char i=0;
P0 = 0x00; /* P0 as Output port */
while (1)
{
for (i=0;i<0xff;i++) /* Generate ON pulse */
{
P0 = i;
}
}

Output Waveform at Port-1 (Logic Analyzer Window):

(d) GENERATING SINE WAVE.


Aim
Write a C program to generate Sine wave.

Algorithm:

1. Compute different step values (θ = 20o,15o…) of sine using the equation

Dept. of EEE, AMCEC, Bangalore-83 Page 44


BEE403 Microcontrollers 2025

V= 2.5V +2.5Vsinθ. Output the values thro P0.


2. More the steps smoother will be sine wave.
3. E.g.: θ = 0o, V= 2.5V +2.5Vsinθ = 2.5V. The value sent to DAC is 25.6 x5V= 128.

Program:

#include <REG51xD2.H>
main()
{
static int a[13]={128,192,238,255,238,192,128,64,17,0,17,64,128};
unsigned char i=0;
P0 = 0x00; /* P0 as Output port */
while (1)
{
for(i=0;i<13;i++) /* Output different values */
{
P0 = a[i];
}
}
}

Output Waveform at Port-0 (Logic Analyzer Window):

Dept. of EEE, AMCEC, Bangalore-83 Page 45


BEE403 Microcontrollers 2025

Dept. of EEE, AMCEC, Bangalore-83 Page 46


BEE403 Microcontrollers 2025

PE LAB VIVA QUESTIONS

1. What is a microcontroller?
2. Why is 8051 called an 8-bit microcontroller?
3. What are the main components inside an 8051 microcontroller?
4. What is the clock frequency of a standard 8051?
5. What is a machine cycle?
6. How many machine cycles are required to execute one instruction?
7. How many I/O ports are available in 8051?
8. What is the size of internal RAM in 8051?
9. What is the difference between a microprocessor and a microcontroller?
10. What are the applications of 8051 microcontroller?
11. What is the function of the Accumulator register?
12. What is the purpose of the B register?
13. What is PSW?
14. List the flags present in PSW.
15. How many register banks are available in 8051?
16. How are register banks selected?
17. What is the stack in 8051?
18. Where is the stack located?
19. What is the default value of the stack pointer?
20. What is the function of the stack pointer?
21. What are the different addressing modes of 8051?
22. What is immediate addressing mode?
23. What is direct addressing mode?
24. What is register indirect addressing mode?
25. What is indexed addressing mode?
26. Which addressing mode is used for accessing lookup tables?
27. What is assembly language programming?
28. What is an assembler?
29. What is the difference between MOV and MOVC instructions?
30. What is the function of DJNZ instruction?
31. What is the purpose of CJNE instruction?
32. What is the difference between SJMP and LJMP?
33. What is the use of NOP instruction?
34. Which instruction is used for subroutine call?
35. Which instruction is used to return from a subroutine?
36. Which instructions are used for addition and subtraction in 8051?
37. Which instruction is used for multiplication?
38. Which instruction is used for division?
39. Which flags are affected by arithmetic instructions?
40. What happens if division by zero occurs in 8051?
41. What are logical instructions in 8051?
42. What is bit manipulation?
43. How many timers are available in 8051?
44. What is the difference between timer and counter?
Dept. of EEE, AMCEC, Bangalore-83 Page 47
BEE403 Microcontrollers 2025
45. Which register is used to select timer mode?
46. What are the different timer modes in 8051?
47. What is the use of TH0 and TL0 registers?
48. How is delay generated using timers?
49. What is serial communication?
50. Which register controls serial communication in 8051?
51. What is baud rate?
52. Which timer is used for baud rate generation?
53. What is the function of SBUF register?
54. What is the difference between synchronous and asynchronous communication?
55. What is an interrupt?
56. How many interrupts are available in 8051?
57. What is interrupt priority?
58. Which register is used to enable interrupts?
59. What is an Interrupt Service Routine (ISR)?
60. What is interrupt nesting?
61. Why is Port 0 called an open-drain port?
62. What is the function of ALE pin?
63. What is LCD interfacing?
64. What is ADC?
65. What is DAC?
66. What is PWM?
67. Why is PWM used for DC motor control?
68. What is the difference between DC motor and stepper motor?
69. What is embedded C?
70. Which compiler is commonly used for 8051 C programming?
71. How is delay generated in 8051 C?
72. What are Special Function Registers (SFRs)?
73. What is the advantage of C programming over assembly language?

Dept. of EEE, AMCEC, Bangalore-83 Page 48


BEE403 Microcontrollers 2025

APPENDIX
The 8051 Instruction Set
The process of writing program for the microcontroller mainly consists of giving instructions
(commands) in the specific order in which they should be executed in order to carry out a specific task. As
electronics cannot “understand” what for example an instruction “if the push button is pressed- turn the
light on” means, then a certain number of simpler and precisely defined orders that decoder can recognize
must be used. All commands are known as INSTRUCTION SET. All microcontrollers compatible with
the 8051 have in total of 255 instructions, i.e. 255 different words available for program writing.
At first sight, it is imposing number of odd signs that must be known by heart. However, it is not
as complicated as it looks like. Many instructions are considered to be “different”, even though they
perform the same operation, so there are only 111 truly different commands. For example: ADD A, R0,
ADD A, R1... ADD A, R7 are instructions that perform the same operation (addition of the accumulator
and register). Since there are 8 such registers, each instruction is counted separately. Taking into account
that all instructions perform only 53 operations (addition, subtraction, copy etc.) and most of them are
rarely used in practice, there are actually 20-30 abbreviations to be learned, which is acceptable.

Types of instructions
Depending on operation they perform, all instructions are divided in several groups:
• Arithmetic Instructions
• Branch Instructions
• Data Transfer Instructions
• Logic Instructions
• Bit-oriented Instructions
The first part of each instruction, called MNEMONIC refers to the operation an instruction
performs (copy, addition, logic operation etc.). Mnemonics are abbreviations of the name of operation
being executed. For example:
INC R1 - Increment register R1 (increment register R1)
LJMP LAB5 - Long Jump LAB5 (long jump to the address marked as LAB5)
JNZ LOOP - Jump if Not Zero LOOP (if the number in the accumulator is not 0,
jump to the address marked as LOOP)
The other part of instruction, called OPERAND is separated from mnemonic by at least one white
space and defines data being processed by instructions. Some of the instructions have no operand, while
some of them have one, two or three. If there is more than one operand in an instruction, they are
separated by a comma. For example:
RET - return from a subroutine
JZ TEMP - if the number in the accumulator is not 0, jump to the address marked
as TEMP
ADD A,R3 - add R3 and accumulator
CJNE A,#20,LOOP - compare accumulator with 20. If they are not equal, jump to the
address marked as LOOP

Dept. of EEE, AMCEC, Bangalore-83 Page 49


BEE403 Microcontrollers 2025

Arithmetic instructions

Arithmetic instructions perform several basic operations such as addition, subtraction, division,
multiplication etc. After execution, the result is stored in the first operand. For example: ADD A,R1 - The
result of addition (A+R1) will be stored in the accumulator.
Arithmetic Instructions
Mnemonic Description Byte Cycle
ADD A,Rn Adds the register to the accumulator 1 1
ADD A,direct Adds the direct byte to the accumulator 2 2
ADD A,@Ri Adds the indirect RAM to the accumulator 1 2
ADD A,#data Adds the immediate data to the accumulator 2 2
Adds the register to the accumulator with a carry
ADDC A,Rn 1 1
flag
Adds the direct byte to the accumulator with a
ADDC A,direct 2 2
carry flag
Adds the indirect RAM to the accumulator with a
ADDC A,@Ri 1 2
carry flag
Adds the immediate data to the accumulator with a
ADDC A,#data 2 2
carry flag
Subtracts the register from the accumulator with a
SUBB A,Rn 1 1
borrow
Subtracts the direct byte from the accumulator
SUBB A,direct 2 2
with a borrow
Subtracts the indirect RAM from the accumulator
SUBB A,@Ri 1 2
with a borrow
Subtracts the immediate data from the
SUBB A,#data 2 2
accumulator with a borrow
INC A Increments the accumulator by 1 1 1
INC Rn Increments the register by 1 1 2
INC Rx Increments the direct byte by 1 2 3
INC @Ri Increments the indirect RAM by 1 1 3
DEC A Decrements the accumulator by 1 1 1
DEC Rn Decrements the register by 1 1 1
DEC Rx Decrements the direct byte by 1 1 2
DEC @Ri Decrements the indirect RAM by 1 2 3
INC DPTR Increments the Data Pointer by 1 1 3
MUL AB Multiplies A and B 1 5
DIV AB Divides A by B 1 5
Decimal adjustment of the accumulator according
DA A 1 1
to BCD code

Dept. of EEE, AMCEC, Bangalore-83 Page 50


BEE403 Microcontrollers 2025

Branch Instructions
There are two kinds of branch instructions:

Unconditional jump instructions: upon their execution a jump to a new location from where the program
continues execution is executed.
Conditional jump instructions: a jump to a new program location is executed only if a specified condition
is met. Otherwise, the program normally proceeds with the next instruction.

Branch Instructions
Mnemonic Description Byte Cycle
ACALL addr11 Absolute subroutine call 2 6
LCALL addr16 Long subroutine call 3 6
RET Returns from subroutine 1 4
RETI Returns from interrupt subroutine 1 4
AJMP addr11 Absolute jump 2 3
LJMP addr16 Long jump 3 4
Short jump (from –128 to +127 locations relative to
SJMP rel 2 3
the following instruction)
JC rel Jump if carry flag is set. Short jump. 2 3
JNC rel Jump if carry flag is not set. Short jump. 2 3
JB bit,rel Jump if direct bit is set. Short jump. 3 4
JBC bit,rel Jump if direct bit is set and clears bit. Short jump. 3 4
JMP @A+DPTR Jump indirect relative to the DPTR 1 2
JZ rel Jump if the accumulator is zero. Short jump. 2 3
JNZ rel Jump if the accumulator is not zero. Short jump. 2 3
Compares direct byte to the accumulator and jumps
CJNE A,direct,rel 3 4
if not equal. Short jump.
Compares immediate data to the accumulator and
CJNE A,#data,rel 3 4
jumps if not equal. Short jump.
Compares immediate data to the register and jumps
CJNE Rn,#data,rel 3 4
if not equal. Short jump.
Compares immediate data to indirect register and
CJNE @Ri,#data,rel 3 4
jumps if not equal. Short jump.
DJNZ Rn,rel Decrements register and jump if not 0. Short jump. 2 3
Decrements direct byte and jump if not 0. Short
DJNZ Rx,rel 3 4
jump.
NOP No operation 1 1

Dept. of EEE, AMCEC, Bangalore-83 Page 51


BEE403 Microcontrollers 2025
Data Transfer Instructions
Data transfer instructions move the content of one register to another. The register the content of which is moved
remains unchanged. If they have the suffix “X” (MOVX), the data is exchanged with external memory.

Data Transfer Instructions


Mnemonic Description Byte Cycle
MOV A,Rn Moves the register to the accumulator 1 1
MOV A,direct Moves the direct byte to the accumulator 2 2
MOV A,@Ri Moves the indirect RAM to the accumulator 1 2
MOV A,#data Moves the immediate data to the accumulator 2 2
MOV Rn,A Moves the accumulator to the register 1 2
MOV Rn,direct Moves the direct byte to the register 2 4
MOV Rn,#data Moves the immediate data to the register 2 2
MOV direct,A Moves the accumulator to the direct byte 2 3
MOV direct,Rn Moves the register to the direct byte 2 3
MOV direct,direct Moves the direct byte to the direct byte 3 4
MOV direct,@Ri Moves the indirect RAM to the direct byte 2 4
MOV direct,#data Moves the immediate data to the direct byte 3 3
MOV @Ri,A Moves the accumulator to the indirect RAM 1 3
MOV @Ri,direct Moves the direct byte to the indirect RAM 2 5
MOV @Ri,#data Moves the immediate data to the indirect RAM 2 3
MOV DPTR,#data Moves a 16-bit data to the data pointer 3 3
Moves the code byte relative to the DPTR to the
MOVC A,@A+DPTR 1 3
accumulator (address=A+DPTR)
Moves the code byte relative to the PC to the
MOVC A,@A+PC 1 3
accumulator (address=A+PC)
Moves the external RAM (8-bit address) to the
MOVX A,@Ri 1 3-10
accumulator
Moves the external RAM (16-bit address) to the
MOVX A,@DPTR 1 3-10
accumulator
Moves the accumulator to the external RAM (8-bit
MOVX @Ri,A 1 4-11
address)
Moves the accumulator to the external RAM (16-
MOVX @DPTR,A 1 4-11
bit address)
PUSH direct Pushes the direct byte onto the stack 2 4
POP direct Pops the direct byte from the stack/td> 2 3
XCH A,Rn Exchanges the register with the accumulator 1 2
XCH A,direct Exchanges the direct byte with the accumulator 2 3
XCH A,@Ri Exchanges the indirect RAM with the accumulator 1 3
Exchanges the low-order nibble indirect RAM with
XCHD A,@Ri 1 3
the accumulator

Dept. of EEE, AMCEC, Bangalore-83 Page 52


BEE403 Microcontrollers 2025
Logic Instructions

Logic instructions perform logic operations upon corresponding bits of two registers. After execution, the result
is stored in the first operand.

Logic Instructions
Mnemonic Description Byte Cycle
ANL A,Rn AND register to accumulator 1 1
ANL A,direct AND direct byte to accumulator 2 2
ANL A,@Ri AND indirect RAM to accumulator 1 2
ANL A,#data AND immediate data to accumulator 2 2
ANL direct,A AND accumulator to direct byte 2 3
ANL direct,#data AND immediate data to direct register 3 4
ORL A,Rn OR register to accumulator 1 1
ORL A,direct OR direct byte to accumulator 2 2
ORL A,@Ri OR indirect RAM to accumulator 1 2
ORL direct,A OR accumulator to direct byte 2 3
ORL direct,#data OR immediate data to direct byte 3 4
XRL A,Rn Exclusive OR register to accumulator 1 1
XRL A,direct Exclusive OR direct byte to accumulator 2 2
XRL A,@Ri Exclusive OR indirect RAM to accumulator 1 2
XRL A,#data Exclusive OR immediate data to accumulator 2 2
XRL direct,A Exclusive OR accumulator to direct byte 2 3
XORL direct,#data Exclusive OR immediate data to direct byte 3 4
CLR A Clears the accumulator 1 1
CPL A Complements the accumulator (1=0, 0=1) 1 1
SWAP A Swaps nibbles within the accumulator 1 1
RL A Rotates bits in the accumulator left 1 1
RLC A Rotates bits in the accumulator left through carry 1 1
RR A Rotates bits in the accumulator right 1 1
RRC A Rotates bits in the accumulator right through carry 1 1

Bit-oriented Instructions
Similar to logic instructions, bit-oriented instructions perform logic operations. The difference is that these
are performed upon single bits.

Bit-oriented Instructions
Mnemonic Description Byte Cycle
CLR C Clears the carry flag 1 1

Dept. of EEE, AMCEC, Bangalore-83 Page 53


BEE403 Microcontrollers 2025
CLR bit Clears the direct bit 2 3
SETB C Sets the carry flag 1 1
SETB bit Sets the direct bit 2 3
CPL C Complements the carry flag 1 1
CPL bit Complements the direct bit 2 3
ANL C,bit AND direct bit to the carry flag 2 2
AND complements of direct bit to the carry
ANL C,/bit 2 2
flag
ORL C,bit OR direct bit to the carry flag 2 2
ORL C,/bit OR complements of direct bit to the carry flag 2 2
MOV C,bit Moves the direct bit to the carry flag 2 2
MOV bit,C Moves the carry flag to the direct bit 2 3

Here is a list of the operands and their meanings:

A- accumulator;
Rn - is one of working registers (R0-R7) in the currently active RAM memory bank;
Direct - is any 8-bit address register of RAM. It can be any general-purpose register or a SFR (I/O
port, control register etc.);
@Ri - is indirect internal or external RAM location addressed by register R0 or R1;
#data - is an 8-bit constant included in instruction (0-255);
#data16 - is a 16-bit constant included as bytes 2 and 3 in instruction (0-65535);
addr16 - is a 16-bit address. May be anywhere within 64KB of program memory;
addr11 - is an 11-bit address. May be within the same 2KB page of program memory as the first byte of the
following instruction;
rel - is the address of a close memory location (from -128 to +127 relative to the first byte of the
following instruction). On the basis of it, assembler computes the value to add or subtract from the number
currently stored in the program counter;
bit - is any bit-addressable I/O pin, control or status bit; and
C- is carry flag of the status register (register PSW).

Dept. of EEE, AMCEC, Bangalore-83 Page 54


BEE403 Microcontrollers 2025

Addressing Modes

Instructions tell the processor which operation to carry out. For example, MOV A, #5EH tells the
processor to move the data 5EH to the accumulator.
Instructions result in the processor performing some operation on some data (the data being #5EH in the
example above). The instruction above is an example of what's known as immediate addressing. The
reason for this is because the data immediately follows the instruction in code memory.
However, it is not always adequate to store the data in code memory itself. If we needed to read the
information from a keyboard, for example, we would need to use something other than immediate
addressing.
There are eight different addressing modes in the 8051.

Opcode and Operand


The first byte of an instruction is known as the opcode (operation code) because this is the byte that is
decoded by the processor - from this code the processor can work out what operation it must perform.
For a one-byte instruction there is only the opcode.
For a two-byte instruction the second byte is the operand. The operand can be data or an 8-bit address.
For a three-byte instruction the second and third bytes make up the operand. A two-byte operand is
usually a 16-bit address, as we shall see when we look at long addressing.

The eight addressing modes are:


• Immediate
• Register
• Direct
• Indirect
• Relative
• Absolute
• Long
• Indexed

Immediate Addressing
If the operand is a constant then it can be stored in memory immediately after the opcode. Remember,
values in code memory (ROM) do not change once the system has been programmed and is in use in the
everyday world. Therefore, immediate addressing is only of use when the data to be read is a constant.
For example, if your program needed to perform some calculations based on the number of weeks in the
year, you could use immediate addressing to load the number 52 (34H) into a register and then perform
arithmetic operations upon this data.
MOV R0, #34
The above instruction is an example of immediate addressing. It moves the data 34H into R0. The
assembler must be able to tell the difference between an address and a piece of data. The has symbol (#)
is used for this purpose (whenever the assembler sees # before a number it knows this is immediate
addressing).
This is a two-byte instruction.
Register Addressing
Often we need to move data from a register into the accumulator so that we can perform arithmetic operations
upon it. For example, we may wish to move the contents of R5 into the accumulator.
Dept. of EEE, AMCEC, Bangalore-83 Page 55
BEE403 Microcontrollers 2025
MOV A, R5
This is an example of register addressing. It moves data from R5 (in the currently selected register bank) into
the accumulator.
ADD A, R6
The above is another example of register addressing. It adds the contents of R6 to the accumulator, storing the
result in the accumulator. Note that in both examples the destination comes first. This is true of all instructions.

Direct Addressing
Direct addressing is used for accessing data in the on-chip RAM. Since there are 256 bytes of RAM (128 bytes
general storage for the programmer and another 128 bytes for the SFRs). That means the addresses go from
00H to FFH, any of which can be stored in an 8-bit location.
MOV A, 67
The above instruction moves the data in location 67H into the accumulator. Note the difference between this
and immediate addressing. Immediate addressing uses the data, which is immediately after the instruction.
With direct addressing, the operand is an address. The data to be operated upon is stored in that address. The
assembler realises this is an address and not data because there is no hash symbol before it.
ADD A, 06
The above instruction adds the contents of location 06H to the accumulator and stores the result in the
accumulator. If the selected register bank is bank 0 then this instruction is the same as ADD A, R6.

Indirect Addressing
Register addressing and direct addressing both restrict the programmer to manipulation of data in fixed
addresses. The address the instruction reads from (MOV A, 30H) or writes to (MOV 30H, A) cannot be altered
while the program is running.
There are times when it is necessary to read and write to a number of contiguous memory locations. For
example, if you had an array of 8-bit numbers stored in memory, starting at address 30H, you may wish to
examine the contents of each number in the array (perhaps to find the smallest number). To do so, you would
need to read location 30H, then 31H, then 32H and so on.
This can be achieved using indirect addressing. R0 and R1 may be used as pointer registers. We can use either
one to store the current memory location and then use the indirect addressing instruction shown below.
MOV A, @Ri
where Ri is either R0 or R1.
Now, we can read the contents of location 30H through indirect addressing:
MOVR0,#30H
MOV A, @R0
The first instruction is an example of immediate addressing whereby the data 30H is placed in R0.
The second instruction is indirect addressing. It moves the contents of location 30H into the
accumulator.
If we now wish to get the data in location 31H we use the following:
INC R0
MOV A, @R0
Once we see how to write a loop in assembly language, we will be able to read the entire contents of the array.

Relative Addressing
Relative addressing is used only with certain jump instructions. The system executes a jump by changing the
Dept. of EEE, AMCEC, Bangalore-83 Page 56
BEE403 Microcontrollers 2025
contents of the PC to the address of the next instruction to be executed. For example, if we wished to jump to
the instruction stored at location 4EH in code memory, the PC would be loaded with 4EH. Then, during the
next execution cycle the contents of the PC (4EH) are placed on the address bus and the instruction at 4EH is
retrieved.
A relative address (or offset) is an 8-bit signed value, which is added to the PC to form the address of the next
instruction to be executed.
With 8-bit signed numbers, the MSB is used to determine whether the number is positive or negative. If the
MSB is 0 then the number is positive, while if the MSB is 1 the number is negative.
The instruction below shows how to jump six locations ahead.
SJMP 06H
SJUMP is an unconditional jump and is a 2-byte instruction. The number following it is an offset address. If
this instruction were stored in code memory at locations 100H and 101H, as shown below:
100H80H
101H06H
The opcode for SJMP is 80H. The operand is the offset address. If this instruction were executed the PC would
get the value 108H. This is what happens:
• The PC contains 100H, therefore the instruction 80H is read into the IR.
• The instruction is decoded as the 2-byte SJMP instruction.
• The PC is incremented so that the operand may be retrieved.
• The operand is read from code memory and the PC is incremented again (because this is a 2-bye instruction).
• The operand (06H) is added to the PC (102H + 06H = 108H).
• The next instruction (at 108H) is executed.

Once we deal with 2's compliment and how negative numbers are dealt with in the CPU, we will look at a
backward jump.
The S in SJMP stands for short. The range of signed 8-bit numbers is -127 to 128. Therefore, using SJMP
allows us to jump 127 locations forward or 128 locations backward. Hence the nameshort jump.
When writing assembly programs we do not need to calculate the offset when using SJMP. Instead, we use
labels. If we wished to jump to the instruction at 108H we would simply label the instruction with an
appropriate name, for example THERE. We would then write the assembly code SJMP THERE. The assembler
does the work of replacing the label with the correct offset.

Absolute Addressing
Absolute addressing is only used with the ACALL and AJMP instructions.
ACALL - subroutine call (2 byte instruction)
AJMP - unconditional jump (2 byte instruction)
These instructions allow you to move to any instruction within the same 2K of memory.
We will look at the AJMP instruction only (at a later date, when we begin dealing with subroutines we will
deal with the ACALL instruction).

The operation of the AJMP instruction is detailed below:


AJMP address
(PC) <- (PC) + 2
(PC10-PC0) <- address10 - address0

Note that only the eleven least significant bits of the PC are altered. The five most significant bits remain the
same. This means the AJMP will only allow you to jump to a location in the same 2K page as the instruction
Dept. of EEE, AMCEC, Bangalore-83 Page 57
BEE403 Microcontrollers 2025
directly after the jump.

For example:

If the label THERE represents an instruction at address 0F46H and the instruction AJMP THERE is in
memory at locations 0900H and 0901H, the assembler will encode the instruction as
11100001 1st byte (A10 - A8 + opcode)
010001102nd byte (A7 - A0)

The underlined bits are the low-order 11 bits of the destination address, 0F46H = 0000111101000110B. The
upper five bits in the program counter will not change when this instruction executes. Note that both the AJMP
instruction and the destination are within the 2K page bounded by 0800H and 0FFFH, and therefore have the
upper five address bits in common.

SOFTWARE DESCRIPTION

PROCESSOR used is Atmel AT89C51ED2 - micro controller that has 64Kbytes of on chip program memory.
It is a version of 8051 with enhanced features.
AT 89C51ED2 operates at 11.0592 MHz.

PROCESSOR FEATURES

ON-CHIP MEMORY: CODE MEMORY: 64K Bytes of flash.

DATA MEMORY: 256 Bytes of RAM, 1792 Bytes of XRAM, 2K Bytes of EEPROM.

ON-CHIP PERIPHERALS
3 16-bit Timers/Counters, Watch Dog Timer, Programmable Counter Array (PCA) on Port1 i.e. PWM and
Capture & Compare, SPI (Serial Peripheral Interface) on Port1, Full duplex enhanced UART.

INTERRUPTS
Nine sources of interrupt (both external and internal).
Two External interrupts INT0 and INT1 are provided with push button switches; these can also be used as
general-purpose switches.

I/O (Port) Lines


Four 10-pin connectors for all the 32 I/O lines. P0, P1 and P2 Port lines are available on a 26-pin connector,
16X2 LCD & SERIAL I/O are also available.

Creating and compiling a μ Vision3 project

1. Double Click on the Vision3 icon on the desktop.

2. Close any previous projects that were opened using – Project->Close.

3. Start Project – New Project, and select the CPU from the device database (Database-Atmel- AT89C51ED2).
(Select AT89C51ED2 or AT89C51RD2 as per the board).On clicking ‘OK’, the following option is displayed.
Choose Yes.

Dept. of EEE, AMCEC, Bangalore-83 Page 58


BEE403 Microcontrollers 2025
4. Create a source file (using File->New), type in the assembly or C program and save this ([Link]/
filename.c) and add this source file to the project using either one of the following two methods.
(i) Project-Components,Environmentand Books->addfiles->browse to the required file -> OK “OR”
(ii) right click on the Source Group in the Project Window and the Add Files to Group option.

5. Set the Target options using ->Project – Options for Target opens the
Vision4 Options for Target – Target configuration dialog. Set the crystal frequency as 11.0592 Mhz, and also
the Options for Target – Debug – use either Simulator / Keil Monitor- 51 driver. If Keil Monitor- 51 driver
is used click on Settings ->COMPort settings select the COMPort to which the board is connected and select the
baud rate as 19200 or 9600 (recommended). Enable Serial Interrupt option if the user application is not using
on chip UART, to stop program execution.

6. Build the project; using Project -> Build Project. Vision translates the entire user application and links.
Any errors in the code are indicated by – “Target not created” in the Build window, along with the error line.
Debug the errors. After an error free build, go to Debug mode.
7. Now user can enter into Debug mode with Debug- Start / Stop Debug session dialog or by clicking in the
icon.

8. The program is run using the Debug-Run command & halted using Debug-Stop Running. Also the (reset,
run, halt) icons can be used. Additional icons are (step, step over, step into, run till cursor).

9. If it is an interface program the outputs can be seen on the LCD, CRO, motor, led status etc. If it is a part A
program, the appropriate memory window is opened using View ->memory window (for data RAM & XRAM
locations), Watch window (for timer program),serial window, etc.
Note: To access data RAM area type address as D:0020h.
Similarly to access the DPTR region (XRAM-present on chip in AT89C51ED2) say 9000h location type in
X:09000H.

Creating and compiling a μVision3 project

1. Double Click on the μVision3 icon on the desktop.

2. Close any previous projects that were opened using – Project->Close.

3. Start Project – New Project, and select the CPU from the device database (Database-Philips- 89C51RD2+).
On clicking ‘OK’, the following option is displayed. Choose Yes.

4. Create a source file (using File->New), type in the assembly or C program and save this ([Link]/
filename.c) and add this source file to the project using either one of the following two methods.

(i) Project-Components,Environmentand Books->addfiles-> browse to the required file -> OK


“OR”
(ii) right click on the Source Group in the Project Window and the Add Files to Group option

Dept. of EEE, AMCEC, Bangalore-83 Page 59


BEE403 Microcontrollers 2025

5. Build the project; using Project -> Build Project. Vision translates all the user application and links. Any errors
in the code are indicated by – “Target not created” in the Build window, along with the error line. Debug the
errors. After an error free build, go to Debug mode.

7. Now user can enter into Debug mode with Debug- Start / Stop Debug session dialog or by clicking in the
icon.

8 .The program is run using the Debug-Run command & halted using Debug-Stop Running. Also the

(reset, run, halt) icons can be used. Additional icons are (step, step
over, step into, run till cursor).

Dept. of EEE, AMCEC, Bangalore-83 Page 60

You might also like