You are on page 1of 82

MICROCONTROLLER LAB ( 06ES L47)

I. PROGRAMMING

1. Data Transfer – Block move, Exchange , Sorting, Finding


largest element in an array
2. Arithmetic Instructions – Addition / Subtraction ,
Multiplication and Division , square , Cube – ( 16 bits
Arithmetic Operations – bit addressable)
3. Counters
4. Boolean & Logical Instructions (Bit manipulations)
5. Conditional CALL & RETURN
6. Code conversion: BCD – ASCII ; ASCII – Decimal; Decimal –
ASCII;HEX – Decimal and Decimal- HEX
7. Programs to generate delay , programs using serial port and on-
Chip timer counter .

II. INTERFACING:

Write C programs to interface 8051 chip to Interfacing modules to


develop single chip solutions

8. Simple Calculator using 6 digit seven segment display and Hex


Keyboard interface to 8051.
9. Alpha numeric LCD panel and Hex keypad input interface to
8051
10.External ADC and Temperature control interface to 8051
11.Generate , different waveforms Sine , Square , Triangular
,Ramp etc. using DAC interface to 8051; change the frequency
and amplitude
12.Stepper and DC motor control interface to 8051
13.Elevator interface to 8051
Procedure to start up with Keil Micro Vision 3

a) Starting microvision 3
 Click on keil Micro Vision icon on the desktop

b) Loading a project into MicroVision 3


 Click on Project menu , Select Close Project if any Projects are
Present or Select New Project from the drop – down menu. Enter the file
name
.UV 2 and Click on OK
 Double Click on ATMEL from the wizard then select AT89c51 and
Press OK
 Micro vision 3 will load 8051 Microcontroller Projects file and
Display as :
Target 1
Source group 1
Startup. A 51

c) Editing and Assembling

 Type the program in the work space window


Now save the file and right click source group 1 .Select Add files to
group ‘ ‘source group’ , Let the files be in ASM . Select the
corresponding file from the list and click OK.
 To assemble select build target , if no error(s) are found the output
window will display. “ 8051 MICROCONTROLLER PROJECTS” – O
error(s) , 0 warning (s)
If error(s) are found then select Rebuild Target an then the Programmer
will find it easy to correct the error(s).

d) Debugging

 To debug Click on debug button.

 For memory display, select Memory window icon under View option

 Enter the bytes(s) at memory window(address)

 Now Click on Run button to run the program continuously

 After debugging ends the value will be stored in registers memory


and will also be displayed in memory window.
1. Write a program for moving a block of data from one memory
location to other location.

ALGORITHM:

1. Initialize source and destination address.


2. Set the block length(counter).
3. Transfer data bytes from source address to destination address.
4. Increment source and destination address.
5. Decrement block length.
6. If block length = 0 , halt the program else go back to step3.
ORG 0000h
SJMP 30h
ORG 30h
MOV DPH, #80h
MOV R0, #35h
MOV R1, #41h
MOV R3, #05h
BACK : MOV DPL,R0
MOVX A ,@DPTR
MOV DPL,R1
MOVX @DPTR,A
INC R0
INC R1
DJNZ R3,BACK
HERE: SJMP HERE
END

OUTPUT:

Before Execution

X: 0X008035 : 01 02 03 04 05 00 00 00
X: 0X008041 : 00 00 00 00 00 00 00 00
After Execution

X: 0X008035 : 01 02 03 04 05 00 00 00
X: 0X008041 : 01 02 03 04 05 00 00 00
2.Write a program to exchange a block of data bytes from one
location to other location.

Algorithm:

1. Initialize source and destination address.


2. Set the block length(counter).
3. Exchange data bytes present in source address and destination
address.
4. Increment source and destination address.
5. Decrement the block length.
6. If block length = 0 , halt the program else go back to step3.
ORG 0000h
SJMP 30h
ORG 30h
MOV R0, # 27h
MOV R1, # 41h
MOV R3, # 05h
BACK: MOVX A, @R0
MOV R2,A
MOVX A, @R1
MOVX @R0,A
MOV A, R2
MOV @R1,A
INC R0
INC R1
DJNZ R3,BACK
HERE: SJMP HERE
END

OUTPUT:
Before Execution

X: 0027h : 10 20 30 40 50 00 00 00 00
X: 0041h : 60 70 80 90 75 00 00 00 00

After Execution

Address X : 0027h : 60 70 80 90 75 00 00 00 00
X : 0041h : 10 20 30 40 50 00 00 00 00

3. Write a program to sort the given array in ascending or


descending order using bubble sort technique

Algorithm

1. Set the block length(counter).


2. Set the number of comparisons (passes).
3. Initialize source address.
4. Get the data from source address.
5. Increment source address.
6. Compare data between 2 source address.
7. If 1st data > 2nd data , decrement the number of comparisons else
exchange the 1st and 2nd data and then decrement the number of
comparisons.
8. The number of comparisons are zero , then decrement the block
length else go to Step 4 .
9. If block length = 0 , halt the program else go BACK to step3.

Descending Order

ORG 0000h
SJMP 30h
ORG 30h
MOV R0 , # 05h
DEC R0
BACK: MOV DPTR, #9000h
MOV A, R0
MOV R1,A
UP : MOVX A, @ DPTR
MOV B,A
INC DPTR
MOVX A, @ DPTR
CLR C
MOV R2,A
SUBB A,B
JC No exchange
MOV A,B
MOVX @ DPTR , A
DEC DPL
MOV A,R2
MOVX @ DPTR , A
INC DPTR
No exchange: DJNZ R1, UP
DJNZ R0, BACK
HERE : SJMP HERE
END
OUTPUT:

Before Execution
X: 9000h : 04 02 06 05 08

After Execution
X: 9000h : 08 06 05 04 02

Algorithm

1. Set the block length(counter).


2. Set the number of comparisons (passes).
3. Initialize source address.
4. Get the data from source address.
5. Increment source address.
6. Compare data between 2 source addresses.
7. If 1st data < 2nd data , decrement the number of comparisons else
exchange the 1st and 2nd data and then decrement the number of
comparisons .
8. The number of comparisons are zero , then decrement the block
length else go to Step 4 .
9. If block length = 0 , halt the program else go back to step3.
Ascending Order

ORG 0000h
SJMP 30h
ORG 30h
MOV R0 , # 05h
DEC R0
BACK: MOV DPTR, #9000h
MOV A, R0
MOV R1,A
UP : MOVX A, @ DPTR
MOV B,A
INC DPTR
MOVX A, @ DPTR
CLR C
MOV R2,A
SUBB A,B
JNC No exchange
MOV A,B
MOVX @ DPTR , A
DEC DPL
MOV A,R2
MOVX @ DPTR , A
INC DPTR
No exchange: DJNZ R1, UP
DJNZ R0, BACK
HERE : SJMP HERE
END

OUTPUT:

Before Execution

X: 9000h : 04 02 06 09 08

After Execution

X: 9000h : 02 04 06 08 09

4. WAP to find the largest element in an array

Algorithm

1. Set the block length.


2. Initialize the source address.
3. Get the data from source address & assume it as reference data.
4. Increment the source address.
5. Get the data from source address and assume it as input data .
6. Compare input data & the reference data.
7. If reference data is greater than the input data , increment the source
address .
8. Else input data will be the reference data & then increment the
source address.
9. Decrement the block length.
10.If block length =0, then reference data = largest number else go to
step 4.
11.Store the largest number.

ORG 0000H
SJMP 30H
ORG 30H
MOV R3,#05H
MOV DPTR,#4000H
MOVX A,@DPTR
MOV R1,A
BACK: INC DPTR
MOVX A,@DPTR
MOV R2,A
CLR C
SUBB A,R1
JC SKIP
MOV A,R2
MOV R1,A
SKIP: DJNZ R3,BACK
MOV DPL,#62H
MOV A,R1
MOVX @DPTR,A
HERE: SJMP HERE
END

RESULT:

BEFORE EXECUTION

X: 4000H: 23 21 24 28 03 18

AFTER EXECUTION

X:4062H: 28

5..WAP TO FIND THE SMALLEST ELEMENT IN AN ARRAY.

Algorithm

1. Set the block length.


2. Initialize the source address.
3. Get the data from source address & assume it as reference data.
4. Increment the source address.
5. Get the data from source address and assume it as input data.
6. Compare input data & the reference data.
7. If reference data is less than the input data , increment the source
address.
8. Else , input data will be the reference data & then increment the
source address.
9. Decrement the block length.
10.If block length =0 then reference data = largest number else go to
step 4.
11.Store the smallest number.

ORG 0000H
SJMP 30H
ORG 30H
MOV R3,#05H
MOV DPTR,#4000H
MOVX A,@DPTR
MOV R1,A
BACK: INC DPTR
MOVX A,@DPTR
MOV R2,A
CLR C
SUBB A,R1
JNC SKIP
MOV A,R2
MOV R1,A
SKIP: DJNZ R3,BACK
MOV DPL,#62H
MOV A,R1
MOVX @DPTR,A
HERE: SJMP HERE
END

RESULT:

BEFORE EXECUTION

X: 4000H: 23 21 24 28 03 18

AFTER EXECUTION

X:4062H: 03

6. Write a Program to add two 16 – bit numbers

Algorithm

1. Store two 16- bit data into R0, R1 & R2 , R3 Register.


2. Add lower bytes of two data’s in R1 & R3.
3. Store lower byte result in R4 reg.
4. Add higher bytes of two data’s in R0 & R2 along with carry.
5. Store higher byte result in R5 reg.
6. Strore carry in R6 reg.
ORG 00H
SJMP 30H
ORG 30H
MOV R0, # 1DH
MOV R1, # 22H
MOV R2, # 33H
MOV R3, # 44H
CLR C
MOV A, R1
ADD A, R3
MOV R4, A
MOV A, R0
ADDC A, R2
MOV R5,A
MOV A, #00H
ADDC A, #00H
MOV R6, A
HERE : SJMP HERE
END
RESULT:

BEFORE EXECUTION : AFTER EXECUTION

R0= 1DH R0 = 1DH


R1 = 22H R1 = 22H
R2 =33H R2 =33H
R3 = 44H R3 = 44H
R4 = 00 R4 = 66 --SUM
R5 = 00 R5 = 50-- SUM
R6 = 00 R6 = 00 -- CARRY

7. WAP to add two 32 bit numbers

Algorithm

1. Set the counter ( no of bytes ).


2. Initialize the two source address as 8000h & 8020h
3. Initialize the destination address as 8060h.
4. Get the data’s from both source addresses.
5. Add data’s along with carry.
6. Store the result into destination address & increment it.
7. Decrement the counter .
8. If counter = 0 , store the carry into destination address.
9. Else go to step 4.
ORG 0000H
SJMP 30H
ORG 30H
MOV DPH, #80H
MOV R0, #00H
MOV R1, #20H
MOV R2, #60H
MOV R3, #04H
CLR C
UP: MOV DPL, R0
MOVX A, @DPTR
MOV R4, A
MOV DPL, R1
MOVX A, @DPTR
ADDC A, R4
MOV DPL, R2
MOVX @DPTR ,A
INC R0
INC R1
INC R2
DJNZ R3, UP
MOV A, #00H
ADDC A, #00H
MOV DPL, R2
MOVX @DPTR, A
HERE: SJMP HERE
END

RESULT:

BEFORE EXECUTION
X: 8000H: EE 0A 0C 0B
X: 8020H: 21 22 23 24

AFTER EXECUTION
X: 8060H: 0F 2C 2F 2F

8 .WAP TO SUBTRACT TWO 16 BIT NUMBERS

ALGORITHM

1. Store two 16 bit data into R0, R1 & R2 ,R3 registers.


2. Subtract lower bytes of both the data in R1 & R3 register along with
the borrow.
3. Store lower byte result in r4 register.
4. Subtract higher bytes of both data’s in R0 & R2 register along with
borrow.
5. Store higher byte result in R5 register.
6. End of code.
ORG 00h
SJMP 30h
ORG 30h
MOV R0, # 1Dh
MOV R1, # 22h
MOV R2, # 33h
MOV R4, #44h
CLR C
MOV A, R1
SUBB A, R3
MOV R4, A
MOV A, R0
SUBB A, R2
MOV R5, A
HERE: SJMP HERE
End
9. WRITE A PROGRAM TO PERFORM 32- BIT SUBTRACTION.

ALGORI THM.

1. Initialize the counter using R3 register.


2. Initialize the two source addresses as 8000h & 8020h.
3. Initialize the destination address as 8060h.
4. Get the data from both source addresses.
5. Subtract both data’s along with borrow.
6. Store the result in to destination address.
7. Increment both the source addresses & the destination address.
8. Decrement the counter.
9. If counter is zero, end the code.
10.Else go to step 4.
ORG 0000H
SJMP 30H
ORG 30H
MOV DPH, # 80H
MOV R0, # 00H
MOV R1, # 20H
MOV R2, #60H
MOV R3, #04H
CLR C
UP: MOV DPL, R0
MOVX A, @DPTR
MOV R4, A
MOV DPL, R1
MOVX A , @DPTR
SUBB A, R4
MOV DPL, R2
MOVX @DPTR, A
INC R0
INC R1
INC R2
DJNZ R3,UP
HERE: SJMP HERE
END

RESULT:
BEFORE EXECUTION
X: 8000H: 10 20 30 40
X: 8020H: 50 60 70 80

AFTER EXECUTION
X: 8060H: 40 40 40 40

10, WRITE A PROGRAM TO FIND SQUARE OF THE GIVEN 8 BIT


NUMBER

Algorithm

1. Store the 8bit data at the memory location 8000h.


2. Multiply with the same data .
3. Store the lower byte result from accumulator into 8001h memory
location & the higher byte result from register B into 8002h
memory location.
4. End of code.
ORG 00H
SJMP 30H
ORG 30H
MOV DPTR, # 8000H
MOVX A, @DPTR
MOV 0F0H , A
MUL AB
INC DPTR
MOVX @DPTR, A
MOV A, 0F0H
INC DPTR
MOVX @DPTR, A
HERE: SJMP HERE
END

RESULT:

BEFORE EXECUTION
X: 8000H: FF 00 00 00

AFTER EXECUTION
X: 8000H: FF 01 FE 00

11.Write a program to find the cube of a given number.

Algorithm.

1. Store the 8bit data at the memory location 8000h.


2. Multiply with the same data .
3. Store the lower byte of result from register A into register R2 &
higher byte of result from reg B into reg R3.
4. Multiply the lower byte of reult in reg R2 from the input data.
5. Store the lower byte result from reg A int0 memory location 8001h
& higher byte result from reg B into reg R4.
6. Get the higher byte result of previous multiplication from reg R3 as
in step 3 into reg A & multiply with the input data .
7. Add along with carry the lower byte of result in reg A & the higher
byte result of previous multiplication in reg R4 & store the
result at memory location 8002h.
8. Add along with carry the higher byte of result in reg B with the
contents of reg A.
9. Store the result at memory loaction 8003h.
10.End of code.

ORG 00H
SJMP 30H
ORG 30H
MOV DPTR, # 8000H
MOVX A, @DPTR
MOV 0F0H , A
MOV R1, A
MUL AB
MOV R2, A
MOV R3, 0F0H
MOV 0F0H, R1
MUL AB
INC DPTR
MOVX @DPTR, A
MOV R4, 0F0H
MOV A, R3
MOV 0F0H , R1
MUL AB
ADD A, R4
INC DPTR
MOVX @DPTR, A
MOV A, #00H
ADDC A, 0F0H
INC DPTR
MOVX @DPTR, A
HERE: SJMP HERE
END

RESULT:

BEFORE EXECUTION
X: 0X008000H: FF 00 00 00 00 00

AFTER EXECUTION X: 0X008020H: FF FF 02 FD 00 00

12. WAP to search the key element in a given array and to


display the position of the key element in an array & display the
message for search failure using linear search technique

Algorithm

1. Set the block length using reg R0.


2. Store the element to be searched in reg R1.
3. Store the data 01h in reg R2 which acts like a pointer.
4. Initialize the source address as 8000h.
5. Get the data from source address & compare it with the content of
memory location 01h.
6. If it is equal , store the contents of reg R2(position) at mem
location 8050h.
7. Else increment the source address & content of reg R2 &
decrement block length.
8. If block length is not zero go to step 5.
9. Else store the result offh for search failure at mem location 8050h.
10.End of code.

ORG 0000H
SJMP 30H
ORG 30H
MOV R0, # 05H
MOV R1, # 23H
MOV R2, #01H
MOV DPTR, # 8000H
UP : MOVX A, @DPTR
CJNE A, 01H, NEXT
SJMP SUCCESS
NEXT : INC R2
INC DPTR
DJNZ R0,UP
MOV R2, # 0FFH
SUCCESS: MOV A, R2
MOV DPTR, # 8050H
MOVX @DPTR,A
HERE: SJMP HERE
END

RESULT:

BEFORE EXECUTION
X: 8000H: 21 24 23 03 08

AFTER EXECUTION
X: 8050H: 03H

13. WAP to evaluate the given boolean expression

ALGORITHM

1. Store two 8 bit data at mem loc’s 8000h & 8001h & move data to reg
R0 & R1 respectively.
2. Get the data in reg R0 to reg A.
3. Complement the first data then AND it with the second data & store
the result in reg R2.
4. Get the second data from reg R1 into reg A.
5. Complement the second data & AND it with first data in reg R0.
6. Logically OR the content of reg A with content of reg R2.
7. Increment the memory location to 8002h & store the result.
8. End of code.

ORG 00H
SJMP 30H
ORG 30H
MOV DPTR, # 8000h
MOVX A, @DPTR
MOV R0, A
INC DPTR
MOVX A, @DPTR
MOV R1, A
MOV A, R0
CPL A
ANL A, R1
MOV R2, A
MOV A, R1
CPL A
ANL A, R0
ORL A, R2
INC DPTR
MOVX @DPTR , A
HERE: SJMP HERE
End

RESULT:

BEFORE EXECUTION:
X: 0X008000H: 22 25 00 00

AFTER EXECUTION:
X: 0X008000H: 22 25 07 00

14. WAP to find 2 out of 5 code.

Algorithm

1. Store the 8bit data at mem location 8000h.


2. Initialise R2 as 00h & reg R3 as counter.
3. Mask higher three bits of data with 0E0h.
4. If the result is not zero , the number is not a 2 out of 5 code.Display
0AAh at mem location 8001h.
5. If the result is zero, get the original 8 bit data into reg A.
6. Count the number of 1's in lower five bits by rotating right the
content of reg A & store in reg R2.
7. If the content of reg R2 is equal to 2, the number is a palindrome.
Display 0FFh at memory loaction 8001h.
8. Else display 0AAh at mem location 8001h which is not a 2 out of 5
code.
9. End of code.
ORG 00H
SJMP 30H
ORG 30H
MOV DPTR, # 8000h
MOVX A, @DPTR
MOV R1,A
ANL A, # 0E0H
JNZ SKIP
MOV R2, #00H
MOV R3, # 05H
CLR C
MOV A, R1
UP: RRC A
JNC DOWN
INC R2
DOWN : DJNZ R3, UP
MOV A, R2
CJNE A , # 02H,SKIP
MOV A , #0FFH
SJMP STOP
SKIP: MOV A, #0AAH
STOP: INC DPTR
MOVX @DPTR , A
HERE: SJMP HERE
End

RESULT:

BEFORE EXECUTION
X: 0X008000H: 0A 00 00 00
AFTER EXECUTION
X: 0X008000H: 0A FF 00 00
15 . WAP to find a number is positive or negative

Algorithm

1. Store the 8bit data at mem location 8000h.


2. Check MSB of the given data.
3. If MSB=1 , the result is negative .Display 0AAH at mem loc
8001h.
4. If MSB=0 , the result is positive.Display 0FFH at mem loc 8001h.
5. End of code.
ORG 0000H
SJMP 30H
ORG 30H
MOV DPTR, # 8000h
MOVX A, @DPTR
CLR C
RLC A
JNC DOWN
MOV A, # 0AAH
SJMP EXIT
DOWN: MOV A, #0FFH
EXIT: INC DPTR
MOVX @DPTR,A
HERE: SJMP HERE
End

RESULT:

BEFORE EXECUTION
X: 8000H: 34H

AFTER EXECUTION
X: 8001H: FFH

BEFORE EXECUTION
X: 8000H: A4H

AFTER EXECUTION
X: 8001H: AAH
16 .WAP to check the given data even or odd

Algorithm

1. Store the 8bit data at mem location 8000h.


2. Clear the carry flag.
3. Rotate the content of reg A right along with carry.
4. Check the LSB of the given data .
5. If LSB=1 , the number is odd. Dispaly 0DDh at mem loc 8001h.
6. Else the number is even. Display 0EEh at mem loc 8001h.
7. End of code.
ORG 0000H
SJMP 30H
ORG 30H
MOV DPTR, # 8000h
MOVX A, @DPTR
CLR C
RLC A
JNC DOWN
MOV A, # 0DDH
SJMP EXIT
DOWN: MOV A, #0EEH
EXIT: INC DPTR
MOVX @DPTR,A
HERE: SJMP HERE
End

RESULT:

BEFORE EXECUTION:
X: 8000H: 5AH
AFTER EXECUTION:
X: 8001H: EEH

BEFORE EXECUTION:
X: 8000H: FFH
AFTER EXECUTION;
X: 8001H: DDH
17. WAP to count the number of 1's & 0's

Algorithm

1. Store the 8bit data at mem location 8000h.


2. Initialize reg R1 & R2 with 00h.
3. Initialize reg R3 as counter.
4. Clear the carry flag.
5. Rotate the content of reg A along with carry.
6. If carry flag is 1 increment R1 else increment R2.
7. Decrement the counter.
8. If counter=0 ,store the contents of regR1 & reg R2 at mem loc at
8001h & 8002h.
9. Else go to step 5.
10.End of code.
ORG 0000H
SJMP 30H
ORG 30H
MOV DPTR, # 8000h
MOVX A, @DPTR
MOV R1, #00H
MOV R2, #00H
MOV R3, #08H
CLR C
UP: RRC A
JNC DOWN
INC R1
SJMP EXIT
DOWN : INC R2
EXIT: DJNZ R3, UP
INC DPTR
MOV A, R1
MOVX @DPTR,A
INC DPTR
MOV A,R2
MOVX @DPTR,A
HERE: SJMP HERE
End
RESULT:

BEFORE EXECUTION:
X: 8000H: 6AH

AFTER EXECUTION:
X: 8000H: 6AH
X: 8001H: 04H
X: 8002H: 04H

18. WAP to check the given data is bitwise palindrome or not.

Algorithm

1. Store the 8bit data at mem location 8000h.


2. Store the input data in reg R2.
3. Initialize reg R3 with 00h to get reverse data & reg R4 as counter.
4. Rotate input data in reg A right through carry and store it in reg
R1.
5. Rotate reverse data with carry left side & store it in reg R3.
6. Decrement the counter.
7. If counter ≠0, go to step 4.
8. Else compare the input data with reverse data.
9. If input data = reverse data , the 8 bit data is palindrome & displays
011h at mem loc 8001h.
10.Else the 8 bit data is not a palindrome & display 0FFh at mem loc
8001h.
11.End of code.
ORG 0000H
SJMP 30H
ORG 30H
MOV DPTR, # 8000h
MOVX A, @DPTR
MOV R1, A
MOV R2, A
MOV R3, #00h
MOV R4, #08h
CLR C
UP: MOV A, R1
RRC A
MOV R1, A
MOV A, R3
RLC A
MOV R3, A
DJNZ R4, UP
MOV A, R2
CJNE A ,03H,DOWN
MOV A, #11H
SJMP EXIT
DOWN: MOV A, #0FFH
EXIT: INC DPTR
MOVX @DPTR, A
HERE: SJMP HERE
END

RESULT:

Before Execution : X : 8000h : 66h


After Execution: X : 8001h : 11h

Before Execution : X : 8000h : 68h


After Execution: X : 8001h : FFh

19. WAP to check the given data is nibble wise palindrome.

Algorithm

1. Store the 8bit data at mem location 8000h & in reg R1.
2. Exchange the nibbles of input data in reg A.
3. Compare the data in reg A with the data in reg R1.
4. If equal , the 8 bit data is a palindrome & display 011h at mem loc
8001h.
5. Else the 8 bit data is not a palindrome & display 0FFh at mem loc
8001h.
6. End of code.
ORG 0000H
SJMP 30H
ORG 30H
MOV DPTR, #8000h
MOVX A, @DPTR
MOV R1,A
SWAP A
CJNE A , 01H,EXIT
MOV A, #11H
SJMP DOWN
EXIT : MOV A, #0FFH
DOWN: INC DPTR
MOVX @DPTR ,A
HERE: SJMP HERE
END

RESULT:
Before Execution : X : 8000h : 77h
After Execution: X : 8001h : 11h

Before Execution : X : 8000h : ABh


After Execution: X : 8001h : FFh

19. WAP to perform 16 bit multiplication.

Algorithm

1. Store two16 bit data in registers R0,R1 & R2 R3.


2. Intialise reg R4 & R5 with 00h.
3. Multiply lower bytes of both data i.e in reg R1 & R3.
4. Store lower byte of product in reg A at mem loc 8000h & higher
byte of product in reg B at mem loc 16h.
5. Multiply lower byte of 1st data i.e. in reg R1 with higher byte of 2nd
data i.e in reg R2 & store the result at meme loc 17h & 18h
respectively.
6. Multiply higher byte of 1st data i.e in reg R0 with lower byte of 2nd
data i.e in reg R3 & store the result of product in reg A & at mem
loc 19h.
7. Add lower byte of product i.e in reg A with the data in meme loc
17h & 16h & store the result at mem loc 8001h & the carry
generated in reg R4.
8. Multiply higher bytes of both data i.e in reg R0 & R2 & store the
result in reg A & at mem loc 20h.
9. Add the lower byte & product in reg A with the data in mem loc
19h & 18h along with carry in reg R4 & store the result at mem
loc 8002h & the carry generated in reg R5.
10.Add the carry in reg R5with the data at mem loc 20h & store the
result at mem loc 8003h.
11.End of code.

ORG 0000H
SJMP 30H
ORG 30H
CLR C
MOV R0, #1Dh
MOV R1, #22h
MOV R2, #44h
MOV R3, #78h
MOV R4, #00h
MOV R5, #00h
MOV A, R1
MOV B, R3
MUL AB
MOV DPTR, # 8000h
MOVX @DPTR , A
MOV 16h, B
MOV A, R1
MOV B, R2
MUL AB
MOV 17H, A
MOV 18H, B
MOV A, R0
MOV B, R3
MUL AB
MOV 19h, B
ADD A, 17H
JNC DOWN
INC R4
DOWN: ADD A, 16h
JNC DOWN1
INC R4
DOWN1: INC DPTR
MOVX @DPTR, A
MOV A,R0
MOV B,R2
MUL AB
MOV 20h, B
ADD A, 19h
JNC DOWN2
INC R5
DOWN2: ADD A ,18h
JNC DOWN3
INC R5
DOWN3: ADD A,R4
JNC DOWN4
INC R5
DOWN4: INC DPTR
MOVX @DPTR , A
MOV A, 20h
ADD A, R5
INC DPTR
MOVX @DPTR , A
HERE: SJMP HERE
END

RESULT:
Before Execution: X : 8000h : F0
After Execution: X : 8001h : AF
X : 8002h : CA
X : 8003h : 07

21. WAP to find square 16 bit numbers.

ALGORITHM:

1. Store 16 bit data in reg R0R1


2. Initialize the reg’s R3 & R4 with 00h
3. Multiply the lower byte of data by itself i.e data in reg R1 & store
the lower byte of Result in reg A mem loc 8000h & higher byte
Result in reg B at mem loc 16h
4. Multiply the lower byte of input data in reg R1 with higher byte of
input data in reg R0 & store the lower byte Result in reg A at mem
loc 17h & higher byte Result in reg B at mem loc 18h.
5. Again multiply the lower byte of input data in reg R1 with higher
byte of input data in reg R0 & store the lower byte Result in reg A
at mem loc 19h & higher byte Result in reg B at mem loc 20h
6. Add the data’s at mem loc’s 19h with data at mem loc 17h & 16h
& store the Result at mem loc 8001h & carry generated in reg R3.
7. Multiply the higher byte itself & store lower byte the Result in reg
A & higher byte Result at mem loc 21h.
8. Add the data in reg A with the data at mem loc 20h & 18h along
with carry in reg R3 & store the Result at mem loc 8002h & the
carry generated in reg R4.
9. Add the data at mem loc 21h with the carry in reg R4 & store the
Result at mem loc 8003h
10.End

ORG 00H
SJMP 30H
ORG 30H
MOV R3, #00h
MOV R4,#00h
MOV R0, #44h
MOV R1,#78h
MOV A,R1
MOV B, A
MUL AB
MOV dptr, # 8000h
MOVX @dptr , A
MOV 16h,B
MOV A,R1
MOV B,R0
MUL AB
MOV 17h, A
MOV 18h, B
MOV 19h,A
MOV 20h,B
add A, 17h
JNC DOWN
INC R3
DOWN: add a, 16h
JNC down1
INC R3
DOWN1: INC DPTR
MOVX @dptr, a
MOV A,R0
MOV B,R0
MUL AB
MOV 21h , B
add A, 20h
JNC down2
INC R4
down2: add a, 18h
JNC DOWN4
INC R4
down 3: add a,R3
JNC down4
INC R4
down 4: INC dptr
MOVX @dptr,a
MOV A,21h
add A,R4
INC dptr
MOVX @dptr,a
HERE: Sjmp HERE
end

RESULT:

Before Execution : X : 8000h : 40h


After Execution: X : 8001h : F8h

Before Execution : X : 8002h : 4Fh


After Execution: X : 8003h: 12h
21. WAP to perform 16 bit division

1. Store the 16 bit dividend in regs R0R1 and the 8 bit divisor in reg
R2
2. Initialize the regs R4,R5 & B with 00h to store the quotient & no of
shifts.
3. Incremenet the no of shifts
4. Rotate lower byte of divisor to left with carry & store as lower byte
of divisor in reg R2.
5. Rotate higher byte of divisor to left with carry & store as higher
byte of divisor in reg R3.
6. Check the carry flag
7. If cy=0 go to step 3.
8. If cy=1 , rotate lower byte of divisor right with carry & store in reg
R3.
9. Rotate higher byte of divisor right with carry & store in reg R2.
10.Subtract lower byte of dividend with data in reg R3 & store in reg
R0.
11.Subtract higher byte of dividend with data in reg R2 & store in reg
R1.
12.Check the carry flag.
13.If cy=0, compliment carry else get the original 16 bbit dividend &
then compliment carry.
14.Rotate lower byte of quotient in reg R4 with carry left & store in
reg R4.
15.Rotate higher byte of quotient in reg R5 with carry left & store in
reg R5.
16.Decrement the no of shifts in reg B
17.If no of shifta =0, store the data in reg’s R0,& R1 as remainder.
18.Else go to step 8.
19.end
ORG 00H
SJMP 30H
ORG 30H
MOV R0 #99h
MOV R1#99h
MOV R2 #15h
MOV R3,#00h
MOV R4, #00h
MOV R5,#00h
MOV B #00h
Div 1: INC B
MOV A,R2
RLC A
MUL AB
MOV R2,A
MOV A,R3
RLC A
MOV R3,A
JNC Div 1
Div 2 : MOV A,R3
RRC A
MOV R3,A
MOV A,R2
RRC A
MOV R2,A
CLR C
MOV 07h, R1
MOV 06h, R0
MOV A, R0
SUBB A,R2
MOV R0,A
MOV A,R1
SUBB A,R3
MOV R1,A
JNC Div 3
MOV R1, 07h
MOV R0,06h
Div 3 CPL C
MOV A, R4
RLC A
MOV R4,A
MOV A,R5
RLC A
MOV R5,A
DJNZ B, Div 2
MOV R3, 05h
MOV R2, 04h
HERE: Sjmp HERE
end

RESULT:
R0 = 09H R4 = 50H
R1 = 00H R5 = 07H
R2 = 50H R6 = 09H
R3 = 07H R7 = 00H

22) Write a Program to perform ASCII to BCD/DECIMAL


Conversion

Algorithm

Store two ASCII numbers at Memory location 8000h & 8001h


Mask first data at Memory location 8000h with 0FH & Reverse the
nibbles of the Result.
Store BCD obtained in reg R1
Mask 2nd data at Memory location 8001h with 0FH
Store the Result in reg A
Logically OR the data in Reg A with data in reg R1
Store the Result at Memory location 8002h
End of code
22
ORG 00H
SJMP 30H
ORG 30H
MOV dptr, # 8000h
MOVX A,@dptr
ANL A, #0FH
SWAP A
MOV R1,A
INC dptr
MOVX A,@dptr
ANL A, #0FH
ORL A,R1
MOVX @dptr,A
HERE : SJMP HERE
end

RESULT:

Before Execution : X : 8000h : 35h 39h


After Execution: X : 8002h : 59h

Before Execution : X : 8000h : 31h 35h


After Execution: X : 8002h: 15h

23. WAP to perform BCD./decimal to ASCII conversion.

1. Store BCD numbers at mem loc 8000h


2. Mask the lower 4 bits of input data with 0Fh
3. Add 30h to get ASCII equivalent & store the Result at mem loc
8001h.
4. Mask the higher 4 bits of input data with F0h & swap the data
5. Add 30h to get ASCII equivalent & store the Result at mem loc
8002h
6. End
ORG 00H
SJMP 30H
ORG 30H
MOV DPTR, # 8000H
MOVX A,@DPTR
MOV R1,A
ANL A, #0FH
ORL A, #30H
INC DPTR
MOVX @DPTR,A
MOV A,R1
ANL A, #0F0H
SWAP A
ORL A, #30H
INC DPTR
MOVX @DPTR,A
HERE : SJMP HERE
END

RESULT:
Before Execution : X : 8000h : 09h
After Execution: X : 8001h : 39h
X : 8002h : 30h
24. WAP to perform hexadecimal/binary to decimal conversion

1. Store the hexadecimal data as input at mem loc 8000h


2. Divide the input data with 0Ah stored in reg B
3. Store the quotient in reg R1 & the remainder in reg B.
4. Again divide the quotient with 0Ah & store the quotient at mem
loc 8001h
5. Exchange the higher & lower nibble of remainder in reg B & OR it
with quotient.
6. Store the Result at mem loc 8002h
7. End
24
ORG 00H
SJMP 30H
ORG 30H
MOV DPTR, # 8000H
MOVX A,@DPTR
MOV B, # 0AH
DIV AB
MOV R1,A
MOV R2,B
MOV B, # 0AH
DIV AB
INC DPTR
MOVX @DPTR,A
MOV A,B
SWAP A
ORL A, R2
INC DPTR
MOVX @DPTR,A
HERE : SJMP HERE
END
RESULT:
Before Execution : X : 8000h : FFh
After Execution: X : 8001h : 02h
X : 8002h : 55h

25. WAP to perform decimal to hexadecimal/binary conversion

1. Store the decimal data as input at mem loc 8000h


2. Mask the lower 4 bits of input data with 0Fh & swap the data &
multiply with 0Ah.
3. Store the lower byte of Result in reg R1.
4. Mask the higher 4 bits with 0Fh & add it with the data in reg R1.
5. Store the Result at mem loc 8001h
6. End.
ORG 00H
SJMP 30H
ORG 30H
MOV DPTR, # 8000H
MOVX A,@DPTR
MOV R1,A
ANL A, #0FH
MOV R2,A
MOV A, R1
ANL A, #0F0H
SWAP A
MOV B , #0AH
MUL AB
ADD A, R2
INC DPTR
MOVX @DPTR,A
HERE : SJMP HERE
END

RESULT:
Before Execution : X : 8000h : 44h
After Execution: X : 8001h : 2Ch
26. WAP to perform hexadecimal/binary to ASCII conversion

1. Store the binary data as input at mem loc 8000h


2. Mask the higher nibble with 0Fh
3. Convert lower nibble to ASCII equivalent by subtracting 0Ah with
it along borrow.if the Result is greater than 0Ah add 30h else add
37h & store the Result at mem loc 8001h.
4. Mask the lower nibble with F0h & swap the data.
5. Convert the higher nibble to ASCII equivalent by subtracting 0Ah
with it along with borrow.
6. If the Result is greater than 0Ah add 30h else add 37h & store the
Result at mem loc 8002h.
7. End
26
ORG 00H
SJMP 30H
ORG 30H
MOV DPTR, # 8000H
MOVX A,@DPTR
MOV R1,A
ANL A, #0FH
ACALL ASCII
INC DPTR
MOV A,03H
MOVX @DPTR,A
MOV A, R1
ANL A, #0F0H
SWAP A
ACALL ASCII
INC DPTR
MOV A,03H
MOVX @DPTR,A
ASCII: MOV R2,A
CLR C
SUBB A, # 0AH
MOV A, R2
JC DOWN
ADD A, #07H
ADD A, #30H
DOWN: ADD A, #30H
MOV 03H ,A
RET
HERE : SJMP HERE
END
RESULT:
Before Execution : X : 8000h : 9Ah
After Execution: X : 8001h : 41h

27. Write a Program to Perform ASCII to hexadecimal binary

1. Store the ASCII data at Memory location 8001h


2. Subtract 30h with the ASCII data
3. If the Result is not equal to 0Ah check if the Result is greater than
0Ah store the Result at number location 8001h
4. Else subtract 07h from the Result & Store it at memory location
8002h
5. End
ORG 00H
SJMP 30H
ORG 30H
MOV DPTR, # 8000H
MOVX A,@DPTR
MOV R1,A
CLR C
SUBB A, #30H
MOV R2, A
CJNE A, # 0AH,EXIT
EXIT: JC LOOP
MOV A, R2
SUBB A, # 07H
LOOP:INC DPTR
MOVX @DPTR,A
HERE : SJMP HERE
END

RESULT:
Before Execution : X : 8000h : 39h
After Execution: X : 8001h : 09h

28.Write a Program UP counter using delay subroutine


(Hexadecimal)

ALGORITHM
1.Initialise the initial count = 00h
2. Call delay
3. Add 01h to initial count
4. Display the count at Port2
5.End of code
ORG 00H
SJMP 30H
ORG 30H
MOV A, #00H
L1: A CALL DELAY
ADD A, #01H
MOV P2,A
SJMP L1
DELAY: MOV R1,#0FFH
UP2: MOV R2,#0FFH
UP1: MOV R3,#20H
UP: DJNZ R3,UP
DJNZ R2,UP1
DJNZ R1,UP2
RET
END

RESULT

PORT 2 00H
01H

1FH
FFH
29 Write a Program DOWN counter using delay subroutine
(Hexadecimal)

ALGORITHM
1.Initialise the initial count = FFH
2. Call delay
3. Subtract 01h from initial count
4. Display the count at Port2
5.End of code
ORG 00H
SJMP 30H
ORG 30H
MOV A, #FFH
L1: A CALL DELAY
SUBB A, #01H
MOV P2,A
SJMP L1
DELAY: MOV R1,#0FFH
UP2: MOV R2,#0FFH
UP1: MOV R3,#20H
UP: DJNZ R3,UP
DJNZ R2,UP1
DJNZ R1,UP2
RET
END

RESULT

PORT 2 FFh

1Fh

00h
30.Write a Program to Perform Decimal UP counter using delay
subroutine

ALGORITHM
1.Initialise the initial value to 00h
2. Call delay
3. Add 01h to the initial value
4. Adjust the count value to decimal
5.Display the count value at port2
6.End of code
ORG 00H
SJMP 30H
ORG 30H
MOV A, #00H
L1: A CALL DELAY
ADD A, #01H
DAA
MOV P2,A
SJMP L1
DELAY: MOV R1,#0FFH
UP2: MOV R2,#0FFH
UP1: MOV R3,#20H
UP: DJNZ R3,UP
DJNZ R2,UP1
DJNZ R1,UP2
RET
END
RESULT

PORT 2 00

50

99

31.Write a Program to Perform Decimal DOWN counter using


subroutine

ALGORITHM
1.Initialise the initial value to 99h
2. Call delay
3. Add 99h to the initial count
4. Adjust the count value to decimal
5.Display the count value at port2
6.End of code
ORG 00H
SJMP 30H
ORG 30H
MOV A, #00H
L1: A CALL DELAY
ADD A, #99H
DAA
MOV P2,A
SJMP L1
DELAY: MOV R1,#0FFH
UP2: MOV R2,#0FFH
UP1: MOV R3,#20H
UP: DJNZ R3,UP
DJNZ R2,UP1
DJNZ R1,UP2
RET
END

RESULT

PORT 2 99

50

00
VIVA QUESTIONS
MICROCONTROLLER
1. What is microprocessor?
Ans. A general-purpose digital central processing unit.

List the difference between MC & MP.


Ans. MC: Has on chip memory & ADC. It has single bit manipulation
instructions.

List the types of MCs.


Ans. 8-bit MC – 8031/8051
16-bit MC – MC68HC12 , 8096
32-bit MC - 80969, M683xx

4. What is CICS & RISC architecture MC?


Ans. CISC: When MC has an instruction set that supports many
addressing modes for arithmetic & logical operations.
RISC: When MC has an instruction set that supports one or two
addressing modes for arithmetic & logical operations.
5. Difference between Harvard & Princeton architecture.
Ans. Harvard architecture: Distinct memory address space for
program & data memory.
Princeton architecture: Separate memory address space for program &
data memory.

6. Examples for Harvard & Princeton architecture.


Ans. Harvard architecture: 8051
Princeton architecture: 68HC11

Define I/O port.


Ans. Input & output circuit that connects MC to external devices.

8. Define Bus.
Ans. Conducting lines that carry information within MC & from MC
to external devices

Types of Buses
Ans. Address Bus: Carries address – 16 bits
Data bus: Carries data – 8 bits

10.Crystal frequency used in 8051.


Ans. 12Mhz

Function of Program counter (PC).


Ans. It is a 16-bit register. PC stores the address of the next instruction
to be executed.

12.Capacity of Internal ROM & RAM


Ans. RAM – 128 bytes
ROM – 4K bytes

13.Define CPU.
Ans. Heart of the processor that process the data.

14.Function of PSEN signal.


Ans. Enables the program memory.

15.Function of EA signal.
Ans. Enables the external memory.

16.Number of timers & counters available in 8051.


Ans. Two 16- bit timers/counters are available in 8051.

17.What do you mean by bit addressable instruction?


Ans. The instruction that allows change of a bit in a register is called
bit addressable instruction.

18.Two 8-bit memory pointers used in 8051.


Ans. R0 & R1 are two 8- bit internal memory pointers.

19.Number of banks available in 8051.


Ans. 4 banks are available. Each bank has 8 registers. Each register is
of 8 – bits.

20.External data memory is accessed by 16 bit register called data


pointer (DPTR)

21.Define Addressing modes.


Ans. The way in which the instructions are executed is called
Addressing modes.

22.Types of addressing modes.


Ans. Indirect addressing modes
Direct addressing modes
Register Addressing modes
Indexed Addressing modes

23.What are Indexed Addressing modes?


Ans. Used in accessing data elements of look up table entries located
in ROM.
Registers DPTR & A are used. Instruction MOVC is used.

24.How to find the relative address?


Ans. @A + DPTR = relative address.

25.Define Asynchronous serial communication.


Ans. Each character is transmitted at a time.

26. Define synchronous serial communication


Ans. Each block of data is transmitted & received at a time.

26.What is Data Framing?


Ans. Placing the character between a start bit & stop bit.
27.Registers used in serial communication.
Ans. SBUF & SCON registers.

28.Name two bits of SCON register that is used for selecting the serial
mode of communication.
Ans. SM0 & SM1

Importance of TI flags.
Ans. Monitors SBUF register to assure that it is not overloaded.

30.How to increase the baud rate?


Ans. To use a higher frequency crystal.
To change a bit SMOD in the PCON register.

RAM location used as Scratch pad


Ans. 30-7FH

Name the instructions that are bit addressability,


Ans. Register A, B, PSW, IP, IE, ACC, SCON and TCON

What is a Flag?
Ans. One bit register that indicates the result of the operation.

Flags used in 8051.


Ans. AC (Auxiliary carry), OV (over flow), P( parity) & CY (carry).

Bits used in PSW register to select register bank.


Ans. RS0 & RS1.

Function of the PCON register?


Ans. Doubles the baud rate

Function of TCON register.


Ans. Indicates the overflow of timer/counters
Controls the start/stop of counters/timers.

Function of TMOD register.


Ans. Selects the timer mode of operation
Selects the timer/counter.

What is polling?
Ans. MC checks the devices in the round robin fashion.
What is an interrupt?
Ans. A signal that request the MC to give its attention.

Advantages of Interrupt.
Ans. MC can serve many devices.
Priority can be assigned to the devices.

What is ISR?
Ans. A small program associated with each interrupt is called ISR.

Six interrupts of 8051


Ans. Reset
Two timer interrupts
One serial communications interrupts
Two external interrupts.

Priority of interrupts
Ans. IE0
TF0
IE1
TF1
RI or TI
TF2 or EXF2

Register used for enabling & disabling interrupts.


Ans. IE registers.

What is level triggered interrupt?


Ans. The interrupt is triggered when the signal is low at INT0 &
INT1.

What is edged triggered interrupt?


Ans. The interrupt is triggered when the signal makes a transition
from high to low level at INT0 & INT1.

What is subroutine?
Ans. A small program that is separately written and is being called
from the main program is called subroutine.

Instruction used in subroutine.


Ans. Call & Return instructions.

What are branching instruction?


Ans. The instruction that enables the execution of the instruction to
jump from one memory location to another location is called as
branching instructions.

Types of branching instructions.


Ans. Conditional & unconditional branching instructions.

What is Conditional branching instruction?


Ans. The Pc jumps to a location specified by the address in instruction
based on the condition is called as conditional branching instruction.
Eg. JNC, JC, JNZ

What is unconditional branching instruction?


Ans. The Pc jumps to a location specified by the address in instruction
without any condition is called as unconditional branching instruction.
Eg JMP.

Difference between LCALL & ACALL.


Ans. LCALL: 16-bit address
ACALL: 11- bit address

55. Difference between LJMP & SJMP


Ans. LJMP: within 64k bytes
SJMP: within –128 to +128 bytes

Type of communication used in 8051.


Ans. Serial communication

What is USART?
Ans. Universal synchronous – asynchronous receiver transmitter.

What is interfacing?
Ans. Connecting the MC to external devices is called as interfacing.

MC is connected to the external devices through IO ports.

8051 has 4 8-bit ports (P0, P1, P2, P3)

8255 is one of the most widely used I/O chip.

Connecting I/O chip to the CPU is called memory mapped I/O since
memory space is used to access I/O device
In I/O mapped I/O, I/O devices uses different address.

You might also like