You are on page 1of 33

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

11.a ALP to realize the 8-bit binary up counter

Dept. of Electronics & Communication


AIT, Tumkur

8051 lab manual - 2011


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 the memory

Dept. of Electronics & Communication


AIT, Tumkur

8051 lab manual - 2011

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

back:

loop:

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 src equ 30h dst equ 35h mov r0,#src+cnt-1 ; no of bytes=10 ;src=30h ;dst=35h ; (src)+(cnt)-1 ---> (r0), pointer for ; (dst)+(cnt)-1 ---> (r1), pointer for ; r2 as counter ; ((r0)) --->(A) ; (A) --->((r1)) ; (r0)-1 --->(r0) ; (r1)-1 --->(r1) ; (r2)-1 --->(r2), If (r2)!=0 goto back

source mov r1,#dst+cnt-1 destinatio back: mov r2,#cnt mov A,@r0 mov @r1,A dec r0 dec r1 djnz r2, back sjmp loop end

loop:

1.c Write an ALP to interchange the contents of two blocks Dept. of Electronics & Communication
AIT, Tumkur

8051 lab manual - 2011

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

xchange:

exchange loop:

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 n equ 10 mov r0,#src mov r2,#n-1 mov r3,#00h mov a,@r0 addn: inc r0 clr c add a,@r0 jnc skip inc r3 djnz r2,addn inc r0 mov @r0,a ; src=30h ; n=10 ; r0 as pointer to the source ; r2 as counter ; r3 stores the carry ; initial izes the sum to the 1st no. ; to move to the next M.L ; updates the sum ; if carry is generated, increment r3 ; stores the sum

skip:

Dept. of Electronics & Communication


AIT, Tumkur

8051 lab manual - 2011 inc r0 mov a, r3 mov @r0,a end

; stores the carry

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=30h ; no2=40h ; n=5, no. of bytes ; r0 as pointer to no.1 ; r1 as pointer to no.2 ; r2 as counter

no1 equ 30h no2 equ 40h n equ 5 mov r0,#no1 mov r1,#no2 mov r2,#n clr c addn: mov a,@r0 addc a,@r1 mov @r1,a inc r0 inc r1 djnz r2,addn mov a,#00h addc a,#00h inc r1 mov @r1,a end

; stores the result

; stores the carry

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) no1 equ 30h ; no1=30h no2 equ 40h ; no2=40h n equ 5 ; n=5, no. of bytes Dept. of Electronics & Communication
AIT, Tumkur

8051 lab manual - 2011 mov r0,#no1 mov r1,#no2 mov r2,#n clr c addn: subb a,@r1 mov @r1,a inc r0 inc r1 djnz r2,addn mov a,#00h addc a,#00h inc r1 mov @r1,a end ; r0 as pointer to no.1 ; r1 as pointer to no.2 ; r2 as counter mov a,@r0 ; stores the result

; stores the carry

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 n equ 5 ; n=5 mov r0,#loc mov r2,#n clr a mov r3,#00h addn: add a,@r0 da a jnc skip inc r3 skip: inc r0 djnz r2,addn mov @r0,a mov a,r3 addc a,#00h inc r0 mov @r0,a end ; loc=35h ;(r0)=loc, r0 as pointer ; r2 as counter ; a stores the sum ; r3 stores the carry

; updates the carry

3.a Write an ALP to Multiply two binary numbers

Dept. of Electronics & Communication


AIT, Tumkur

8051 lab manual - 2011 ; ; ; ; 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. 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 addc a,#00h mov @r0,a Dept. of Electronics & Communication
AIT, Tumkur

multc:

8051 lab manual - 2011 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 no2 equ 31h mov r0,#no1 mov a,@r0 mov r1,#no2 mov 0f0h,@r1 div AB inc r1 mov @r1,a inc r1 mov @r1,0f0h end ; no1=30h ; no2=31h ; no1 to A ; no2 to B ; stores the quotient ; stores the reminder

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

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

Dept. of Electronics & Communication


AIT, Tumkur

8051 lab manual - 2011

cube:

loc equ 30h mov r0,#loc mov a, @r0 mov 0f0h,@r0 mov sp,#30h mul AB inc r0 mov @r0,a inc r0 mov @r0,0f0h mov r0,#loc mov r1,#loc+1 mov r3,#00h mov r2,#2 mov 0F0h,@r0 mov A,@r1 mul AB add A,r3 push 0e0h mov r3,0f0h inc r1 djnz r2,cube push 0f0h end

; loc=30h ; (r0)=loc ; loads the no. to the Acc. ; loads the no. to B reg. ; to move to the next. M.L ; stores the LS bit if the square in 31h ; MS bit of the square ; address of the LS bit of the square ; r2 as counter ; loads the no. to B reg.

5.a Write an ALP to find the largest in the given array

; No. of elements =10 Dept. of Electronics & Communication


AIT, Tumkur

8051 lab manual - 2011

cnt equ 10 src equ 30h mov r0,#src mov r2,#cnt-1 mov a,@r0 mov r3,a comp: i nc r0 clr c mov a,r3 subb a,@r0 (A)>=((R0)) jnc skip mov a,@r0 mov r3,a skip: djnz r2,comp mov a,r3 inc r0 mov @r0,a loop: sjmp loop end

;cnt=10 ;src=30h ;R0 as pointer for the array ; r2 as counter ; r3 stores the largest no. ; to move to the next location ; 0h--->(c) ; largest no. is loaded onto A ;(A)-((r0))-(c)--->(A),no carry ==> ; carry ==> ((ro))>(A) ; updates the largest no. ; stores the largest no. in r3 ; to move to the next M.L ; copy the largest no. to the next ; Memory location i n the array

5.b Write an ALP to find the smallest in the given array

; No. of elements =10 cnt equ 10 src equ 30h mov r0,#src mov r2,#cnt-1 mov a,@r0 mov r3,a inc r0 clr c mov a,r3 subb a,@r0 jc skip mov a,@r0 mov r3,a djnz r2,comp mov a,r3 inc r0 Dept. of Electronics & Communication
AIT, Tumkur

;cnt=10 ;src=30h ;R0 as pointer for the array ; r2 as counter ; r3 stores the small est no. ; to move to the next location ; 0h--->(c) ; smal l est no. is l oaded onto A ;(A)-((r0))-(c)--->(A),no carry ==> ; carry ==> ((ro))>(A) ; updates the small est no. ; stores the smal l est no. in r3

comp:

(A)>=((R0))

skip:

; to move to the next M.L

8051 lab manual - 2011 mov @r0,a loop: sjmp loop end ; copy the smal l est no. to the next ; Memory l ocati on in the array

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

pass: back:

; carry ==> (a)<((r0)) ; no carry ==> (A)>=(r0) , exchange

noswap:

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 n equ 5 mov r2,#n-1 mov a,r2 mov r3,a mov r0,#loc mov a,@r0 inc r0 clr c subb a,@r0 ; loc=40h ; n=5 ; no. 0f passes=n-1 ; r2 as counter for the inner loop ; r2 as pointer

pass: back:

Dept. of Electronics & Communication


AIT, Tumkur

8051 lab manual - 2011 jnc noswap mov a,@r0 dec r0 xch a,@r0 inc r0 mov @r0,a djnz r3,back djnz r2,pass end

noswap:

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 mov R0,#loc mov A,@r0 rrc A inc r0 jc odd mov @r0,#0ffh sjmp exit mov @r0,#11h end ; loc=30h ; r0 as pointer ; No. is loaded into the Acc. ; rotate right, along with the carry ; carry ==> no. is odd

odd: exit:

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 mov R0,#loc mov A,@r0 rlc A inc r0 jc neg mov @r0,#0ffh sjmp exit mov @r0,#11h sjmp exit end ; loc=30h ; r0 as pointer ; No. is loaded into the Acc. ; rotate right, along with the carry ; carry ==> no. is negative

neg: exit:

8.a Write an ALP to check whether the given number is valid number in 2 out of 5 code or not

Dept. of Electronics & Communication


AIT, Tumkur

8051 lab manual - 2011 ; if the no. is valid 2 out0f 5 code, store FFh in 31h else store 11h in 31h loc equ 30h mov r0,#loc mov a,@r0 ANL a,#0e0h jnz notval id mov a,@r0 mov r2,#5 mov r1,#00h clr c rrc a jnc skip inc r1 djnz r2,check mov A,r1 clr c subb A,#02h jnz notvalid inc r0 mov @r0,#0ffh sjmp exit notval id: inc r0 mov @r0,#11h sjmp exit end ; loc=30h ; r0 as pointer ; no. to the Acc. ; checks the 1st 3bits ; r2 as counter ; r0 stores no. of ones

check: skip:

; if carry increment r1

; (a)!= 0 ===> (r2)!= 2 no. is not ; valid 2 out of 5 code ; in valid 2 out of 5 code

valid.

exit:

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 movx A,@dptr mov r2,#8 clr c check: rlc A jnc zeroes inc 31h djnz r2,check sjmp exit inc 32h djnz r2,check sjmp exit ; dptr as pointer ; ((dptr))--->(A) ; R2 as counter ; clears the contents of the carry flag ; rotate left along with the carry ; 31h stores the no. of ones ; decrement R2, go to check if (r2)!=0 ; 32h stores no. of zeroes ; decrement R2, go to check if (r2)!=0

zeroes: exit:

Dept. of Electronics & Communication


AIT, Tumkur

8051 lab manual - 2011 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 skip: jc next1 cjne A,#47h,next sjmp not_valid next: jnc not_vali d clr c subb A,#07h mov R2,A next1: mov A,@r0 cjne A,#29h,next2 sjmp not_valid next2: jc not_valid mov A,r2 subb A,#30h inc r0 mov @r0,A inc r0 mov @r0,#0ffh sjmp exit not_val id: inc r0 inc r0 mov @r0,#11h exit: sjmp exit end

; if num=40h, go to not_val id ; if num<40h, go to next ; if num=47h, go to not_val id ; if num>47h, go to not_valid

; if num=29h, go to not_valid ; if num<29h, go to not_val id

; valid input

; invalid input

9.b Write an ALP to convert to convert the 2 digit packed BCD number into its ASCII equivalent.

Dept. of Electronics & Communication


AIT, Tumkur

8051 lab manual - 2011 ; 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 mov r0,#loc mov A,@r0 anl A,#0fh mov R2,A mov A,@r0 swap A anl A,#0fh mov 0f0h,#0ah mul AB add a,r2 inc r0 mov @r0,A end ; location of the number ; pointer to the number ; number to reg.A ; to unpack the BCD ; LS byte of the BCD ; number to the reg.A ; exchange the nibbles of A ; to unpack the BCD ; MSB*10 ; hex= MSB*10+LSB

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 Dept. of Electronics & Communication
AIT, Tumkur

8051 lab manual - 2011

loc equ 30h mov r0,#loc mov A,@r0 mov 0f0h,#64h div AB inc r0 mov @r0,A mov A,0f0h mov 0f0h,#0ah div AB inc r0 mov @r0,A inc r0 mov @r0,0f0h end

; location of the number ; pointer to the number ; number to the reg.A ; 64h=100 in decimal ; divides the number by 100 ; stores the MSB of decimal equivalent ; 0Ah=10 in decimal ; divides the number by 10

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

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

DELAY: back3: back2: back1:

11.b Write an ALP to realize 8-bit binary down counter

; Alp to realize binary down counter mov A,#0ffh ; initial ization Dept. of Electronics & Communication
AIT, Tumkur

8051 lab manual - 2011 back: mov p0,A acall DELAY dec a sjmp back mov r4,#100 mov r3,#225 mov r2,#225 djnz r2,back1 djnz r3,back2 djnz r4,back3 ret end ; moves the contents of A to port P0 ; calls the subroutine DELAY ; decrements A ; go to back

DELAY: back3: back2: back1:

11.c Write an ALP to realize 8-bit BCD up counter

mov a,#00h ; initial ization 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 back:
11.d Write an ALP to realize 8-bit BCD down counter

mov a,#99h ; initial ization 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 Dept. of Electronics & Communication
AIT, Tumkur

8051 lab manual - 2011 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 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 Dept. of Electronics & Communication
AIT, Tumkur

8051 lab manual - 2011 mov R1,#00h inc R0 cjne R0,#200,return cpl p0.0 setb TR0 reti end ; to reset R1 ; if (R0)/=200 go to return ; to start the timer ; return to main

return:

8051 connection to DAC808:

13.a1)Write a C program to generate square wave ( variable frequency & amplitude ) using DAC interface to 8051. #include <REG51xD2.H> #include "lcd.h" sbit Amp = P3^3; sbit Fre = P3^2; /* Switch to vary amplitude */ /* Switch to vary frequency */

main() { unsigned char on = 0x33,off=0x00; unsigned int fre = 1;

Dept. of Electronics & Communication


AIT, Tumkur

8051 lab manual - 2011


InitLcd(); 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) { unsigned char i,j; for(i=0 ; i<x ; i++) for (j=0 ; j<7; j++); } /* delay function */ /* Initialize LCD */

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

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

Dept. of Electronics & Communication


AIT, Tumkur

8051 lab manual - 2011

#include <REG51xD2.H> #include "lcd.h" void T0_on (); void T1_off(); sbit Amp = P3^3; main() { unsigned char on = 0x33, off = 0x00; InitLcd(); WriteString("Square Wave"); 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; 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;

/* Initialize LCD */ /* Display on LCD */

Dept. of Electronics & Communication


AIT, Tumkur

8051 lab manual - 2011


} 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; } for(i=0xfe;i>0x00;i--) /* Generate OFF pulse */ { P0 = i; P1 = i; } } } RESULT: P0 0xFF = 5V Triangular wave is generated using DAC interface to 8051.

Dept. of Electronics & Communication


AIT, Tumkur
Ton Toff

8051 lab manual - 2011

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. P0 0xFF = 5V

t(ms)

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

Dept. of Electronics & Communication


AIT, Tumkur

8051 lab manual - 2011

#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.

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() {

Dept. of Electronics & Communication


AIT, Tumkur

8051 lab manual - 2011


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 ; 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;

Dept. of Electronics & Communication


AIT, Tumkur

8051 lab manual - 2011


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 ++); } RESULT: The value of Key pressed on Hex keypad is displayed on the LCD using Hex keypad & LCD interface to 8051.

Dept. of Electronics & Communication


AIT, Tumkur

8051 lab manual - 2011

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; if( (Adc > 0x656) && (relay != 0)) { ClrLcd(); WriteString("RELAY OFF"); P1_1 = 0; relay = 0;

Dept. of Electronics & Communication


AIT, Tumkur

8051 lab manual - 2011


} 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; } RESULT: Temperature is maintained in the range 450 C to 500 C using the external ADC & temperature control interface to 8051.

Dept. of Electronics & Communication


AIT, Tumkur

8051 lab manual - 2011

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; for(i=0;i<4;i++) { P0 = Val;

Dept. of Electronics & Communication


AIT, Tumkur

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 either in Clockwise or Anticlockwise direction depending on the condition of direction flag using Stepper Motor Interface to 8051 16. 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; Val = Val<<1; delay(575); }

Dept. of Electronics & Communication


AIT, Tumkur

8051 lab manual - 2011


} } } 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) { while(inc==0); if (i>10) Dept. of Electronics & Communication
AIT, Tumkur

8051 lab manual - 2011 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/ps*/ 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 */ { P0 = FClr[CurFlr]; /* Clear Floor Indicator */ continue; /* Go up to read again */ } else if(CurFlr > ReqFlr) /* If Current floor is > request floor */

Dept. of Electronics & Communication


AIT, Tumkur

8051 lab manual - 2011


{ i = Flr[CurFlr] - Flr[ReqFlr]; j = Flr[CurFlr]; for(;i>0;i--) { P0 = 0x0f0|j; j--; delay(25000); } } else { i = Flr[ReqFlr] - Flr[CurFlr]; j = Flr[CurFlr]; for(;i>0;i--) { P0 = 0x0f0 | j; j++; delay(25000); } } CurFlr = ReqFlr; P0 = FClr[CurFlr]; } } void delay(unsigned int x) { for(;x>0;x--); } RESULT: The elevator action is simulated by using an elevator interface to 8051.

/* Get the no of floors to travel */ /* Move the indicator down */

/* If Current floor is < request floor */ /* Get the no of floors to travel */ /* Move the indicator Up */

/* Update Current floor */ /* Clear the indicator */

Dept. of Electronics & Communication


AIT, Tumkur

You might also like