Microcontroller LAB Updated
Microcontroller LAB Updated
IV SEMESTER B.E.
LABORATORY MANUAL
BEE403-MICROCONTROLLERS
Academic year: 2025-26
Staff in charge:
BRANCH :
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.
12 Generate different waveforms: Sine, Square, Triangular, Ramp using DAC interface.
13 Appendix
14 Viva Questions
MICROCONTROLLERS
SYLLABUS
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
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.
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
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.
After execution
0040H 0041H 0042H 0043H
00 10 40 50
After execution
0040H 0041H 0042H 0043H
01 10 40 30
After execution
0040H 0041H 0042H 0043H
02 02 22 44
After execution
0040H 0041H 0042H 0043H
03 02 22 11
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
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
Trial II
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.
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
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
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
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
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
PROCEDURE:
1. Create the project and save it.
2. Select target device – Atmel 89C51ED2.
3. Create a new file/ develop the source code.
Result:
TRIAL I: before execution
X:5000H 5001H 5002H 5003H 5004H
34 23 FA 08 11
After execution:
5052H
FA
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
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.
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:
ALGORITHM:
PROCEDURE:
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:
b) LOGICAL INSTRUCTIONS
Aim
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.
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:
Result:
TRIAL I:
D: 050H
b) ASCII TO DECIMAL
Aim
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.
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.
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.
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
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.
RESULT:
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.
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.
RESULT:
UART#1
HI
Hardware Interfacing
Features of Embedded C:
Symbol Operation
& Bitwise AND
| Bitwise OR
~ Bitwise NOT
>> Shift Right
<< Shift Left
^ P0.0
Aim
Write a C program to control the speed of rotation of DC Motor.
Algorithm:
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)
{
while(!dcr);
if(i<0xf0)
i=i+10; /*decrease the DC motor speed*/
}
P0=i;
}
}
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
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);
}
}
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.
P0=temp;
rs=1; //select data register
rw=0; //write
en=1;
tdelay(1);
en=0;
}
Result:
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).
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);
}
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;}
}
}
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;
}
}
Algorithm:
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];
}
}
}
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?
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
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
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
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
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).
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.
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).
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
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.
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.
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.
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.
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).