Professional Documents
Culture Documents
8051 Manual
8051 Manual
PART 1- PROGRAMMING I. Data Transfer 1. Write an ALP to transfer n=5 bytes of data from location 8035h to location 8041h (without overlap) org 9000h mov dph,#80h mov r0,#35h mov r1,#41h mov r3,#05h back:mov dpl,r0 movx a,@dptr mov dpl,r1 movx @dptr,a inc r0 inc r1 djnz r3,back lcall 0003h org 8035h db 01h db 02h db 03h db 04h db 05h end Result: 8041: 01, 02,03,04,05
R.Y.M.E.C,BELLARY
DEPT OF ECE
MICROCONTROLLER LAB
2. Write an ALP to exchange n=5 bytes of data at location 35h & at location 41h. org 9000h mov r0,#35h mov r1,#41h mov r3,#05h back:mov a,@r0 mov r2,a mov a,@r1 mov @r0,a mov a,r2 mov @r1,a inc r0 inc r1 djnz r3,back l call 0003h org 35h db 01h,02h,03h,04h,05h org 41h db 06h,07h,08h,09h,0ah end Result: 0035:06,07,08,09,0a 0041:01,02,03,04,05
R.Y.M.E.C,BELLARY
DEPT OF ECE
MICROCONTROLLER LAB
3.Write an ALP to exchange n=5 bytes of data at location 35h & at location 41h using Xch instructions. org 9000h mov r0,#35h mov r1,#41h mov r3,#05h back:mov a,@r0 mov r2,a mov a,@r1 xch a,r2 mov @r1,a xch a,r2 mov @r0,a inc r0 inc r1 djnz r3,back lcall 0003h org 35h db 01h,02h,03h,04h,05h org 41h db 06h,07h,08h,09h,0ah end Result: 003506,07,08,09,0a 004101,02,03,04,05
R.Y.M.E.C,BELLARY
DEPT OF ECE
MICROCONTROLLER LAB
4. Write an ALP to sort an array of n=6 bytes of data in ascending order(using bubble sort algorithm). org 8000h mov r0,#05 l1:mov dptr,#9000h mov a,r0 mov r1,a l2:movx a,@dptr mov b,a inc dptr movx a,@dptr clr c mov r2,a subb a,b jnc noexchg mov a,b movx @dptr,a dec dpl mov a,r2 movx @dptr,a inc dptr noexchg:djnz r1,l2 djnz r0,l1 lcall 0003h org 9000h db 2,5,1,9,7,6 end Result: 9000:01, 02, 05,06,07,09
R.Y.M.E.C,BELLARY
DEPT OF ECE
MICROCONTROLLER LAB
5. Write an ALP to sort an array of n=6 bytes of data in descending order(using bubble sort algorithm). org 8000h mov r0,#05 l1:mov dptr,#9000h mov a,r0 mov r1,a l2:movx a,@dptr mov b,a inc dptr movx a,@dptr clr c mov r2,a subb a,b jc noexchg mov a,b movx @dptr,a dec dpl mov a,r2 movx @dptr,a inc dpt noexchg:djnz r1,l2 djnz r0,l1 lcall 0003h org 9000h db 2,5,1,9,7,6 end Result: 9000:09, 07, 06,05,02,01
R.Y.M.E.C,BELLARY
DEPT OF ECE
MICROCONTROLLER LAB
6. Write an ALP to find the smallest element in a given string of n=6 bytes & store the smallest element at location 8062h. org 9000h mov r3,#05h mov dptr,#8000h movx a,@dptr mov r1,a nextbyte:inc dptr movx a,@dptr clr c mov r2,a subb a,r1 jnc skip mov a,r2 mov r1,a skip:djnz r3,nextbyte mov dpl,#62h mov a,r1 movx @dptr,a lcall 0003h org 8000h db 04,05,06,07,08,02 end Result: 806202
R.Y.M.E.C,BELLARY
DEPT OF ECE
MICROCONTROLLER LAB
7.Write an ALP to find the largest element in a given string of n=6 bytes & store the largest element at location 8062h. org 9000h mov r3,#05h mov dptr,#8000h movx a,@dptr mov r1,a nextbyte:inc dptr movx a,@dptr clr c mov r2,a subb a,r1 jc skip mov a,r2 mov r1,a skip:djnz r3,nextbyte mov dpl,#62h mov a,r1 movx @dptr,a lcall 0003h org 8000h db 04,05,06,07,08,02 end Result: 806208
R.Y.M.E.C,BELLARY
DEPT OF ECE
MICROCONTROLLER LAB
II.Arithmetic Instructions 1. Write an ALP to add 2 8-bit numbers. org 9000h mov a,#30h add a,#50h lcall 0003h Result: A=80h 2. Write an ALP to add 2 16-bit numbers. org 9000h mov dptr,#1234h mov a,#12h mov b,#32h add a,dpl mov dpl,a mov a,b addc a,dph mov dph,a lcall 0003h Result:1234 3. Write an ALP to add n 8-bit numbers. org 9000h mov dptr,#1234h mov r0,#08h movx a,@dptr up:mov b,a inc dptr movx a,@dptr add a,b djnz r0,up lcall 0003h org 1234h db 1,2,3,4,5,6,7,8,9 end Result: A=2D
R.Y.M.E.C,BELLARY
DEPT OF ECE
MICROCONTROLLER LAB
4. Write an ALP to find average of 10 numbers. org 8000h mov dptr,#9000h mov a,#00h mov b,#0ah mov r2,#09h movx a,@dptr back:mov r0,a inc dptr movx a,@dptr add a,r0 djnz r2,back div ab inc dptr movx @dptr,a lcall 0003h org 9000h db 01h,02h,03h,04h,05h,06h,07h,08h,09h,0ah end Result: 05h 5. Write an ALP to subtract 2 8-bit number org 9000h mov a,#42h mov r3,#32h clr c subb a,r3 lcall 0003h Result:A=10
R.Y.M.E.C,BELLARY
DEPT OF ECE
MICROCONTROLLER LAB
6.Write an ALP to subtract 16-bit numbers. org 8000h mov dptr,#1234h mov a,#46h mov b,#32h clr c subb a,dpl mov dpl,a mov a,b subb a,dph mov dph,a lcall 0003h Result: dph 12 dpl 20 7. Write an ALP for division. org 9000h mov a,#80h mov b,#20h div ab lcall 0003h 8. Write an ALP to multiply 2 8-bit number. org 9000h mov a,#04h mov b,#02h mul ab mov dptr,#1234h movx @dptr,a inc dptr mov a,b movx @dptr,a lcall 0003h
R.Y.M.E.C,BELLARY
DEPT OF ECE
10
MICROCONTROLLER LAB
9.Write an ALP to multiply 2 16-bit unsigned numbers. org 8000h middle:mov a,r5 mov r3,#00h mov b,r6 mov r4,#23h mul ab mov r5,#10h add a,r1 mov r6,#0bch mov r1,a mov r7,#0ah mov a, b mov a,r4 addc a,r2 mov b,r6 mov r2,a mul ab jnc byte mov r0,a inc r3 mov r1,b byte:mov a,r5 mov a,r4 mov b,r7 mov b,r7 mul ab mul ab add a,r2 mov r2,b mov r2,a add a,r1 mov a,b mov r1,a addc a,r3 jnc middle mov r3,a inc r2 lcall 0003h mov a,r2 end Result: R3 R2 R1 R0 00 AD 37 B4 10.Write an ALP to add 2 32-bit number stored in RAM locations. org 9000h movx @dptr,a clr c inc dptr mov r2,#04h djnz r2,back mov r0,#40h lcall 0003h mov r1,#50h org 40h mov dptr,#8000h db 0c0h,3bh,45h,01h back:mov a,@r0 org 50h addc a,@r1 db 0feh,05h,c7h,56h inc r0 end inc r1 Result: 8000BE 41 0C 58
R.Y.M.E.C,BELLARY
DEPT OF ECE
11
MICROCONTROLLER LAB
11. Write an ALP to square the number. org 8000h mov dptr,#9000h movx a,@dptr mov r0,a mov b,a mul ab inc dptr movx @dptr,a mov a,b inc dptr movx @dptr,a lcall 0003h org 9000h db ffh end Result: 9000:ff 01 fe 12. Write an ALP to find cube of a number org 8000h mov dptr,#9000h movx a,@dptr mov r0,a mov b,a mul ab push b mov b,a mov a,r0 mul ab inc dptr movx @dptr,a mov r2,b pop b mov a,r0 mul ab add a,r2 inc dptr movx @dptr,a mov a,b inc dptr movx @dptr,a lcall 0003h org 9000h db 56h end Result: 900056 98 B4 09
R.Y.M.E.C,BELLARY
DEPT OF ECE
12
MICROCONTROLLER LAB
end
R.Y.M.E.C,BELLARY
DEPT OF ECE
13
MICROCONTROLLER LAB
III.Counters 1. Write a program to display decimal up-counter. org 8000h start:mov r6,#00h back:push r6 lcall 66d3h lcall 6796h lcall 6796h pop r6 mov a,r6 add a,#01h da a mov r6,a cjne r6,#00h,back sjmp start end 2. Write a program to display decimal down-counter org 8000h start:mov r6,#99h back:push r6 lcall 66d3h lcall 6796h lcall 6796h pop r6 mov a,r6 add a,#99h da a mov r6,a cjne r6,#00h,back sjmp start end
R.Y.M.E.C,BELLARY
DEPT OF ECE
14
MICROCONTROLLER LAB
3. Write a program to display Hexadecimal up-counter. org 8000h start:mov r6,#00h back:push r6 lcall 66d3h lcall 6796h lcall 6796h pop r6 mov a,r6 add a,#01h mov r6,a cjne r6,#00h,back sjmp start
end
4. Write a program to display Hexadecimal Down-counter. org 8000h start:mov r6,#ffh back:push r6 lcall 66d3h lcall 6796h lcall 6796h pop r6 mov a,r6 add a,#ffh mov r6,a cjne r6,#00h,back sjmp start
end
5. Write an ALP to display Hex-up count.(16-bit) org 8000h mov r6,#00h mov r7,#00h l1:push r7 lcall 665fh pop r7 back:push r6 lcall 665fh
R.Y.M.E.C,BELLARY DEPT OF ECE 15
MICROCONTROLLER LAB
pop r6 mov a,r6 add a,#01h mov r6,a cjne r6,#00h,back mov a,r7 add a,#01h mov r7,a cjne r7,#00h,l1 lcall 0003h 6. Write an ALP to display 8-bit up/down count. org 8000h start:mov r6,#00h back:push r6 lcall 66d3h lcall 6796h lcall 6796h pop r6 mov a,r6 add a,#01h da a mov r6,a cjne r6,#00h,back mov r6,#99h back1:push r6 lcall 66d3h lcall 6796h lcall 6796h pop r6 mov a,r6 add a,#99h da a mov r6,a cjne r6,#00h,back1 sjmp start end
R.Y.M.E.C,BELLARY
DEPT OF ECE
16
MICROCONTROLLER LAB
IV.Boolean & Logical instructions 1.Three 8-bit numbers, X, NUM1, and NUM2 are stored in internal data RAM locations 20h, 21h, and 22h respectively. Write an ALP to compute the following. If X=0; then NUM1 (AND) NUM2, If X=1; then NUM1 (OR) NUM2, If X=2; then NUM1 (XOR) NUM2. Else res=00,res is 23h location in RAM. org 9000h mov a,20h mov r1,a mov a,21h cjne r1,#00h,chkor anl a,22h sjmp last chkor:cjne r1,#01h,chkxor orl a,22h sjmp last chkxor:cjne r1,#02h,other xrl a,22h sjmp last other:clr a last:mov 23h,a lcall 0003h org 20h db 01h,0fh,12h end Result: If X=00,0023h=02h If X=01,0023h=1fh If X=02,0023h=1dh Else,0023=00h
R.Y.M.E.C,BELLARY
DEPT OF ECE
17
MICROCONTROLLER LAB
2.Write an ALP to reverse 8-bit number. org 9000h mov a,#12h rr a rr a rr a or swap a rr a lcall 0003h Result:A=21H
3.Write an ALP to reverse 16-bit number. org 9000h mov dptr,#1234h mov a,dpl rl a rl a rl a rl a mov dpl,a mov a,dph rl a rl a rl a rl a xch a,dpl mov dph,a lcall 0003h Result:DPH=43 DPL=21
R.Y.M.E.C,BELLARY
DEPT OF ECE
18
MICROCONTROLLER LAB
4. Write an ALP to check whether the given number is +ve or -ve.If the number is +ve store 00h else store ffh. org 8000h mov a,#70h anl a,#80h jz posi mov a,#0ffh lcall 0003h posi:mov a,#00h lcall 0003h Result: 00 [Positive] 5. Write an ALP to check whether the given number is odd or even.If number is odd store ffh else store 00h org 8000h mov a,#11h rrc a jc add mov a,#00h sjmp last add:mov a,#0ffh last:lcall 0003h lcall 0003h Result: Affh 6. Write an ALP to count number of 1's and 0's in a byte. org 8000h mov r0,#00h mov r1,#00h mov r3,#08h mov dptr,#9000h movx a,@dptr back:clr c rlc a jc down inc r0
R.Y.M.E.C,BELLARY DEPT OF ECE 19
MICROCONTROLLER LAB
sjmp next down:inc r1 next:djnz r3,back lcall 0003h org 9000h db 12h end Result: R0-->06(0's)
R1-->02(1's)
7. Write an ALP to find 2's complement of 8-bit number. org 8000h mov a,#0fh cpl a add a,#01h lcall 0003h Result:A=F1 8. Write an ALP to find 2's complement of 16-bit number. org 8000h mov dptr,#1234h mov a,dpl cpl a add a,#01h mov dpl,a mov a,dph cpl a addc a,#00h mov dph,a lcall 0003h Result:DPTR-->EDCC
R.Y.M.E.C,BELLARY
DEPT OF ECE
20
MICROCONTROLLER LAB
Programs illustrating Bit Manipulations. 1. Two 8-bit number NUM1 & NUM2 are stored in external memory location 8000h & 8001h respectively. Write an ALP to compare the 2 numbers.Reflect the result as. (i)If NUM1<NUM2,set LSB of data RAM 2FH(bit address 78H) (ii)If NUM1>NUM2,set MSB of data RAM 2FH(bit address 7FH) (iii)If NUM1=NUM2,clear both LSB &MSB of data Ram 2FH. org 8000h mov dptr,#9000h movx a,@dptr mov r0,a inc dptr movx a,@dptr clr c subb a,r0 jz equal jnc big setb 78h sjmp last big:setb 7fh sjmp last equal:clr 7fh clr 78h last:sjmp exit exit:lcall 0003h org 9000h db 12h,12h end
R.Y.M.E.C,BELLARY
DEPT OF ECE
21
MICROCONTROLLER LAB
2.Three 8-bit numbers X,NUM1,NUM2 are stored in internal data RAM locations 20h,21h,22h respectively.Write an ALP to compute the following. If X=0,then LSB of num1 (AND) LSB of NUM2, If X=1,then MSB of NUM1 (OR) MSB of NUM2, If X=2,then compliment MSB of num1 & store the bit result in res, where res is MSB of 23h locations. org 8000h mov r0,20h cjne r0,#00h,ck1 mov c,08h anl c,10h sjmp last ck1:cjne r0,#01h,ck2 mov c,0fh anl c,17h sjmp last ck2:cjne r0,#02h,ck3 cpl 0fh mov c,0fh sjmp last ck3:clr c last:mov 1fh,c lcall 0003h org 20h db 00h,11h,11h end
R.Y.M.E.C,BELLARY
DEPT OF ECE
22
MICROCONTROLLER LAB
V.Conditional CALL & RETURN 1. Write an ALP to convert hexadecimal to Ascii.(Conditional CALL & RETURN) org 9000h mov r1,#50h mov a,@r1 mov r2,a anl a,#0fh acall Ascii inc r1 mov @r1,a mov a,r2 swap a anl a,#0fh acall Ascii inc r1 mov @r1,a lcall 0003h Ascii:mov r4,a clr c subb a,#0ah mov a,r4 jc skip add a,#07h skip:add a,#30h ret end
Result:50:2c 43 32
R.Y.M.E.C,BELLARY
DEPT OF ECE
23
MICROCONTROLLER LAB
VI.Code Conversion 1. Write a program to convert Decimal to Hex. org 9000h mov dptr,#1234h movx a,@dptr anl a,#0f0h swap a mov b,#0ah mul ab mov r1,a movx a,@dptr anl a,#0fh add a,r1 inc dptr movx @dptr,a lcall 0003h org 1234h db 45h end Result: 123445 2D 2. Write an ALP to convert Hex to Decimal org 8000h mov dptr,#9000h movx a,@dptr mov b,#10 div ab inc dptr xch a,b movx @dptr,a xch a,b mov b,#10 div ab inc dptr xch a,b movx @dptr,a xch a,b inc dptr movx @dptr,a lcall 0003h org 9000h db 0ffh end Result: 900005 05 02
R.Y.M.E.C,BELLARY
DEPT OF ECE
24
MICROCONTROLLER LAB
3. Write an ALP to convert BCD to Acsii. org 9000h mov r1,#50h mov a,@r1 mov r2,a anl a,#0fh orl a,#30h inc r1 mov @r1,a mov a,r2 swap a anl a,#0fh orl a,#30h inc r1 mov @r1,a lcall 0003h org 50h db 28h,38h,32h end Result: 5028 38 32 4. Write an ALP to convert Ascii to hexadecimal conversion. org 9000h mov r1,#50h mov a,@r1 clr c subb a,#41h mov a,@r1 jc skip clr c subb a,#07h skip:clr c subb a,#30h inc r1 mov @r1,a lcall 0003h org 50h db 45h end Result: 5045 0E
R.Y.M.E.C,BELLARY DEPT OF ECE 25
MICROCONTROLLER LAB
VII.Program to generate delay, Program using serial port and on-chip timer/counter. org 8000h start:mov r6,#00h back:push r6 lcall 66d3h lcall delay pop r6 mov a,r6 add a,#01h da a mov r6,a cjne r6,#00h,back sjmp start delay:mov TMOD,#10h mov TL1,#00h mov TH1,#ffh setb TCON.6 back:jnb TCON.7,back clr TCON.6 clr TCON.7 ret end
R.Y.M.E.C,BELLARY
DEPT OF ECE
26
MICROCONTROLLER LAB
VIII.Serial Data Transmission 1. Write an ALP to transfer letter "g" serially at 9600 baud rate, continuously. org 9000h mov tmod,#20h mov th1,#fdh mov scon,#50h setb tr1 start:mov sbuf,#"g" back:jnb t1,back clr t1 sjmp start
lcall 0003h
2. Write an ALP to transfer the message "HELLO" serially at 9600 baud,8bit,1 stop bit. org 9000h mov tmod, #20h mov th1,#fdh mov scon,#50h setb tr1 start:mov a,#"h" acall trans mov a,#"e" acall trans mov a,#"l" acall trans mov a,#"l" acall trans mov a,#"o" acall trans lcall 0003h trans:mov sbuf,a here:jnb ti,here clr ti ret end TI flag=1,after the transmission.Hence in the subroutine wait untill TI is set. Later clear the TI flag &continue with transmission of the next byte by writing into the SBUF register.
R.Y.M.E.C,BELLARY DEPT OF ECE 27
MICROCONTROLLER LAB
II.INTERFACING
1. Write C 'program to display the key pressed (no. of times) by interfacing Alphanumeric LCD panel & Hex keypad interface module to 8051 micro controller. #include <Intel\8051.h> #include <standard.h> #define PORTA 0x2040 #define PORTB 0x2041 #define PORTC 0x2042 #define CNTL 0x2043 #define CNTL_BYTE 0x81 void lcd_init(void); void clr_disp(void); void lcd_com(void); void lcd_data(void); void scan(void); void get_key(void); void display(void); void display1(void); void dec_ascii(void); xdata unsigned char *p8255_cntl ; xdata unsigned char *p8255_porta ; xdata unsigned char *p8255_portb ; xdata unsigned char *p8255_portc ; idata unsigned char row,col,key; code unsigned char scan code[16]={ 0xED, 0xDD, 0xBD, 0x7D, 0xEB, 0xDB, 0xBB, 0x7B, 0xE7, 0xD7, 0xB7, 0x77}; 0xEE, 0xDE, 0xBE, 0x7E,
code unsigned char ASCII_CODE[16]= {'0','4','8','C', 1','5','9','D', '2','6','A','E', '3','7','B','F'}; code unsigned char rarr[4] = {0xFE,0xFD,0xFB,0xF7}; idata unsigned char temp, temp2,temp3,res1,flag,i,result; unsigned char ind, temp1,count,pre_key,acount1,acount2; void main () { delay_ms(25); ind = 0;
R.Y.M.E.C,BELLARY
DEPT OF ECE
28
MICROCONTROLLER LAB p8255_cntl = CNTL; *p8255_cntl = CNTL_BYTE ; p8255_portc = PORTC; p8255_porta = PORTA; p8255_portb =PORTB; count=1; lcd_init(); delay_ms(5); pre_key = 0xFF; while(1) { get_key(); display();
if(result == pre_key) //if pressed key is same as previous count++; //increment the count value else count =1; dec_ascii(); display1(); delay_ms(100); pre_key = result; } } //end of main() void get_key(void) { int i; flag = 0x00; while(1)//flag == 0x00) { for(row=0;row<4;row++) //find out the row which is logic low { if( row == 0) temp3=0xfe; else if(row == 1) temp3=0xfd; else if(row == 2) temp3=0xfb; else if(row == 3) temp3=0xf7; *p8255_portb = temp3; //corresponding values are stored in temp3 scan(); delay_ms(10); if(flag == 0xff) break; R.Y.M.E.C,BELLARY DEPT OF ECE 29
MICROCONTROLLER LAB } // end of for if(flag == 0xff) break; } // end of while for(i=0;i<16;i++) { if(scan_code[i] == res1) //scan code is same as res1 { //position of that value is taken result = ASCII_CODE[i]; //corresponding ascii value is stored in result break; } } }// end of get_key(); void scan(void) { unsigned char t; temp2 = *p8255_portc; temp2 temp2 = temp2 & 0x0f; if(temp2 != 0x0f) { delay_ms(30); delay_ms(30); temp2 = *p8255_portc; temp2 = temp2 & 0x0f; if(temp2 != 0x0f) // check again portc changes { flag = 0xff; //if yes make flag bit high res1 = temp2; // res1 = res1 << 4; t = (temp3 << 4) & 0xf0; //temp3 value is left shifted by 4 times res1 = res1 | t; //mask with 0x0f,or with res1 } //final value stored in res1 only else { flag = 0x00; } } } // end of scan() R.Y.M.E.C,BELLARY DEPT OF ECE 30
//store the changes in portc value in //mask with 0x0f //if temp2 value is not equl to 0x0f //then give debounce delay
MICROCONTROLLER LAB void display(void) { temp = 0x80; lcd_com(); delay_ms(5); temp = result; lcd_data(); delay_ms(5); } void display1(void) { temp = 0xC0; lcd_com(); delay_ms(5); temp = acount1; lcd_data(); delay_ms(5); temp = acount2; lcd_data(); delay_ms(5); } // lcd initialisation routine. void lcd_init(void) { temp = 0x38; lcd_com(); delay_ms(5); temp = 0x38; lcd_com(); delay_ms(5); temp = 0x0f; lcd_com(); delay_ms(5); temp = 0x06; lcd_com(); delay_ms(5); clr_disp(); }// clears display void clr_disp(void) { temp = 0x01; lcd_com(); delay_ms(5); R.Y.M.E.C,BELLARY //disply on,cursor on //shift cursor right
//address of display
DEPT OF ECE
31
MICROCONTROLLER LAB } // get the command to be presented to lcd in byte fashion // and present it in nibble fashion void lcd_com(void) { *p8255_porta = temp; //copy temp value to porta temp1=0x0f;// RS=0 *p8255_portc = temp1; temp1=temp1 | 0x4f; // enable 1 *p8255_portc = temp1; delay(5); temp1=temp1 & 0xbf; *p8255_portc = temp1; } // get the data to be presented to lcd in byte fashion in temp1 // and present it in nibble fashion void lcd_data(void) { *p8255_porta=temp; //put the temp value in porta temp1=0x1f; //RS=1 *p8255_portc = temp1; temp1=temp1 | 0xdf; //r/w=0 for write *p8255_portc = temp1; temp1=temp1 | 0x4f; //E=1 for high pulse *p8255_portc = temp1; delay(5); temp1=temp1 & 0xbf; //E=0 for high to low pulse *p8255_portc = temp1; } void dec_ascii(void) { acount1 = (count/10) +0x30; acount2 = (count % 10) + 0x30; } Result:
R.Y.M.E.C,BELLARY
DEPT OF ECE
32
MICROCONTROLLER LAB 2. Write C' program to measure temperature by interfacing External ADC & Temperature control interface module to 8051 microcontroller. #include <Intel\8051.h> #include <standard.h> #define PORTA 0x2040 #define PORTB 0x2041 #define PORTC 0x2042 #define CNTL 0x2043 #define buff 0x196 xdata unsigned char *p8255_cntl ; xdata unsigned char *p8255_porta ; xdata unsigned char *p8255_portb ; xdata unsigned char *p8255_portc ; xdata unsigned char *buff_ptr; idata unsigned char temp1,adc_val,temperature,dtemp,i; xdata unsigned char adc_table[100]={ 0X00,0X03,0X05,0X08,0X0A,0X0D,0X0F,0X11,0X14,0X16, 0X19,0X1C,0X1E,0X21,0X24,0X27,0X2A,0X2C,0X2E,0X32, 0X34,0X36,0X39,0X3C,0X3F,0X42,0X45,0X48,0X4A,0X4C, 0X4E,0X50,0X52,0X54,0X56,0X58,0X5B,0X5E,0X61,0X64, 0X67,0X6A,0X6D,0X70,0X72,0X74,0X77,0X7A,0X7D,0X7F, 0X82,0X85,0X87,0X8A,0X8D,0X90,0X92,0X95,0X97,0X9A, 0X9D,0XA0,0XA2,0XA5,0XA8,0XAA,0XAD,0XAF,0XB0,0XB3, 0XB6,0XB9,0XBC,0XBF,0XC1,0XC4,0XC6,0XC9,0XCC,0XCF, 0XD0,0XD2,0XD5,0XD7,0XDA,0XDC,0XDF,0XE0,0XE2,0XE5, 0XE7,0XE9,0XEB,0XEE,0XF1,0XF4,0XF6,0XF9,0XFC,0XFF}; extern void display(void); void main () { buff_ptr=buff; // mem. locn to hold adc data to display p8255_porta = PORTA; p8255_portc = PORTC; p8255_portb = PORTB; p8255_cntl = *p8255_cntl = delay(200); while(1) { *p8255_portc = *p8255_portc = *p8255_portc = delay(200); R.Y.M.E.C,BELLARY 0x0;// channel 0 selection Wr=1,PC1=1 0x01;// start of conversion pulse 0x0;// start of conversion pulse CNTL; 0x98;// Ppa=i/p,Pb=o/p,PCu=i/p,PCl=o/p,
DEPT OF ECE
33
MICROCONTROLLER LAB do { temp1=*p8255_portc; temp1=temp1 & 0x80; } while(temp1 != 0x80); delay(200); adc_val = *p8255_porta; // display adc result on the data field // *buff_ptr = adc_val; for(i=0;i<100;i++) { if(adc_val==adc_table[i]) { temperature = i; break; } } dtemp = ((temperature/10) <<4 ) | ((temperature%10) & 0x0f); *buff_ptr=dtemp; // WRITE TO 0X196 LOCAN display(); delay(200); } // end of while(1) } DISPLAY PROGRAM: PUBLIC _display _display: lcall 676fh ret Result:
R.Y.M.E.C,BELLARY
DEPT OF ECE
34
MICROCONTROLLER LAB
3. DAC PROGRAMS:
(i) Write C'program to generate Ramp waveform using DAC Interface to 8051. #include <Intel\8051.h> #include <standard.h> #define p8255_ctl 0x2043 #define porta 0x2040 #define portb 0x2041 #define ctl_word 0x80 #define SET_FREQ 0xF000 xdata unsigned char *ptr_8255_ctl; xdata unsigned char *ptr_8255_porta; xdata unsigned char *ptr_8255_portb; xdata unsigned char *buff_ptr; idata unsigned char count,con1,con2,temp2; interrupt(INT_TMR0) tmr_isr(void) using 1 { TL0 = con2; TH0 = con1; if(count!=0xff) { *ptr_8255_porta = count; count++; } else { count=0x00; *ptr_8255_porta = count; } } void main () { ptr_8255_ctl = p8255_ctl; *ptr_8255_ctl = ctl_word; ptr_8255_porta= porta; buff_ptr = SET_FREQ; temp2 = *buff_ptr; switch(temp2) { case 0x10: con1 = 0xfe; con2 = 0x90; break;
//get the value of frq from location f000h //depending on diff frq we are going to load //different counts
R.Y.M.E.C,BELLARY
DEPT OF ECE
35
MICROCONTROLLER LAB case 0x20: con1 = 0xff; con2 = 0x4d; break; case 0x30: con1 = 0xff; con2 = 0x8a; break; case 0x40: con1 = 0xff; con2 = 0xa6; break; default: con1 = 0xfe; con2 =0x90; break; } TMOD = 0x01; TL0 = con2; TH0 = con1; ET0 = 1; EA = 1; TR0 = 1; here: goto here; } // end of main() Result: //select the timer in mode 1 //different counts depends on frq //enable timer0,enable all interrupt bit,start timer0
(ii)Write C'program to generate Square waveform using DAC Interface to 8051. #include <Intel\8051.h> #include <standard.h> #define p8255_ctl 0x2043 #define porta 0x2040 #define portb 0x2041 #define ctl_word 0x80 #define SET_FREQ 0xF000 xdata unsigned char *ptr_8255_ctl; xdata unsigned char *ptr_8255_porta; xdata unsigned char *ptr_8255_portb; xdata unsigned char *buff_ptr; idata unsigned char count,con1,con2,temp2; interrupt(INT_TMR0) tmr_isr(void) using 1 { TL0 = con2; TH0 = con1; if(count==0xff) { count = 0x00; *ptr_8255_porta = count; } R.Y.M.E.C,BELLARY DEPT OF ECE 36
MICROCONTROLLER LAB else { count = 0xFF; *ptr_8255_porta = count; } } void main () { ptr_8255_ctl = p8255_ctl; *ptr_8255_ctl = ctl_word; ptr_8255_porta= porta; buff_ptr = SET_FREQ; temp2 = *buff_ptr; count = 0x00; switch(temp2) { //depending on different frq case 0x10: con1 = 0xdb; //0xff; //we are setting diff values to timer0 con2 = 0xff; //0x4d; break; case 0x20: con1 = 0xed; //0xfe; con2 = 0xff; //0x90; break; case 0x30: con1 = 0xf3; //0xfd; con2 = 0xff; //0xa6; break; case 0x40: con1 = 0xf6 ;//0xfc; con2 = 0xff; //0x8a; break; default: con1 = 0xdb; //0xfb; con2 = 0xff; //0x4d; break; } TMOD = 0x01; //mode 1 selection TL0 = con2; TH0 = con1; ET0 = 1; //enable timer0,all interrupt bit EA = 1; TR0 = 1; //start timer0 here: goto here; } // end of main() Result:
R.Y.M.E.C,BELLARY
DEPT OF ECE
37
MICROCONTROLLER LAB (iii)Write C'program to generate Triangle waveform using DAC Interface to 8051. #include <Intel\8051.h> #include <standard.h> #define p8255_ctl 0x2043 #define porta 0x2040 #define portb 0x2041 #define ctl_word 0x80 #define SET_FREQ 0xF000 xdata unsigned char *ptr_8255_ctl; xdata unsigned char *ptr_8255_porta; xdata unsigned char *ptr_8255_portb; xdata unsigned char *buff_ptr; idata unsigned char count,con1,con2,temp2,df; interrupt(INT_TMR0) tmr_isr(void) using 1 { TL0 = con2; TH0 = con1; if(df) { *ptr_8255_porta = count; count = count + 1; if(count==0x7f) df=0x00; } else { *ptr_8255_porta = count; count = count - 1; if(count==0x00) df=0xff; } } void main () { ptr_8255_ctl = p8255_ctl; *ptr_8255_ctl = ctl_word; ptr_8255_porta= porta; buff_ptr = SET_FREQ; temp2 = *buff_ptr; df = 0xff; switch(temp2) //different counts for timer depending on frq { case 0x10: con1 = 0x0fe;//0xdb; //0xff; con2 = 0x90; //0xff; //0x4d; break; R.Y.M.E.C,BELLARY DEPT OF ECE 38
MICROCONTROLLER LAB case 0x20: con1 =0xff;//0xed; //0xfe; con2 = 0x4d; //0xff; //0x90; break; case 0x30: con1 = 0xff;//0xf3; //0xfd; con2 =0x79; //0xff; //0xa6; break; case 0x40: con1 = 0xff;//0xf6 ;//0xfc; con2 =0xc6;//0xff; //0x8a; break; default: con1 = 0xfe;//0xdb; //0xfb; con2 =0x90; //0xff; //0x4d; break; } TMOD = 0x01; //mode1 selection TL0 = con2; TH0 = con1; ET0 = 1; EA = 1; //enable all interrupt bit,timer0 bit TR0 = 1; //start timer0 here: goto here; } // end of main() Result: (iv) Write C' program to generate Sinusoidal waveform using DAC Interface to 8051. #include <Intel\8051.h> #include <standard.h> #define p8255_ctl 0x2043 #define porta 0x2040 #defineportb 0x2041 #define ctl_word 0x80 #define SET_FREQ 0xF000 xdata unsigned char sine_tab[48]= { 0x80,0x90,0xA1,0xB1,0xC0,0xCD,0xDA,0xE5,0xEE,0xF6,0xFB,0xFE,0xFF, 0xFE,0xFB,0xF6,0xEE,0xE5,0xDA,0xCD,0xC0,0xB1,0xA1,0x90,0x80, 0x70,0x5F,0x4F,0x40,0x33,0x26,0x1B,0x12,0x0A,0x05,0x02,0x00, 0x02,0x05,0x0A,0x12,0x1B,0x26,0x33,0x40,0x4F,0x5F,0x70,0x80 }; xdata unsigned char *ptr_8255_ctl; xdata unsigned char *ptr_8255_porta; xdata unsigned char *ptr_8255_portb; xdata unsigned char *buff_ptr; idata unsigned char sine_val,con1,con2,temp2; idata int count; R.Y.M.E.C,BELLARY DEPT OF ECE 39
MICROCONTROLLER LAB interrupt(INT_TMR0) tmr_isr(void) using 1 { TL0 = con2; TH0 = con1; if(count<48) { sine_val = sine_tab[count]; *ptr_8255_porta = sine_val; count++; } else count = 0; } void main () { ptr_8255_ctl = p8255_ctl; *ptr_8255_ctl = ctl_word; ptr_8255_porta= porta; buff_ptr = SET_FREQ; //value is from location F000h temp2 = *buff_ptr; count = 0; switch(temp2) //different count values are loaded { //according to the different frequency case 0x10: con1 = 0xf8; con2 = 0x7f; break; case 0x20: con1 = 0xfc; con2 = 0x3f; break; case 0x30: con1 = 0xfd; con2 = 0x7f; break; case 0x40: con1 = 0xfe; con2 = 0x1f; break; default: con1 = 0xf8; con2 = 0x7f; break; } TMOD = 0x01; TL0 = con2; TH0 = con1; //select the timer1 //values loaded according to the frequency
R.Y.M.E.C,BELLARY
DEPT OF ECE
40
MICROCONTROLLER LAB ET0 = 1; timer0 EA = 1; TR0 = 1; l1: goto l1; } // end of main() Result:
R.Y.M.E.C,BELLARY
DEPT OF ECE
41
MICROCONTROLLER LAB
4. STEPPER MOTOR:
(i) Write C' program to rotate Stepper motor in Clockwise direction by interfacing Stepper motor interface module with 8051 microcontroller. #include <Intel\8051.h> #define p8255_ctl 0x2043 #define portc 0x2042 #define ctlr_word 0x80 #define phasea 0x0d #define phaseb 0x0e #define phasec 0x07 #define phased 0x0b xdata unsigned char *ptr_8255_ctl; xdata unsigned char *ptr_8255_portc; void delay(void); void main () { int i; ptr_8255_ctl = p8255_ctl; *ptr_8255_ctl = ctlr_word; while(1) { ptr_8255_portc=portc; is taken in pointer variable //different speeds are loaded into pointer *ptr_8255_portc = phasea; delay(); delay(); *ptr_8255_portc = phaseb; delay(); delay(); *ptr_8255_portc = phasec; delay(); delay(); *ptr_8255_portc = phased; delay(); delay(); } } void delay(void) { int i=0; { for(i=0;i<=10000;i++) } } Result: R.Y.M.E.C,BELLARY DEPT OF ECE 42
//port a address
MICROCONTROLLER LAB (ii) Write C'program to rotate Stepper motor in AntiClockwise direction by interfacing Stepper motor interface module with 8051 microcontroller. #include <Intel\8051.h> #define p8255_ctl 0x2043 #define portc 0x2042 #define ctlr_word 0x80 #define phasea 0x0b #define phaseb 0x07 #define phasec 0x0e #define phased 0x0d xdata unsigned char *ptr_8255_ctl; xdata unsigned char *ptr_8255_portc; void delay(void); void main () { ptr_8255_ctl = p8255_ctl; *ptr_8255_ctl = ctlr_word; while(1) { ptr_8255_portc=portc; //address of portc in pointer //different speeds are loaded in pointer *ptr_8255_portc = phasea; delay(); delay(); *ptr_8255_portc = phaseb; delay(); delay(); *ptr_8255_portc = phasec; delay(); delay(); *ptr_8255_portc = phased; delay(); delay(); } } void delay(void) { unsigned int i; { for(i=0;i<=10000;i++) } } Result:
R.Y.M.E.C,BELLARY
DEPT OF ECE
43
MICROCONTROLLER LAB
5. DC MOTOR:
Write C' program to rotate DC motor by interfacing Dc motor interface module to 8051 microcontroller. // program to test nifc55 dc motor // by varying off-time and on-time with different values // user can observe different speed on dc motor // typically the off values are 30,24,18 & 12 // on values are 10,16,12 & 28 // in this source off value is taken as 30 and // on value is taken as 10 #include <Intel\8051.h> #define p8255_ctl 0x2043 #define ctlr_word 0x80 idata unsigned char off_time,on_time; idata unsigned char i; xdata unsigned char *ptr_8255_ctl; xdata unsigned char *ptr_8255_portc; void main () { ptr_8255_ctl = p8255_ctl; *ptr_8255_ctl = ctlr_word; // configure 8255 all o/p TCON = 0; TMOD = 0x21; //select mode1, timer 0 off_time = 30; on_time = 10; while(1) { TL0 = 0x66; //timer count set for 5msec TH0 = 0xFC; *ptr_8255_ctl = 0x09; // make PC4 high for(i=0;i<off_time;i++) { TR0 =1; while(TF0) { } TF0 = 0; TR0=0; } TL0 = 0x66; //timer count set for 5msec TH0 = 0xFC; *ptr_8255_ctl =0x08; // make PC4 low for(i=0;i<on_time;i++) { TR0 =1;
R.Y.M.E.C,BELLARY
DEPT OF ECE
44
R.Y.M.E.C,BELLARY
DEPT OF ECE
45
MICROCONTROLLER LAB
6. ELEVATOR:
Write C' program to implement Elevator by interfacing Elevator interface module with 8051 microcontroller. #include <Intel\8051.h> #include <standard.h> #define p8255_ctl 0x2043 #define porta 0x2040 #defineportb 0x2041 #defineportc 0x2042 #define ctl_word 0x82 void floor1(void); void floor2(void); void floor3(void); void floor4(void); void moveup(void); void movedown(void); idata unsigned char req,preq,freq,temp,fg,i,j; xdata unsigned char *ptr_8255_ctl; xdata unsigned char *ptr_8255_porta; xdata unsigned char *ptr_8255_portb; xdata unsigned char *ptr_8255_portc; void main () { ptr_8255_ctl = p8255_ctl; *ptr_8255_ctl = ctl_word; ptr_8255_porta = porta; ptr_8255_portb = portb; ptr_8255_portc = portc; *ptr_8255_porta = 0x0f; //porta lower bits for green and amber delay(50); //leds,higher bits for enabling 4 flipflops *ptr_8255_porta = 0x0f0; // mem. locn to hold adc data to display freq=0xf0; //default request is from ground floor while(1) { // floor status 1st floor if(freq==0xf0) { *ptr_8255_porta=0xf0; req = *ptr_8255_portb; //read the request from portb temp = req; req = req & 0x0f; if(req == 0x0e) //request is from same floor when it is in flr1 { floor1(); R.Y.M.E.C,BELLARY DEPT OF ECE 46
MICROCONTROLLER LAB } if(req ==0x0d) //request from second floor when it is in flr1 { preq = 0xf0; //correspondingly update the preq and freq freq = 0xf3; //freq holds the future request, moveup(); //preq holds present stage floor2(); } if(req ==0x0b) //request from third floor when it is in flr1 { preq = 0xf0; freq = 0xf6; moveup(); //blink leds from floor 1 to 3 floor3(); //red led is off and green led is on } if(req ==0x07) { preq = 0xf0; freq = 0xf9; moveup(); floor4(); } } //request from fourth floor when it is in flr1
//blink leds from floor 1 to 4 //red led is off and green led is on
// floor status 2nd floor if(freq==0xf3) { req = *ptr_8255_portb; temp = req; req = req & 0x0f; if(req ==0x0e) //request from first floor when it is flr2 { preq = 0xf3; freq = 0xf0; movedown(); //blink leds from floor 2 to 1 floor1(); //red led is off and green led is on } if(req == 0x0d) //request from same floor when it is flr2 { floor2(); //red led is off and green led is on }
R.Y.M.E.C,BELLARY
DEPT OF ECE
47
MICROCONTROLLER LAB if(req ==0x0b) //request from third floor when it is flr2 { preq = 0xf3; freq = 0xf6; moveup(); //blink leds from floor 2 to 3 floor3(); //red led is off and green led is on } if(req ==0x07) //request from fourth floor when it is flr2 { preq = 0xf3; freq = 0xf9; moveup(); //blink leds from floor 2 to 4 floor4(); //red led is off and green led is on } } //end of for loop // floor status 3rd floor if(freq==0xf6) { req=*ptr_8255_portb; temp =req; req = req & 0x0f; if(req ==0x0e) //request from first floor when it is flr3 { preq = 0xf6; freq = 0xf0; movedown(); //blink leds from floor 3 to 1 floor1(); //red led is off and green led is on } if(req ==0x0d) { preq = 0xf6; freq = 0xf3; movedown(); floor2(); } //request from second floor when it is flr3
//blink leds from floor 3 to 2 //red led is off and green led is on
if(req == 0x0b) //request from same floor when it is flr3 { floor3(); //red led is off and green led is on } if(req ==0x07) { preq = 0xf6; freq = 0xf9; R.Y.M.E.C,BELLARY //request from fourth floor when it is flr3
DEPT OF ECE
48
MICROCONTROLLER LAB moveup(); floor4(); } } //end of for loop // floor status 4th floor if(freq==0xf9) {
//blink leds from floor 3 to 4 //red led is off and green led is on
req = *ptr_8255_portb; temp = req; req = req & 0x0f; if(req ==0x0e) //request from first floor when it is flr4 { preq = 0xf9; freq = 0xf0; movedown(); //blink leds from floor 4 to 1 floor1(); //red led is off and green led is on } if(req ==0x0d) { preq = 0xf9; freq = 0xf3; movedown(); floor2(); } if(req ==0x0b) { preq = 0xf9; freq = 0xf6; movedown(); floor3(); } if(req == 0x07) { floor4(); } } //end of for loop }// end of while(1) } //end of main() void floor1(void) R.Y.M.E.C,BELLARY //request from same floor when it is flr4 //red led is off and green led is on //request from second floor when it is flr4
//blink leds from floor 4 to 2 //red led is off and green led is on //request from third floor when it is flr4
//blink leds from floor 4 to 3 //red led is off and green led is on
DEPT OF ECE
49
MICROCONTROLLER LAB { *ptr_8255_porta=0xe0; on delay_ms(20); } void floor2(void) { *ptr_8255_porta=0xd3; delay_ms(20); } void floor3(void) { *ptr_8255_porta=0xb6; delay_ms(20); } void floor4(void) { *ptr_8255_porta=0x79; delay_ms(20); } void moveup(void) { // initialise loop to on leds // unsigned char i; for(i=preq;i<freq;i++) { delay_ms(250); *ptr_8255_porta = i; //leds blink from lower to upper value delay_ms(50); } } // end of move up void movedown(void) /* initialise loop to on leds */ // unsigned char j; for(j =preq;j>freq;j--) { delay_ms(250); *ptr_8255_porta = j; //leds blink from upper to lower value delay_ms(50); } } /* end of move down */ Result:
R.Y.M.E.C,BELLARY
DEPT OF ECE
50
MICROCONTROLLER LAB
R.Y.M.E.C,BELLARY
DEPT OF ECE
51
MICROCONTROLLER LAB
INDEX
SL NO. TITLE OF THE EXPERIMENT PAGE NO.
I
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
PROGRAMMING
ALP to transfer bytes of data (Block transfer) ALP to exchange bytes of data b/n two memory locations ALP to sort an array of data bytes in Ascending/Descending order using Bubble sort algorithm ALP to find Largest/Smallest element in a given array ALP to find Square of a number ALP to find Cube of a number ALP to add two 8-bit unsigned numbers ALP to add two 16-bit unsigned numbers ALP to add two N- unsigned numbers ALP to subtract two 8-bit unsigned numbers ALP to subtract two 16-bit unsigned numbers ALP to Multiply two 8-bit unsigned numbers. ALP to Multiply two 16-bit unsigned numbers. ALP to Divide two 8-bit unsigned numbers. ALP to find two's complement an 8-bit unsigned number. ALP to find two's complement an 16-bit unsigned number. ALP to Reverse an 8-bit unsigned number. ALP to Reverse an 16-bit unsigned number. ALP to Count number of ones & zeros in a given byte of data. ALP to perform AND,OR &Ex-OR logic ALP to perform BIT Manipulations ALP to find whether the data byte is Odd or Even ALP to find whether the data byte is Positive or negative ALP to implement (display) BCD-UP counter. ALP to implement (display) BCD-DOWN counter. ALP to implement (display) DECIMAL-UP counter. ALP to implement (display) DECIMAL-DOWN counter. ALP to implement (display) HEXADECIMAL-UP counter. ALP to implement (display) HEXADECIMAL-DOWN counter. ALP to implement (display) HEXADECIMAL UP-DOWN counter.
R.Y.M.E.C,BELLARY
DEPT OF ECE
52
MICROCONTROLLER LAB
R.Y.M.E.C,BELLARY
DEPT OF ECE
53
MICROCONTROLLER LAB
SL NO.
31 32 33 34 35 36 37 38 39
PAGE NO.
II
01 02 03
INTERFACING
C' program to display the key pressed by interfacing Alphanumeric LCD panel & Hex keypad interface module to 8051 microcontroller. C' program to measure temperature by interfacing External ADC &Temperature control interface module to 8051 microcontroller.
DAC PROGRAMS:
(i) C' program to generate Ramp waveform using DAC Interface to 8051. (ii) C' program to generate Square waveform using DAC Interface to 8051. (iii) C' program to generate Triangle waveform using DAC Interface to 8051. (iv) C 'program to generate Sinusoidal waveform using DAC Interface to 8051
04
STEPPER MOTOR:
(i) C' program to rotate Stepper motor in Clockwise direction by interfacing Stepper motor interface module with 8051 microcontroller. (ii) C' program to rotate Stepper motor in Clockwise direction by interfacing Stepper motor interface module with 8051 microcontroller.
05 06
C' program to rotate DC motor by interfacing Dc motor interface module to 8051 microcontroller.
C' program to implement Elevator by interfacing Elevator interface module with 8051 microcontroller. R.Y.M.E.C,BELLARY DEPT OF ECE
54
MICROCONTROLLER LAB
SL NO.
04
PAGE NO.
05 06
C' program to rotate DC motor by interfacing Dc motor interface module to 8051 microcontroller. C' program to implement Elevator by interfacing Elevator interface module with 8051 microcontroller.
R.Y.M.E.C,BELLARY
DEPT OF ECE
55
MICROCONTROLLER LAB
R.Y.M.E.C,BELLARY
DEPT OF ECE
56
MICROCONTROLLER LAB
R.Y.M.E.C,BELLARY
DEPT OF ECE
57
MICROCONTROLLER LAB
R.Y.M.E.C,BELLARY
DEPT OF ECE
58
MICROCONTROLLER LAB
R.Y.M.E.C,BELLARY
DEPT OF ECE
59