Microcontroller Lab Manual
Microcontroller Lab Manual
/Microcontroller/Ver-02
DEPARTMENT OF
ELECTRONICS & COMMUNICATION ENGINEERING
LAB MANUAL
FOR
MICROCONTROLLERS
(10ESL47)
(IV SEMESTER E&CE)
Prepared by
Prof. POORNIMA G R
Mr. DILEEP REDDY BOLLA
Ms. ARPITA KULKARNI
Approved By:
Dr. SHIVSHANKAR
HoD, E&CE
INDEX
Course Syllabus
I. PROGRAMMING (On both 8051 & MSP 430)
1. Data Transfer - Block move, Exchange, Sorting, Finding largest element in an array.
2. Arithmetic Instructions - Addition/subtraction, multiplication and division, square, Cube – (16
bits Arithmetic operations – bit addressable).
3. Counters.
4. Boolean & Logical Instructions (Bit manipulations).
5. Conditional CALL & RETURN.
6. Code conversion: BCD – ASCII; ASCII – Decimal; Decimal - HEX and HEX - Decimal.
7. Programs to generate delay, Programs using serial port and on-Chip timer /Counter.
II. INTERFACING:
8. Write C programs to interface 8051 chip to Interfacing modules to develop single chip
solutions.
9. Simple Calculator using 6 digit seven segment displays and Hex Keyboard interface to 8051.
10. Alphanumeric LCD panel and Hex keypad input interface to 8051.
11. External ADC and Temperature control interface to 8051.
12. Generate different waveforms Sine, Square, Triangular, Ramp etc. using DAC interface to
8051; Change the frequency and amplitude.
13. Stepper and DC motor control interface to 8051.
14. Elevator interface to 8051.
Course Outcomes:
Level of
CO. No After the completion of the course the student will be able to Blooms
Taxonomy
Implement, demonstrate and design hardware and software for
C247.1 small digital systems involving microcontrollers of 8051 & L3,L4,L5
MSP 430.
Write assembly language & C program in 8051 & MSP 430 for
C247.2 various embedded system applications. L3,L4
Mapping of Course Outcomes with Program Outcomes :
Program Outcomes
Course outcomes a b c d e f g h i j k l
(After completion of the course the student
will be able to)
Implement, demonstrate and design
hardware and software for small x x x x x x x x x x
C247.1 digital systems involving
microcontrollers of 8051 & MSP 430.
Write assembly language & C program
C247.2 in 8051 & MSP 430 for various x x x x x x x xx
embedded system applications.
Aim: To write and execute an assembly language program to transfer a block of data from source
to destination.
Apparatus Required:
No Name Quantity
1 8051 microcontroller Keil Simulator kit 1
Algorithm:
1. Initialize the source and destination memory pointers to address external memory.
2. Initialize the counter.
3. Read data from source location.
4. Write data to destination location.
5. Decrement the counter and check for 0, if not go to repeat from step 3, otherwise terminate
the program.
Program:
Apparatus Required:
No Name Quantity
1 8051 microcontroller Keil Simulator kit 1
Algorithm:
1. Initialize the memory pointers to address two blocks x and y in external memory.
2. Initialize the counter with N value.
3. Read data from location X to temporary memory location.
4. Readdata from Y location and write into X memory location.
5. Write data from temporary memory location to Y memory location.
6. Increment memory pointers of x and y locations.
7. Decrement the counter and check for 0, if not, repeat from step 3, otherwise terminate the
program.
Program:
Aim: Write a program to arrange a set of 8 bit numbers at location x in ascending order using
bubble sort technique.
Apparatus Required:
No Name Quantity
1 8051 microcontroller Keil Simulator kit 1
Algorithm:
Algorithm:
1. Store the elements of the array from the address 100h.
2. Initialize a pass counter with array size – 1 count (for number of passes) on R1.
3. Load compare counter with pass counter contents and initialize a register (say DPTR to
point to the start address of the array (100h).
4. Compare the next element with the current element, if smaller skip the exchange(go to step
6)
5. Exchange the two numbers in the array.
6. Decrement the exchange counter and repeat through step 5 until the counter becomes zero.
7. Decrement the pass counter and repeat through step 4 until the counter becomes 0.
8. Endless loop to terminate the program.
Program:
Memory Hex Label – Mnemonics - Operand(s) – Comments
address code(s)
ORG00H
SJMP0X030
ORG0X030
MOVR1,#03H; enter the number of data (n-1)
L5: MOV R2,#03H
MOVDPTR,#0X0100; dptr loaded with contents of 0x0100
MOVX A,@DPTR; acc. is loaded with contents of 0x0100
MOVB,A; contents of a loaded to b
L2:INCDPTR; increment dptr
MOVX A,@DPTR; acc. loaded with data pointed by dptr
CJNEA,B,L1; compare a & b, if not equal jump to l1
DJNZR2,L2; decrement r2 if not zero jump to l2
L1:JC L3
XCHA,B; exchange b & a
L3:DECDPL; decrement dpl
MOVX @DPTR, A; content of acc. loaded to address pointed to dptr
INC DPL; increment dpl
DJNZ R2,L2; decrement r2 if not zero jump TO L2
MOV A,B; accumulator is loaded with b
MOVX @DPTR,A;content of acc. loaded to address pointed to dptr
DJNZ R1,L5; decrement r1 if not zero jump to l5
HERE: SJMP HERE
END
RESULT:
Result:
Thus an assembly language program was written to arrange 8 bit array of data in
descending order and it was executed successfully.
Aim: Write a program to arrange a set of 8 bit numbers at location x in Descending order using
bubble sort
Apparatus Required:
No Name Quantity
1 8051 microcontroller Keil Simulator kit 1
Algorithm:
1. Store the elements of the array from the address 100h.
2. Initialize a pass counter with array size – 1 count (for number of passes) on R1.
3. Load compare counter with pass counter contents and initialize a register (say DPTR to
point to the start address of the array (100h).
4. Compare the next element with the current element, if smaller skip the exchange(go to step
6)
5. Exchange the two numbers in the array.
6. Decrement the exchange counter and repeat through step 5 until the counter becomes zero.
7. Decrement the pass counter and repeat through step 4 until the counter becomes 0.
8. Endless loop to terminate the program.
Program:
Memory Hex Label – Mnemonics - Operand(s) – Comments
address code(s)
ORG 00H
SJMP 0X030
ORG 0X030
MOV R1,#03H; enter the number of data (n-1)
L5:MOV R2,#03H
MOV DPTR,#0X0100; dptr loaded with contents of
0x0100
MOVX A,@DPTR; acc. is loaded with contents of 0x0100
MOV B,A; contents of a loaded to b
L2:INC DPTR; increment dptr
MOVXA,@DPTR; acc. loaded with data pointed by dptr
CJNE A,B,L1; compare a & b, if not equal jump to l1
DJNZ R2,L2; decrement r2 if not zero jump to l2
L1:JNC L3
XCH A,B; exchange b & a
L3:DEC DPL; decrement dpl
MOVX @DPTR, A; content of acc. loaded to address pointed
to dptr
INC DPL; increment dpl
DJNZ R2,L2; decrement r2 if not zero jump TO L2
MOV A,B; accumulator is loaded with b
MOVX @DPTR,A;content of acc. loaded to address pointed to dptr
DJNZ R1,L5; decrement r1 if not zero jump to l5
HERE: SJMP HERE
END
Result:
EXPT. NO : 1e. Largest Element
Aim: Write
a program to find the largest element in a given string of n bytes at location 4000h.
Sort the largest element at location 4062h.
Result:
Algorithm:
1. Initialize counter and pointer
2. Read data in temporary location
3. Compare first and second data
4. Store the smaller value in destination address location
5. Decrement counter and goto step 2
6. When counter equal to zero end the program
Program
Memory Hex code(s) Label – Mnemonics - Operand(s) – Comments
address
ORG 00H
SJMP 0X030
ORG 0X030
MOV R3, #04H; array length
MOV DPTR, #0X4000
MOVX A, @DPTR ; first byte
MOV R1, A
NEXTBYTE: INC DPTR
MOVX A, @DPTR; get next byte
CLR C
MOV R2, A
SUBB A, R1
JNC SKIP
MOV A, R2; keep present large no in acc
MOV R1, A; keep present large no in r1
SKIP : DJNZ R3, NEXTBYTE; if r3 # 0 goto nextbyte
MOV DPL, #62H
MOV A, R1
MOVX @DPTR, A
HERE : SJMP HERE
END
Result:
BEFORE EXECUTION : AFTER EXECUTION :
EXPT. NO : 1g. Block Transfer-With Overlap
Program:
Memory Hex Label – Mnemonics - Operand(s) – Comments
address code(s)
ORG 0X00
SJMP 0X30
ORG 0X30
MOV R1,#0X29; source block higher order address
MOV R2,#0X30; destination block higher order address
MOV R3,#0X40; higher order address of block1 & block 2
MOV R4,#0X0A ; initialized counter
MOV DPH,R3; dph loaded with r3
LOOP1 :MOV DPL, R1; dpl loaded with r1
MOVX A, @DPTR; accumulator loaded with 4029 address
MOV DPL, R2; dpl loaded with r2
MOVX @DPTR, A
MOV R2, DPL; r2 is loaded with dpl
DEC R2; decrement r2
DEC R1; decrement r1
DJNZ R4, LOOP1; decrement r0, if not equal to zero then jump to loop1
HERE : SJMP HERE
END
Result:
BEFORE EXECUTION :
AFTER EXECUTION:
EXPT. NO : 2a. Multibyte-Addition
Program:
Memory Hex code(s) Label – Mnemonics - Operand(s) – Comments
address
ORG 00H
SJMP 0X30
ORG 0X30
MOV R1,#40H
MOV R2,#00H; operand 1
MOV R3,#20H; operand 2
MOV R4,#50H; result
MOV R5,#02H; number of bytes
MOV DPH,R1; move dph with r1 contents
L2: MOV DPL,R2; move dpl with r2 contents
MOVX A,@DPTR; move the 1st byte of the1st no to acc
MOV B,A; move to b
MOV DPL,R3;move dpl with r3 contents
MOVX A,@DPTR; move the 1st byte of the 2nd no to acc
ADDC A,B; add a and b with carry
MOV DPL,R4; load dpl with contents of r4
MOVX @DPTR,A; move 1st byte of the sum to dptr address
INC R2; increment r2
INC R3; increment r3
INC R4; increment r4
DJNZ R5,L2; decrement r5 jump to l2 if not 0
INC DPTR; increment dptr
JC L3;jump to l3 if carry=1
MOV A,#00H;mov 00 to acc
MOVX @DPTR,A; mov 00 to dptr
SJMP HERE
L3: MOV A,#01H; move 01 to acc
MOVX @DPTR,A ;move 01 to dptr address
HERE: SJMP HERE
END
BEFORE EXECUTION : AFTER EXECUTION :
EXPT. NO : 2b. Subtraction-8bit
Algorithm:
1. Initialize the counter.
2. Initialize source and destination address in external memory.
3. Subtract and read the result in destination address location.
4. Increment data pointer decrement counter.
5. If carry is generated take 2’s compliment of the subtrahend and add with minuend.
6. Write the result in the destination memory location.
Program:
Program:
Memory Hex Label – Mnemonics - Operand(s) – Comments
address code(s)
ORG 00H
SJMP 0X30
ORG 0X30
MOV DPTR, #4050H ; load dptr with address 4050
MOVX A, @DPTR ; move the 8bit no to a
MOV B,A ; move to b
PUSH B ; push the contents of b to stack
INC DPTR; increment dptr (1st byte of the 2nd no)
MOVX A,@DPTR; move 1st byte of the 2nd no to a
MUL AB ; multiply a and b
MOV R1, A ;move the 1st byte of the ans1 to r1
MOV R2, B; move the 2nd byte of the ans1 to r2
INC DPTR; increment dptr (2nd byte of the 2nd no)
MOVX A,@DPTR; move 2nd byte of the 2nd no to a
POP B;retrieve the 1st 8bit no from the stack
MUL AB ; multiply a and b
ADD A, R2; add a with 2nd byte of the ans1 to R2
PUSH 0E0H ; push the contents of a to stack
MOV DPTR, #4055H ; ; load dptr with address 4055
MOV A, R1 ; move the 1st byte of the ans1(r1) to a
MOVX @DPTR, A ;move the ans to the dptr address
INC DPTR ; increment dptr
POP 0E0H; retrieve the 2nd byte of the ans to a from the stack
MOVX @DPTR, A; move the 2nd byte of the ans to dptr adsress
MOV A,B ;move the 3rd byte of the ans to a
ADDC A, #00H ; add acc with carry with 00
INC DPTR ; increment dptr
MOVX @DPTR, A; move the 3rd byte of the ans to dptr adsress
HERE: SJMP HERE
END
Result:
BEFORE EXECUTION :
AFTER EXECUTION :
Program:
Memor Hex Label – Mnemonics - Operand(s) – Comments
y code(s)
address
org 00h
sjmp 0x030
org 0x030
mov dptr, #4000h
movx a, @dptr ; divisor
mov b, a ; move the content from A to B
inc dptr
movx a, @dptr
div ab ; Q-> a , Rem-> b
inc dptr
movx @dptr, a
inc dptr
mov a, b
movx @dptr, a
here : sjmp here
end
RESULT:
INPUT : INPUT :
0x4000 : 05 0x4000 : 05
0x4001 : 15 0x4001 : 15
OUTPUT : OUTPUT :
0x4002 : XX 0x4002 : 04
0x4003 : XX 0x4003 : 01
Aim: To write and execute an assembly language program to find the Square of a number
Objective:To find the square of an 8 bit number and store it to desired destination location in
memory locations
.
Apparatus Required:
No Name Quantity
1 8051 microcontroller Keil Simulator kit 1
Algorithm:
1. Initialize the program counter with 0x00,Store the program from 0x30h to avoid interrupt area.
2. Move DPTR content with B through A.
3. After multiply load A (LSB) to Port A.
4. After multiply load B (MSB) to Port B.
Program:
Memor Hex Label – Mnemonics - Operand(s) – Comments
y code(s)
address
org 00h
sjmp 030h
org 030h
mov dptr,#4000h
movx a, @dptr
mov b, a
mul ab
mov 80h, a ;value of accumulator sent to port 0(80h)
mov 90h, b ; value of register b sent to port 1(90h)
here : sjmp here
end
RESULT :
OUTPUT : OUTPUT :
Port 0 : XX Port 0 : 0x00
Port 1 : XX Port 1 : 0x10
EXPT. NO : 2 f) Write a program to find cube of a number
Aim: To write and execute an assembly language program to find the Cube of a number
Objective:To find the Cube of an 8 bit number and store it to desired destination location in
memory locations
.
Apparatus Required:
No Name Quantity
1 8051 microcontroller Keil Simulator kit 1
Algorithm:
end
RESULT :
BEFORE EXECUTION : AFTER EXECUTION :
INPUT : INPUT :
0x4100 : AA 0x4000 : 40
OUTPUT : OUTPUT :
0x4055 : XX 0x4055 : 68
0x4056 : XX 0x4056 : F7
0x4057 : XX 0x4057 : 4A
Objective:To Initialize down counter for Decimal numbers and show the count using ports .
Apparatus Required:
No Name Quantity
1 8051 microcontroller Keil Simulator kit 1
Algorithm:
1. Move 00H to A register
2. Call the delay subroutine for , in delay program move FFH to any three registers ,
loop and decrement until 0.
3. Increment A registers Add 99H to A register.
4. Increment A register(decrement for down counter)
Program:
org 00h
sjmp 030h
org 030h
mov a,#00h load A with 00h
back: acall delay ; call the delay subroutine
add a, #99h ; add A with 99h
da a ; adjust the A with decimal equivalent
mov 80h, a ; move the contents of A to the port P0
jnz back ; if P0is not zero, go back
here : sjmp here ;if P0=0 be here
delay: mov r1, #0ffh ; delay routine
loop2 : mov r2, #0ffh
loop1 : mov r3, #0ffh
loop3 : djnz r3, loop3
djnz r2, loop1
djnz r1, loop2
ret
end
RESULT :
EXPT. NO :
3 b)
Write an ALP for Decimal up counter
Aim: To
write and execute an assembly language program to count up the Decimal numbers
Objective:To Initialize up counter for Decimal numbers and show the count using ports.
.
Apparatus Required:
No Name Quantity
1 8051 microcontroller Keil Simulator kit 1
Algorithm:
1. Move 00H to A register
2. Call the delay subroutine for , in delay program move FFH to register r1, r2 and r3,
loop and decrement until 0.
3. Increment A register Add 01h to A register .
4. Increment A register (Increment for Up counter)
Program:
org 00h
sjmp 0x30
org 0x30
mov a, #00h ;load A with 00h
back: acall delay ;Call the delay routine
add a, #01h ;ADD Acc with 01h
da a ;adjust the A with decimal equivalent
mov 80h, a ;move the contents of A to the port P0
jnz back ;if P0is not zero, go back
here : sjmp here ;if P0=0 be here
delay: mov r1, #0ffh ;delay routine
loop2 : mov r2, #0ffh
loop1 : mov r3, #0ffh
loop3 : djnz r3, loop3
djnz r2, loop1
djnz r1, loop2
ret ;return from the subroutine
end
RESULT :
EXPT. NO :
3 c)
Write an ALP for Hexadecimal down counter
Aim: To
write and execute an assembly language program to count down the Hexadecimal numbers
Objective:To Initialize down counter for Hexadecimal numbers and show the count using ports.
.
Apparatus Required:
No Name Quantity
1 8051 microcontroller Keil Simulator kit 1
Algorithm:
1. Move 00H to A register
2. Call the delay subroutine for , in delay program move FFH to register r1, r2 and r3,
loop and decrement until 0.
3. Increment A register Add 0FFh to A register.
4. Increment A register(decrement for down counter)
Program
Memor Hex Label – Mnemonics - Operand(s) – Comments
y code(s)
address
org 00h
sjmp 030h
org 030h
mov a,#00h ;load A with 00
back: acall delay ;call delay routine
add a, #0FFh ;ADD A with FFh
mov 80h, a ;move the contents of A to the port P0
jnz back ;if P0is not zero, go back
here : sjmp here ;if P0=0 be here
delay : mov r1, #0FFh ;delay subroutine
loop2 : mov r2, #0FFh
loop1 : mov r3, #0FFh
loop3 : djnz r3, loop3
djnz r2, loop1
djnz r1, loop2
ret ;return from the sub routine
end
RESULT :
EXPT. NO :
3 d)
Write an ALP for Hexadecimal up counter
Aim: To write and execute an assembly language program to count up the Hexadecimal numbers
Objective:To Initialize up counter for Hexadecimal numbers and show the count using ports.
.
Apparatus Required:
No Name Quantity
1 8051 microcontroller Keil Simulator kit 1
Algorithm:
1. Move 00H to A register
2. Call the delay subroutine for , in delay program move FFH to register r1, r2 and r3,
loop and decrement until 0.
3. Increment A register Add 01H to A register.
4. Increment A register (Increment for Up counter)
Program :
org 00h
sjmp 030h
org 030h
mov a,#00h ;load A with 00
back: acall delay ;call delay routine
add a, #01h ;ADD A with 01h
mov 80h, a ;move the contents of A to the port P0
jnz back ;if P0is not zero, go back
here : sjmp here ;if P0=0 be here
delay : mov r1, #0FFh ;delay subroutine
loop2 : mov r2, #0FFh
loop1 : mov r3, #0FFh
loop3 : djnz r3, loop3
djnz r2, loop1
djnz r1, loop2
ret ;return from the sub routine
end
RESULT :
Objective: To find the output value of Y for the given expression and store it to desired
destination location in bit level memory locations
.
Apparatus Required:
No Name Quantity
1 8051 microcontroller Keil Simulator kit 1
Algorithm:
1. Store the condition x in R1
2. Store the result present in A and B registers to the appropriate memory
Locations
3. Compute the operations using ANL ,ORL operators using c(carry bit register )
4. Increment the memory and store the result
Program:
org 00h
ljmp 8000h
org 8000h
setb 00h ; set the 00 bit as 1
clr 01h ; clear the 01 bit as 0
setb 02h ; set the 02 bit as 1
mov c, 01h ;1st bit i.e. B is taken as c; c=0
anl c, 02h ; 2nd bit i.e. c is given 0 by anding
anl c, /00h ; 00th bit is anded with c
mov 03h, c ; c is moved to 03h
mov c, 00h ; c gets values as 1
anl c, /01h ; c gets values as 11
anl c, 02h ;c gets values as 1
mov 04h, c ;move 1 to4th location
mov c, 00h ; first for ab/c take c as i.e. c=1
anl c, 01h ; c is given 0
anl c, /02h ;0 anding with c bar i.e. c=0
orl c, 04h ;0 is given to 04h
orl c, 03h
mov 08h, c
here: sjmp here
end
RESULT :
BEFORE EXECUTION : AFTER EXECUTION :
D: 0x0020 : XX D: 0x0020 : 15
D: 0x0021 : XX D: 0x0021 : 01
EX. NO: 5 a) Write an ALP to convert BCD to ASCII
Aim: To write and execute an assembly language program to convert the given BCD number to
ASCII number
Objective: To know the conversion process involved in BCD number to ASCII number
And store it to desired destination location in memory locations
.
Apparatus Required:
No Name Quantity
1 8051 microcontroller Keil Simulator kit 1
Algorithm
1. Move the BCD data to be converted to accumulator.
2. Get the lower nibble(BCD digit) & ADD (or ORL) with 30h
3. Store the converted ASCII value
4. Get the higher nibble(tens BCD digit) & ADD (or ORL) with 30h
Program :
org 00h
sjmp 030h
org 030h
mov dptr,#8100h ;load the data pointer
movx a, @dptr ;move the content in data pointer to accumulator
mov r2, a ;move the content in accumulator to r2 register
anl a, #0fh ;do logical AND operation for accumulator and 0fh
orl a, #30h ;do logical OR operation for accumulator with 30h
mov r6, a ;move the content in accumulator to r6 register
mov a, r2 ;then move the content from r2 to accumulator
anl a, #0f0h ;do logical AND operation for accumulator and 0f0h
swap a ;swap the contents in accumulator
orl a, #30h ;do logical OR operation for accumulator with 30h
inc dptr ;increment the data pointer
movx@dptr, a ;move the content in accumulator to data pointer
address
inc dptr ;increment data pointer
mov a, r6 ;move the content from r6 register to accumulator
movx@dptr, a ;move the content in accumulator to data pointer
address
here : sjmp here
end
RESULT :
Objective: To know the conversion process involved in ASCII number to BCD number
And store it to desired destination location in memory locations
.
Apparatus Required:
No Name Quantity
1 8051 microcontroller Keil Simulator kit 1
Algorithm
1. Move the ASCII data to be converted to Accumulator
2. do logical AND operation for accumulator with 0Fh
3. Swap the contents of the accumulator and move the data to B register
4. Take another ASCII data and do logical AND operation for accumulator and 0fh
5. do logical OR operation for accumulator and b
6. move the content in accumulator to 60h address
Program :
org 00h
sjmp 030h
org 030h
mov a, #'4' ;move 4 to accumulator
anl a, #0Fh ; do logical AND operation for accumulator and 0fh
swap a ;swap the content an accumulator
mov b, a ;move the data from accumulator to b register
mov a, #'7' ;move 7 to accumulator
anl a, #0Fh ; do logical AND operation for accumulator and 0fh
orl a, b ;do logical OR operation for accumulator and b
mov 60h, a ;move the content in accumulator to 60h address
here: sjmp here
end
RESULT :
Aim: To write and execute an assembly language program to convert the given decimal number
to hexadecimal number
Objective: To know the conversion process involved in decimal number to hexadecimal number,
and store it to desired destination location in memory locations
.
Apparatus Required:
No Name Quantity
1 8051 microcontroller Keil Simulator kit 1
Algorithm
1. Move the decimal data to be converted from external memory 40h to accumulator
2. AND A reg with 0f0h and obtain the upper MSB of the decimal digit and swapth
e LSB and MSB of accumulator to bring the same to units place
3. Move 0Ah to B register and multiply with A reg to convert to hex value, storethe
converted tens value in r1
4. Get the LSB of the decimal number and add to the converted tens value
5. Point to the next memory location and store the result (hexadecimal).
Program :
org 00h
sjmp 0x30
org 0x30
mov ;initiate the data pointer with 8100h address location
dptr,#8100h
movx a, @dptr ;move the content in data pointer to accumulator
mov b, a ;move the data from a to b
anl a, # 0x0F ;do logical AND operation for accumulator with 0Fh
mov r1, a ;move the content from accumulator to R1 register
mov a, b ;move the content in b to accumulator
anl a, # 0xF0 ;do logical AND operation for accumulator with F0h
swap a
mov b, #0Ah ;move 0Ah to b register
mul ab ;multiply a and b registers
add a, r1 ;add the content in accumulator with R1 register
inc dptr ;increment data pointer
movx @dptr, a ;move the data from accumulator to data pointer
here : sjmp here
end
RESULT :
BEFORE EXECUTION : AFTER EXECUTION :
Aim: To write and execute an assembly language program to convert the given hexadecimal
number to decimal number
Objective: To know the conversion process involved in hexadecimal number to decimal number,
and store it to desired destination location in memory locations
.
Apparatus Required:
No Name Quantity
1 8051 microcontroller Keil Simulator kit 1
Algorithm
Program :
Memor Hex Label – Mnemonics - Operand(s) – Comments
y code(s)
address
org 00h
sjmp 0x30
org 0x30
mov dptr,#8100h ;initiate the data pointer with 8100h address
location
movx a, @dptr ;move the content in data pointer to accumulator
mov dpl, #50h ;move 50h to lower byte of the data pointer
mov b, #0ah ;move 0ah to b register
div ab ;divide a and b registers
mov r1, a ;move the content in a to r1 register
mov r2, b ;move the content in b to r2 register
mov a, r2 ;move the content in r2 to accumulator
movx @dptr, a ;move the data from accumulator to data pointer
mov r3, a ;move data from accumulator to r3 register
inc dptr ;increment the data pointer
mov b, #0ah ;move 0ah to b register
mov a, r1 ;copy the content of r0 to accumulator
div ab ;divide a and b
mov r1, a ;copy the contents of A into r1
mov r2, b ;copy the contents of b into r2
mov a, r2 contents of r2 into a
movx @dptr, a
inc dptr ;increment the data pointer
mov a, r1 ;copy the content of r0 to accumulator
movx @dptr, a
mov a, r2 ;copy the contents of r2 into a
swap a ;swap the content in a
orl a, r3 ; do the OR logic for and r3 register
mov 60h, a ;save content of A in RAM location 60h
mov 61h, r1 ;save content in r1 register in RAM location 61h
here : sjmp here
end
RESULT :
Aim: To write and execute an assembly language program to convert the given ASCII number to
Hex decimal number
Objective: To know the conversion process involved ASCII number to Hex decimal number
, and store it to desired destination location in memory locations
.
Apparatus Required:
No Name Quantity
1 8051 microcontroller Keil Simulator kit 1
Algorithm
1. Move the ASCII character to be converted to accumulator.
2. If character is greater than 41h,(for A-F), then subtract 07h & 30h
3. Else (i.e., for 0-9) subtract only 30h
4. Store the converted hexadecimal number
Program :
Memor Hex Label – Mnemonics - Operand(s) – Comments
y code(s)
address
org 00h
sjmp 0x30
org 0x30
mov r1, #50h ;load 50H into R1
mov a, @r1 ;get a byte from R1 register
clr c ;clear carry
subb a, #0Ah ;subtract Accumulator ,0Ah
mov a, @r1 ; get a byte from R1 register
jc skip ;jump with carry to skip location
clr c ;clear the carry
subb a, #07h ;subtract Accumulator ,07h
skip: clr c
subb a, #30h ;subtract Accumulator ,30h
inc r1 ;increment r1 register
mov @r1, a ;get a byte from accumulator to r1 register
here: sjmp here
end
Result :
Aim: To write and execute an assembly language program to convert the given Hexadecimal to
ASCII
Objective: To know the conversion process involved Hexadecimal to ASCII , and store it to
desired destination location in memory locations
.
Apparatus Required:
No Name Quantity
1 8051 microcontroller Keil Simulator kit 1
Algorithm
1. Move the hexadecimal data to be converted to accumulator.
2. Get the lower nibble & call ASCII routine
3. Store the converted ASCII value
4. Get the higher nibble & call ASCII routine
5. Store the converted ASCII value
6. If digit greater than 09,(for A-F) add 07h & 30h
7. Else (i.e., for 0-9) add only 30h
8. Return
org 00h
sjmp 0x30
org 0x30
mov r1, #50h ; move r1 as 50 h
mov a, @r1 ; move the content of r1 to a
mov r2, a ; move the content of A to r2 register
anl a, #0Fh ; and logic A with 0Fh
acall ASCII ; call the ASCII subroutine
inc r1 ; increment r1 register
mov @r1, a
mov a, r2
swap a
anl a, #0Fh ; and logic A with 0Fh
acall ASCII ; call the ASCII subroutine
inc r1
mov @r1, a
here : sjmp here
ASCII : mov r4, a ;subroutine of ASCII
clr c
subb a, #0Ah
mov a, r4
jc skip
add a, #07
skip : add a, #30
ret
RESULT :
Aim:
To transfer the data serially between two 8051 microcontroller kit and IBM PC
using serial port (RS232).
Apparatus Required:
No Name Quantity
Theory:
1. The TMOD register is loaded with the value 20H, indicating the use of timer 1 in mode 2
(8-bit auto-reload) to set the baud rate.
2. The TH1 is loaded with -3H to set the baud rate of 9600 for serial data transfer
3. The SCON register is loaded with the value 50 H, indicating serial mode 1, where an 8-
bit data is framed with start and stop bits.
4. TRI is set to l to start timer l.
5. TI is cleared by the “CLR TI” instruction.
6. The character byte to be transferred serially is written into the SBUF register.
7. The TI flag bit is monitored with the use of the instruction “JNB TI, $” to see if the
character has been transferred completely.
8. To transfer the next character, go to Step 5.
1. The TMOD register is loaded with the value 20H, indicating the use of timer 1 in mode 2
(8-bit auto-reload) to set the baud rate.
2. The TH1 is loaded with -3H to set the baud rate of 9600 for serial data transfer
3. The SCON register is loaded with the value 50 H, indicating serial mode 1, where an 8-
bit data is framed with start and stop bits.
DEPT. OF E&CE 49
MC LAB MANUAL SRI VENKATESHWARA COLLEGE OF ENGINEERING, BENGALURU
Procedure:
1. Connect the 8051 microcontroller kit and IBM PC using RS232 kit.
2. Enter and run the program in 8051 microcontroller kit to transmit GTU serially.
3. Enter and run the program in 8051 microcontroller kit to receive data from PC
terminal program window.
4. Enter and run the program in 8051 microcontroller kit to receive data from PC
terminal program window and sent it to P2 port of 8051 where relay is connected.
HOME:
MOV A,#'G'
ACALL SEND
MOV A,#'T'
ACALL SEND
MOV A,#'U'
ACALL SEND
AJMP HOME
SEND:
MOV SBUF,A
JNB TI,$
CLR TI
RET
END
DEPT. OF E&CE 50
MC LAB MANUAL SRI VENKATESHWARA COLLEGE OF ENGINEERING, BENGALURU
Result:
Thus the assembly language was written to transfer the data serially between micro
controller kit and IBM PC using RS232 cable and it was executed.
DEPT. OF E&CE 51
MC LAB MANUAL SRI VENKATESHWARA COLLEGE OF ENGINEERING, BENGALURU
RESULT:
DEPT. OF E&CE 52
MC LAB MANUAL SRI VENKATESHWARA COLLEGE OF ENGINEERING, BENGALURU
DEPT. OF E&CE 53
MC LAB MANUAL SRI VENKATESHWARA COLLEGE OF ENGINEERING, BENGALURU
RESULT:
DEPT. OF E&CE 54
MC LAB MANUAL SRI VENKATESHWARA COLLEGE OF ENGINEERING, BENGALURU
DEPT. OF E&CE 55
MC LAB MANUAL SRI VENKATESHWARA COLLEGE OF ENGINEERING, BENGALURU
RESULT:
DEPT. OF E&CE 56
MC LAB MANUAL SRI VENKATESHWARA COLLEGE OF ENGINEERING, BENGALURU
DEPT. OF E&CE 57
MC LAB MANUAL SRI VENKATESHWARA COLLEGE OF ENGINEERING, BENGALURU
RESULT:
DEPT. OF E&CE 58
MC LAB MANUAL SRI VENKATESHWARA COLLEGE OF ENGINEERING, BENGALURU
DEPT. OF E&CE 59
MC LAB MANUAL SRI VENKATESHWARA COLLEGE OF ENGINEERING, BENGALURU
RESULT:
DEPT. OF E&CE 60
MC LAB MANUAL SRI VENKATESHWARA COLLEGE OF ENGINEERING, BENGALURU
END
RESULT:
P2DIR=0XFF
Observe at
DEPT. OF E&CE 61
MC LAB MANUAL SRI VENKATESHWARA COLLEGE OF ENGINEERING, BENGALURU
RESULT:
P2DIR=0XFF
Observe at
DEPT. OF E&CE 62
MC LAB MANUAL SRI VENKATESHWARA COLLEGE OF ENGINEERING, BENGALURU
RESULT:
P2DIR=0XFF
Observe at
P2OUT=00, 01, 02,………………. 99.
DEPT. OF E&CE 63
MC LAB MANUAL SRI VENKATESHWARA COLLEGE OF ENGINEERING, BENGALURU
RESULT:
P2DIR=0XFF
Observe at
P2OUT=99, 98, 97,………………. 00.
Apparatus Required:
No Name Quantity
1 IAR Embedded Workbench KickStart for MSP430 1
Algorithm:
1. Stop the watch dog timer.
2. Initialize source & destination locations
3. Add the operands and store the result.
4. Perform RCD addition and store the result in nest location.
5. Perform subtractionand store the result in nest location
6. Perform logical AND operationand store the result in nest location
7. Perform logical OR operationand store the result in nest location
8. Perform logical XOR operationand store the result in nest location
9. Endless loop
Program:
Memory Hex
Label - Mnemonics - Operand(s) – Comments
address code(s)
MAIN: NOP ;main program
MOV.W #WDTPW+WDTHOLD,&WDTCTL ; stop watch dog timer
DEPT. OF E&CE 64
MC LAB MANUAL SRI VENKATESHWARA COLLEGE OF ENGINEERING, BENGALURU
MOV #0X20,R4
MOV.B @R4+,R5
MOV.B @R4+,R6
MOV.B R6,0(R4)
ADD.B R5,0(R4)
INC R4
MOV.B R6,0(R4)
DADD.B R5,0(R4)
INC R4
MOV.B R6,0(R4)
SUB.B R5,0(R4)
INC R4
MOV.B R6,0(R4)
AND.B R5,0(R4)
INC R4
MOV.B R6,0(R4)
BIS.B R5,0(R4)
INC R4
MOV.B R6,0(R4)
XOR.B R5,0(R4)
INC R4
JMP $;endless loop
END
OUT PUT
DEPT. OF E&CE 65
MC LAB MANUAL SRI VENKATESHWARA COLLEGE OF ENGINEERING, BENGALURU
INTERFACING
Objective:
Interface one common cathode type seven segment display with port 1.then write a
program that displays pattern 'SVCE' continuously.
Apparatus Required:
No Name Quantity
1 8051 microcontroller kit & Simulator 1
2 SEVEN SEGMENT display interface 1
Procedure:
DEPT. OF E&CE 66
MC LAB MANUAL SRI VENKATESHWARA COLLEGE OF ENGINEERING, BENGALURU
# include <stdio.h>
# include <reg51.h>
//sfr P0=0x80;
//sfr P1=0x90;
void delay (int g);
int port [20] = {0xff, 0xff, 0xff, 0xff, 0x06, 0xC6, 0xC1, 0x92},i;
int i;
void main ()
{
char d,b,j,k,s;
while (1)
{
i=0; P2=0;
for (d=0; d<2; d++)
{
for (b=0; b<4; b++)
{
k = port [i++];
for (j=0; j<8; j++)
{
s=k;
s=s&0x80;
if (s==0x00)
P1=0x00;
else
P1=0x01;
P2=0x01;
P2=0x00;
s=k;
s=s<<1;
k=s;
}
}
delay (10000);
delay (10000);
}
}
}
void delay (int g)
{ int h;
for (h=0; h<=g; h++)
{ ;}
}
DEPT. OF E&CE 67
MC LAB MANUAL SRI VENKATESHWARA COLLEGE OF ENGINEERING, BENGALURU
----------------------ooooo--------------------
Write a C program to interface 8051 chip to develop SINE, square, triangle, ramp using
DAC interface
#include<reg51.h>
sfr p0=0x80;
sfr p1=0x90;
void main()
unsigned char
i,m[]={0x7F,0x9F,0xAA,0xBE,0XD0,0XE0,0XEC,0XF6,0XEF,0XFC,0XF6,0XE5,0XE0,0XB0
,0XBE,0XAA,0X95,0X7F,0X68,0X83,0X3F,0X3D,0X2D,0X3F,0X53,0X68,0X7F};
while(1)
for(i=0;i<36;i++)
p0=m[i];
p1=m[i];
#include<reg51.h>
sfr p0=0x80;
sfr p1=0x90;
DEPT. OF E&CE 68
MC LAB MANUAL SRI VENKATESHWARA COLLEGE OF ENGINEERING, BENGALURU
void main()
while(1)
for(i=0;i<0xff;i++)
p0=i;
p1=i;
for(j=0xff;j>0;j--)
p0=j;
p1=j;
#include<reg51.h>
sfr p0=0x80;
sfr p1=0x90;
void main()
DEPT. OF E&CE 69
MC LAB MANUAL SRI VENKATESHWARA COLLEGE OF ENGINEERING, BENGALURU
unsigned char k;
while(1)
p0=0xff;
p1=0xff;
delay(50);
p0=0x00;
p1=0x00;
delay(50);
unsigned char j;
for(j=0;j<111;j++);
#include <reg51.h>
sfrP0=0x80;
DEPT. OF E&CE 70
MC LAB MANUAL SRI VENKATESHWARA COLLEGE OF ENGINEERING, BENGALURU
sfrP1=0x90;
void main ()
unsigned char i;
while (1)
P0=i;
P1=i;
ELEVATER Interface
#include<reg51.h>
void main()
P0=0x00;
P0=0xF0;
DEPT. OF E&CE 71
MC LAB MANUAL SRI VENKATESHWARA COLLEGE OF ENGINEERING, BENGALURU
while(1)
P1=0x0F;
regflr=P1|0xf0;
while(regflr==0x0ff)
regflr=P1|0xf0;
regflr=~regflr;
if(curflr==regflr)
P0=fclr[curflr];
continue;
else if(curflr>regflr)
i=flr[curflr]-flr[regflr];
j=flr[curflr];
P0=0xf0|j; j--;
delay(2500);
else
DEPT. OF E&CE 72
MC LAB MANUAL SRI VENKATESHWARA COLLEGE OF ENGINEERING, BENGALURU
i=flr[regflr]-flr[curflr];
j=flr[curflr];
for(; i>0;i--)
P0=0xf0|j;
j++;
delay(2500);
curflr=regflr;
P0=fclr[curflr];
-----------ooooooo----------
#include<reg51.h>
#include<stdio.h>
sbit en =P3^5;
sbit rs =P3^7;
sbit rw =P3^6;
DEPT. OF E&CE 73
MC LAB MANUAL SRI VENKATESHWARA COLLEGE OF ENGINEERING, BENGALURU
void lcd_setup(void);
void main ()
while(1)
c=0x0e;
x=0;
for(e=0;e<4;e++)
P0=c;
d=0x0e;
for(b=0;b<4;b++)
f=P1;
f=f&0x0f;
if(f==d)
lcd_setup();
DEPT. OF E&CE 74
MC LAB MANUAL SRI VENKATESHWARA COLLEGE OF ENGINEERING, BENGALURU
b=4;
e=4;
else
x=x+1;
d=d<<1;
d=d|0x01;
d=d&0x0f;
c=c<<1;
c=c|0x01;
c=c&0x0f;
void lcd_setup(void)
rs=0;
rw=0;
en=0;
for(l=0;l<5;l++)
DEPT. OF E&CE 75
MC LAB MANUAL SRI VENKATESHWARA COLLEGE OF ENGINEERING, BENGALURU
i=cmd[l];
data_write(i);
delay();
rs=1;
for(l=1;l<14;l++)
i=data_byte[l];
data_write(i);
delay();
i=key[x];
data_write(i);
delay();
P2=db;
en=1;
en=0;
void delay(void)
DEPT. OF E&CE 76
MC LAB MANUAL SRI VENKATESHWARA COLLEGE OF ENGINEERING, BENGALURU
unsigned int j;
for(j=0;j<111;j++); }
STEPPER MOTOR
#include<reg51.h>
bit dir=0;
for(; x>0;x--);
dir=~dir;
delay(32000);
void main()
EA=0x1;
EX0=0x1;
P0=0x00;
while(1)
if(dir)
DEPT. OF E&CE 77
MC LAB MANUAL SRI VENKATESHWARA COLLEGE OF ENGINEERING, BENGALURU
val=0x88;
for(i=0;i<4;i++)
P0=val;
val=val>>1;
delay(575);
else
val=0x11;
for(i=0;i<4;i++)
P0=val;
val=val<<1;
delay(575);
DEPT. OF E&CE 78
MC LAB MANUAL SRI VENKATESHWARA COLLEGE OF ENGINEERING, BENGALURU
Ans:
Microproccessor Microcontroller
Memory, ports, timers, interrupts and controllers Memory, ports, interrupts and controllers are
are external to the chip. internal to the chip.
Based on Von-Neumann architecture. Based on Harvard architecture.
Have many instructions for data transfer b/w the Have limited instructions for data transfer b/w
microprocessor and external memory. the microcontroller and external memory.
The instruction sets contains limited number of
It performs large number of bit level operation.
instructions for bit operation.
Ans: Flags are 1-bit registers provided to store the result of certain program instructions.
PSW: The PSW contains the math flags, user program flag F0, and the register select bits that
identify which of the four general purpose register banks is currently in use by the program.
CY AC F0 RS1 RS0 OV -- P
Ans: The stack refers to an area of internal RAM that is used in conjugation with certain opcodes
to store and retrieve data quickly.
Ans; Counter is the process of counting the pulses generated by any system.
DEPT. OF E&CE 79
MC LAB MANUAL SRI VENKATESHWARA COLLEGE OF ENGINEERING, BENGALURU
Explain the above table based on your microcontroller knowledge of flags1 and 0’s and others.
Q7. Draw the special function register for SCON and PCON.
Ans: SCON
SM0 SM1 SM2 REN TB8 RB8 TI RI
PCON:
SM0D -- -- -- GF1 GF0 PD IDL
Ans: When a timer/counter overflows, the corresponding timer flag, TF0 or TF1, is set to 1. The
flag is cleared to 0 when the resulting interrupt generates a program call to the appropriate timer
subroutine in memory.
Q9. Draw and explain the Interrupt enable (IE) and Interrupt Priority (IP) special function
register.
DEPT. OF E&CE 80
MC LAB MANUAL SRI VENKATESHWARA COLLEGE OF ENGINEERING, BENGALURU
Q10. What are different addressing modes in microcontroller? Give one example for each.
Q12. For the given set of instruction name the operation and addressing modes.
i) MOV A,#n
ii) MOV A, addr
iii) MOVX A,@DPTR
iv) MOVC A,@A+DPTR
v) XCH A,Rn
Ans: i) Operation: Copy the immediate 8 bit data into the accumulator.
Addressing mode: Immediate add mode.
It is a 2 byte instruction.
ii) Operation: Copy the contents of the internal RAM address (addr) into the accumulator.
Addressing mode: Direct addressing mode.
It is a 2 byte instruction.
iii) Operation: Copy the contents of the external RAM address stored in DPTR into the
accumulator.
Addressing mode: Indirect addressing mode.
It is a 1 byte instruction.
iv) Operation: Copy the code byte, found at the ROM address formed by adding A and the
DPTR, into the accumulator.
DEPT. OF E&CE 81
MC LAB MANUAL SRI VENKATESHWARA COLLEGE OF ENGINEERING, BENGALURU
It is a 1 byte instruction.
v) Operation: Exchange data bytes between the accumulator and the register Rn.
Addressing: Register Addressing mode.
It is a 1 byte instruction.
Ans: It is of two types- conditional jump and unconditional jump. Conditional jump is of two
types byte level instruction an bit level instruction. Similarly unconditional jump is also divided
into 3 sub-jump and these are- Short jump, Absolute jump, Long jump.
Ans: A subroutine is a program that may be used many times in the execution of a larger
program.
DEPT. OF E&CE 82
MC LAB MANUAL SRI VENKATESHWARA COLLEGE OF ENGINEERING, BENGALURU
DEPT. OF E&CE 83
MC LAB MANUAL SRI VENKATESHWARA COLLEGE OF ENGINEERING, BENGALURU
The name Harvard Architecture comes from the Harvard Mark. The most obvious
characteristic of the Harvard Architecture is that it has physically separate signals and storage for
code and data memory. It is possible to access program memory and data memory
simultaneously. Typically, code (or program) memory is read-only and data memory is read-
write. Therefore, it is impossible for program contents to be modified by the program itself.
The von Neumann Architecture is named after the mathematician and early computer scientist
John von Neumann. Von Neumann machines have shared signals and memory for code and data.
Thus, the program can be easily modified by itself since it is stored in read-write memory.
Intel’s original MCS-51 family was developed using NMOS technology, but later versions,
identified by a letter C in their name (e.g., 80C51) used CMOS technology and consume less
power than their NMOS predecessors. This made them more suitable for battery-powered
devices.
The Intel 8051 is an 8-bit microcontroller which means that most available operations are limited
to 8 bits.
DEPT. OF E&CE 84
MC LAB MANUAL SRI VENKATESHWARA COLLEGE OF ENGINEERING, BENGALURU
Direct memory
Q28.What location code memory space and data memory space begins?
128 bytes of RAM (from 0x00 to 0x7F) and can be used to store data.
Q30.With 12 MHz clock frequency how many instructions (of 1 machine cycle and 2
machine cycle) it can execute per second?
A cycle is, in reality, 12 pulses of the crystal. That is to say, if an instruction takes one machine
cycle to execute, it will take 12 pulses of the crystal to execute. Since we know the crystal is
pulsing 11,059,000 times per second and that one machine cycle is 12 pulses, we can calculate
how many instruction cycles the 8051 can execute per second:
11,059,000 / 12 = 921,583
This means that the 8051 can execute 921,583 single-cycle instructions per second. Since a large
number of 8051 instructions are single-cycle instructions it is often considered that the 8051 can
execute roughly 1 million instructions per second, although in reality it is less–and, depending on
the instructions being used, an estimate of about 600,000 instructions per second is more
realistic.
DEPT. OF E&CE 85