Top Banner
8051 lab manual - 2011 CONTENTS 1. a ALP to transfer a block of data of ‘n’ data bytes stored in the memory locations starting from 30h to the memory locations starting from 40h b ALP to transfer a block of data from the source memory location starting memory location ‘x’ to the destination memory location starting from ‘y’ with overlap c. ALP to interchange the contents of two blocks 2. a ALP to add ‘n’ one byte binary numbers b ALP to add two multibyte numbers c. ALP to subtract a multibyte number from another multibyte number d. ALP to add ‘n’ 2 digit packed BCD numbers 3. a ALP to Multiply two binary numbers b ALP to divide a binary number by another binary number 4. a ALP to find square of the given number b ALP to find cube of the given number 5.a ALP to find the largest in the given array b ALP to find the smallest in the given array 6.a ALP to sort the given array in ascending order b ALP to sort the given array in descending order 7.a ALP to check whether the given no. is odd or even b ALP to check whether the given number is positive or negative 8. a ALP to check whether the given number is valid number in ” 2 out of 5 code” or not b ALP to find the number of ones and zeroes in the given number 9.a ALP to convert the given ASCII number into its HEX equivalent. b ALP to convert to convert the 2 digit packed BCD number into its ASCII Dept. of Electronics & Communication AIT, Tumkur
42

Uc Lab Manual Updated

Aug 24, 2014

Download

Documents

Hemanth Kumar
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Uc Lab Manual Updated

8051 lab manual - 2011

CONTENTS

1. a ALP to transfer a block of data of ‘n’ data bytes stored in the memory locations starting from 30h to the memory locations starting from 40h b ALP to transfer a block of data from the source memory location starting memory location ‘x’ to the destination memory location starting from ‘y’ with overlap c. ALP to interchange the contents of two blocks

2. a ALP to add ‘n’ one byte binary numbers b ALP to add two multibyte numbers c. ALP to subtract a multibyte number from another multibyte number d. ALP to add ‘n’ 2 digit packed BCD numbers 3. a ALP to Multiply two binary numbers b ALP to divide a binary number by another binary number

4. a ALP to find square of the given number b ALP to find cube of the given number 5.a ALP to find the largest in the given array b ALP to find the smallest in the given array 6.a ALP to sort the given array in ascending order b ALP to sort the given array in descending order 7.a ALP to check whether the given no. is odd or even b ALP to check whether the given number is positive or negative

8. a ALP to check whether the given number is valid number in ” 2 out of 5 code” or not b ALP to find the number of ones and zeroes in the given number

9.a ALP to convert the given ASCII number into its HEX equivalent. b ALP to convert to convert the 2 digit packed BCD number into its ASCII equivalent

10.a ALP to convert the given two digits packed BCD number into its HEX Equivalent b ALP to convert the given HEX number into its BCD equivalent

Dept. of Electronics & Communication AIT, Tumkur

Page 2: Uc Lab Manual Updated

8051 lab manual - 2011

11.a ALP to realize the 8-bit binary up counter b ALP to realize 8-bit binary down counter c ALP to realize 8-bit BCD up counter d ALP to realize 8-bit BCD down counter

12.a. ALP to transfer the data available in the program memory serially in mode1 b ALP to generate a delay of 1sec using timer0 interrupt

13. Generate different waveforms a) Ramp b) Triangular c) Square d) Sine e) Staircase using DAC interface to 8051.

14. Alphanumeric LCD panel and Hex keyboard interface to 8051

15. ADC interface to 8051 16. Stepper and DC motor interface to 8051. 17. Elevator interface to 8051.

1.a Write an ALP to transfer a block of data of ‘n’ data bytes stored in Dept. of Electronics & Communication AIT, Tumkur

Page 3: Uc Lab Manual Updated

8051 lab manual - 2011

the memory

locations starting from 30h to the memory locations starting from 40h ; To move a block of data ; Source location 30h ; Destination 40h ; No. of bytes in the block=10

cnt equ 10 ; no. of bytes=10 src equ 30h ;src=30h dst equ 40h ;dst=40h mov r0,#src ;r0 is the pointer for source location mov r1,#dst ;r1 is the pointer for destination mov r2,#cnt ;r2 as counter back: mov A,@r0 ;((r0)) ---> (A) mov @r1,A ;(A) ---> ((r1)) inc r0 ;(r0)+1 ---> (r0) inc r1 ;(r1)+1 ---> (r1) djnz r2,back ;(r2)-1 ---> (r2) if r2!=0 go to back loop: sjmp loop end

1.b Write an ALP to transfer a block of data from the source memory location starting memory location ‘x’ to the destination memory location starting from ‘y’ with overlap ; To move a block of data with overlap ; Source location 30h ; Destination 35h ; No. of bytes=10

cnt equ 10 ; no of bytes=10 src equ 30h ;src=30h dst equ 35h ;dst=35h mov r0,#src+cnt-1 ; (src)+(cnt)-1 ---> (r0), pointer for source mov r1,#dst+cnt-1 ; (dst)+(cnt)-1 ---> (r1), pointer for destinatio mov r2,#cnt ; r2 as counter back: mov A,@r0 ; ((r0)) --->(A) mov @r1,A ; (A) --->((r1)) dec r0 ; (r0)-1 --->(r0) dec r1 ; (r1)-1 --->(r1) djnz r2, back ; (r2)-1 --->(r2), If (r2)!=0 goto back loop: sjmp loop end

Dept. of Electronics & Communication AIT, Tumkur

Page 4: Uc Lab Manual Updated

8051 lab manual - 2011

1.c Write an ALP to interchange the contents of two blocks

; To interchange the contents of two bl ocks ; Block1: starts from the memory l ocation 30h ; Block2: starts from the memory l ocation 40h ; No. of bytes in each bl ock=10

cnt equ 10 ; cnt=10 blk1 equ 30h ; bl k1=30h blk2 equ 40h ; bl k2=40h mov r0,#bl k1 ; R0 as pointer for bl k1 mov r1,#bl k2 ; R1 as pointer for bl k2 mov r2,#cnt ; R2 as counter xchange: mov A,@r0 ; (( R0)) ---> (A) xch A,@r1 ; ((R1)<-->(A) mov @r0,A ; (A)--->((R0)) inc r0 ; (R0)+1--->(R0) inc r1 ; (r1)+1--->(R1) djnz r2,xchange ; (R2)-1--->(R2) If (R2)!=0 go to exchange loop: sjmp loop end

2.a Write an ALP to add ‘n’ one byte binary numbers.

; Prg to add 'n' one byte no.s ; No. of bytes=n=10 ; Source location=30h-39h ; Resulted sum in 3ah ; Resulted carry in 3bh

src equ 30h ; src=30h n equ 10 ; n=10 mov r0,#src ; r0 as pointer to the source mov r2,#n-1 ; r2 as counter mov r3,#00h ; r3 stores the carry mov a,@r0 ; initial izes the sum to the 1st no. addn: inc r0 ; to move to the next M.L clr c add a,@r0 ; updates the sum jnc skip inc r3 ; if carry is generated, increment r3 skip: djnz r2,addn Dept. of Electronics & Communication AIT, Tumkur

Page 5: Uc Lab Manual Updated

8051 lab manual - 2011

inc r0 mov @r0,a ; stores the sum inc r0 mov a, r3 mov @r0,a ; stores the carry end

2.b Write an ALP to add two multibyte numbers ; Prg to add two multibyte no.s ; No. of bytes=5 ; No.1: 30h-34h LSB:(30h) MSB:(34h) ; No.2: 40h-44h LSB:(40h) MSB:(44h) ; Result: 40h-45h LSB:(40h) MSB:(45h) no1 equ 30h ; no1=30h no2 equ 40h ; no2=40h n equ 5 ; n=5, no. of bytes mov r0,#no1 ; r0 as pointer to no.1 mov r1,#no2 ; r1 as pointer to no.2 mov r2,#n ; r2 as counter clr c addn: mov a,@r0 addc a,@r1 mov @r1,a ; stores the result inc r0 inc r1 djnz r2,addn mov a,#00h addc a,#00h inc r1 mov @r1,a ; stores the carry end

2. c Write an ALP to subtract a multibyte number from another multibyte number ; Prg to subtract a multibyte number from another multibyte number ; No. of bytes=5 ; no.1: 30h-34h LSB:(30h) MSB:(34h) ; no.2: 40h-44h LSB:(40h) MSB:(44h) ; Result: 40h-45h LSB:(40h) MSB:(45h)

Dept. of Electronics & Communication AIT, Tumkur

Page 6: Uc Lab Manual Updated

8051 lab manual - 2011

no1 equ 30h ; no1=30h no2 equ 40h ; no2=40h n equ 5 ; n=5, no. of bytes

mov r0,#no1 ; r0 as pointer to no.1 mov r1,#no2 ; r1 as pointer to no.2 mov r2,#n ; r2 as counter clr c addn: mov a,@r0 subb a,@r1 mov @r1,a ; stores the result inc r0

inc r1 djnz r2,addn mov a,#00h addc a,#00h inc r1 mov @r1,a ; stores the carry end

2.d Write an ALP to add ‘n’ 2 digit packed BCD numbers ; Prg to add 'n' two Digit packed BCD no.s ; Source l ocation 35h-39h ; Result 3ah-3bh sum: 3ah carry: 3bh

loc equ 35h ; loc=35h n equ 5 ; n=5

mov r0,#loc ;(r0)=loc, r0 as pointer mov r2,#n ; r2 as counter clr a ; a stores the sum mov r3,#00h ; r3 stores the carry

addn: add a,@r0 da a jnc skip inc r3 ; updates the carry skip: inc r0 djnz r2,addn mov @r0,a mov a,r3 addc a,#00h inc r0 mov @r0,a end

Dept. of Electronics & Communication AIT, Tumkur

Page 7: Uc Lab Manual Updated

8051 lab manual - 2011

3.a Write an ALP to Multiply two binary numbers ; To multiply two multibyte no.s ; no1: 30h-31h MSB: 31h LSB: 30h ; no2: 32h-33h MSB: 32h LSB: 33h ; Result 34h-37h MSB: 37h LSB: 34h multiplier_size equ 2 multipl icand_size equ 2 multiplier_loc equ 33h multipl icand_loc equ 31h result_loc equ 35h mov r0,#multiplier_loc ; pointer to the multiplier mov r2,#multiplier_size ; counter to the multiplier mov SP,#result_loc-1 multp: mov r1,#multiplicand_loc ; pointer to the multiplicand

mov r4,#multiplicand_size ; counter to the multiplicand mov r3,#00h ; stores the MSB of the last mult. multc: mov 0f0h,@r0 ; multiplier to B mov a,@r1 ; multiplicand to A mul AB

add a,r3 push 0e0h

mov r3,0f0h ; B to r3 inc r1 djnz r4,multc

push 3 ; r3 to stack inc r0

djnz r2,multp mov r0,#result_loc+1

clr c mov r2,#2 ; r2 as counter result: mov a,r0

mov r1,a inc r1

inc r1 mov a,@r0

addc a,@r1 mov @r0,a mov @r1,#00h inc r0 djnz r2,result inc r1

mov a,@r1 Dept. of Electronics & Communication AIT, Tumkur

Page 8: Uc Lab Manual Updated

8051 lab manual - 2011

addc a,#00h mov @r0,a mov @r1,#00h end

3.b Write an ALP to divide a binary number by another binary number ; To divide an 8 bit no. by another 8 bit no. ; no.1=(30h) no.2=(31h) ; Quotient=(32h) reminder=(33h)

no1 equ 30h ; no1=30h no2 equ 31h ; no2=31h mov r0,#no1

mov a,@r0 ; no1 to A mov r1,#no2 mov 0f0h,@r1 ; no2 to B div AB inc r1 mov @r1,a ; stores the quotient inc r1 mov @r1,0f0h ; stores the reminder end

4.a Write an ALP to find square of the given number ; To find the square of the given number ; No. is stored in the memory location 30h ; result in 31h and 32h, MSB in 32h and LSB in 31h

loc equ 30h ; loc=30h mov r0,#loc ; loc ---> (30h) mov a,@r0 ; ((r0))--->(A) mov 0f0h,@r0 ; ((r0))--->(B) mul AB inc r0 ; to move to the next M.L mov @r0,A ; LSB to the next immediate M.L inc r0 mov @r0,0f0h ; MSB end

Dept. of Electronics & Communication AIT, Tumkur

Page 9: Uc Lab Manual Updated

8051 lab manual - 2011

4.b Write an ALP to find cube of the given number

loc equ 30h ; loc=30h mov r0,#loc ; (r0)=loc mov a, @r0 ; loads the no. to the Acc. mov 0f0h,@r0 ; loads the no. to B reg. mov sp,#30h mul AB inc r0 ; to move to the next. M.L mov @r0,a ; stores the LS bit if the square in 31h inc r0 mov @r0,0f0h ; MS bit of the square mov r0,#loc mov r1,#loc+1 ; address of the LS bit of the square mov r3,#00h mov r2,#2 ; r2 as counter

cube: mov 0F0h,@r0 ; loads the no. to B reg. mov A,@r1 mul AB add A,r3 push 0e0h mov r3,0f0h inc r1 djnz r2,cube push 0f0h end

Dept. of Electronics & Communication AIT, Tumkur

Page 10: Uc Lab Manual Updated

8051 lab manual - 2011

5.a Write an ALP to find the largest in the given array ; No. of elements =10

cnt equ 10 ;cnt=10 src equ 30h ;src=30h mov r0,#src ;R0 as pointer for the array mov r2,#cnt-1 ; r2 as counter mov a,@r0 mov r3,a ; r3 stores the largest no. comp: i nc r0 ; to move to the next location clr c ; 0h--->(c) mov a,r3 ; largest no. is loaded onto A subb a,@r0 ;(A)-((r0))-(c)--->(A),no carry ==> (A)>=((R0)) jnc skip ; carry ==> ((ro))>(A) mov a,@r0 ; updates the largest no. mov r3,a ; stores the largest no. in r3 skip: djnz r2,comp mov a,r3 inc r0 ; to move to the next M.L mov @r0,a ; copy the largest no. to the next ; Memory location i n the array loop: sjmp loop end

5.b Write an ALP to find the smallest in the given array ; No. of elements =10

cnt equ 10 ;cnt=10 src equ 30h ;src=30h mov r0,#src ;R0 as pointer for the array mov r2,#cnt-1 ; r2 as counter mov a,@r0 mov r3,a ; r3 stores the small est no. comp: inc r0 ; to move to the next location clr c ; 0h--->(c) mov a,r3 ; smal l est no. is l oaded onto A subb a,@r0 ;(A)-((r0))-(c)--->(A),no carry ==> (A)>=((R0)) jc skip ; carry ==> ((ro))>(A) mov a,@r0 ; updates the small est no. mov r3,a ; stores the smal l est no. in r3 skip: djnz r2,comp mov a,r3 Dept. of Electronics & Communication AIT, Tumkur

Page 11: Uc Lab Manual Updated

8051 lab manual - 2011

inc r0 ; to move to the next M.L mov @r0,a ; copy the smal l est no. to the next ; Memory l ocati on in the array loop: sjmp loop end

6.a Write an ALP to sort the given array in ascending order ; Source l ocation 40h-44h ; No. of bytes=5

loc equ 40h ; loc=40h n equ 5 ; n=5 mov r2,#n-1 ; no. 0f passes=n-1 pass: mov a,r2 ; r2 as counter for external loop mov r3,a ; r3 as counter for the inner loop mov r0,#loc ; r2 as pointer back: mov a,@r0 inc r0 clr c subb a,@r0 jc noswap ; carry ==> (a)<((r0)) mov a,@r0 ; no carry ==> (A)>=(r0) , exchange dec r0 xch a,@r0 inc r0 mov @r0,a noswap: djnz r3,back djnz r2,pass end

6.b Write an ALP to sort the given array in descending order ; Source l ocation 40h-44h ; No of bytes=5

loc equ 40h ; loc=40h n equ 5 ; n=5 mov r2,#n-1 ; no. 0f passes=n-1 pass: mov a,r2 mov r3,a ; r2 as counter for the inner loop mov r0,#loc ; r2 as pointer Dept. of Electronics & Communication AIT, Tumkur

Page 12: Uc Lab Manual Updated

8051 lab manual - 2011

back: mov a,@r0 inc r0 clr c subb a,@r0 jnc noswap mov a,@r0 dec r0 xch a,@r0 inc r0 mov @r0,a noswap: djnz r3,back djnz r2,pass end

7.a Write an ALP to check whether the given no. is odd or even. ; To check whether the given no. is odd or even ; No. avail able in 30h ; If even, store 0FFh in 31h else store 00h in 31h loc equ 30h ; loc=30h mov R0,#loc ; r0 as pointer mov A,@r0 ; No. is loaded into the Acc. rrc A ; rotate right, along with the carry inc r0 jc odd ; carry ==> no. is odd mov @r0,#0ffh sjmp exit odd: mov @r0,#11h exit: end

7.b Write an ALP to check whether the given number is positive or negative. ; No. avail able in 30h ; If +ve, store FFh in 31h else store 11h in 31h

loc equ 30h ; loc=30h mov R0,#loc ; r0 as pointer mov A,@r0 ; No. is loaded into the Acc. rlc A ; rotate right, along with the carry inc r0 jc neg ; carry ==> no. is negative mov @r0,#0ffh sjmp exit neg: mov @r0,#11h exit: sjmp exit

Dept. of Electronics & Communication AIT, Tumkur

Page 13: Uc Lab Manual Updated

8051 lab manual - 2011

end

8.a Write an ALP to check whether the given number is valid number in” 2 out of 5 code” or not ; if the no. is valid 2 out0f 5 code, store FFh in 31h else store 11h in 31h

loc equ 30h ; loc=30h mov r0,#loc ; r0 as pointer mov a,@r0 ; no. to the Acc. ANL a,#0e0h jnz notval id ; checks the 1st 3bits mov a,@r0 mov r2,#5 ; r2 as counter mov r1,#00h ; r0 stores no. of ones clr c check: rrc a jnc skip inc r1 ; if carry increment r1 skip: djnz r2,check mov A,r1 clr c subb A,#02h jnz notvalid ; (a)!= 0 ===> (r2)!= 2 no. is not valid. inc r0 mov @r0,#0ffh ; valid 2 out of 5 code sjmp exit notval id: inc r0 mov @r0,#11h ; in valid 2 out of 5 code exit: sjmp exit end

8.b Write an ALP to find the number of ones and zeroes in the given number. ; To find the no. of ones and zeroes in the given no.

mov dptr, #8000h ; dptr as pointer movx A,@dptr ; ((dptr))--->(A) mov r2,#8 ; R2 as counter clr c ; clears the contents of the carry flag check: rlc A ; rotate left along with the carry jnc zeroes

Dept. of Electronics & Communication AIT, Tumkur

Page 14: Uc Lab Manual Updated

8051 lab manual - 2011

inc 31h ; 31h stores the no. of ones djnz r2,check ; decrement R2, go to check if (r2)!=0

sjmp exitzeroes: inc 32h ; 32h stores no. of zeroes djnz r2,check ; decrement R2, go to check if (r2)!=0 exit: sjmp exit end

9.a Write an ALP to convert the given ASCII number into its HEX equivalent. ; ALP to convert to convert the given ASCII number ; into the equivalent HEX number

loc equ 30h mov r0,#loc mov A,@r0 mov R2,A cjne A,#40h,skip sjmp not_valid ; if num=40h, go to not_val id skip: jc next1 ; if num<40h, go to next cjne A,#47h,next sjmp not_valid ; if num=47h, go to not_val id next: jnc not_vali d ; if num>47h, go to not_valid clr c subb A,#07h mov R2,A next1: mov A,@r0 cjne A,#29h,next2 sjmp not_valid ; if num=29h, go to not_valid next2: jc not_valid ; if num<29h, go to not_val id mov A,r2 subb A,#30h inc r0 mov @r0,A inc r0 mov @r0,#0ffh ; valid input sjmp exit not_val id: inc r0 inc r0 mov @r0,#11h ; invalid input exit: sjmp exit end

Dept. of Electronics & Communication AIT, Tumkur

Page 15: Uc Lab Manual Updated

8051 lab manual - 2011

9.b Write an ALP to convert to convert the 2 digit packed BCD number into its ASCII equivalent. ; ALP to convert the given 2 digit packed BCD number into its ASCII equivalent loc equ 30h mov r0,#loc mov a,@r0 ; number to 'A' register swap a anl a,#0fh ; to consider the MS nibble of the number add a,#30h inc r0 mov @r0,A dec r0 mov a,@r0 anl a,#0fh ; LS nibble of the number add a,#30h inc r0 inc r0 mov @r0,a end

10.a Write an ALP to convert the given two digits packed BCD number into its HEX equivalent . ; ALP to convert two digits packed BCD number into the equivalent HEX number

loc equ 30h ; location of the number mov r0,#loc ; pointer to the number mov A,@r0 ; number to reg.A anl A,#0fh ; to unpack the BCD mov R2,A ; LS byte of the BCD mov A,@r0 ; number to the reg.A swap A ; exchange the nibbles of A anl A,#0fh ; to unpack the BCD mov 0f0h,#0ah mul AB ; MSB*10 add a,r2 ; hex= MSB*10+LSB inc r0 mov @r0,A end Dept. of Electronics & Communication AIT, Tumkur

Page 16: Uc Lab Manual Updated

8051 lab manual - 2011

10.b Write an ALP to convert the given HEX number into its BCD equivalent. ; ALP to convert the given HEX number into its decimal equivalent loc equ 30h ; location of the number mov r0,#loc ; pointer to the number mov A,@r0 ; number to the reg.A mov 0f0h,#64h ; 64h=100 in decimal div AB ; divides the number by 100 inc r0 mov @r0,A ; stores the MSB of decimal equivalent mov A,0f0h mov 0f0h,#0ah ; 0Ah=10 in decimal div AB ; divides the number by 10 inc r0 mov @r0,A inc r0 mov @r0,0f0h end

11.a Write an ALP to realize the 8-bit binary up counter ; Alp to realize binary up counter

mov A,#00h ; initialization back: mov p0,A ; moves the contents of ‘A’ to port P0 acall DELAY ; calls the subroutine “DELAY” inc a ; increments ‘A’ sjmp back ; go to back DELAY: mov r4,#100 back3: mov r3,#225 back2: mov r2,#225 back1: djnz r2,back1 djnz r3,back2 djnz r4,back3 ret end

Dept. of Electronics & Communication AIT, Tumkur

Page 17: Uc Lab Manual Updated

8051 lab manual - 2011

11.b Write an ALP to realize 8-bit binary down counter ; Alp to realize binary down counter mov A,#0ffh ; initial ization back: mov p0,A ; moves the contents of ‘A’ to port P0 acall DELAY ; calls the subroutine “DELAY” dec a ; decrements ‘A’ sjmp back ; go to back DELAY: mov r4,#100 back3: mov r3,#225 back2: mov r2,#225 back1: djnz r2,back1 djnz r3,back2 djnz r4,back3 ret end

11.c Write an ALP to realize 8-bit BCD up counter mov a,#00h ; initial ization back: mov p0,A ; moves the contents of ‘A’ to port P0 acall DELAY ; calls the subroutine “DELAY” add a,#01h ; add ‘1’ to ‘A’ da a ; decimal adjust ‘A’ sjmp back ; go to back DELAY: mov r4,#100 back3: mov r3,#225 back2: mov r2,#225 back1: djnz r2,back1 djnz r3,back2 djnz r4,back3 ret end

11.d Write an ALP to realize 8-bit BCD down counter

mov a,#99h ; initial ization

Dept. of Electronics & Communication AIT, Tumkur

Page 18: Uc Lab Manual Updated

8051 lab manual - 2011

back: mov p0,A ; moves the contents of ‘A’ to port P0 acall DELAY ; calls the subroutine “DELAY” add a,#99h ; adds “99h” to ‘A’ register da a ; decimal adjust ‘A’ sjmp back ; go to back DELAY: mov r4,#100 back3: mov r3,#225 back2: mov r2,#225 back1: djnz r2,back1 djnz r3,back2 djnz r4,back3 ret end

12.a Write an ALP to transfer the data available in the program memory serially in mode1

mov DPTR,#mydata ; address of the data to be transmitted clr A ; clears the contents of A mov TMOD,#20h ; timer0 in mode2 mov TH1,#0fdh ; baud rate=9600 mov SCON,#40h ; mode1 serial data transfer setb TR1 ; start the timer mov R3,#9 ; R3 as counter back: movc A,@A+DPTR ; ((A)+(DPTR)) (A) mov SBUF,A ; (A) (DPTR) wait: jnb TI,wait clr TI clr A inc DPTR ; to move to the next M.L djnz R3,back mydata: db “GOOD LUCK”,0 ; data to be transmitted serially end

12.b Write an ALP to generate a delay of 1sec using timer0 interrupt

org 0000h ljmp main org 000bh ljmp ISR org 0030h main: mov TMOD,#00000010h ; timer0 in mode2

Dept. of Electronics & Communication AIT, Tumkur

Page 19: Uc Lab Manual Updated

8051 lab manual - 2011

mov TH0,#0d2h ; to create a delay of 50 µs mov IE,#10000010b ; enables timer0 interrupt mov R0,#00h mov R1,#00h mov A,#00h setb TR0 ; to start the timer wait: sjmp wait ; wait forever ISR: clr TR0 ; to stop the timer inc R1 cjne R1,#100,return ; if (R1)/=100 go to return mov R1,#00h ; to reset R1 inc R0 cjne R0,#200,return ; if (R0)/=200 go to return cpl p0.0 return: setb TR0 ; to start the timer reti ; return to main end

8051 connection to DAC808:

13.a1)Write a C program to generate square wave ( variable frequency & amplitude ) using DAC interface to 8051.Dept. of Electronics & Communication AIT, Tumkur

Page 20: Uc Lab Manual Updated

8051 lab manual - 2011

#include <REG51xD2.H>#include "lcd.h"

sbit Amp = P3^3; /* Switch to vary amplitude */sbit Fre = P3^2; /* Switch to vary frequency */

main(){ unsigned char on = 0x33,off=0x00; unsigned int fre = 1; InitLcd(); /* Initialize LCD */ WriteString("Squarewave"); while(1) { if(!Amp) { while(!Amp);

on+=0x33; }

if(!Fre) { if(fre > 8)

fre = 1;while(!Fre);

fre *= 2; } P0=on; P1=on; delay(fre); P0 = off; P1 = off; delay(fre); }}

void delay(unsigned int x) /* delay function */{ unsigned char i,j; for(i=0 ; i<x ; i++) for (j=0 ; j<7; j++); }

RESULT : Square wave (variable amplitude & frequency) is generated using DAC interface to 8051.

Dept. of Electronics & Communication AIT, Tumkur

Page 21: Uc Lab Manual Updated

8051 lab manual - 2011

13.a2)Write a C program to generate square wave ( variable duty cycle & amplitude ) using DAC interface to 8051.

#include <REG51xD2.H>#include "lcd.h"

void T0_on ();void T1_off(); sbit Amp = P3^3;

main(){ unsigned char on = 0x33, off = 0x00; InitLcd(); /* Initialize LCD */ WriteString("Square Wave"); /* Display on LCD */ while(1) { if(!Amp) { while(!Amp); on += 0x33; } P0 = on; P1 = on; T0_on(); P0 = off; P1 = off; T1_off(); }}

void T0_on(){ TMOD = 0x11; TL0 = 0x6D; TH0 = 0xFF;Dept. of Electronics & Communication AIT, Tumkur

Page 22: Uc Lab Manual Updated

8051 lab manual - 2011

TR0 = 1; while (TF0 == 0); TR0 = 0; TF0 = 0;}void T1_off(){ TMOD = 0x11; TL1 = 0xDA; TH1 = 0xFF; TR1 = 1; while (TF1 == 0); TR1 = 0; TF1 = 0;}RESULT : Square wave (variable amplitude & duty cycle) is generated using DAC interface to 8051.

13. b) Write a C program to generate triangular wave using DAC interface to 8051.

#include <REG51xD2.H>#include "lcd.h"

main(){ unsigned char i=0; InitLcd(); /* Initialise LCD */ WriteString("Triangular Wave"); /* Display on LCD */ P0 = 0x00; /* P0 as Output port */ P1 = 0x00; while(1) { for(i=0;i<0xff;i++) { /* Generate ON pulse */ P1 = i; P0 = i; }

Dept. of Electronics & Communication AIT, Tumkur

Page 23: Uc Lab Manual Updated

8051 lab manual - 2011

for(i=0xfe;i>0x00;i--) /* Generate OFF pulse */ { P0 = i; P1 = i; } } }

RESULT: Triangular wave is generated using DAC interface to 8051.

P0 0xFF = 5V

T t(ms)

13.c) Write a C program to generate ramp wave using DAC interface to 8051.

#include <REG51xD2.H>#include "lcd.h"

main(){ unsigned char i; InitLcd(); /* Initialize LCD */ WriteString("Ramp Wave"); /* Display on LCD */ P0 = 0x00; /* P0 as Output port */ P1 = 0x00; while(1) { for(i=0;i<0xff;i++) { /* Generate ON pulse */ P1 = i; P0 = i; } // for(i=0xff;i>0x00;i--) /* Generate OFF pulse */ //{ // P0 = i; // P1 = i; //} } }RESULT: Ramp wave is generated using DAC interface to 8051.

Dept. of Electronics & Communication AIT, Tumkur

Ton Toff

Page 24: Uc Lab Manual Updated

8051 lab manual - 2011

P0 0xFF = 5V

t(ms)

13.d) Write a C program to generate sine wave using DAC interface to 8051.

#include <REG51xD2.H>#include "lcd.h"

main(){unsigned char i;unsigned int tab[40]={128, 150, 172, 192, 210, 226, 238, 248, 254, 255, 254, 248, 238, 226, 210, 192, 172, 150, 128, 105, 84, 64, 45, 37, 30, 29, 17, 8, 2, 0, 2, 8, 17, 29, 30, 37, 45, 64, 84, 105};InitLcd(); /* Initialize LCD */WriteString("Sine Wave"); /* Display on LCD */P0 = 0x00; /* P0 as Output port */P1 = 0x00;while(1){for(i=0;I<40;i++){P0=tab[i];P1=tab[i];}}}

RESULT: Sine wave is generated using DAC interface to 8051.

Dept. of Electronics & Communication AIT, Tumkur

Page 25: Uc Lab Manual Updated

8051 lab manual - 2011

14. Write a C program to display the key pressed on a Hex – Keypad using the LCD & Hex –Keypad interface to 8051.

#include <REG51XD2.H>#include "lcd.h"unsigned char rows,columns,result,temp = 0;

void delay();void Display();void KeyScan();

void main(){ P0 = 0xff; P1 = 0x00; InitLcd(); WriteString("KEY PRESSED="); while(1) { KeyScan();

WriteString("KEY PRESSED="); Display();

}}

void KeyScan(){again: columns = 0x77; rows = 0x04; result = 0x0c; next: P1 = columns; columns >>=1; if(CY) columns = columns | 0x08 ;

Dept. of Electronics & Communication AIT, Tumkur

Page 26: Uc Lab Manual Updated

8051 lab manual - 2011

temp = P0; temp = (temp & 0x0f); if(temp != 0x0f) {rot: temp >>= 1;

if(!CY){ ClrLcd(); return;}else{ result += 1; goto rot; }

} else { result -= 0x04; rows --; if(rows == 0) goto again; else goto next; } }

void Display(){ if(result > 0x09) { result += 0x37;

WriteChar(result);delay();

} else { result += 0x30;

WriteChar(result);delay();

}}

void delay(){ unsigned int i; for(i = 0; i <= 20000; i ++);

Dept. of Electronics & Communication AIT, Tumkur

Page 27: Uc Lab Manual Updated

8051 lab manual - 2011

}

RESULT: The value of Key pressed on Hex keypad is displayed on the LCD using Hex keypad & LCD interface to 8051.

15. Write a C program for Temperature control using External ADC & Temperature control interface to 8051.

# include <REG51XD2.H>#include "lcd.h"unsigned int Adc;unsigned char Low_adc,High_adc,relay;read_adc(); main(){ float Temp,Vol,Res;

unsigned char Temp1,Temp2,Temp3; P0 = 0xFF ; P2 = 0xFF ; P1_1 = 0 ; P2_3 = 0 ;

relay = 10; while(1)

{ read_adc(); Adc = High_adc; Adc <<= 8; Adc = Adc | Low_adc;

Dept. of Electronics & Communication AIT, Tumkur

Page 28: Uc Lab Manual Updated

8051 lab manual - 2011

if( (Adc > 0x656) && (relay != 0)) {

ClrLcd(); WriteString("RELAY OFF"); P1_1 = 0; relay = 0; }

else if ( (Adc < 0x5b9) && (relay!= 1)) {

ClrLcd(); WriteString("RELAY ON"); P1_1 = 1; relay = 1; }

Vol =-((Adc/10)*0.000488); Res =((100*(1.8-Vol)-100*Vol)*100)/(100*Vol + 100*(1.8+Vol));

Res = Res - 100; Temp = Res/ 0.384;

Temp1 = Temp; Temp2 = 0x30 + (Temp1 / 0x0A);

Temp3 = 0x30 + (Temp1 % 0x0A); GotoXY(0,1); WriteString("Temperature "); WriteChar(Temp2); WriteChar(Temp3); WriteString("'C");

}}

read_adc(){ unsigned char status; P2_3 = 1 ; status = P1; while((status & 0x01) != 0x01) {status = P1; } P2_2 = 0; P2_0 = 0; Low_adc = P0; P2_0 = 1; P2_1 = 0; High_adc = P0; High_adc = High_adc & 0x0F; P2_1 = 1; P2_2 = 1; P2_3 = 0; }

Dept. of Electronics & Communication AIT, Tumkur

Page 29: Uc Lab Manual Updated

8051 lab manual - 2011

RESULT: Temperature is maintained in the range 450 C to 500 C using the external ADC & temperature control interface to 8051.

16. Write a C program to rotate the stepper motor in clockwise / anticlockwise direction using stepper motor interface to 8051.

#include <REG51xD2.H>#include "lcd.h"

main(){ unsigned char Val, i, dir = 0; while(1) { if(dir)

{ InitLcd();

WriteString(“Clockwise”); Val = 0x88;

for(i=0;i<4;i++) { P0 = Val; Val = Val>>1; delay(575); }}else {

InitLcd(); WriteString(“AntiClockwise”);

Val = 0x11; Dept. of Electronics & Communication AIT, Tumkur

Page 30: Uc Lab Manual Updated

8051 lab manual - 2011

for(i=0;i<4;i++) { P0 = Val; Val = Val<<1; delay(575); } }

}}

void delay(unsigned int x) { for(;x>0;x--);}

RESULT: The Stepper motor is rotated continuously either in Clockwise or Anticlockwise direction depending on the condition of direction flag using Stepper Motor Interface to 805116. b) Write a C program to rotate the stepper motor in clockwise & anticlockwise direction using stepper motor interface to 8051.

#include <REG51xD2.H>#include "lcd.h"

main(){ unsigned char Val, i, j; while(1) { InitLcd(); WriteString(“Clockwise”); for (j=0 ; j<50 ; j++) { Val = 0x88; for(i=0;i<4;i++) { P0 = Val; Val = Val>>1; delay(575); } } ClrLcd(); WriteString(“AntiClockwise”); for (j=0 ; j<50 ; j++) { Val = 0x11; for(i=0;i<4;i++) {

P0 = Val;

Dept. of Electronics & Communication AIT, Tumkur

Page 31: Uc Lab Manual Updated

8051 lab manual - 2011

Val = Val<<1; delay(575);

} } }}

void delay(unsigned int x) { for(;x>0;x--);}

RESULT: The Stepper motor is rotated continuously in Clockwise & Anticlockwise direction using Stepper Motor Interface to 8051.

PROGRAM: #include<reg51xd2.h> sbit inc=P3^2; sbit dec=P3^3; main() { unsigned char i=0x80; P0=0x0f; while(1) { if (inc==0) Dept. of Electronics & Communication AIT, Tumkur

Page 32: Uc Lab Manual Updated

8051 lab manual - 2011

{ while(inc==0); if (i>10) i=i-10; } if (dec==0) { while(dec==0); if (i<0xf0) i=i+10; } P0=i; } }

17. Write a C program to simulate the action of an Elevator by using the Elevator Interface to 8051.#include <REG5XD2.H>void delay(unsigned int);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; /*o/p port*/ P0 = 0x0f0; while(1) { P1 = 0x0f; /*lower nibble of P1 are i/p’s*/ ReqFlr = P1 | 0x0f0; /*read request floor through P1*/ while(ReqFlr == 0x0ff) /*wait till the request is made*/

ReqFlr = P1 | 0x0f0; ReqFlr = ~ReqFlr; if(CurFlr == ReqFlr) /* If Request floor is equal to Current Floor

*/{

Dept. of Electronics & Communication AIT, Tumkur

Page 33: Uc Lab Manual Updated

8051 lab manual - 2011

P0 = FClr[CurFlr]; /* Clear Floor Indicator */ continue; /* 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--) /* Move the indicator down */ { P0 = 0x0f0|j; j--; delay(25000); }}else /* 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--) /* Move the indicator Up */ { P0 = 0x0f0 | j;

j++; delay(25000); }

}CurFlr = ReqFlr; /* Update Current floor */P0 = FClr[CurFlr]; /* Clear the indicator */

}}

void delay(unsigned int x){ for(;x>0;x--);}

RESULT: The elevator action is simulated by using an elevator interface to 8051.

Dept. of Electronics & Communication AIT, Tumkur