Microcontroller Lab Manual
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
Page 2
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.
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.
Mnemonic
MOV DPTR,#16 BitAddr
MOVX A,@DPTR
MOV R0,A
INC DPTR
MOVX A,@DPTR
XCH A,R0
MOVX @DPTR,A
Loop:
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
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
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
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
SJMP Loop1
Next :
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
Loop:
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
Mnemonic
Loop:
Loop1:
Dept ECE, PESITM
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
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:
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
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.
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
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
MOV 40h,A
MOV 41h,R1
SJMP Loop
Loop:
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
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
Next:
Loop1:
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.
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.
Mnemonic
MOV R0, # SRC ADDR
MOV A, @R0
Comments
Use R0 to point to the source addr.
Get the data into the A register.
Page 10
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.
Mnemonic
MOV R0, # SRC ADDR
MOV A,# 99h
SUBB A, @R0
ADD A,# 01
DA A
INC R0
ADD A,@R0
Loop:
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
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.
Mnemonic
MOV R0,#SRC ADDR
MOV A, @R0
INC R0
MOV 0F0h,@R0
MUL AB
INC R0
MOV @R0,A
INC R0
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
Loop
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:
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.
Mnemonic
Loop:
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
Loop1:
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:
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
Loop:
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.
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
Loop:
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.
Mnemonic
Comments
Page 16
Loop :
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 :
MOV 41h,A
SJMP Loop
JNC Stop
INC 41h
SJMP Back
SQR:
MOV A, @R0
MOV 0F0h, @R0
MUL AB
RET
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.
Mnemonic
Comments
Page 17
Next :
Loop:
SJMP Loop
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.
Mnemonic
MOV R1, # 00
MOV R0,# 40h
MOV A,@R0
JNB 0E0h, Next
DEC R1
Next :
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
SJMP Loop
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.
Mnemonic
MOV R1,#00
MOV R0,#40h
MOV R2,#05
MOV A,@R0
ANL A,#0E0h
JNZ Invalid
MOV A,@R0
Loop:
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
Next:
Invalid:
Loop1:
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
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
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
Loop1:
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
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
MOV @R0,#00
SJMP Loop1
Not pal:
INC R0
MOV @R0,#0FFh
SJMP Not pal
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
INC R0
MOV 50h,@R0
Next:
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
Next1:
Loop1:
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
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
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
Neg Cnt:
Next:
MOV A,R3
MOVX @DPTR,A
INC DPTR
MOV A,R4
MOVX @DPTR,A
SJMP Loop
Loop:
Mnemonic
MOV A,#00
MOV R2,#N
MOV R0,#40h
MOV R3,#N1
MOV 50h,@R0
Again:
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.
Mnemonic
Comments
Page 24
Again:
Loop:
Next:
DEC A
SJMP Again
MOV 50h,A
SJMP Loop1
Last:
Loop1:
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:
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
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:
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.
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.
Mnemonic
Comments
Page 26
Loop:
ADD A,@R1
INC R0
INC R1
MOV @R1,A
DJNZ R2, Loop
SJMP Loop1
Loop1:
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
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
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:
Mnemonic
MOV R0,#40h
MOV A,@R0
CJNE A,#00, Next
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
Next:
Next1:
CJNE A,#02,Next2
SJMP Prime
Next2:
CJNE A,#03,Next3
SJMP Prime
Next3:
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:
Mnemonic
Loop:
MOV R6,#00
LCALL Delay
INC R6
Comments
Clear the content of reg R6.
Call delay Subroutine.
Increment the content of R6.
Page 29
Loop1:
Delay:
Back:
Here:
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
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
Mnemonic
Loop:
Dept ECE, PESITM
MOV R6,#99h
LCALL Delay
Comments
Clear the contents of register R6.
Call delay Subroutine.
Page 30
Loop1:
Delay:
Back:
Here:
DJNZ R3,Back
RET
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
Mnemonic
MOV R6,#00
MOV R7,#00
Comments
Clear the contents of register R6 and
R7.
Page 31
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
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
Mnemonic
Comments
Page 32
Loop:
MOV R6,#00
LCALL Delay
MOV A,R6
ADD A,#01
DA A
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
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.
Mnemonic
Comments
Page 33
Next:
Small:
Loop:
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
Comments
Load the count in register R1.
Page 34
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:
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.
Mnemonic
MOV R0,#40h
MOV A,@R0
MOV 0F0h,A
ANL A,#0Fh
ADD A,#30h
INC R0
MOV @R0,A
MOV A,0F0h
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
Loop:
Mnemonic
MOV R0,#40h
MOV A,@R0
CLR C
SUBB A,#30h
Loop:
INC R0
MOV @R0,A
SJMP Loop
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
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
Page 38
Page 39
Page 40
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
Page 42
/*
for(;i>0;i--)
{
P0 = 0x0f0 | j;
j++;
delay(50000);
}
}
CurFlr = ReqFlr;
P0 = FClr[CurFlr];
}
Dept ECE, PESITM
Page 43
//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
// RS=0
//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
//RS=1pc4
Page 46
//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
// 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;
}
Dept ECE, PESITM
Page 48
Page 49
//
num1 = number;
dg4 = LEDCODE[number];
dg3=0x00;
display();
delay_ms(500);
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
{
}
delay_ms(500);
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
//
Page 51
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];
//
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
//
//
}
}// end of get_key();
void scan(void)
{
unsigned char t;
temp2 = P2;
temp2 = temp2 & 0x0f;
if(temp2 != 0x0f)
{
Dept ECE, PESITM
Page 53
//
}
} // end of scan()
void delay_ms(unsigned int r)
{
for(r1=0;r1<r*4;r1++);
}
Page 54