Professional Documents
Culture Documents
Microcontroller (8051) Lab: Rajeshwari Hegde
Microcontroller (8051) Lab: Rajeshwari Hegde
Microcontroller(8051) Lab
Rajeshwari Hegde
This work is produced by The Connexions Project and licensed under the Creative Commons Attribution License
The 8051 Architecture The 8051 architecture consists of the following features.
Eight bit CPU with registers A(the accumulator) and B. Sisteen bit program counter (PC) and Data pointer (DPTR) Eight bit Program status word (PSW) Eight bit Stack Pointer (SP) Internal ROM or EPROM (8751) of 0(8031) to 4kbytes(8051) Internal RAM of 128 bytes:
Four register banks each containing 8 registers. Sixteen bytes, bit addressable. Eighty bytes of general purpose Data memory.
Thirty two I/O pins arranged as four 8-bit ports:P0-P3. Two 16-bit Timer/counters: T0 & T1. Full Duplex serial data receiver/transmitter :SBUF. Control registers :TCON, TMOD, SCON, PCON, IP, IE. Two external and three internal interrupt sources. Oscillator and clock circuits.
1.1: Nov 14, 2009 1:07 am US/Central
Version
http://creativecommons.org/licenses/by/3.0/
http://cnx.org/content/m32822/1.1/
Table 1
- Bit Addressable
http://cnx.org/content/m32822/1.1/
Byte Address F0h E0h D0h B8h B0h A8h A0h 98h 90h 88h 80h F7 E7 D7 B7 AF A7 9F 97 8F 87 F6 E6 D6 B6 A6 9E 96 8E 86
BITaddress F5 E5 D5 B5 A5 9D 95 8D 85 F4 E4 D4 BC B4 AC A4 9C 94 8C 84 F3 E3 D3 BB B3 AB A3 9B 93 8B 83 F2 E2 D2 BA B2 AA A2 9A 92 8A 82 F1 E1 D1 B9 B1 A9 A1 99 91 89 81 F0 E0 D0 B8 B0 A8 A0 98 90 88 80
Table 2
Table 4 EA Bit 7 6 5 4 3 2 1 0 Symbol EA ET2 ES ET1 EX1 ET0 EX0 ET2 ES ET1 EX1 ET0 EX0
Function Interrupt Enable bit Not implemented Reserved for future use Enable serial port interrupt Enable timer1 overow interrupt Enable external Enable Timer 0 overow interrupt Enable External interrupt 0
Table 3
1.Program to add two multibyte numbers. mov r4,#0h ;move 00 to r4 mov r3,#0h ;move 00 to r3 mov dptr,#9000h ; Load 9000h into dptr register movx a,@dptr ;move the data from memory location to a register mov r0,a ;move the data from a to r0 inc dptr ;increment dptr movx a,@dptr ;move the data from memory location to a mov r1,a ;move the data from a register to r1 inc dptr ;increment dptr movx a,@dptr ;move the data from memory to a mov r2,a ;move the data form a to r2
http://cnx.org/content/m32822/1.1/
inc dptr ;increment dptr movx a,@dptr ; move the data from memory location to a register clr c ;clear carry bit add a,r1 ;add a and r1 jnc res ;if no carry, jump to label res mov r3,a ;move data from a to r3 mov a,r0 ;move data from r0 to a addc a,r2 ; add with carry and r2 jnc end1 ;if no carry, jump to label end1 inc r4 ;increment r4 ajmp end1 ;jump to label end1 res:mov r3,a ; move data from a to r3 mov a,r0 ; move data from r0 to a add a,r2 ;add a and r2 jnc end1 ; if no carry, jump to label end1 inc r4 ;increment r4 end1:mov r5,a ;move data from a to r5 mov dptr,#900ah ;load 9000h into dptr register mov a,r4 ; move data from r4 to a movx @dptr,a ;move data from a to memory location mov a,r5 ;move data from r5 to a inc dptr ;increment dptr movx @dptr,a ; move data from a to memory location mov a,r3 ; move data from r3 to a inc dptr ; increment dptr movx @dptr,a ; move data from a to memory location here:sjmp here end Input Location:9000h,9001h,9002h,9003h Output Location:900ah Input Data: 1111 1111(hex) Output : 2222h
2 nd method
mov r1,#0h ;load r1 with immediate data mov r2,#0h ; load r1 with immediate data mov r3,#0h ;load r1 with immediate data mov r4,#0h ;load r1 with immediate data clr c ;Clear carry bit mov a,r1 ;move data from r1 to a add a,r4 ;add a and r4 mov 31h,a ;move data from a to internal RAM location 31h mov a,r2 ;move data from r2 to a addc a,r3 ;add with carry a and r3 mov 32h,a ; move data from a to internal RAM location 32h mov a,#00 ;Clear a register addc a,#00 ;add with carry and 0 mov 33h,a ; move data from a to internal RAM location 33h here:sjmp here end 2. Program to convert a BCD number to ASCII
http://cnx.org/content/m32822/1.1/
To convert packed BCD to ASCII, it must rst be converted to to unpacked BCD. Then the unpacked BCD is tagged with 30h. mov dptr,#9000h ;Load 9000h into dptr register movx a,@dptr ;Move the content of location 9000h to a mov r2,a ;Move the data from a to r2 anl a,#0f0h ;And a with 0f0h swap a ;Swap a orl a,#30h ;Or a with 30h inc dptr ;increment dptr movx @dptr,a ;move the data from a to memory location mov a,r2 ; Move the data from r2 to a anl a,#0fh ; And a with 0fh orl a,#30h ;Or a with 30h inc dptr ;increment dptr movx @dptr,a ;move the data from a to memory location here:sjmp here end
Input: 45
Output:34 35 3;ASCII to BCD conversion To convert ASCII to packed BCD, it is rst converted to unpacked BCD(to mask 3) and then combined to make packed BCD. For example, for 4 and 5 the keyboard gives 34 and 35, respectively. The goal is to produce 45h, which is packed BCD. ;ASCII to packed BCD conversion mov r0,#10h ;R0=10h,Internal memory adress mov a,@r0 ;a=hex value of Ist ASCII number anl a,#0fh ;mask upper nibble swap a ;swap upper and lower nibble of a mov b,a ;save the number in B inc r0 ;Increment R0 mov a,@r0 ;Get the next number from memory anl a,#0fh ;Mask Higher nibble orl a,b ;Or a & B inc r0 ;Increment memory address to store the result mov @r0,a ;Move the content of A to internal RAM location here:sjmp here end
Input:35
Output:
mov dptr,#9000h ;Load 9000h into dptr register clr a ;Clear a register mov 0f0h,a ;move data from a to b mov r1,#05h ;move 05 to r1 register up: movx a,@dptr ; ;move data from external memory location to a add a,0f0h ;add a and b mov 0f0h,a ;move the result to b dec r1 ;decrement r1 inc dptr ;increment dptr cjne r1,#00h,up ;compare r1 with 0, if not equal, jump to label up mov a,0f0h ;move data from b to a
http://cnx.org/content/m32822/1.1/
mov 0f0h,#05h ;move 05h to b div ab ;divide a by b mov dptr,#09005h ; Load 9005h into dptr register movx @dptr,a ;move data from a to external memory location mov a,0f0h ;move data from b to a inc dptr ;increment dptr movx @dptr,a ;move data from a to external memory location here:sjmp here end
over:mov a,r2 ;move data from r2 to a inc dptr ;increment dptr movx @dptr,a ;move data from a to external memory location here:sjmp here end Input:99 Output:63
mov r1,#0ah ;Load r1 with immediate data 0ah mov dptr,#9000h ;load 9000h into dptr register movx a,@dptr ; move data from external memory location to a inc dptr ;increment dptr mov r0,a ;move data from a to r0 movx a,@dptr ; move data from external memory location to a back:mov r2,a ;move data from a to r2 add a,r0 ;add a and r0 inc dptr ;increment dptr movx @dptr,a ;move data from a to external memory location mov r3,a ;move data from a to r3 mov a,r2 ; move data from r2 to a mov r0,a ; move data from a to r0 mov a,r3 ; move data from r3 to a djnz r1,back ;decrement r1, if not 0, jump to label back here:sjmp here end
Output:00 01 01 02 03 05 08 0bh, 15h, 22h 8.Program to nd the GCF of two numbers
mov dptr,#9000h ;Load 9000h into dptr register movx a,@dptr ; move data from external memory location to a mov b,a ; move data from a to b inc dptr ;increment dptr movx a,@dptr ; move data from external memory location to a back:mov r1,b ;move data from b to r1 div ab ;divide a by b mov a,b ;move data from b to a jz mess ;if a=0, jump to label mess mov a,r1 ;move data from r1 to a jmp back ;jump to label back mess:mov dptr,#9005h ;Load dptr with immediate data 9005h mov a,r1 ;move data from r1 to a movx @dptr,a ;move data from a to external memory location here:sjmp here end Input: 05 02 Output:01
mov dptr,#9000h ; Load dptr with immediate data 9005h movx a,@dptr ; Load dptr with immediate data 9005h clr c ;clear carry bit subb a,#0ah ;subtract with borrow, 0ah from movx a,@dptr ; move data from external memory location to a
http://cnx.org/content/m32822/1.1/
jc down ;if carry, jump to label down add a,#07h ;add 07 to a down:add a,#30h ;add 30h to a inc dptr ;increment dptr movx @dptr,a ; move data from a to external memory location here:sjmp here end Input:45 Output:34 35
over:mov a,r2 ;move data from r2 to a inc dptr ;increment dptr movx @dptr,a ;move data from a to external memory location here:sjmp here end1:sjmp end1 end Input: 0h Output:02 05 05
mov dptr,#9000h ;Load 9000h into dptr register mov r0,#04h ;Move immediate data 04 to r0 mov r1,#90h ;move immediate data 90h to r1 mov r2,#91h ;move immediate data 91h to r2 back:movx a,@dptr ;move data from external memory location to a mov r3,a ;move data from a to r3 mov dph,r2 ;move data from r2 to dph movx a,@dptr ;move data from external memory location to a mov dph,r1 ;move data from r1 to dph movx @dptr,a ;move data from external memory location to a mov a,r3 ;move data from r3 to a mov dph,r2 ;move data from r2 to dph movx @dptr,a ;move data from a to external memory location inc dptr ;increment dptr mov dph,r1 ;move data from r1 to dph djnz r0,back ;decrement r0, if not equal to 0, jump to label back here:sjmp here end
mov dptr,#9000h ;Load dptr with immediate data 9000h movx a,@dptr ;move data from external memory location to a mov r0,a ;move data from a to r0 mov b,r0 ;move data from r0 to b inc dptr ;increment dptr movx a,@dptr ; move data from external memory location to a mov r2,a ;move data from a to r2 l2:push 0e0h ;push a onto the stack div ab ;divide a by b mov r1,b ;move data from b to r1 cjne r1,#00,l1 ;compare r1 with 0, if not equal, jump to label l1 pop 0e0h ;pop a from satck inc dptr ;increment dptr movx @dptr,a ;move data from a to external memory location sjmp here l1:pop 0e0h ;pop a from stack add a,r2 ;add a and r2 mov b,r0 ;move data from r0 to b sjmp l2 here:sjmp here end
Input: 05 02
Output:0ah
http://cnx.org/content/m32822/1.1/
10
mov r0,#05h ;Load r0 with immediate data 05h clr a ;clear a mov dptr,#9000h ;load dptr with address 9000h mov r1,#0ah ;load r1 with immediate data 0ah l1:mov a,#0h ;load a with 00 movc a,@a+dptr ;move data from code memory location to a mov r2,a ;move data from a to r2 inc dptr ;increment dptr dec r1 ;decrement r1 cjne r2,#05h,l2 ;compare r2 with 05h, if not equal, jump to label l2 mov dptr,#900ah ;load dptr with immediate data 900ah mov a,#0h ;laod a with immediate data 0h movx @dptr,a ;move data from a to external memory location sjmp here l2:cjne r1,#0h,l1 ; compare r1 with 0, if not equal, jump to label l1 mov dptr,#900ah ;load dptr with 900ah mov a,#0h ;load a with 00h movx @dptr,a ;move data from a to external memory location here:sjmp here end
Input:05,
9000h: 01 02 03 04 05 06 07 08 09 0a Output:0
11
start:mov a,r3 ;move data from r3 to a mov r0,a ;move data from a to r0 mov dptr,#9000h ;Load dptr with address 9000h l1:movx a,@dptr ;move data from external memory location to a mov r1,a ;move data from a to r1 inc dptr ;increment dptr movx a,@dptr ; move data from external memory location to a clr c ;clear carry bit subb a,r1 ;subtract with borrow, r1 from a jnc next ;if no carry, jump to label next movx a,@dptr ;move data from external memory location to a mov r2,a ;move data from a to r2 mov a,r1 ;move data from r1 to a movx @dptr,a ; move data from a to external memory location dec dpl ;decrement dpl mov a,r2 ;move data from r2 to a movx @dptr,a ; move data from a to external memory location inc dptr ;increment dptr next:djnz r0,l1 ;decrement r0, if not equal to 0, jump to label next djnz r3,start ;decrement r3, if not equal to 0, jump to label start here:sjmp here end
mov r0,#26h ;load r0 with immediate data 26h mov a,@r0 ;load a with data from internal RAM location(dividend) inc r0 ;increment r0 mov b,@r0 ;move data from internal RAM location to b(divisor) div ab ;divide a by b inc r0 ;increment r0 mov @r0,a ;load internal RAM location with data from a inc r0 ; increment r0 mov a,b ;move data from b to a mov @r0,b ;move data from b to internal RAM location here:sjmp here end 17. Program to count number of 1's in a given data byte mov dptr,#9000h ;Load dptr with 9000h movx a,@dptr ;move data from external memory location to a mov r0,#0h ;load r0 with 0 mov r1,#8h ;load r1 with 8 clr c ;clear carry bit up:rlc a ;rotate a left through carry jnc next ;if no carry, jump to label next inc r0 ;increment r0 next:djnz r1,up ;decrement r1, and jump to label next, if r1=0 inc dptr ;increment dptr mov a,r0 ;move data from r0 to a movx @dptr,a ;move data from a to external memory location here:sjmp here end 18. Program to nd largest of n numbers
http://cnx.org/content/m32822/1.1/
12
mov r0,#10h ;move immediate data 10h to r0. mov a,@r0 ;move data from internal RAM location to a mov r2,a ;move data from a to r2 dec r2 ;decrement r2 inc r0 ;increment r0 mov a,@r0 ;move data from internal RAM location to a l2:push 0e0h ;save the content of a on stack inc r0 ;increment r0 subb a,@r0 ;subtract content of internal RAM location from a jnc down ;if no carry, jump to label down mov a,@r0 ; move data from internal RAM location to a sjmp d1 ;jump to location d1 down:pop 0e0h ;pop a from stack d1:djnz r2,l2 ;decrement r2, if not zero, jump to location l2 inc r0 ;increment r0 mov @r0,a ;move data from a to internal RAM location here:sjmp here end 19. Program to convert ASCII to hex ASCII codes 30 to 39 represent 0 to 9 in binary and 41 to 46 represent A to F. Therefore if the ASCII code is between 30-39h then 30h is subtracted from the code. If the number lies between A to F then 37h is subtracted from the code to get its binary equivalent mov dptr, #9000h ;load dptr with address 9000h movx a,@dptr ; move data from external memory location to a clr c ;clear carry bit mov r1,a ;move data from a to r1 subb a,#40h ;subtract 40h from a jc l2 ;jump to location l2, if carry mov a,r1 ;move data from r1 to a subb a,#37h ;subtract with borrow, 37h from a sjmp here ;jump to location here l2:mov a,r1 ;move data from r1 to a clr c ;clear carry bit subb a,#30h ;subtract with borrow, 30h from a here:inc dptr ;increment dptr movx @dptr,a ;move data from a to external memory location rep:sjmp rep end 20. Program to check whether a 4th bit of a byte is 1, Store FFh if 1, else store 00 in the same location mov dptr,#9000h movx a,@dptr ;move data from external memory location to a jnb 0e3h,down ;jump to location down, if 4th bit of a is set inc dptr ;increment dptr mov a,#0h ;move 0h to a movx @dptr,a ;move data from a to external memory location sjmp last ;jump to location last down:mov a,#0h ;move 00 to a inc dptr ;increment dptr movx @dptr,a ; move data from a to external memory location last:sjmp last
http://cnx.org/content/m32822/1.1/
13
end 21. Program to add two BCD numbers mov dptr,#9000h ;move dptr with 9000h movx a,@dptr ; move data from external memory location to a mov b,a ;move data from a to b inc dptr ;increment dptr movx a,@dptr ; move data from external memory location to a add a,b ;add a and b da a ;decimal adjust accumulator after addition jc down ;if carry, jump to label down inc dptr ;increment dptr movx @dptr,a ;move data from a to external memory location sjmp last ;jump to label last down:mov r2,a ;move data from a to r2 mov a,#01h ;move data 01h to a movx @dptr,a ;move data from a to external memory location inc dptr ;increment dptr mov a,r2 ;move data from r2 to a movx @dptr,a ;move data from external memory location to a last:sjmp last end
mov dptr,#9000h ;load dptr with 9000h movx a,@dptr ;move data from external memory location to a mov b,a ;move data from a to b mul ab ;multiply and b inc dptr ;increment dptr mov r0,a ;move data from a to r0 mov a,b ;move data from b to a movx @dptr,a ;move data from a to external memory location inc dptr ;increment dptr mov a,r0 ;move data from r0 to a movx @dptr,a ;move data from a to external memory location here:sjmp here end 23. Program to count from 0-9 here:mov a,#0h ;move 0 to a up:mov p0,a ;move data from a to port0 acall delay ;call delay routine add a,#01 ;increment a cjne a,#010,up ;compare a with 10, if not equal, jump to location up sjmp here ;jump to location here ;delay routine delay:mov r1,#0h ;move 0h to r1 l3:mov r2,#0h ;move 0h to r2 l2:mov r3,#0h ;move 0h to r3 l1:djnz r1,l1 ;decrement r1, repeat till r1=0 djnz r3,l2 ;decrement r3,repeat l2 till r3=0 djnz r2,l3 ;decrement r2, repeat l3 till r2=0 ret ;return end
http://cnx.org/content/m32822/1.1/
14
here:mov a,#0h ;move 0 to a up:mov p0,a ;move data fr acall delay ;call delay program inc a ;increment a da a ;decimal adjust accumulator after addition cjne a,#100,up ;compare a with 100, if not equal, jump to location up sjmp here ;jump to location here delay routine delay:mov r1,#0h ;move 0h to r1 l3:mov r2,#0h ;move 0h to r2 l2:mov r3,#0h ;move 0h to r3 l1:djnz r1,l1 ;decrement r1, repeat till r1=0 djnz r3,l2 ;decrement r3,repeat l2 till r3=0 djnz r2,l3 ;decrement r2, repeat l3 till r2=0 ret ;return end
here:mov b,#99h ;move 99h to b up:mov a,b ;move data from b to a add a,#99h ;add 99h to a da a ;decimal adjust accumulator after addition mov b,a ;move data from b to a mov p0,a ;move data from a to p0 acall delay ;call delay routine mov a,b ;move data from b to a cjne a,#0h,up ;compare a with 0h, if not equal , jump to location up sjmp here ;jump to location here delay routine: delay:mov r1,#25h ;move 25h to r1 l3:mov r2,#0h ;move 0h to r2 l2:mov r3,#0h ;move 0h to r3 l1:djnz r3,l1 ;decrement r3, jump if not equal to 0 to l1 djnz r2,l2 ;decrement r2, jump if not equal to 0 to l2 djnz r1,l3 ; decrement r1, jump if not equal to 0 to l3 ret end
mov tmod,#01 ;select timer 0 here:mov tl0,#0fdh ;load tl0 with 0fdh mov th0,#4bh ;load th0 with 4bh cpl p1.5 ;compliment p1.5 acall delay ;call delay routine sjmp here ;jump to here delay:mov r1,#0c8h ;load r1 with data 0c8h l1:setb tr0 ;set bit tr0 again:jnb tf0,again ;repeat till tf0=0 clr tr0 ;clear tr0 clr tf0 ;clear tf0 djnz r1,l1 ;decrement r1, jump to l1, if not equal to 0 ret
http://cnx.org/content/m32822/1.1/
15
end
1.Write a C program to display the code of the key pressed #include <REG51xD2.H> #include "lcd.h" unsigned char getkey(); void delay(unsigned int); main() { unsigned char key; InitLcd(); /* Initialise LCD */ WriteString("Key Pressed="); /* Display msg on LCD */ while(1) { GotoXY(12,0); /* Set Cursor Position */ key = getkey(); /* Call Getkey method */ } } unsigned char getkey() { unsigned char i,j,k,indx,t; P0=0x0; P1=0x0; P2 = 0x00; /* P2 as Output port */ indx = 0x00; /* Index for storing the rst value of scanline */ for(i=0x00E;i>=0x00B;i =1) /* for 4 scanlines */ { P2 = i; /* write data to scanline */ t = P0; /* Read readlines connected to P0*/ t = t; if(t>0) /* If key press is true */ { delay(6000); /* Delay for bouncing */ for(j=0;j<=7;j++) /* Check for 8 lines */ { t =1; if(t==0) /* if get pressed key*/ { k = indx+j; /* Display that by converting to Ascii */ t = k 4; t +=0x30; WriteChar(t); /* Write upper nibble */ t = k & 0x0f; if(t > 9) t+=0x37; else t+=0x30; WriteChar(t); /* write lower nibble */ return(indx+j); /* Return index of the key pressed */ }
http://cnx.org/content/m32822/1.1/
16
} } indx += 8; /* If no key pressed increment index */ } } void delay(unsigned int x) /* Delay routine */ { for(;x>0;x); } 2.Elevator Interface #include <REG51F.H> void delay(unsigned int); main() { unsigned char Flr[9] = {0x,0x00,0x03,0x,0x06,0x,0x,0x,0x09}; unsigned char FClr[9] = {0x,0x0E0,0x0D3,0x,0x0B6,0x,0x,0x,0x79}; unsigned char ReqFlr,CurFlr = 0x01,i,j; P0 = 0x00; P0 = 0x0f0; while(1) { P1 = 0x0f; ReqFlr = P1 | 0x0f0; while(ReqFlr == 0x0) ReqFlr = P1 | 0x0f0; /* Read Request Floor from P1 */ ReqFlr = ReqFlr; if(CurFlr == ReqFlr) /* If Request oor is equal to Current Floor */ { P0 = FClr[CurFlr]; /* Clear Floor Indicator */ continue; /* Go up to read again */ } else if(CurFlr > ReqFlr) /* If Current oor is > request oor */ { i = Flr[CurFlr] - Flr[ReqFlr]; /* Get the no of oors to travel */ j = Flr[CurFlr]; for(;i>0;i) /* Move the indicator down */ { P0 = 0x0f0|j; j; delay(25000); } } else /* If Current oor is < request oor */ { i = Flr[ReqFlr] - Flr[CurFlr]; /* Get the no of oors to travel */ j = Flr[CurFlr]; for(;i>0;i) /* Move the indicator Up */ { P0 = 0x0f0 | j; j++; delay(25000);
http://cnx.org/content/m32822/1.1/
17
} } CurFlr = ReqFlr; /* Update Current oor */ P0 = FClr[CurFlr]; /* Clear the indicator */ } } void delay(unsigned int x) { for(;x>0;x); } 3.Stepper motor interface #include <REG51xD2.H> static bit Dir=0; void delay(unsigned int x) /* Delay Routine */ { for(;x>0;x); } void ChangeDir(void) interrupt 0 /* Int Vector at 000BH, Reg Bank 1 */ { Dir = Dir; /* Complement the Direction ag */ ClrLcd(); if(Dir) delay(32000); else delay(32000); } main() { unsigned char Val,i; EA=0x1; /* Enable Interrupt ag and Interrupt 0 & Serial Interrupt */ EX0=0x1; ES=0x1; P0=0x00; /*since the monitor is using the serial interrupt it has to be enabled*/ while(1) { if(Dir) /* If Dir Clockwise */ { Val = 0x88; for(i=0;i<4;i++) { P0 = Val; /* Write data for clock wise direction*/ Val = Val 1; delay(575); } } else /* AntiClockwise Direction */ { Val = 0x11; for(i=0;i<4;i++) { P0 = Val; /* Write data for anticlock wise direction*/
http://cnx.org/content/m32822/1.1/
18
Val = Val 1; delay(575); } } } } 4. Seven segment display #include<stdio.h> #include <REG51.H> void delay(int g); int port[20] = {0x,0x,0x,0x, 0xc6,0x86,0xc7,0x86, 0xbf,0xc0,0xde,0x87, 0xbf,0x92,0x91,0x92, 0x92,0xc8,0x86,0x87},i; void main(){ int d,b,j,k,s; while(1){ i=0; for(d=0;d<5;d++) { for(b=0;b<4;b++) { k=port[i++]; for(j=0;j<8;j++){ s=k; s=s&0x80; if(s==0x00) P1= 0x00; else P1=0x1; P2 = 0x1; P2=0x00; s=k; s=s 1; k=s; } } delay(10000); delay(10000); } } } void delay(int g) { int h; for(h=0;h<=g;g++) { ; } }
http://cnx.org/content/m32822/1.1/
19
#include <REG51xD2.H> #include "lcd.h" sbit Amp = P3^3; /* Port line to change amplitude */ sbit Fre = P3^2; /* Port line to change frequency */ void delay(unsigned int x) /* delay routine */ { for(;x>0;x); } main() { unsigned char on = 0x7f,o=0x00; unsigned int fre = 100; InitLcd(); /* Initialize LCD */ WriteString("Squarewave"); /* Write to LCD */ while(1) { if(!Amp) /* if user choice is to change amplitude */ { while(!Amp); /* wait for key release */ on+=0x08; /* Increase the amplitude */ } if(!Fre) /* if user choice is to change frequency */ { if(fre > 1000) /* if frequency exceeds 1000 reset to default */ fre = 100; while(!Fre); /* wait for key release */ fre += 50; /* Increase the frequency */ } P0=on; /* write amplitude to port */ P1=on; delay(fre); P0 = o; /* clear port */ P1 = o; delay(fre); } } 7. Dual DAC, triangle wave #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 */ while(1) { for(i=0;i<0x;i++){ /* Generate ON pulse */ P1 = i; P0 = i; }
http://cnx.org/content/m32822/1.1/
20
for(i=0xfe;i>0x00;i) /* Generate OFF pulse */ {P0 = i; P1 = i;} } } 8. Dual DAC, RAMP wave #include <REG51xD2.H> main() { unsigned char i=0; P0 = 0x00; /* P0 as Output port */ while(1) { for(i=0;i<0x;i++) { P1 = i; P0 = i; } P0 = 0; P1 =0; } } Microcontroller Lab Question Bank 1. 2. 3. 4. 5. 6. 7. Write Write Write Write Write Write Write an ALP to add, subtract, multiply and divide 2 8 bit numbers an ALP to transfer a block of data from one location to another location an ALP to exchange two blocks of data stored in memory. an ALP to add/subtract 2 16 bit numbers a program to add two BCD numbers a program to count the number of 1's in a given data byte. a program to check whether the 4th bit of a byte is 0. Store FF if it 1.Else
store 00 in the same location 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. Write Write Write Write Write Write Write Write Write Write Write Write Write Write Write Write Write Write an ALP to multiply a 16 bit number by an 8 bit number a programto nd the average of 10 numbers an ALP to nd the square of a given number an ALP to nd the cube of a given number an ALP to arrange a set of data in ascending/descending order a program to nd the GCF of two numbers a program to generate Fibonacci series. a program to nd LCM of two numbers a program to search an element in an array of N numbers an ALP to nd the largest of N numbers. an ALP to implement BCD counter to count from 0-9 a program to implement BCD counter to count from 9-0 a program to implement BCD counter to count from 99-00 a program to implement BCD counter to count from 00-99 a program to to convert a hexadecimal number to ASCII number a program to convert hexadecimal number to a decimal number. a program to convert an ASCII number to hexadecimal number a program to convert a BCD to ASCII number.
http://cnx.org/content/m32822/1.1/
21
19. 20. 21. 22. 23. 24. 25. 26. 27. 28. 29.
Write Write Write Write Write Write Write Write Write Write Write
a a a a a a a a a a a
program to convert an ASCII number to BCD number program to generate a delay of 50ms. C program to interface Alphanumeric LCD panel and Hex keypad input to 8051 program to generate Square wave using DAC interface to 8051. program to generate Triangular wave using DAC interface to 8051. program to generate Ramp wave using DAC interface to 8051. program to generate Sine wave using DAC interface to 8051. C program to rotate Stepper motor in clockwise direction. C program to rotate stepper motor in anticlockwise direction. C program to interface Elevator to 8051. C program to display a string on seven segment display interface
http://cnx.org/content/m32822/1.1/