Вы находитесь на странице: 1из 54

Microcontroller Lab Manual

Part A:
1. 8051 Instruction set summary.
2. Program to exchange the contents of the register.
3. Program to interchange the contents in the external memory.
4. Program to interchange the contents in two internal memory locations.
5. Program to transfer a block of data bytes in internal memory.
6. Program to transfer the block of data bytes in external memory.
7. Program to transfer the block of overlapping data.
8. Program to reverse the block of data in internal memory.
9. Program to interchange the block of data bytes in internal memory.
10. Program to add two 8 bit binary numbers in internal memory.
11. Program to add n 8 bit binary number in internal memory.
12. Program to add two n byte binary numbers in internal memory.
13. Program to add two 8 bit BCD numbers.
14. Program to add n 8 bit BCD numbers.
15. Program to add two n byte BCD numbers.
16. Program to perform 8 bit binary subtraction.
17. Program to perform 8 bit binary subtraction by 1s compliment method.
18. Program to perform 8 bit binary subtraction by 2s compliment method.
19. Program to perform 8 bit BCD subtraction by 9s compliment method.
20. Program to perform 8 bit BCD subtraction using 10s compliment method.
21. Program to perform two n byte binary subtraction.
22. Program to perform 8 bit binary multiplication using
a) Normal Method.
b) Successive Addition Method.
23. Program to perform 8 bit BCD multiplication.
24. Program to perform 16 bit binary multiplication.
25. Program to Perform 8 bit binary division using
a) Normal Method.
b) Successive Approximation Method.
26. Program to find GCD of two 8 bit numbers.
27. Program to copy the contents of A to all general purpose registers in bank2.
28. Program to find LCM of two bit numbers.
29. Program to find the square root of a given number.
30. Program to check whether the given byte is positive or negative.
31. Program to find the cube root of a given byte.
32. Program to check whether the given byte is odd or even.
33. Program to count number of 1s and 0s in a given byte.
34. Program to check whether the given byte is 2 out of 5 code.
35. Program to reverse a given byte nibble wise.
36. Program to reverse a 8 bit data bitwise.
37. Program to check whether the given byte is nibble wise palindrome.
38. Program to check whether the given data byte is bitwise palindrome or not.
39. Program to check whether the given data byte is largest number in an array.
40. Program to find the smallest number in an array.
41. Program to count odd and even number present in an array and group them in
separate array.
42. Program to count positive and negative numbers in an array and separate them
into two different arrays.
Dept ECE, PESITM

Page 1

Microcontroller Lab Manual


43. Program to find the nth smallest number in an array.
44. Program to find the nth largest number in an array.
45. Program to arrange given set of numbers in ascending order.
46. Program to arrange given set of numbers in descending order.
47. Program to convert 8 bit binary to BCD.
48. Program to convert 8 bit BCD to binary.
49. Program to generate Fibonacci series.
50. Program to generate prime numbers.
51. Program to check whether the given number is prime or not.
52. Program to implement 8 bit binary
a) Up counter
b) Down Counter
53. Program to implement 8 bit BCD
a) Up counter
b) Down counter
54. Program implement mod 6 counter.
55. Program to implement 16 bit binary up counter.
56. Program to implement 8 bit binary up down counter.
57. Program to implement 8 bit BCD up down counter.
58. Program to convert 1 digit binary number to ASCII equivalent.
59. Program to convert 8 bit ASCII code to its binary equivalent.
60. Program to convert 8 bit binary number to its ASCII equivalent.
61. Program to convert 16 bit ASCII code to its equivalent binary number in
packed format.
62. Program to convert 1 digit BDC number to its ASCII equivalent.
63. Program to convert 8 bit BCD to its ASCII equivalent.
64. Program to convert ASCII to its BCD equivalent.
65. Program to convert 16 bit ASCII to its BCD equivalent.
Part B
1. C Program to interface 8051 to DAC module to generate ramp waveform.
2. C Program to interface 8051 to DAC module to generate Triangular
waveform.
3. C Program to interface 8051 to DAC module to generate square waveform.
4. C Program to interface 8051 to DAC module to generate sine wave.
5. C Program to interface stepper motor to 8051.
6. C Program to interface 8051 for DC motor control
7. C Program to interface 8051 to elevator.
8. C Program to interface Alphanumeric LCD panel and Hex keypad input.
9. C Program to interface External ADC and Temperature Control to 8051.
10. Simple Calculator using 6 digit seven segment display and Hex KeyBoard
interface to 8051.

Dept ECE, PESITM

Page 2

Microcontroller Lab Manual


1. Program to store the data ABh in accumulator, copy the same to all general purpose
registers.
Label

Mnemonic
MOV A,#0ABh
MOV 0F0h,A
MOV R0,A
MOV R1,A
MOV R2,A
MOV R3,A
MOV R4,A
MOV R5,A
MOV R6,A
MOV R7,A
SJMP Loop

Loop:

Comments
Get data ABh to Accumulator.
Get data from A reg to B.
Copy data from Accumulator to all
registers from R0 to R7.

2. Program to exchange the contents of the registers.


Label

Mnemonic
MOV R0,#data1
MOV R1,#data2
MOV A,R0
XCH A,R1
MOV R0,A
SJMP Loop

Loop:

Comments
Get data1 to R0.
Get data2 to R1.
Get the data1 to accumulator.
Interchange contents of A and R1.
Copy the same to R0.

3. Program to interchange the contents of two external memory locations.


Label

Mnemonic
MOV DPTR,#16 BitAddr
MOVX A,@DPTR
MOV R0,A
INC DPTR
MOVX A,@DPTR
XCH A,R0
MOVX @DPTR,A

Loop:

Dept ECE, PESITM

DEC 82h
MOV A,R0
MOVX @DPTR,A
SJMP Loop

Comments
Point DPTR to the source addr.
Get the data from the addr to A reg.
Copy the data in A to reg R0.
Increment the source addr.
Get the 2nd data from the addr.
Exchange the contents of A and r0.
Store the data in A to the addr
pointed by DPTR
Decrement the lower byte of DPTR.
Move the data in R0 to A reg.
Copy the data in A reg to the addr
pointed by DPTR.

Page 3

Microcontroller Lab Manual

4. Program to exchange the contents of two internal data memory location.


Label

Mnemonic
MOV A,40h
XCH A,41h
MOV 40h,A
Loop:

Comments
Get the data to A register from
memory location 40h.
Interchange the contents in A and
41h location.
Copy the contents of 41 in A register
to 40h location.

SJMP Loop

5. Program to transfer a block of data bytes starting from the Memory location 30h to
another block starting from the Memory location 40h.
Label

Loop:

Mnemonic

Comments

MOV R0,#30h
MOV R1,#40h
MOV R2,#Count

Point R0 to the source block.


Point R1 to the destination block.
Count of the data bytes to be
transferred.
Get the 1st data into A register.
Move the 1st data in A to the
destination block.
Increment the source addr.
Increment the destination addr
Repeat the above process till R2 goes
to 0.

MOV A,@R0
MOV@R1,A
INC R0
INC R1
DJNZ R2, Loop

Loop1:

SJMP Loop1

6. Program to transfer block of data bytes starting from memory location 9000h to another
block starting from the memory location 9100h in external memory location.
Label

Mnemonic
MOV R2,#Count
MOV DPTR,#9100h

Loop:

MOV R0,82h
MOV R1,83h
MOV DPTR,#9000h
MOVX A,@DPTR
INC DPTR
PUSH 82h

Dept ECE, PESITM

Comments
Load the count of data bytes to be
transferred.
DPTR being pointed to the
destination address.
Copy the contents of the DPTR i.e
DPL and DPH into R0 and R1.
DPTR pointed to the source address.
Copy the 1st data from the source
address pointed by DPTR.
Increment the DPTR to point to the
next address in the source block.
Save the incremented source address
Page 4

Microcontroller Lab Manual


PUSH 83h
MOV 82h,R0
MOV 83h,R1
MOVX @DPTR,A
INC DPTR
MOV R0,82h
MOV R1,83h
POP 83h
POP 82 h
DJNZ R2, Loop
Loop1:

in DPTR on to the stack.


Load the lower byte of destination
address in DPL.
Load the higher byte of destination
address in DPH.
Copy the data in A to the address
pointed by DPTR.
Increment the DPTR to point to the
next address in the destination block.
Copy the contents of the DPTR into
R0 and R1 registers.
Retrieve the source address from the
stack to the DPTR.
Repeat the above process till all the
data bytes are transferred.

SJMP Loop1

7. Program to transfer a block of data to another block considering overlapping


blocks in
Internal memory.
Label
Mnemonic
Comments

Next :

MOV R0, SRC ADDRS


MOV R1,DEST ADDRS
MOV R2,#CNT
MOV A, R0
CJNE A,R1, Next
SJMP Stop
JC Bottom Trf

Back:

MOV A,@R0
MOV @R1,A

Stop:
Bottom Trf:

INC R0
INC R1
DJNZ R2, Back
LCALL 03
MOV A,R0
ADD A,R2
DEC A
MOV R0,A
MOV A,R1
ADD A,R2
DEC A

Back1:
Dept ECE, PESITM

MOV R1,A
MOV A,@R0

Point Ro to the source addr.


Point R1 to the destination addr.
Load the count of data bytes to R2.
Get the data in R0 to A reg.
Compare data in A and R1. if not
equal jump to next, else stop.
If theres a carry perform bottom
transfer, else top transfer.
Get the data from the addr pointed
by R0.
Copy the same data to the addr
pointed by R1.
Increment the addr pointed by R0.
Increment the addr pointed by R1.
Repeat the above process till all the
data bytes are transferred.
Move the data in R0 to A reg.
Add the contents of A and R2.
Decrement the content of A to get the
addr of the last data.
Move the content of A to reg R0.
Move the content of reg R1 to A.
Decrement the content of A to point
the last addr of the destination block.
Move the content of the A reg to R1.
Get the data from the addr pointed
by R1.
Page 5

Microcontroller Lab Manual


MOV @R1,A
DEC R0
DEC R1
DJNZ R2, Back1
SJMP Loop

Loop:

Move the same data to the addr


pointed by R1.
Decrement the addr of the source and
the destination block.
Repeat the above process till all the
data bytes are transferred

8. Program to reverse a block of data byte in internal memory.


Label

Mnemonic
MOV R2,#Count
MOV R0,# Source Addr
MOV A,R0
ADD A,R2
DEC A
MOV R1,A
MOV B,#02
MOV A,R2
DIV AB
MOV R3,A
Loop:

MOV A,@R0
XCH A,@R1
MOV @R0,A
INC R0
DEC R1
DJNZ R3, Loop
SJMP Loop1

Loop1:

Comments
Load the count of number of bytes.
Use R0 to point to the source addr.
Copy the source addr to A.
Add the data in A with reg R2.
Decrement the content in A to get the
addr of the last byte.
Copy the addr to reg R1..
Divide the count in the R2 register to
perform the reverse operation.
Copy the count of data bytes to be
reversed in R3 register.
Get the 1st data byte into A
Exchange the data byte present in A
and addr pointed by R1.
Copy the data in A to the addr
pointed by R0.
Increment the addr pointed by R0.
Decrement the addr pointed by R1.
Repeat the above process till all the
data bytes are transferred

9. Program to interchange the block of data bytes present in internal memory.


Label

Mnemonic

Loop:

Loop1:
Dept ECE, PESITM

MOV R2, #COUNT


MOV R0,# block1 ADDR
MOV R1,# block2 ADDR
MOV A,@R0
XCH A,@R1
MOV @R0,A
INC R0
INC R1
DJNZ R2, Loop
SJMP Loop1

Comments
Load the count of the data bytes.
Point R0 to source address
Point R1 to dest address
Get the 1st data byte from block1.
Exchange the 1st data bytes of both
the blocks.
Increment both R0 and R1 to point to
the address of the next data bytes.
Check whether the count is zero, if
not loop back else halt.
Page 6

Microcontroller Lab Manual

10. Program to add n 8 bit binary numbers present in the internal memory location.
Label

Mnemonic
MOV R0,#SOURCE ADDR
MOV R1,#COUNT
MOV 40h,#00
MOV 41h,#00
MOV A,@R0

Back:

ADD A,40h
MOV 40h,A
JNC Next
INC 41h
INC R0
DJNZ R1, Back
SJMP Loop

Next:
Loop:

Comments
Use register R0 to point to the source
addr.
Load the count of numbers in R1
Clear the addresses 40 and 41
Get the data from the addr pointed
by R0.
Add the contents of A with addr 40
Copy the result into addr 40
Check whether carry is generated. If
no, go ahead. Else increment 41.
Increment the addr pointed by R0.
Continue the above operation till the
count becomes zero and halt.

11. Program to add two 8 bit binary numbers present in internal memory location store the
Sum in next consecutive memory location.
Label

Mnemonic

Next:

Loop:

Dept ECE, PESITM

MOV R1,#00
MOV R0,#SRC ADDR
MOV A,@R0
INC R0
ADD A,@R0
JNC Next
INC R1
INC R0
MOV @R0,A
INC R0
MOV A,R1
MOV @R0,A
SJMP Loop

Comments
Clear the contents of register R1.
Point register R0 to the source addr.
Get the 1st number in Accumulator.
Increment the source address.
Add the 1st and the 2nd data byte.
Check whether there is a carry.
If theres a carry increment the R1.
Increment the source addr.
Copy the sum to the addrs pointed by
R0.
Increment the addr pointed by R0.
Get the carry in R1 to A register.
Store the carry in the next location.

Page 7

Microcontroller Lab Manual

12. Program to add two n byte numbers.


Label

Mnemonic
CLR C
MOV R2, # COUNT
MOV R3, # 00
MOV R0, #SRC ADDR
MOV R1, #DEST ADDR
MOV DPTR,#RSLT ADDR
Loop:

MOV A,@R0
ADDC ,@R1
MOVX @DPTR,A
INC R0
INC R1
INC DPTR
DJNZ R2, Loop
JNC Next
INC R3
MOV A,R3
MOVX @DPTR,A
SJMP Loop1

Next:
Loop1:

Comments
Clear the carry.
Load the count in R2.
Clear the register R3.
Use R0 to point to the source addr.
Use R1 to point to the dest addr.
Use DPTR to point to the addr where
result is stored.
Get the 1st byte in accumulator.
Add the 1st byte with carry.
Store the result in the addr pointed
by DPTR.
Increment the source address.
Increment the destination address.
Increment addrs in DPTR.
Check whether the count is zero if
not loop back else check for no carry.
If carry, increment R3.
Get the carry in A register.
Save the carry.

13. Program to add two 8 bit BCD numbers.


Label

Mnemonic
MOV R0,# SRC ADDR
MOV R1, #00
MOV A,@R0
INC R0
ADD A,@R0
DA A
JNC Next
MOV R2,A
MOV A,R1
ADD A,#01
DA A
MOV R1,A
MOV A,R2

Dept ECE, PESITM

Comments
Use the register to point to the source
location.
Clear the contents of register R1.
Get the data from src location.
Increment the source addr by 1
Add the data bytes present in A and
the address pointed R0.
Convert the result in A to its
equivalent decimal value.
Check whether there is a carry.
Copy the result in A to R2.
Copy the content of R1into A.
Add the accumulator content with
data.
Convert the result to its equivalent
decimal value.
Copy the result into R1.
Copy the content of R2 into A.
Page 8

Microcontroller Lab Manual


Next:

MOV 40h,A
MOV 41h,R1
SJMP Loop

Loop:

Copy the data in A to the addr 40.


Copy the carry in R1 to the addr 41.

14. Program to add n 8 bit BCD numbers.


Label

Mnemonic
MOV R2,# COUNT
CLR A
MOV R0,# SRC ADDR
MOV R1,# 00
ADD A,@R0

Back:

DA A
JNC Next
MOV R3,A
MOV A,R1
ADD A,#01
DA A
MOV R1,A
MOV A,R3
Next:
INC R0
DJNZ R2, Back
MOV 40h,A
MOV 41h,R1
Loop:
SJMP Loop
15. Program to two n byte BCD numbers.
Label

Mnemonic
MOV R2,# COUNT
CLR C
MOV R0,# SRC ADDR1
MOV R1,# SRC ADDR2
MOV DPTR,#9000

Loop:

MOV R3,# 00
MOV A,@R0
ADDC A,@R1
DA A
MOVX @DPTR,A
INC R0
INC R1

Dept ECE, PESITM

Comments
Load the count of number bytes in
R2.
Clear the contents of the A register.
Get the source addr in R0.
Clear the contents of R1.
Add the contents in A with the data
present in the address pointed by R0,
Convert the result to its equ decimal.
Check whether there is a carry.
Copy the content of A into R3 reg.
Clear the accumulator contents.
Add A reg with immediate value.
Convert the result to its equ decimal.
Copy the contents of the A to R1.
Copy the contents of R3 to A.
Increment the address pointed by R0.
Check whether the count is 0.
Save the contents of A in Addr 40.
Save the contents of R1 in Addr 41.

Comments
Load the count into the R2 register.
Clear the carry.
Point R0 to the source address1.
Point R1 to the source address2.
Load DPTR with the addr where
result is stored.
Clear the R3 register.
Get the 1st byte from the src addr1.
Add the content of A and the byte at
the R0 with carry.
Convert the result to its equ decimal.
Store the Lower byte of the result.
Increment the Src Addr1.
Increment the Src Addr2.
Page 9

Microcontroller Lab Manual


INC DPTR
DJNZ R2, Loop
JNC Next
INC R3
MOVX @DPTR, A
MOV A,R3
MOVX @DPTR,A
SJMP Loop1

Next:
Loop1:

Increment the result address.


Check whether the count is zero.
Check whether there is a carry.
Incement the content of R3.
Store the result.
Copy the contents of the R3 reg.
Store the higher byte.

16. Program to perform 8 bit binary subtraction.


Label

Mnemonic
CLR C
MOV R0, # SRC ADDR
MOV R1, #00
MOV A, @R0
INC R0
SUBB A,@R0

Next:

JNC Next
INC R1
INC R0

Loop:

MOV @R0, A
INC R0
MOV A,R1
MOV @R0,A
SJMP Loop

Comments
Clear the carry.
Get the source address into R0.
Clear the R1 register.
Get the 1st data into Accumulator.
Increment the source addr by 1.
Subtract with borrow the data
present in A with the data at the addr
Check for carry. If no go ahead.
Increment the reg R1 if carry.
Increment R0 to point the next
location.
Store the result.
Increment R0 to next address.
Copy the content of R1 to A.
Store the carry.

17. Program to perform 8 bit subtraction by 1s compliment method.


Label

Mnemonic
MOV R0, # SRC ADDR
MOV A,@R0
INC R0
CPL A
ADD A,@ R0
JNC Next.
ADD A, #01
INC R0
MOV @R0, A
SJMP Loop

Next :
Loop:

Comments
Point reg Ro to the source addr.
Get the 1st databye into A register.
Increment the R0 to next address.
Compliment the contents of A reg.
Add the content of A with the data
present in the addrs pointed by R0.
Check whether there is a carry.
I there is a carry add A with 1.
Increment the addr pointed by R0.
Store the result.

18. Program to perform 8 bit binary subtraction by 2s compliment method.


Label

Mnemonic
MOV R0, # SRC ADDR
MOV A, @R0

Dept ECE, PESITM

Comments
Use R0 to point to the source addr.
Get the data into the A register.
Page 10

Microcontroller Lab Manual


INC R0
CPL A
ADD A,# 01
ADD A,@R0

Increment R0 to point the next addr.


Compliment the contents of A.
Convert the content in A to 2s
compliment.
Add the content in A with the data at
the addr pointed by R0.
Increment the addr pointed by R0.
Store the result.

INC R0
MOV @R0, A
Loop:
SJMP Loop
19. Program to perform 8 bit BCD subtraction using 9s compliment method.
Label

Mnemonic
CLR C
MOV R0,# SRC ADDR
MOV A,# 99h
SUBB A,@R0
INC R0
ADD A,@R0
DA A
JNC Next
ADD A,# 01
DA A
INC R0
MOV @R0, A
SJMP Loop

Next:
Loop:

Comments
Clear the carry.
Point the register R0 to the src addr.
Move the immediate data to A reg.
Subtract with borrow the contents of
A and the data present at the addr.
Increment the contents of R0.
Add the contents of A with the data
present at the addr pointed by R0.
Convert the data to its decimal equ.
Check for carry. If no, go ahead else,
Add the contents of A with
immediate value.
Convert the result to its equ decimal.
Increment R0 to point the next addr.
Store the result.

20. Program to perform BCD subtraction using 10s compliment method.


Label

Mnemonic
MOV R0, # SRC ADDR
MOV A,# 99h
SUBB A, @R0
ADD A,# 01
DA A
INC R0
ADD A,@R0

Loop:

Dept ECE, PESITM

DA A
INC R0
MOV @R0, A
SJMP Loop

Comments
Point the register R0 to the src addr.
Get the immediate data to the A reg.
Subtract with borrow the contents of
A and the data at R0.
Add the immediate data to the
content of the A.
Convert the result to its decimal equ.
Increment the addr pointed by R0.
Add the A contents with the data at
R0.
Convert the result to its decimal equ.
Increment R0 point the next location.
Copy the result to the location.

Page 11

Microcontroller Lab Manual

21. Program to perform two n byte binary subtraction.


Label

Mnemonic
CLR C
MOV R0,# SRC ADDR1
MOV R1,# SRC ADDR2
MOV DPTR, # R ADDR
MOV R2,# COUNT
MOV R3,# 00
MOV A,@ R0
SUBB A,@ R1

Loop:

MOVX @DPTR, A
INC R0
INC R1
INC DPTR
DJNZ R2, Loop
JNC Next
INC R3
MOV A,R3
MOVX @DPTR, A
SJMP Loop1

Next:
Loop1:

Comments
Clear the carry.
Point R0 to the source address1.
Point R1 to the source address2.
Point DPTR to the result address.
Load the count of number of bytes.
Clear the contents of R3.
Get the 1st byte into A
Subtract with borrow the data in A
with the data present at R1.
Store the lower byte of the result.
Increment the addr pointed by R0
Increment the addr pointed by R1
Increment the result addr.
Check whether the count is zero if
not loop back.
Check for carry. If no go to next.
Increment R3 if there is a carry.
Copy the contents of R3 into A.
Copy the higher byte of the result.

22. Program to perform 8 bit binary multiplication using


a) Normal Method
Label

Mnemonic
MOV R0,#SRC ADDR
MOV A, @R0
INC R0
MOV 0F0h,@R0
MUL AB
INC R0
MOV @R0,A
INC R0

Dept ECE, PESITM

Comments
Point R0 to the source location.
Get the data into A from the addr
pointed by R0.
Increment R0 to point to the next
addr.
Get the next data to register B.
Perform multiplication.
Increment the addr pointed by R0.
Copy the lower byte of the result.
Increment the addr to point the next
location.
Page 12

Microcontroller Lab Manual


MOV @R0, 0F0h
SJMP Loop

Loop

Copy the higher byte of the result.

b) Successive addition method.


Label

Mnemonic
CLR A
MOV R0, 40h
MOV R1, 41h
MOV 0F0h, A
ADD A,R0

Loop:

JNC Next
INC 0F0h
DJNZ R1, Loop
MOV 42h, A

Next:

MOV 43h, 0F0h


SJMP Loop1

Loop1:

Comments
Clear the accumulator.
Get data to R0 from the addr
mentioned in the instruction.
Get data to R1 from the addr in the
instruction and use as the count.
Clear the B register contents.
Add the contents in A with that of
R0.
Check for carry. If no, jump next.
If there is a carry increment B reg.
Check whether count is zero.
Copy the result to the location in the
instruction.
Copy the carry to the location.

23. Program to perform 8 bit BCD multiplication.


Label

Mnemonic

Loop:

Dept ECE, PESITM

CLR A
MOV R0, 40h
MOV R1,41h
MOV 0F0h,A
ADD A,R0
DA A
XCH A,0F0h
ADDC A,#00
DA A
XCH A,0F0h
XCH A,R1
ADD A,# 99

Comments
Clear the accumulator.
Get the data1 into R0 from addr 40.
Get the data2 into R1 from addr 41.
Clear the register B.
Add the content in A with that in R0
and convert the result to equ decimal
Exchange the contents of A with B.
Add the content in A with carry and
convert the result to its equ decimal.
Exchange the contents of A and B.
Exchange the contents of A and R1.
Add the A reg with imm data 99.
Page 13

Microcontroller Lab Manual


DA A
XCH A,R1
CJNE R1,# 00, Loop
MOV 42h,A
MOV 43h,0F0h
SJMP Loop1

Loop1:

Convert the result to its equ decimal.


Exchange the contents of A and R1.
Compare the contents of R1 with 00
and loop bask if they are not equal.
Copy the A reg content to addr 42.
Copy the B reg content to addr 43.

24. Program to perform 16 bit binary multiplication.


Label

Mnemonic
MOV DPTR,# 9000h
MOV R4,# 00
MOV R0, #30h
MOV R1,# 40h
MOV A,@ R0
MOV 0F0h,@R1
MUL AB
MOVX @DPTR, A
MOV R2,0F0h
INC R1
MOV A,@R0
MOV 0F0h,@R1
MUL AB
ADD A,R2
MOV R2,A
MOV A,0F0h
ADDC A,#00
MOV R3,A
INC R0
DEC R1
MOV A,@R0
MOV 0F0h,@R1
MUL AB
ADD A,R2
INC DPTR
MOVX @DPTR,A
MOV A, 0F0h

Next:

Dept ECE, PESITM

ADDC A,R3
MOV R3,A
JNC Next
INC R4
INC R1
MOV A,@R0
MOV 0F0h,@R1
MUL AB

Comments
Use DPTR to point to the location
where output is stored.
Clear the content of reg R4.
Point reg R0 to the source addr1.
Point reg R1 to the source addr2
Get the data from source addr1.
Get the 2nd data from source addr2.
Multiply the data in A and B reg.
Copy the lower byte of the result in
the dest addr pointed by DPTR.
Copy the data in B reg to R2 reg.
Increment the source addr2.
Get the data from source addr1 to A.
Get the data from addr2 to the B reg.
Multiply the data in A and B reg.
Add carry in R2 to the A reg.
Copy the result in A reg to R2.
Move the data in B to A reg.
Add data 00 to A reg with Carry.
Copy the data in A to reg R3.
Increment the addr pointed by R0.
Decrement the addr pointed by R1.
Get the next data from the addr to A
Get the data from addr2 to B reg.
Multiply the data present in A and B.
Add the data in R2 to the data in A.
Increment the destination addr.
Copy the data in A to the dest addr.
Get the data present in B reg to A.
Add with carry the contents of A and
R3 reg.
Move the data present in A to R3 reg
If no Carry then jump to next.
Increment the content of R4 reg.
Increment the source addr2.
Get the data from addr1 to A reg.
Get the data from addr2 to B reg.
Multiply the data in A and B reg.
Page 14

Microcontroller Lab Manual


ADD A, R3
INC DPTR
MOVX DPTR,A
MOV A,0F0h
ADDC A, R4
INC DPTR
MOVX @DPTR, A
SJMP Loop

Loop:

Add the content of A and R3 reg.


Increment the dest addr.
Copy the data in A reg to the addr
pointed by DPTR.
Copy the data present in reg B to A.
Add the content of A and R4 with
carry.
Increment the addr pointed by DPTR
Store the carry in the addr.

25. Program to perform 8 bit binary division using.


a) Normal method
Label

Mnemonic
MOV R0, # 40h
MOV A,@ R0
INC R0
MOV 0F0h,@R0
DIV AB
INC R0
MOV @R0, A
INC R0
MOV @R0, 0F0h
SJMP Loop

Loop:

Comments
Point reg R0 to the source addr.
Get the data to the A register from
the source.
Increment content in reg R0 and get
the data to reg B from addr in R0.
Divided content in A and B and store
result in A and B.
Increment R0 and copy the
accumulator contents to the addr in
reg R0.
Increment R0 and copy the data from
reg B into the addr in reg R0.

b) Successive Subtraction Method.


Label

Mnemonic
CLR C
MOV R0,# 40h
MOV R1,#00h
MOV A,@R0

Back:

Next:
Dept ECE, PESITM

INC R0
SUBB A,@R0
JC Next
INC R1
SJMP Back
ADD A, @R0

Comments
Clear the carry.
Point R0 to point to the addr 40.
Clear the register R1.
Get the data to A reg from the addr
pointed by R0.
Increment the addr pointed by R0.
Subtract with borrow the content of
A reg and the data at the addr R0.
Check for carry. If yes jump next.
Increment the content of reg R1.
Add the content of A with the
Page 15

Microcontroller Lab Manual


MOV 42h,R1
MOV 43h,A
SJMP Loop

Loop:

content at the addr pointed by R0.


Copy the content in R1 to location
42.
Copy the content of A reg to 43.

26. Program to find GCD of two 8 bit numbers.


Label

Mnemonic
MOV R0,# 40h
MOV A,@ R0
Loop :
CJNE A,41h, Next
MOV 42h,A
SJMP Loop1
JC Next1
SUBB A,41h

Loop1:
Next :

MOV 40h,A
SJMP Loop
MOV A, 41h
CLR C
SUBB A,40h

Next1 :

MOV 41h,A
SJMP Loop

Comments
Point reg R0 to the source addr.
Get the data to the A reg from the
addr pointed by R0.
Compare the content of A and the
data at addr 41.
Copy the content in A to the addr 42.
Check for carry. If yes jump to next1
Subtract with borrow the content of
A and the data addr 41.
Copy the data in A to the addr 40
Get the data in the addr 41 to A reg.
Clear the carry.
Subtract with borrow the contents of
A and the data at addr 40.
Copy the contents of A reg to addr
41.

27. Program to copy the contents of A to all the general purpose registers in Bank2.
Label

Mnemonic
MOV A,# 0ABh
CLR 0D3h
SETB 0D0h.4
MOV R0,A
MOV R1,A
MOV R2,A
MOV R3,A
MOV R4,A
MOV R5,A
MOV R6,A
MOV R7,A
SJMP Loop

Loop:

Comments
Get the data AB to Accumulator.
Copy the same to registers R0-R7
present in Bank2.

28. Program to find the LCM of two 8 bit numbers.


Label

Mnemonic

Dept ECE, PESITM

Comments
Page 16

Microcontroller Lab Manual


MOV 50h,40h
MOV 51h,41h
MOV R0, #50h
MOV A,@R0

Loop :

CJNE A,51h, Next


Loop1:
Next :

SJMP Loop1
JC Next1
MOV A,51h
ADD A,41h
MOV 51h,A
SJMP Loop
MOV A,50h
ADD A,40h

Next1 :

MOV 50h,A
SJMP Loop
29. Program to find the square root of a given number.
Label

Mnemonic
MOV R0, # 40h
MOV 41h, # 00
LCALL SQR

Back :

CJNE A, 40h, Next


Stop :
Loop:
Next :

MOV 41h,A
SJMP Loop
JNC Stop
INC 41h
SJMP Back

SQR:
MOV A, @R0
MOV 0F0h, @R0
MUL AB
RET

Get the data from the memory


location 40 and 41h to 50 and 51h.
Point reg R0 to the source addr.
Get the data to A reg from the addr
pointed by R0.
Compare whether the data present in
A and addr 51. If not equal jmp.
Check for carry and jump if yes.
Copy the data at addr 51 to A reg.
Add the data in A with data in 41.
Copy the content of A to loc 51.
Copy the data in loc 50 to A reg.
Add the data in A reg with the data in
loc 40
Copy the result in A reg to the loc 50

Comments
Point R0 to the source addr.
Clear the content of addr loc 41..
Call the subroutine to get square of a
given number.
Compare the content of A and addr
loc 40. if not equal jump.
Copy the content of the A reg to 41.
Check for the carry. If no stop else
increment the content of addr 41.
Get the data into A reg from the
location pointed by R0.
Copy the data from the addr loc
pointed by R0 to the B reg.
Multiply the contents of A and B.
Return to the main prog.

30. Program to check whether the given byte is positive or negative.


Label

Mnemonic

Dept ECE, PESITM

Comments

Page 17

Microcontroller Lab Manual


MOV R1,# 00
MOV R0,# 40h
MOV A,@R0

Next :

JNB 0 E7h, Next


DEC R1
INC R0
MOV A,R1
MOV @R0, A

Loop:

SJMP Loop

Clear the contents of reg R1.


Point the reg R2 to the source addr.
Get the data to the A reg from the
addr pointed by R0.
Decrement the content of reg R1.
Increment the content of Reg R0.
Get the content of R1 to the A reg.
Copy the content of the A reg to the
location pointed by R0.

31. Program to find the cube root of a given number.


Label

Mnemonic
MOV R0,# 40h
INC R0
LCALL Cube
CJNE A,40h, Next
MOV 43h,41h
SJMP Stop
JNC Stop
INC 41h
SJMP Back
MOV A,@R0

Back :
Stop :
Next :
Cube :

MOV 0F0h,@R0
MUL AB
MUL 0F0h,@ R0
MUL AB
RET

Comments
Point reg R0 to the source location.
Increment the memory addr in R0.
Calling the sub programme cube.
Get the data from 41h loc to 43h.
Check for carry. If no jump to stop.
Increment the content in 41h loc.
Get the content of memory location
pointed by R0 into A.
Copy the same content to reg B.
Multiply the content of reg A and B
Copy the data from loc pointed by
R0.
Perform the multiplication again.
Return to the calling prog.

32. Program to check whether the given byte is odd or even.


Label

Mnemonic
MOV R1, # 00
MOV R0,# 40h
MOV A,@R0
JNB 0E0h, Next
DEC R1
Next :

Dept ECE, PESITM

INC R0
MOV A,R1
MOV @R0,A

Comments
Clear the register R1.
Point reg R0 to the source addr.
Get the data from the addr pointed
by R0.
Check the LSB. If not set jump.
If LSB is set decrement R1 contents.
If bit not set increment the contents
of R0.
Copy the content of R1 into A reg.
Copy reg A contents to the location
pointed by R0.
Page 18

Microcontroller Lab Manual


Loop:

SJMP Loop

33. Program to count the number of 1s and 0s in a given byte.


Label

Mnemonic
MOV R0,#40h
MOV R1,#00
MOV R2,#00
MOV R3,#08
MOV A,@R0
Loop:

RLC A
JNC Next
INC R2

Next:
Next1:

SJMP Next1
INC R1
DJNZ R3, Loop

Loop1:

INC R0
MOV 41h,R2
MOV 42h,R1
SJMP Loop1

Comments
Point R0 to the source register.
Clear the contents of register R1 and
R2.
Load the count to the register R3.
Get the data from the addr pointed
R0.
Rotate the contents of A reg left
through carry.
Check for carry. If no go ahead.
If theres a carry increment the
contents of R2.
Jump to the location next1.
Increment the contents of the R1 reg.
Decrement the R3 register and check
whether it is zero.
Increment the contents of R3 reg.
Copy the contents of register R1 and
R2 to the location 41 and 42h.

34. Program to check whether the given byte is 2 out of 5 code.


Label

Mnemonic
MOV R1,#00
MOV R0,#40h
MOV R2,#05
MOV A,@R0
ANL A,#0E0h
JNZ Invalid
MOV A,@R0
Loop:

Dept ECE, PESITM

RRC A

Comments
Clear the contents of register R1.
Point R0 to the source addr.
Load the count in register R0.
Get the data into A register from the
addr pointed by R0.
And the contents of A reg with the
imm value.
If the result is not zero then the given
byte is not a 2 out of 5 code.
Get the data into A reg from addr
pointed by R0.
Rotate the data in accumulator right
through carry.
Page 19

Microcontroller Lab Manual


JNC Next
INC R1
DJNZ R2, Loop

Next:

CJNE R1,#02, Invalid


MOV 41h,#00
LCALL 03
MOV 41h,#0FFh
SJMP Loop1

Invalid:
Loop1:

If carry bit is not set go to next.


If there is a carry increment the R1.
Decrement the count and check for
zero.
Compare the content of R1 with 02.
if its not equal then its not a valid
code.
If the code is valid move 00 to the
location 41.
If the code is invalid move FF.

35. Program to reverse a given byte nibble wise.


Label

Mnemonic
MOV R0,#40h
MOV A,@R0
SWAP A
INC R0
MOV @R0,A
Loop:

Comments
Point reg R0 to the source addr.
Get the data from the addr pointed
by R0.
Swap the lower and higher nibbles of
the data in A reg.
Point R0 to the next location.
Copy the content of Reg A to the
location pointed by R0.

SJMP Loop

36. Program to reverse a 8 bit data block.


Label

Mnemonic

Loop:

MOV R0,#40h
MOV R1,#00
MOV R2,#08
MOV A,@R0
RLC A
MOV R3,A
MOV A,R1
RRC A
MOV R1,A
MOV A,R3
DJNZ R2,Loop

Dept ECE, PESITM

Comments
Point register R0 to the source addr.
Clear the contents of register R1.
Load the count in the Reg R2.
Get the data frm addr pointed by R0
Rotate left through carry the contents
of A reg.
Copy the content of A into reg R3.
Clear the Accumulator.
Rotate the contents of A reg right
through carry.
Copy the content of A reg to R1.
Get back the original data from reg
R3 into A reg.
Decrement the count and check
Page 20

Microcontroller Lab Manual


INC R0
MOV A,R1
MOV @R0,A
SJMP Loop1

Loop1:

whether it has reached.


Increment the source addr.
Copy the content of reg R1 to A.
Copy the contents of A reg to the
location pointed by R0.

37. Program to check whether the given byte is nibble wise palindrome.
Label

Mnemonic
MOV R0,#40h
MOV A,@R0
SWAP A
CJNE A,40h,Notpal
INC R0
MOV @R0,#00
SJMP Loop

Loop:
Notpal:

INC R0
MOV @R0,#0FFh
SJMP Loop

Loop:

Comments
Point R0 to the source addr.
Get the data from the location
pointed by R0.
Swap the lower and higher nibbles of
the Accumulator.
Compare the data in A reg with the
data present in addr 40.
Increment the addr pointed by R0.
Move 00 to the addr pointed by R0
to indicate the given data is nibble
wise palindrome.
Move FF to the addr if its not a
palindrome.

38. Program to check whether the given data byte is bit wise palindrome or not.
Label

Mnemonic
MOV R0,#40h
MOV R1,#00
MOV R2,#08
MOV A,@R0
Loop:

RLC A
MOV R3,A
MOV A,R1
RRC A
MOV R1,A
MOV A,R3
DJNZ R2, Loop
MOV A,R1
CJNE A,40h,Not pal
INC R0

Dept ECE, PESITM

Comments
Point R0 to the source addr.
Clear the contents of the reg R1.
Load reg R2 with the count.
Get the data from the addr pointed
by R0.
Rotate the content of the reg A left
through carry.
Copy the data in A reg to R3.
Clear the content of the A reg.
Rotate right through carry the
content of the A reg.
Copy the data present in A to R1.
Get the data present in R3 back to
the A reg.
Check whether count is zero. Else
loop back.
Get the data in R1 back A reg.
Compare the data present in A with
that present addr 40h. if they are
equal the num is palindrome.
Page 21

Microcontroller Lab Manual


Loop1:

MOV @R0,#00
SJMP Loop1

Not pal:

INC R0
MOV @R0,#0FFh
SJMP Not pal

Increment the addr pointed by R0.


Store 00 in the addr if the num is
palindrome.
Else store FF if the number is not
palindrome.

39. Program to find the largest number in an array.


Label

Mnemonic
MOV R0,#40h
MOV R1,#(N-1)
MOV A,@R0
Loop:

INC R0
MOV 50h,@R0
CJNE A,50h, Next

Next:

SJMP Next1
JNC Next1

Next1:

MOV A,@R0
DJNZ R1,Loop

Loop1:

MOV 50h,A
SJMP Loop1

40. Program to find the smallest number in an array.


Label
Mnemonic
MOV R0,#40h
MOV R1,#(N-1)
MOV A, @R0
Loop:

INC R0
MOV 50h,@R0

Next:

Dept ECE, PESITM

CJNE A,50h, Next


SJMP Next1
JC Next1

Comments
Point register R0 to the source addr.
Load the count in the register R1.
Get the data from the addr pointed
by R0.
Point R0 to the next addr.
Copy the data present at the addr
pointed by R0 to location 50h.
Compare the 1st and 2nd data. If they
are not equal jump to next.
Check whether there is a carry. If no,
jump to next1
Get the largest number in the A reg.
Decrement the count and loop back
till it goes to zero.
Store the largest number in the
location 50h.

Comments
Point reg R0 to the source addr.
Load the count in the register R1.
Get the data from the addr pointed
by R0.
Increment the pointer to the next
addr.
Copy the data at the addr pointed by
R0 to location 50h.
Compare the 1st and 2nd data. If they
are not equal jump to next.
Check whether there is a carry. If
yes, jump to next1
Page 22

Microcontroller Lab Manual


MOV A,@R0
DJNZ R1, Loop
MOV 50h,A
SJMP Loop1

Next1:
Loop1:

Get the smallest number in the A.


Decrement the count and loop back
Store the smallest number in the
location 50h.

41. Program to count odd and even numbers in an array and group them in separate array.
Label

Mnemonic

Comments

MOV DPTR,#Addr
MOV R0,#20h
MOV R1,#30h
MOV R2,# N
MOV R3,#00
MOV R4,#00
MOVX A,@DPTR

Point DPTR to the source addr.


Point registers R0 and R1 to the
destination addr.
Load the count in the register
Clear the contents of the registers R3
and R4.
Load:
Get the data from into A from the
location pointed by DPTR.
JB 0E0h, Odd Cnt
If bit E0 is set jump to increment the
odd count.
INC R4
Else increment the even count.
MOV @R1,A
Store the even number.
INC R1
Increment the addr pointed by R1.
SJMP Next
Jump to the addr in the instruction.
Odd Cnt:
INC R3
Increment the odd count.
MOV @R0,A
Store the odd number.
INC R0
Increment the addr pointed by R0.
Next:
INC DPTR
Increment the source address.
DJNZ R2, Loop
Decrement the count and loop back
if count is not zero.
MOV A,R3
Get the count of odd numbers in A.
MOVX @DPTR,A
Store the count in the addr pointed
by DPTR.
INC DPTR
Increment the source addr.
MOV A,R4
Get the even count in reg A.
MOVX @DPTR,A
Store the count in the addr pointed
Loop:
SJMP Loop
by DPTR
42. Program to count positive and negative numbers in an array and separate them into two
different arrays.
Label

Mnemonic

Load:

MOV DPTR,#Addr
MOV R0,#20h
MOV R1,#30h
MOV R2,# N
MOV R3,#00
MOV R4,#00
MOVX A,@DPTR
JB 0E7h, Neg Cnt

Dept ECE, PESITM

Comments
Point DPTR to the source addr.
Point registers R0 and R1 to the
destination addr.
Load the count in the register
Clear the contents of the registers R3
and R4.
Get the data from into A from the
location pointed by DPTR.
If bit E0 is set jump to increment the
negative count.
Page 23

Microcontroller Lab Manual


INC R4
MOV @R1,A
INC R1
SJMP Next
INC R3
MOV @R0,A
INC R0
INC DPTR
DJNZ R2, Loop

Neg Cnt:
Next:

MOV A,R3
MOVX @DPTR,A
INC DPTR
MOV A,R4
MOVX @DPTR,A
SJMP Loop

Loop:

Else increment the positive count.


Store the positive number.
Increment the addr pointed by R1.
Jump to the addr in the instruction.
Increment the negative count.
Store the negative number.
Increment the addr pointed by R0.
Increment the source address.
Decrement the count and loop back
if count is not zero.
Get the count of negative numbers.
Store the negative count in the addr
Increment the source addr.
Get the positive count in reg A.
Store the count in the addr pointed
by DPTR

43. Program to find the nth smallest number in an array.


Label

Mnemonic
MOV A,#00
MOV R2,#N
MOV R0,#40h
MOV R3,#N1
MOV 50h,@R0

Again:
Loop:

CJNE A,50h, Next


DEC R2
MOV @R0,#0FFh
CJNE R2,#00, Again
SJMP Last
INC R0
DJNZ R3, Loop

Next:

INC A
SJMP Again
MOV 50h,A
SJMP Loop

Last:
Loop:

Comments
Clear the contents of the A reg.
Get the value of n in reg R2.
Point reg R0 to the source addr.
Load the count of data bytes in R3.
Get the data into addr 50h from the
addr pointed by R0.
Compare the data in A with the data
addr 50h. if not equal jump.
Decrement the content of register R2
Store FF in the addr pointed by R0.
Check whether the data in R2 is 00.
if not go back.
if R2 is 00 then jump to last.
Increment the content of R0.
Decrement the count and check
whether it has reached 00. if no, loop
Increment the contents of the A reg.
Store the contents of the A reg in the
location 50h.

44. Program to find the nth largest number in an array.


Label

Mnemonic

Dept ECE, PESITM

Comments

Page 24

Microcontroller Lab Manual


MOV A,#00
MOV R2,#N
MOV R0,#40h
MOV R3,#N1
MOV 50,@R0

Again:
Loop:

CJNE A,50h, Next


DEC R2
MOV @R0,#00
CJNE R2,#00, Again
SJMP Last
INC R0
DJNZ R3, Loop

Next:

DEC A
SJMP Again
MOV 50h,A
SJMP Loop1

Last:
Loop1:

Get the immediate data into A reg..


Get the value of n in reg R2.
Point reg R0 to the source addr.
Load the count of data bytes in R3.
Get the data into addr 50h from the
addr pointed by R0.
Compare the data in A with the data
addr 50h. if not equal jump.
Decrement the content of register R2
Store FF in the addr pointed by R0.
Check whether the data in R2 is 00.
if not go back.
if R2 is 00 then jump to last.
Increment the content of R0.
Decrement the count and check
whether it has reached 00. if no, loop
Increment the contents of the A reg.
Store the contents of the A reg in the
location 50h.

45. Program to arrange given set of numbers in ascending order.


Label

Mnemonic
MOV R2,#(N-1)
Loop1:

MOV R0,#40
MOV R3,#(N-1)

Loop:

MOV A,@R0
INC R0
MOV 50h,@R0
CJNE A,50h, Next

Next:

SJMP NCHNG
JC NCHNG
MOV @R0,A

NCHNG:

DEC R0
MOV @R0,50h
INC R0
DJNZ R3, Loop

Loop2:

DJNZ R2, Loop1


SJMP Loop2

Dept ECE, PESITM

Comments
Load the count of number of
iterations in R2.
Point reg R0 to the source addr.
Load the number of comparison in
R3.
Get the data into A from the addr
pointed by R0.
Increment the addr pointed by R0.
Copy the data form addr pointed by
R0 to the location 50h.
Compare the data in A with the data
in addr 50h. if not equal jump.
If they are equal, jump to NCHNG
If there is a carry, jump to NCHNG
Copy the data from the reg A to the
location pointed by R0.
Decrement the addr pointed by R0,
and copy the data at loc 50h to it.
Increment the addr.
Decrement the count of number of
comparisons and loop back if its not
zero.
Decrement the count of number of
iterations; continue till it goes to 0.
Page 25

Microcontroller Lab Manual


46. Program to arrange the given set of numbers in descending order.
Label

Mnemonic
MOV R2,#(N-1)

Loop1:

MOV R0,#40h
MOV R3,#(N-1)

Loop:

MOV A,@R0
INC R0
MOV 50h,@R0
CJNE A,50h, Next
SJMP NCHNG
JC NCHNG
MOV @R0,A

Next:

NCHNG:

DEC R0
MOV @R0,50h
INC R0
DJNZ R3, Loop

Loop2:

DJNZ R2, Loop1


SJMP Loop2

Comments
Load the count of number of
iterations in R2.
Point reg R0 to the source addr.
Load the number of comparison in
R3.
Get the data into A from the addr
pointed by R0.
Increment the addr pointed by R0.
Copy the data form addr pointed by
R0 to the location 50h.
Compare the data in A with the data
in addr 50h. if not equal jump.
If they are equal, jump to NCHNG
If there is a carry, jump to NCHNG
Copy the data from the reg A to the
location pointed by R0.
Decrement the addr pointed by R0,
and copy the data at loc 50h to it.
Increment the addr.
Repeat the above process till 3 goes
to zero.
Decrement the count of number of
iterations; continue till it goes to 0.

47. Program to convert 8 bit binary to BCD.


Label

Mnemonic
MOV A,40H
MOV 0F0h,#0A
DIV AB
MOV 41h,0F0h
MOV 0F0h,#0Ah
DIV AB
MOV 42h,A
MOV A,0F0h
SWAP A
ADD A,41h
MOV 41h,A
SJMP Loop

Loop

Comments
Get the data into A reg from 40h.
Get the immediate data to B reg.
Divide the content in A by B reg.
Move the content of B reg to 41h.
Get the immediate data to B reg.
Divide the content in A by B reg.
Get the data into 42h from A reg.
Get the content of B reg into A.
Add the content of A and location 41
and copy the result to 41h location.

48. Program to generate Fibonacci numbers.


Label

Mnemonic

Dept ECE, PESITM

Comments

Page 26

Microcontroller Lab Manual


MOV R0,#40h
MOV R1,#41h
MOV R2,#(n-2)
MOV 40h,#00
MOV 41h,#01
MOV A,@R0

Loop:

ADD A,@R1
INC R0
INC R1
MOV @R1,A
DJNZ R2, Loop
SJMP Loop1

Loop1:

Point reg R0 to the source/dest addr.


Point reg R1 to the source/dest addr.
Load the count into reg R2.
Clear the content of location 40h.
Get the immediate data to 41h addr.
Get the data from the addr pointed
by R0 to A reg.
Add the data in A reg with the data
in the addr pointed by R1.
Increment the addr pointed by R0
and R1.
Move the data in A reg to the addr
pointed by R1.
Repeat the above process till count
drops to 0.

49. Program to convert 8 bit BCD to binary.


Label

Mnemonic
MOV R0,#40h
MOV A,@R0
ANL A,#0Fh
MOV R1,A
MOV A,@R0
ANL A,#0E0h

SWAP A
MOV R2,A
CJNE R2,#00, Next
MOV 41h,R1
SJMP Stop
Next:
MOV A,#00
Back:
ADD A,#0Ah
DJNZ R2, Back
ADD A,R1
MOV 41h,A
Stop:
SJMP Stop
50. Program to generate prime numbers.
Label

Mnemonic
MOV R2,# N
MOV R0,#40h
MOV @R0,#02

Dept ECE, PESITM

Comments
Point reg R0 to the source addr.
Get the data into A from the addr
pointed by R0.
AND A with immediate data 0F
Get the result in A to the reg R1.
Get the data into A reg from the addr
pointed by R0.
AND the contents of the A reg with
the immediate data.
Swap the nibbles of the A reg.
Move the content of the A reg to R2.
Check if R2 is 0. if not, jump
Move the content of R1 to addr 41h.
Jump to label Stop.
Clear the contents of the A reg.
Add immediate data 0A to A reg.
Repeat the above process till R2
drops to 0.
Copy the contents of A reg to the
location 41h.
Comments
Load the count into register R2.
Point Reg R0 to the source addr.
Move data 02 to the addr pointed by
R0.
Page 27

Microcontroller Lab Manual


DJNZ R2, Next
SJMP Stop
INC R0
MOV @R0,#03
DJNZ R2, Next1
SJMP Stop
MOV 50h,#04
MOV A,50h
MOV R1,#02
MOV 0F0h,R1
DIV AB
MOV 51h,A
INC 51h
MOV A,0F0h
CJNE A,#00, Loop

Next:

Next1:

SJMP Next2
MOV A,50h
INC R1
MOV 0F0h,R1
DIV AB
MOV A,0F0h
CJNE A,#00, Next3
Next2:

INC 50h
SJMP Nextno
MOV A,R1
CJNE A,51h,Loop

Next3:

INC R0
MOV A,50h
MOV @R0,A
INC 50h
DJNZ R2,Nextno
SJMP Stop

Stop:

Decrement the content of reg R2. If


its not zero take a jump to Next.
If R2 is 00, then stop.
Increment the addr pointed by R0.
Move data 03 to the addr.
Decrement content of reg R2. If its
not zero take a jump to Next1.
Move data 04 to the location 50h.
Copy the same into A reg.
Move data 02 to the reg R2.
Copy the same data to B reg.
Divide the content of A by B reg.
Move the quotient in A to 51h.
Increment the contents of 51h
Copy the content of reg B to reg A.
Check if content of A is 0. if not,
loop.
If content of A is 00, then take a
jump.
Get the data from the location 50h
into A reg.
Increment the content of reg R1
Move the content of reg R1 to B reg.
Divide the content of A by B reg.
Copy the remainder in reg B to A.
Check if content of A is 0. if not,
jump to Next3.
Increment the content of addr 50h.
Get the content of reg R1 to the A.
Compare the data in A and location
51h. If they arent equal, loop
Increment the content of reg R0.
Copy the content of loc 50h to A.
Copy the same to the addr pointed
by R0.
Increment the content of location
50h.
Repeat the above process till R2
goes to zero.

51. Program to check whether the given byte is prime or not.


Label

Mnemonic
MOV R0,#40h
MOV A,@R0
CJNE A,#00, Next

Dept ECE, PESITM

Comments
Point reg R0 to the source addr.
Get data to the A reg from the addr
pointed by R0.
Compare whether the content of A
Page 28

Microcontroller Lab Manual


SJMP Illegal
CJNE A,#01,Next1
SJMP Illegal

Next:
Next1:

CJNE A,#02,Next2
SJMP Prime

Next2:

CJNE A,#03,Next3
SJMP Prime

Next3:

CJNE A,#04, Next4


SJMP Not Prime

Next4:

MOV R1,#02
MOV 0F0h,R1
DIV AB
MOV 41h,A
INC 41h
MOV A,@R0
INC R1
MOV 0F0h,R1
DIV AB
MOV A,0F0h
CJNE A,#00,Next5
SJMP Not Prime

Next5:

MOV A,R1
CJNE A,41h,Loop

Prime:
Loop2:
Not Prime:

MOV 42h,#00
SJMP Loop2
MOV 42,#0FFh
SJMP Loop1
MOV 42,#11
SJMP Loop1

Illegal:
Loop1:

reg is equal to 00. if not, go next.


If equal, then num is illegal.
Compare the data in A reg with 01.
If not equal, go next1 else num is
illegal.
Compare the data in A with 02. If
equal the num is prime else go
next2.
Compare the data in A with 03. If
equal the num is prime else go
next3.
Compare the data in A with 04. If
equal the num is not prime else go
next4.
Move data 02 to reg R1.
Copy the same to register B.
Divide the content of A by B.
Move the data in A to loc 41h.
Increment the contents of loc 41h.
Get the data from the addr pointed
by R0 to A reg.
Increment the content of reg R1.
Copy the data in reg R1 to Reg B.
Divide the content of A by B.
Copy the data in B to reg A.
Compare the content of A with 00. If
not equal go to next5. Else the num
is not prime.
Copy the data in R1 to the A reg.
Compare the data in A reg with that
of 41. If not equal, loop back else.
Move data 00h to the location to
indicate the number is prime.
Move data FFh to indicate the
number is not prime.
Move data 11h to indicate the
number is invalid.

52. Program to implement 8 bit binary up counter.


Label

Mnemonic

Loop:

Dept ECE, PESITM

MOV R6,#00
LCALL Delay
INC R6

Comments
Clear the content of reg R6.
Call delay Subroutine.
Increment the content of R6.
Page 29

Microcontroller Lab Manual


CJNE R6,#00,Loop
SJMP Loop1
MOV R3,#0FFh
MOV R2,#0FFh
DJNZ R2,Here
DJNZ R3,Back
RET

Loop1:
Delay:
Back:
Here:

Compare data in R6 with 00. if not


equal loop back, else stop.
Move data FF to the reg R3.
Move data FF to reg R2.
Decrement R2 till it reaches zero.
Decrement R3 till it reaches zero.

53. Program to implement 8 bit binary down counter.


Label

Mnemonic
MOV R6,#0FFh
LCALL Delay
DEC R6
CJNE R6,#0FFh,Loop
SJMP Loop1
MOV R3,#0FFh
MOV R2,#0FFh
DJNZ R2,Here

Loop:

Loop1:
Delay:
Back:
Here:

DJNZ R3,Back
RET

Comments
Load the register R6 with FFh..
Call delay Subroutine.
Decrement the content of reg R6.
Compare content of R6 with FF. If
not equal loop back else stop.
Load R3 with data FFh
Load R2 with data FFh
Decrement the content of R2 till it
reaches zero.
Decrement the content of R3, if not
zero go back and load R2

54. Program to implement 8 bit BCD up counter.


Label

Mnemonic
MOV R6,#00
LCALL Delay
MOV A,R6
ADD A,#01
DA A

Loop:

MOV R6,A
CJNE R6,#00,Loop
SJMP Loop1
MOV R3,#0FFh
MOV R2,#0FFh
DJNZ R2,Here

Loop1:
Delay:
Back:
Here:

DJNZ R3,Back
RET

Comments
Clear the contents of register R6.
Call delay Subroutine.
Get the data in reg R6 to A reg.
Add 01 to the content of A reg.
Convert the data in A to its decimal
equivalent.
Copy the data in A reg ot R6 reg.
Compare content of R6 with 00. If
not equal loop back. Else stop.
Load R3 with data FFh
Load R2 with data FFh
Decrement the content of R2 till it
reaches zero.
Decrement the content of R3, if not
zero go back and load R2

55. Program to implement 8 bit BCD down counter.


Label

Mnemonic

Loop:
Dept ECE, PESITM

MOV R6,#99h
LCALL Delay

Comments
Clear the contents of register R6.
Call delay Subroutine.
Page 30

Microcontroller Lab Manual


MOV A,R6
ADD A,#99h
DA A
MOV R6,A
CJNE R6,#99h,Loop
SJMP Loop1
MOV R3,#0FFh
MOV R2,#0FFh
DJNZ R2,Here

Loop1:
Delay:
Back:
Here:

DJNZ R3,Back
RET

Get the data in reg R6 to A reg.


Add 01 to the content of A reg.
Convert the data in A to its decimal
equivalent.
Copy the data in A reg ot R6 reg.
Compare content of R6 with 00. If
not equal loop back. Else stop.
Load R3 with data FFh
Load R2 with data FFh
Decrement the content of R2 till it
reaches zero.
Decrement the content of R3, if not
zero go back and load R2

56. Program to implement mod6 counter.


Label

Mnemonic
MOV R6,#00
LCALL Delay
INC R6
CJNE R6,#06,Loop

Loop:

SJMP Loop1
MOV R3,#0FFh
MOV R2,#00
DJNZ R2,Here

Loop1:
Delay:
Back:
Here:

DJNZ R3,Back
RET

Comments
Clear the contents of register R6.
Call Delay Subroutine.
Increment the contents of reg R6.
Check whether content of R6 is
equal to 06. If not loop back else
stop.
Load R3 with data FFh
Load R2 with data FFh
Decrement the content of R2 till it
reaches zero.
Decrement the content of R3, if not
zero go back and load R2

57. Program to implement 16 bit binary up counter.


Label

Mnemonic
MOV R6,#00
MOV R7,#00

Dept ECE, PESITM

Comments
Clear the contents of register R6 and
R7.
Page 31

Microcontroller Lab Manual


Loop:

LCALL Delay
INC R6
CJNE R6,#00,Loop
INC R7
CJNE R7,#00,Loop
SJMP Loop1
MOV R3,#0FFh
MOV R2,#0FFh
DJNZ R2,Here

Loop1:
Delay:
Back:
Here:

DJNZ R3,Back
RET

Call Delay Subroutine.


Increment the content of reg R6.
Check whether content of R6 is
equal to 00. If not loop back.
Else increment the contents of R7.
Check whether content of R6 is
equal to 00. If not loop back.
Else stop.
Load R3 with data FFh
Load R2 with data FFh
Decrement the content of R2 till it
reaches zero.
Decrement the content of R3, if not
zero go back and load R2

58. Program to implement 8 bit binary up-down Counter.


Label

Mnemonic
MOV R6,#00
LCALL Delay
INC R6
CJNE R6,#00,Loop1

Loop1:

MOV R6,#0FFh
Loop2:

LCALL Delay
DEC R6
CJNE R6,#0FFh,Loop2
SJMP Loop3
MOV R3,#0FFh
MOV R2,#0FFh
DJNZ R2,Here

Loop3:
Delay:
Back:
Here:

DJNZ R3,Back
RET

Comments
Clear the content of reg R6.
Call delay Subroutine.
Increment the content of R6.
Compare data in R6 with 00. if not
equal loop back,
Load the register R6 with FFh..
Call delay Subroutine.
Decrement the content of reg R6.
Compare content of R6 with FF. If
not equal loop back else stop.
Load R3 with data FFh
Load R2 with data FFh
Decrement the content of R2 till it
reaches zero.
Decrement the content of R3, if not
zero go back and load R2

59. Program to implement 8 bit BCD up-down Counter.


Label

Mnemonic

Dept ECE, PESITM

Comments

Page 32

Microcontroller Lab Manual

Loop:

MOV R6,#00

Clear the contents of register R6.

LCALL Delay
MOV A,R6
ADD A,#01
DA A

Call delay Subroutine.


Get the data in reg R6 to A reg.
Add 01 to the content of A reg.
Convert the data in A to its decimal
equivalent.
Copy the data in A reg ot R6 reg.
Compare content of R6 with 00.if
not equal loop back.
Load register R6 with 99.

MOV R6,A
CJNE R6,#00,Loop
MOV R6,#99h
Loop1:

LCALL Delay
MOV A,R6
ADD A,#99h
DA A
MOV R6,A
CJNE R6,#99,Loop1
SJMP Loop2
MOV R3,#0FFh
MOV R2,#0FFh
DJNZ R2,Here

Loop2:
Delay:
Back:
Here:

DJNZ R3,Back
RET

Call delay Subroutine.


Get the data in reg R6 to A reg.
Add 01 to the content of A reg.
Convert the data in A to its decimal
equivalent.
Copy the data in A reg ot R6 reg.
Compare content of R6 with 00. If
not equal loop back. Else stop.
Load R3 with data FFh
Load R2 with data FFh
Decrement the content of R2 till it
reaches zero.
Decrement the content of R3, if not
zero go back and load R2

60. Program to convert one digit binary number to its ASCII Equivalent.
Label

Mnemonic
MOV R0,#40h
MOV A,@R0
CJNE A,#09,Next
SJMP Small
JC Small
ADD A,#07h
ADD A,#30h
INC R0
MOV @R0,A
SJMP Loop

Next:
Small:
Loop:

Comments
Point Reg R0 to the source addr.
Get data from the addr pointed by
R0.
Check whether the data in A with
data 09. If equal jump next.
If equal jump to small.
If there is a carry jump to small.
Add the content in A with data 07h.
Add the data in A with data 30h
Increment the addr pointed by R0.
Copy the data in A to the addr.

61. Program to convert 8 bit ASCII code to its binary equivalent.


Label

Mnemonic

Dept ECE, PESITM

Comments

Page 33

Microcontroller Lab Manual


CLR C
MOV R0,#40h
MOV A,@R0
CJNE A,#39h,Next
SJMP Small
JC Small
SUBB A,#07
CLR C
SUBB A,#30h
INC R0
MOV @R0,A
SJMP Loop

Next:
Small:

Loop:

Clear the carry.


Point Reg R0 to the source addr.
Get the data from the addr to A reg.
Compare the data with 39. If not
equal jump to next.
Else jump to label small.
If there is a carry jump to small.
Subtract A reg by 07 with borrow.
Clear the carry.
Subtract A reg by 30h with borrow.
Increment the addr pointed by R0.
Move the data in A reg to the addr.

62. Program to convert 8 bit binary to its ASCII equivalent.


Label

Mnemonic
MOV R1,#02
MOV R0,#40h
MOV A,@R0
MOV 0F0h,A
ADD A,#0Fh
CJNE A,#09,Next
SJMP Small
JC Small
ADD A,#07
ADD A,#30h
INC R0
MOV @R0,A
DEC R1
CJNE R1,#00,Again
SJMP Loop1

Loop:
Next:
Small:

Loop1:
Again:

MOV A,0F0h
ANL A,#0F0h
SWAP A
SJMP Loop

Comments
Load the count into Register R1.
Point reg R0 to the source addr.
Get the data from the addr to A reg.
Copy the same to reg B.
Add the data in A reg with 0f.
Compare the data with 09. If not
equal jump to next else to small.
If there is a carry jump to small.
Add 07 to the contents of the A reg.
Add 30 to the content of the A reg.
Increment the addr pointed by R0.
Copy the data in A to the addr.
Decrement the count in reg R1.
Compare whether the content of
register R1with zero. If not equal go
to again, else stop.
Get the content of register B to A.
AND the content of register A with
immediate data.
Swap the contents of lower and
higher nibbles.
Repeat the above process.

63. Program to convert 16 bit ASCII code to its equivalent binary number in packed format.
Label

Mnemonic
MOV R1,#02

Dept ECE, PESITM

Comments
Load the count in register R1.
Page 34

Microcontroller Lab Manual


MOV R0,#40h
MOV A,@R0

Loop:

CLR C
CJNE A,#39h,Next
SJMP Small
Next:

JC Small
SUBB A,#07

Small:

CLR C
SUBB A,#30h
INC R0
DEC R1
CJNE R1,#00,Again
SWAP A
ADD A,0F0h
MOV @R0,A
SJMP Loop1
MOV 0F0h,A
SJMP Loop

Loop1:
Again:

Point reg R0 to the source addr.


Get the data from the addr pointed
by R0.
Clear the carry flag.
Compare the data in A reg with 39.
If they are not equal jump next, else
small.
If there is a carry then jump to small.
Subtract the data in A from 07 with
borrow.
Clear the carry flag.
Subtract the data in A from 30 with
borrow.
Increment the addr pointed by R0.
Decrement the count in reg R2.
Compare the data in R1 with 00. if
they are equal swap the lower and
higher nibbles else jump to again.
Add the data present in A and B reg.
Store the data in A at the addr
pointed by R0.
Copy the data in A reg to B reg.
Repeat the above process.

64. Program to convert 1 digit BCD number to its ASCII equivalent.


Label

Mnemonic
MOV R0,#40h
MOV A,@R0
ADD A,#30h
INC R0
MOV @R0,A
SJMP Loop

Loop:

Comments
Point reg R0 to the source addr.
Get the data from the addr pointed
by R0.
Add the data in A reg with 30h.
Increment the addr pointed by R0.
Copy the data in A reg to the addr
pointed by R0.

65. Program to convert 8 bit BCD to its ASCII equivalent.


Label

Mnemonic
MOV R0,#40h
MOV A,@R0
MOV 0F0h,A
ANL A,#0Fh
ADD A,#30h
INC R0
MOV @R0,A
MOV A,0F0h

Dept ECE, PESITM

Comments
Point register R0 to the source addr.
Get the data from the addr pointed
by R0.
Copy the data in A reg to B.
AND the data in A with data 0F.
Add the content of A reg with 30h.
Increment the addr pointed by R0.
Copy the data present in A reg to the
addr pointed by R0.
Page 35

Microcontroller Lab Manual


ANL A,#F0h
SWAP A
ADD A,#30h
INC R0
MOV @R0,A
SJMP Loop

Loop:

AND the data in A reg with F0.


Swap the higher and lower nibbles
of the A reg.
Add the data in A reg with 30.
Increment the addr pointed by R0.
Copy the data present in A reg to the
addr pointed by R0.

66. Program to convert 8 bit ASCII to its BCD equivalent.


Label

Mnemonic
MOV R0,#40h
MOV A,@R0
CLR C
SUBB A,#30h

Loop:

INC R0
MOV @R0,A
SJMP Loop

67. Program to convert 16bit ASCII to its BCD equivalent.


Label
Mnemonic
MOV R2,#02
MOV R0,#40h
MOV A,@R0
CLR C
SUBB A,#30h
INC R0
DEC R2
CJNE R2,#00, Again
SWAP A

Loop1:
Again:

ADD A,0F0h
MOV @R0,A
SJMP Loop1
MOV 0F0h,A
SJMP Loop

Comments
Point reg R0 to the source addr.
Get the data from the addr pointed
by R0,
Clear the carry flag.
Subtract with borrow the data in A
reg from 30h.
Increment the addr pointed by R0.
Copy the data in A reg to the addr
pointed by R0.

Comments
Load the count in reg R2.
Point the reg R0 to the source addr.
Get the data from the addr to A reg.
Clear the carry flag.
Subtract with borrow the data in A
from 30h.
Increment the addr pointed by R0.
Decrement the count in reg R2.
Compare the data in R2 with 00. if
not equal jump to again.
If R2is equal to 00 swap the higher
and lower nibbles of A reg.
Add the data in A reg with that of B.
Copy the data in A reg the addr
pointed by R0.
Copy the data in A reg to B.
Repeat the above process.

Hardware Experiments
1. C Program to generate ramp waveform using DAC interface to 8051.
#include "C:\RIDE\INC\51\REG51.H"
Dept ECE, PESITM

Page 36

Microcontroller Lab Manual


void delay_ms(unsigned char r1);
void display(void);
idata unsigned char count;
idata unsigned char sine_val;

void main(void)
{
count =0;
while(1)
{
P0 = count;
P1 = count;
count--;
}
} //end of main()
void delay_ms(unsigned char r1)
{
while(r1)
r1++;
}
2. C Program to generate Triangular waveform using DAC interface to 8051.
#include "C:\ride\inc\51\reg51.h"
void delay_ms(unsigned char r1);
idata unsigned char count=0;
unsigned char df;
void main(void)
{
df=0xff;
while(1)
{
if(df)
{
//while(count != 0)
P0 = count;
P1 = count;
count++;
if(count==0x7f)
Dept ECE, PESITM

Page 37

Microcontroller Lab Manual


df=0x00;
}
else
{
P0 = count;
P1 = count;
count--;
if(count==0x00)
df=0xff;
}
}
} //end of main()
void delay_ms(unsigned char r1)
{
while(r1)
r1--;
}
3. C Program to generate Square waveform using DAC interface to 8051.
#include "C:\RIDE\INC\51\REG51.H"
idata unsigned char count;
idata unsigned char i;
void delay_ms(unsigned char r1);
idata unsigned char row,col,key,t;
void main(void)
{
count = 0xff;
while(1)
{
if(count==0xff)
{
count = 0x00;
P1 = count;
P0 = count;
delay_ms(100);
}
else
{
count = 0xFF;
P1 = count;
P0=count;
delay_ms(100);
Dept ECE, PESITM

Page 38

Microcontroller Lab Manual


}
}
} //end of main()
void delay_ms(unsigned char r1)
{
while(r1)
{
P0=count;
P1=count;
r1--;
}
}
4. C Program to generate Sine wave using DAC interface to 8051
#include "C:\RIDE\INC\51\REG51.H"
idata unsigned char sine_val;
idata unsigned int count;
void delay_ms(unsigned char r1);
data unsigned char sine_tab[48]=
{ 0x80,0x90,0xA1,0xB1,0xC0,0xCD,0xDA,0xE5,0xEE,0xF6,0xFB,0xFE,0xFF,
0xFE,0xFB,0xF6,0xEE,0xE5,0xDA,0xCD,0xC0,0xB1,0xA1,0x90,0x80,
0x70,0x5F,0x4F,0x40,0x33,0x26,0x1B,0x12,0x0A,0x05,0x02,0x00,
0x02,0x05,0x0A,0x12,0x1B,0x26,0x33,0x40,0x4F,0x5F,0x70};
void main(void)
{
while(1)
{
if( count < 48 )
{
//sine_val = sine_tab[count];
P0 = sine_tab[count];//sine_val;
P1 = sine_tab[count];//sine_val;
count = count + 1;
delay_ms(25);
}
else
count=0;
}
} //end of main()
void delay_ms (unsigned char r1)
{
Dept ECE, PESITM

Page 39

Microcontroller Lab Manual


while(r1)
r1--;
}
5. C Program to interface stepper motor to 8051.
#include "C:\ride\inc\51\reg51.h"
#define phasea
0x0b
#define phaseb
0x07
#define phasec
0x0e
#define phased
0x0d
void clockwise(void);
void anticlockwise(void);
void delayl(void);
int i;
unsigned char test;
unsigned char disp_data;
void main ()
{
// sense switch
P1 = 0xFF;
P1 |= 0x40;
while(1)
{
test = P1;
test = test & 0x40;
if(test == 0x40)
{
clockwise();
P3 = P3 | 0x10;
}
else
{
anticlockwise();
P3 = P3 & 0xef;
}
} // end of while(1)
} // end of main()
// clockwise
//depending on switch position stepper motor will rotate in clock wise
//or anticlockwise
void clockwise(void)
Dept ECE, PESITM

Page 40

Microcontroller Lab Manual


{
P2 = phasea;
delayl();
delayl();

//different speeds are loaded in P2

P2 = phaseb;
delayl();
delayl();
P2 = phasec;
delayl();
delayl();
P2 = phased;
delayl();
delayl();
}
// anti-clock wise pgm
void anticlockwise(void)
{
P2 = phased;
delayl();
delayl();
P2 = phasec;
delayl();
delayl();
P2 = phaseb;
delayl();
delayl();
P2 = phasea;
delayl();
delayl();
}
void delayl(void)
{
unsigned int i;
// delay_ms(250);
for(i=0;i<=40000;i++);
}
6. C Program to interface DC Motor to 8051.
#include "c:\ride\inc\51\reg51.h"
Sbit
P24= P2^4;
idata unsigned char off_time,on_time;
Dept ECE, PESITM

Page 41

Microcontroller Lab Manual


idata unsigned char i;
void main ()
{
TCON = 0;
TMOD = 0x21; //select mode1, timer 0
off_time = 30;
on_time = 10;
while(1)
{
P24 = 1;
// make P2.4 high
for(i=0;i<on_time;i++)
{
TL0 = 0x66; //timer count set for 5msec
TH0 = 0xFC;
TR0 =1;
while(!TF0)
{
}
TF0 = 0;
TR0=0;
}
P24 = 0; // make P2.4 low
for(i=0;i<off_time;i++)
{
TL0 = 0x66; //timer count set for 5msec
TH0 = 0xFC;
TR0 =1;
while(!TF0)
{
}
TF0 = 0;
TR0=0;
}
}
}

7. C Program to interface Elevator to 8051.


#include "c:\ride\inc\51\reg51.h"
void delay(unsigned int);
Dept ECE, PESITM

Page 42

Microcontroller Lab Manual


main()
{
unsigned char Flr[9] = {0xff,0x00,0x03,0xff,0x06,0xff,0xff,0xff,0x09};
unsigned char FClr[9] = {0xff,0x0E0,0x0D3,0xff,0x0B6,0xff,0xff,0xff,0x79};
unsigned char ReqFlr,CurFlr = 0x01,i,j;
P0 = 0x00;
P0 = 0x0f0;
while(1)
{
P1 = 0x0f;
ReqFlr = P1 | 0x0f0;
while(ReqFlr == 0x0ff)
ReqFlr = P1 | 0x0f0;
ReqFlr = ~ReqFlr;
if(CurFlr == ReqFlr)
{
P0 = FClr[CurFlr];
continue;
}

/* Read Request Floor from P1 */


/* If Request floor is equal to Current Floor */
/* Clear Floor Indicator */
/* Go up to read again */

else if(CurFlr > ReqFlr) /* If Current floor is > request floor */


{
i = Flr[CurFlr] - Flr[ReqFlr]; /* Get the no of floors to travel */
j = Flr[CurFlr];
for(;i>0;i--)
{
P0 = 0x0f0|j;
j--;
delay(50000);
}
}
else
{

/*

Move the indicator down */

/* If Current floor is < request floor */


i = Flr[ReqFlr] - Flr[CurFlr]; /* Get the no of floors to travel */
j = Flr[CurFlr];

for(;i>0;i--)
{
P0 = 0x0f0 | j;
j++;
delay(50000);
}

/* Move the indicator Up */

}
CurFlr = ReqFlr;
P0 = FClr[CurFlr];

/* Update Current floor */


/* Clear the indicator */

}
Dept ECE, PESITM

Page 43

Microcontroller Lab Manual


}
void delay(unsigned int x)
{
for(;x>0;x--);
}
8. C Program to interface Alphanumeric LCD panel and Hex keypad input.
#include "c:\ride\inc\51\reg51.h"
void lcd_init(void);
void clr_disp(void);
void lcd_com(void);
void lcd_data(void);
void delay_ms(unsigned char);
idata unsigned char row,col,key;
idata unsigned char temp,flag,i;
unsigned char temp1,count,r,r1;
code unsigned char msg[8]={"PESITM"};
void main ()
{
delay_ms(25);
lcd_init();
flag =0;
temp = 0x80;
lcd_com();
delay_ms(5);

//address of display

for(i=0;i<16;i++)
{
if(i>7 && flag==0)
{
temp = 0xC0;
lcd_com();
delay_ms(5);
flag = 0xff;
}
temp = msg[i];
lcd_data();
delay_ms(5);
} // end of for() loop
l1: goto l1 ;
} // end of main()
// lcd initialisation routine.
Dept ECE, PESITM

Page 44

Microcontroller Lab Manual


void lcd_init(void)
{
temp = 0x38; //init.lcd 2 lines 5x7 matrix
lcd_com();
delay_ms(5);
temp = 0x38;
lcd_com();
delay_ms(5);
temp = 0x0f; //display on,cursor on
lcd_com();
delay_ms(5);
temp = 0x06; //shift cursor right
lcd_com();
delay_ms(5);
clr_disp();
}// clears display
void clr_disp(void)
{
temp = 0x01;
lcd_com();
delay_ms(5);
}
// get the command to be presented to lcd in byte fashion
// and present it in nibble fashion
void lcd_com(void)
{
P0 = temp;
temp1=0x0f;
P2 = temp1;

// RS=0

temp1=temp1 | 0x4f; // enable=1


P2 = temp1;
delay_ms(5);
temp1=temp1 & 0xbf;
P2 = temp1;

//enable=0

}
// get the data to be presented to lcd in byte fashion in temp1
// and present it in nibble fashion
Dept ECE, PESITM

Page 45

Microcontroller Lab Manual


void lcd_data(void)
{
P0 = temp;
temp1=0x1f;
P2 = temp1;

//RS=1pc4

temp1=temp1 | 0xdf; //r/w =0 for write


P2 = temp1;
temp1=temp1 | 0x4f; //enable=1
P2 = temp1;
delay_ms(5);
temp1=temp1 & 0xbf; //enable=0
P2 = temp1;
}
void delay_ms(unsigned char r1)
{
for(r=0;r<r1*2 ;r++);
}
9. C Program to interface External ADC and Temperature Control to 8051.
#include "C:\RIDE\INC\51\REG51.H"
void isr(void);
void delay(unsigned char);
sbit start=P3^5;
sbit ch_sel= P3^6;
sbit disp10=P1^0;
sbit disp11=P1^1;
unsigned char LED_CODE[16]= {0x3f,0x06,0x5b,0x4f,
0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,
0x39,0x5e,0x79,0x71};
idata unsigned char temp1,adc_val,msd_flag;
idata unsigned char MSD,LSD,i,temperature;
void tmr_isr (void) interrupt 1
{
TL0 = 0x00;
TH0 = 0xee;
if(msd_flag)
{
msd_flag=0;
disp10 = 0;
disp11 = 1;
Dept ECE, PESITM

Page 46

Microcontroller Lab Manual


P0=LSD;
}
else
{
disp10 = 1;
disp11 = 0;
P0=MSD;
msd_flag=0xff;
}
}
void main ()
{
TMOD = 0x01;
TL0 = 0x00;
TH0 = 0xee;

//mode1 selection
//for every 5msec there is interrupt

ET0 = 1;
EA = 1;
TR0 = 1;
msd_flag=0x00;
P2 = 0xff;
P3 = 0x08;
delay(200);
ch_sel = 0x00; // channel 1 selection Wr = 1 setb p3.6
start = 0; // clr p3.5
while(1)
{
start = 1;
delay(200);
start = 0;
do
{
temp1=P3;
temp1=temp1 & 0x08;
} while(temp1 != 0x08);
delay(200);// after eoc, read the adc data from P2
adc_val = P2;// display adc result on the data field
// calculate the temperature from adc value read
// 100 deg c = 255 counts adc value
// 1 deg c = adc value /2.55
temperature = (int)((float)adc_val/2.55);
MSD = LED_CODE[temperature/10];
Dept ECE, PESITM

Page 47

Microcontroller Lab Manual


LSD = LED_CODE[temperature%10];
}

// end of while(1)

}
void delay(unsigned char r)
{
unsigned char r1;
for(r1=0;r1<r;r1++);
}
10. Simple Calculator using 6 digit seven segment display and Hex KeyBoard interface to
8051
#include "c:\ride\inc\51\reg51.h"
void scan(void);
void get_key(void);
void isr (void);
void delay_ms(unsigned int);
idata unsigned char row,col,key,dg1,dg2,dg3,dg4,dg5,dg6;
unsigned char scan_code[16]={0xEE, 0xED, 0xEB, 0xE7,
0xDE, 0xDD, 0xDB,0xD7,
0xBE, 0xBD, 0xBB, 0xB7,
0x7E, 0x7D, 0x7B, 0x77 };
unsigned char LEDCODE[16]= { 0x3f, 0x06, 0x5b, 0x4f,
0x66, 0x6d, 0x7d, 0x07,
0x7f, 0x6f, 0x77, 0x7c,
0x39, 0x5e, 0x79, 0x71 };
idata unsigned char temp,temp2,temp3,res1,flag,i,result=0;
idata unsigned char ind,temp1,num1=0,num2=0,number;
idata unsigned char operator,df,res12;
idata int result1;
idata unsigned int r,r1;
void isr(void) interrupt 1
{
TL0 = 0xcc;
TH0 = 0xf8;
if(df==1)
{
P2 =0x0f;
P0 = dg4;
df=2;

//P2 holding position of led from right


//P0 holding value to be display

}
Dept ECE, PESITM

Page 48

Microcontroller Lab Manual


else if(df ==2)
{
P2 =0x1f;
P0 = dg3;
df=3;
}
else if(df ==3)
{
P2 =0x2f;
P0 = dg2;
df=4;
}
else if(df ==4)
{
P2 =0x3f;
P0 = dg1;
df=5;
}
else if(df ==5)
{
P2 =0x4f;
P0 = dg5;
df=1;
}
}
void main ()
{
TMOD = 0x01;
TL0 = 0xcc;
TH0 = 0xf8;
ET0 = 1;
EA = 1;
TR0 = 1;
df = 1;
ind = 0;
operator = 0x00;
dg1 = 0x00;
dg2 = 0x00;
Dept ECE, PESITM

Page 49

Microcontroller Lab Manual


dg3 = 0x0;
dg4 = 0x0;
dg5= 0x0;
dg6= 0x00;
P2=0x0f;
// get first digit of the first number
do{
get_key();
}while (number>9);

//

num1 = number;
dg4 = LEDCODE[number];
dg3=0x00;
display();
delay_ms(500);

//display first bit in unit place

// get operator or second digit of the first number


do{
get_key();
}while(number>13);

//get next key

if(number <10)
//if it is less than 10 then it
{
//now that is unit value previous value is
dg4 = LEDCODE[number]; //in tens place
dg3 = LEDCODE[num1];
num1= num1*10 + number; //whole no is stored in num1
operator = 0;
}
else
{

//if no is greater tha 9 then consider


//as a operator
operator =number;
dg4=LEDCODE[operator]; //display in unit place
dg3 = 0x0;

}
delay_ms(500);

//if we are not found operator before


//now get operator key

if(operator == 0)
{
do
{
get_key();
}while (number <10 || number >13);
//

operator = number;
display operator key
result=LEDCODE[operator];
dg4=LEDCODE[number]; //result = 0x00;
dg3 = 0x0; //res12=0x00;
Dept ECE, PESITM

Page 50

Microcontroller Lab Manual


}
display();
delay_ms(500);
// get first digit of the second number
do{
get_key();
}while (number>9);
num2 = number;
dg4 = LEDCODE[number];
dg3=0x00;
display();
delay_ms(500);

//

//display first bit in unit place

//get second digit of the second number or EXEC key('E')


do{
get_key();
}while(number>10 && number != 14);//get next key
// is it continuation of second number?
if(number <10) //if it is less than 10 then it is no
{
dg4 = LEDCODE[number];//now that is unit value previous value
is
dg3 = LEDCODE[num2]; //in tens place
num2= num2*10 + number; //whole no is stored in num2
// get execute key (.) or second number
do{
get_key();
}while (number != 14); //get execute key
}
dg6 = LEDCODE[number];
dg3 = 0x0;
dg4 = 0x0;
//display();
delay_ms(5000);
//operator
//0A=addition;0B=substraction;0C=multiplication;0D=division
switch (operator)
{
case 10: result1 = (num1 + num2);
break;
case 11: result1 =(int) (num1 - num2);
Dept ECE, PESITM

Page 51

Microcontroller Lab Manual


break;
case 12: result1 =(int) (num1 * num2);
break;
case 13: result1 = (num1 / num2);
break;
default :result1 = 5;
break;
}
//

result = LEDCODE[result1];
if(result1<0)
{
result1=~result1+1; //displaying sign
dg5=0x40;
}
dg1 = result1 / 1000; //get thousands bit
result1=result1-(dg1*1000);
dg2 = result1 / 100; //get hundreds bit
result1=result1-(dg2*100);
dg3 = result1 / 10;
//get tens bit
result1=result1-(dg3*10);
dg4 = result1 % 10;
//get unit bit
dg1=LEDCODE[dg1];
dg2=LEDCODE[dg2];
dg3=LEDCODE[dg3];
dg4=LEDCODE[dg4];

//

//get led code values to all bits

display_res();
delay_ms(1000);
delay_ms(1000);
delay_ms(1000);
delay_ms(1000);
delay_ms(1000);
delay_ms(1000);
delay_ms(1000);
delay_ms(1000);
delay_ms(1000);
// while(1)
}
void get_key(void)
{
int i;
Dept ECE, PESITM

Page 52

Microcontroller Lab Manual


flag = 0x00;
while(1)//flag == 0x00)
{
for(row=0;row<4;row++) //find which row is low
{
// store corresponding value in temp3
if( row == 0)
temp3=0xfe;
else if(row == 1)
temp3=0xfd;
else if(row == 2)
temp3=0xfb;
else if(row == 3)
temp3=0xf7;
P1 = temp3;
scan();
delay_ms(10);
if(flag == 0xff)
break;
} // end of for
if(flag == 0xff)
break;
} // end of while
//asm cpl p1.0
for(i=0;i<16;i++)
{
if(scan_code[i] == res1) //find position of scan code
{
//corresponding to result value
number = i;
//that value is assigned to number
NUM_CODE[i];
result = LEDCODE[i];
break;
}

//
//

}
}// end of get_key();
void scan(void)
{
unsigned char t;
temp2 = P2;
temp2 = temp2 & 0x0f;
if(temp2 != 0x0f)
{
Dept ECE, PESITM

Page 53

Microcontroller Lab Manual


delay_ms(100);
//read P2 mask with 0x0f if the value is
delay_ms(100);
//other than 0f then give debounce delay
delay_ms(100);
delay_ms(100);
delay_ms(100);
delay_ms(100);
temp2 = P2;
temp2 = temp2 & 0x0f; //repeat process if there is change
if(temp2 != 0x0f)
{
flag = 0xff;
res1 = temp2;
res1 = res1 << 4;
t = (temp3 << 4) & 0xf0;
res1 = res1 | t;//assign the scan code in res1
}
else
{
flag = 0x00;
}

//

}
} // end of scan()
void delay_ms(unsigned int r)
{
for(r1=0;r1<r*4;r1++);
}

Dept ECE, PESITM

Page 54

Вам также может понравиться