alp for 8051

21
* · · · *

Upload: sumanth-s-murthy

Post on 02-Apr-2015

756 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: ALP for 8051

Connexions module: m32822 1

Microcontroller(8051) Lab∗

Rajeshwari Hegde

This work is produced by The Connexions Project and licensed under the

Creative Commons Attribution License †

The 8051 ArchitectureThe 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.

∗Version 1.1: Nov 14, 2009 1:07 am US/Central†http://creativecommons.org/licenses/by/3.0/

http://cnx.org/content/m32822/1.1/

Page 2: ALP for 8051

Connexions module: m32822 2

1 Table 1: Register Addresses and functions

Register Address Function

P0 80h* Port0

SP 81h Stack pointer

DPL 82h Data pointer(low)

DPH 83h Data Pointer(high)

TCON 88h* Timer Control Register

TMOD 89h Timer Mode Register

TL0 8Ah Timer 0 (low byte)

TL1 8Bh Timer 1 (low byte)

TH0 8Ch Timer 0 (high byte)

TH1 8Dh Timer 1 (high byte)

P1 90h* Port 1

SCON 98h* Serial control Register

SBUF 99h Serial Bu�er Register

P2 0A0h* Port 2

IE 0A8h* Interrupt Enable Register

P3 0B0h* Port 3

IP 0B8h* Interrupt Priority Register

PSW 0D0h* Program Status Word

ACC 0E0h* Accumulator

B 0F0h*

Table 1

• - Bit Addressable

Table 3: Byte & Bit Addresses for 8051 Hardware Register

http://cnx.org/content/m32822/1.1/

Page 3: ALP for 8051

Connexions module: m32822 3

Byte Address B I T a d d r e s s Register

F0h F7 F6 F5 F4 F3 F2 F1 F0 B

E0h E7 E6 E5 E4 E3 E2 E1 E0 ACC

D0h D7 D6 D5 D4 D3 D2 D1 D0 PSW

B8h � � � BC BB BA B9 B8 IP

B0h B7 B6 B5 B4 B3 B2 B1 B0 P3

A8h AF � � AC AB AA A9 A8 IE

A0h A7 A6 A5 A4 A3 A2 A1 A0 P2

98h 9F 9E 9D 9C 9B 9A 99 98 SCON

90h 97 96 95 94 93 92 91 90 P1

88h 8F 8E 8D 8C 8B 8A 89 88 TCON

80h 87 86 85 84 83 82 81 80 P0

Table 2

Table 4

EA � ET2 ES ET1 EX1 ET0 EX0

Bit Symbol Function

7 EA Interrupt Enable bit

6 � Not implemented

5 ET2 Reserved for future use

4 ES Enable serial port interrupt

3 ET1 Enable timer1 over�ow interrupt

2 EX1 Enable external

1 ET0 Enable Timer 0 over�ow interrupt

0 EX0 Enable External interrupt 0

Table 3

1.Program to add two multibyte numbers.mov r4,#0h ;move 00 to r4mov r3,#0h ;move 00 to r3mov dptr,#9000h ; Load 9000h into dptr registermovx a,@dptr ;move the data from memory location to a registermov r0,a ;move the data from a to r0inc dptr ;increment dptrmovx a,@dptr ;move the data from memory location to amov r1,a ;move the data from a register to r1inc dptr ;increment dptrmovx a,@dptr ;move the data from memory to amov r2,a ;move the data form a to r2

http://cnx.org/content/m32822/1.1/

Page 4: ALP for 8051

Connexions module: m32822 4

inc dptr ;increment dptrmovx a,@dptr ; move the data from memory location to a registerclr c ;clear carry bitadd a,r1 ;add a and r1jnc res ;if no carry, jump to label resmov r3,a ;move data from a to r3mov a,r0 ;move data from r0 to aaddc a,r2 ; add with carry and r2jnc end1 ;if no carry, jump to label end1inc r4 ;increment r4ajmp end1 ;jump to label end1res:mov r3,a ; move data from a to r3mov a,r0 ; move data from r0 to aadd a,r2 ;add a and r2jnc end1 ; if no carry, jump to label end1inc r4 ;increment r4end1:mov r5,a ;move data from a to r5mov dptr,#900ah ;load 9000h into dptr registermov a,r4 ; move data from r4 to amovx @dptr,a ;move data from a to memory locationmov a,r5 ;move data from r5 to ainc dptr ;increment dptrmovx @dptr,a ; move data from a to memory locationmov a,r3 ; move data from r3 to ainc dptr ; increment dptrmovx @dptr,a ; move data from a to memory locationhere:sjmp hereendInput Location:9000h,9001h,9002h,9003hOutput Location:900ahInput Data: 1111 1111(hex)Output : 2222h2 nd method

mov r1,#0�h ;load r1 with immediate datamov r2,#0�h ; load r1 with immediate datamov r3,#0�h ;load r1 with immediate datamov r4,#0�h ;load r1 with immediate dataclr c ;Clear carry bitmov a,r1 ;move data from r1 to aadd a,r4 ;add a and r4mov 31h,a ;move data from a to internal RAM location 31hmov a,r2 ;move data from r2 to aaddc a,r3 ;add with carry a and r3mov 32h,a ; move data from a to internal RAM location 32hmov a,#00 ;Clear a registeraddc a,#00 ;add with carry and 0mov 33h,a ; move data from a to internal RAM location 33hhere:sjmp hereend2. Program to convert a BCD number to ASCII

http://cnx.org/content/m32822/1.1/

Page 5: ALP for 8051

Connexions module: m32822 5

To convert packed BCD to ASCII, it must �rst be converted to to unpacked BCD. Then the unpackedBCD is tagged with 30h.

mov dptr,#9000h ;Load 9000h into dptr registermovx a,@dptr ;Move the content of location 9000h to amov r2,a ;Move the data from a to r2anl a,#0f0h ;And a with 0f0hswap a ;Swap aorl a,#30h ;Or a with 30hinc dptr ;increment dptrmovx @dptr,a ;move the data from a to memory locationmov a,r2 ; Move the data from r2 to aanl a,#0fh ; And a with 0fhorl a,#30h ;Or a with 30hinc dptr ;increment dptrmovx @dptr,a ;move the data from a to memory locationhere:sjmp hereendInput: 45

Output:34 353;ASCII to BCD conversionTo convert ASCII to packed BCD, it is �rst converted to unpacked BCD(to mask 3) andthen combined to make packed BCD. For example, for 4 and 5 the keyboard gives 34 and35, respectively. The goal is to produce 45h, which is packed BCD.;ASCII to packed BCD conversionmov r0,#10h ;R0=10h,Internal memory adressmov a,@r0 ;a=hex value of Ist ASCII numberanl a,#0fh ;mask upper nibbleswap a ;swap upper and lower nibble of amov b,a ;save the number in Binc r0 ;Increment R0mov a,@r0 ;Get the next number from memoryanl a,#0fh ;Mask Higher nibbleorl a,b ;Or a & Binc r0 ;Increment memory address to store the resultmov @r0,a ;Move the content of A to internal RAM locationhere:sjmp hereendInput:35

Output:4. Program to �nd the average of 10 numbers

mov dptr,#9000h ;Load 9000h into dptr registerclr a ;Clear a registermov 0f0h,a ;move data from a to bmov r1,#05h ;move 05 to r1 registerup: movx a,@dptr ; ;move data from external memory location to aadd a,0f0h ;add a and bmov 0f0h,a ;move the result to bdec r1 ;decrement r1inc dptr ;increment dptrcjne r1,#00h,up ;compare r1 with 0, if not equal, jump to label upmov a,0f0h ;move data from b to a

http://cnx.org/content/m32822/1.1/

Page 6: ALP for 8051

Connexions module: m32822 6

mov 0f0h,#05h ;move 05h to bdiv ab ;divide a by bmov dptr,#09005h ; Load 9005h into dptr registermovx @dptr,a ;move data from a to external memory locationmov a,0f0h ;move data from b to ainc dptr ;increment dptrmovx @dptr,a ;move data from a to external memory locationhere:sjmp hereend5. To transfer a block of data from one memory location to another.

mov r2,#0ah ;Load r2 with immediate data 05hmov dptr,#9000h ;Load 9000h into dptr registermov r0,#0a0h ;Load r0 with immediate data 0a0hmov r1,#00h ;Load r1 with immediate data 00hmov a,#0h ; Load a with immediate data 00hup:movx a,@dptr ;move data from external memory to apush dph ;push dph on the stackpush dpl ;push dpl on the stackmov dph,r0 ;move data from r0 to dphmov dpl,r1 ;move data from r1 t dplmovx @dptr,a ;move data from a to external memory locationpop dpl ;pop dpl from the stackpop dph ;pop dph from the stackinc dptr ;increment dptrinc r1 ;increment r1dec r2 ;decrement r2cjne r2,#00,up ;compare r2 with 0, if not equal, jump to label uphere:sjmp hereendOutput:I block:9000h: 01 02 03 04 05II block:a000h:01 02 03 04 05( after block transfer)6. Program to convert a decimal number to hex number

mov dptr,#9000h ; Load 9000h into dptr registermovx a,@dptr ; move data from external memory location to amov r2,a ;move data from r2 to aclr c ;clear carry bitsubb a,#0ah ;subtract with borrow, 0ah from ajc over ;if carry, jump to label overmov a,r2 ;move data from r2 to aanl a,#0f0h ;and a with 0f0hswap a ;swap amov b,#0ah ;move 0ah to bmul ab ;multiply and bmov r3,a ;move data from a to r3mov a,r2 ;move data from r2 to aanl a,#0fh ;move 0fh to aadd a,r3 ;add a and r3inc dptr ;increment dptrmovx @dptr,a ; move data from a to external memory locationsjmp here

http://cnx.org/content/m32822/1.1/

Page 7: ALP for 8051

Connexions module: m32822 7

over:mov a,r2 ;move data from r2 to ainc dptr ;increment dptrmovx @dptr,a ;move data from a to external memory locationhere:sjmp hereendInput:99Output:637. Program to generate Fibonacci series

mov r1,#0ah ;Load r1 with immediate data 0ahmov dptr,#9000h ;load 9000h into dptr registermovx a,@dptr ; move data from external memory location to ainc dptr ;increment dptrmov r0,a ;move data from a to r0movx a,@dptr ; move data from external memory location to aback:mov r2,a ;move data from a to r2add a,r0 ;add a and r0inc dptr ;increment dptrmovx @dptr,a ;move data from a to external memory locationmov r3,a ;move data from a to r3mov a,r2 ; move data from r2 to amov r0,a ; move data from a to r0mov a,r3 ; move data from r3 to adjnz r1,back ;decrement r1, if not 0, jump to label backhere:sjmp hereendOutput: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 registermovx a,@dptr ; move data from external memory location to amov b,a ; move data from a to binc dptr ;increment dptrmovx a,@dptr ; move data from external memory location to aback:mov r1,b ;move data from b to r1div ab ;divide a by bmov a,b ;move data from b to ajz mess ;if a=0, jump to label messmov a,r1 ;move data from r1 to ajmp back ;jump to label backmess:mov dptr,#9005h ;Load dptr with immediate data 9005hmov a,r1 ;move data from r1 to amovx @dptr,a ;move data from a to external memory locationhere:sjmp hereendInput: 05 02Output:019.Program to convert hex number to ASCII number

mov dptr,#9000h ; Load dptr with immediate data 9005hmovx a,@dptr ; Load dptr with immediate data 9005hclr c ;clear carry bitsubb a,#0ah ;subtract with borrow, 0ah frommovx a,@dptr ; move data from external memory location to a

http://cnx.org/content/m32822/1.1/

Page 8: ALP for 8051

Connexions module: m32822 8

jc down ;if carry, jump to label downadd a,#07h ;add 07 to adown:add a,#30h ;add 30h to ainc dptr ;increment dptrmovx @dptr,a ; move data from a to external memory locationhere:sjmp hereendInput:45Output:34 3510. Program to convert an 8bit Hex number to decimal number

mov dptr,#9000h ;Load 9000h into dptr registermovx a,@dptr ;move data from external memory location to amov r2,a ;move data from a to r2clr c ;clear carry bitsubb a,#0ah ;subtract with borrow, 0ah from ajc over ;if carry, jump to label overmov a,r2 ;move data from r2 to asubb a,#064h ;subtract with borrow, 64h from ajc d1 ; if carry, jump to label d1mov a,r2 ;move data from r2 to amov b,#64h ;move immediate data 64h to bdiv ab ;divide a by binc dptr ;increment dptrmovx @dptr,a ; move data from external memory location to amov a,b ;move data from b to amov r3,a ;move data from a to r3clr c ; clear carry bitsubb a,#0ah ;subtract with borrow, 0ah from ajc d2 ;if carry, jump to label d2mov b,#0ah ;Load b with immediate data 0ahmov a,r3 ;move data from r3 to adiv ab ;divide a by binc dptr ;increment dptrmovx @dptr,a ;move data from a to external memory locationmov a,b ;move data from b to ainc dptr ;increment dptrmovx @dptr,a ;move data from a to external memory locationsjmp end1d2:mov a,r3 ;move data from r3 to ainc dptr ;increment dptrmovx @dptr,a ;move data from a to external memory locationsjmp end1d1:mov a,r2 ;move data from r2 to amov b,#0ah ;load b with immediate data 0ahdiv ab ;divide a by binc dptr ; increment dptrmovx @dptr,a ;move data from a to external memory locationmov a,b ;move data from b to ainc dptr ;increment dptrmovx @dptr,a ;move data from a to external memory locationsjmp end1

http://cnx.org/content/m32822/1.1/

Page 9: ALP for 8051

Connexions module: m32822 9

over:mov a,r2 ;move data from r2 to ainc dptr ;increment dptrmovx @dptr,a ;move data from a to external memory locationhere:sjmp hereend1:sjmp end1endInput: 0�hOutput:02 05 0511. Program to interchange two blocks of data

mov dptr,#9000h ;Load 9000h into dptr registermov r0,#04h ;Move immediate data 04 to r0mov r1,#90h ;move immediate data 90h to r1mov r2,#91h ;move immediate data 91h to r2back:movx a,@dptr ;move data from external memory location to amov r3,a ;move data from a to r3mov dph,r2 ;move data from r2 to dphmovx a,@dptr ;move data from external memory location to amov dph,r1 ;move data from r1 to dphmovx @dptr,a ;move data from external memory location to amov a,r3 ;move data from r3 to amov dph,r2 ;move data from r2 to dphmovx @dptr,a ;move data from a to external memory locationinc dptr ;increment dptrmov dph,r1 ;move data from r1 to dphdjnz r0,back ;decrement r0, if not equal to 0, jump to label backhere:sjmp hereend12. Program to �nd the LCM of two numbers

mov dptr,#9000h ;Load dptr with immediate data 9000hmovx a,@dptr ;move data from external memory location to amov r0,a ;move data from a to r0mov b,r0 ;move data from r0 to binc dptr ;increment dptrmovx a,@dptr ; move data from external memory location to amov r2,a ;move data from a to r2l2:push 0e0h ;push a onto the stackdiv ab ;divide a by bmov r1,b ;move data from b to r1cjne r1,#00,l1 ;compare r1 with 0, if not equal, jump to label l1pop 0e0h ;pop a from satckinc dptr ;increment dptrmovx @dptr,a ;move data from a to external memory locationsjmp herel1:pop 0e0h ;pop a from stackadd a,r2 ;add a and r2mov b,r0 ;move data from r0 to bsjmp l2here:sjmp hereendInput: 05 02

Output:0ah

http://cnx.org/content/m32822/1.1/

Page 10: ALP for 8051

Connexions module: m32822 10

13. Program to multiply 16 bit number by 8 bit number

mov r0,#11h ;load r0 with immediate data 11hmov r1,#22h ;load r1 with immediate data 22hmov r2,#33h ;load r2 with immediate data 33hclr c ;clear carry bitmov a,r0 ;move data from r0 to amov b,r2 ;move data from r2 to bmul ab ;multiply and bmov dptr,#9000h ;Load dptr with 9000hmovx @dptr,a ; move data from external memory location to amov r0,b ;move data from b to r0mov a,r2 ;move data from r2 to amov b,r1 ;move data from r1 to bmul ab ;multiply and badd a,r0 ;add a and r0inc dptr ;increment dptrmovx @dptr,a ;move data from a to external memory locationinc dptr ;increment dptrmov a,b ;move data from b to amovx @dptr,a ;move data from a to external memory locationhere:sjmp hereendInput: 1122, 33Output:369c6h14. Program to search an element in an array

mov r0,#05h ;Load r0 with immediate data 05hclr a ;clear amov dptr,#9000h ;load dptr with address 9000hmov r1,#0ah ;load r1 with immediate data 0ahl1:mov a,#0h ;load a with 00movc a,@a+dptr ;move data from code memory location to amov r2,a ;move data from a to r2inc dptr ;increment dptrdec r1 ;decrement r1cjne r2,#05h,l2 ;compare r2 with 05h, if not equal, jump to label l2mov dptr,#900ah ;load dptr with immediate data 900ahmov a,#0�h ;laod a with immediate data 0�hmovx @dptr,a ;move data from a to external memory locationsjmp herel2:cjne r1,#0h,l1 ; compare r1 with 0, if not equal, jump to label l1mov dptr,#900ah ;load dptr with 900ahmov a,#0h ;load a with 00hmovx @dptr,a ;move data from a to external memory locationhere:sjmp hereendInput:05,

9000h: 01 02 03 04 05 06 07 08 09 0aOutput:0�15. Program to sort an array of 10 elements

mov r3,#0ah ;load r3 with immediate data 0ahdec r3 ;decrement r3

http://cnx.org/content/m32822/1.1/

Page 11: ALP for 8051

Connexions module: m32822 11

start:mov a,r3 ;move data from r3 to amov r0,a ;move data from a to r0mov dptr,#9000h ;Load dptr with address 9000hl1:movx a,@dptr ;move data from external memory location to amov r1,a ;move data from a to r1inc dptr ;increment dptrmovx a,@dptr ; move data from external memory location to aclr c ;clear carry bitsubb a,r1 ;subtract with borrow, r1 from ajnc next ;if no carry, jump to label nextmovx a,@dptr ;move data from external memory location to amov r2,a ;move data from a to r2mov a,r1 ;move data from r1 to amovx @dptr,a ; move data from a to external memory locationdec dpl ;decrement dplmov a,r2 ;move data from r2 to amovx @dptr,a ; move data from a to external memory locationinc dptr ;increment dptrnext:djnz r0,l1 ;decrement r0, if not equal to 0, jump to label nextdjnz r3,start ;decrement r3, if not equal to 0, jump to label starthere:sjmp hereend16. Program to divide an 8 bit no by another 8 bit number

mov r0,#26h ;load r0 with immediate data 26hmov a,@r0 ;load a with data from internal RAM location(dividend)inc r0 ;increment r0mov b,@r0 ;move data from internal RAM location to b(divisor)div ab ;divide a by binc r0 ;increment r0mov @r0,a ;load internal RAM location with data from ainc r0 ; increment r0mov a,b ;move data from b to amov @r0,b ;move data from b to internal RAM locationhere:sjmp hereend17. Program to count number of 1's in a given data bytemov dptr,#9000h ;Load dptr with 9000hmovx a,@dptr ;move data from external memory location to amov r0,#0h ;load r0 with 0mov r1,#8h ;load r1 with 8clr c ;clear carry bitup:rlc a ;rotate a left through carryjnc next ;if no carry, jump to label nextinc r0 ;increment r0next:djnz r1,up ;decrement r1, and jump to label next, if r1 6=0inc dptr ;increment dptrmov a,r0 ;move data from r0 to amovx @dptr,a ;move data from a to external memory locationhere:sjmp hereend18. Program to �nd largest of n numbers

http://cnx.org/content/m32822/1.1/

Page 12: ALP for 8051

Connexions module: m32822 12

mov r0,#10h ;move immediate data 10h to r0.mov a,@r0 ;move data from internal RAM location to amov r2,a ;move data from a to r2dec r2 ;decrement r2inc r0 ;increment r0mov a,@r0 ;move data from internal RAM location to al2:push 0e0h ;save the content of a on stackinc r0 ;increment r0subb a,@r0 ;subtract content of internal RAM location from ajnc down ;if no carry, jump to label downmov a,@r0 ; move data from internal RAM location to asjmp d1 ;jump to location d1down:pop 0e0h ;pop a from stackd1:djnz r2,l2 ;decrement r2, if not zero, jump to location l2inc r0 ;increment r0mov @r0,a ;move data from a to internal RAM locationhere:sjmp hereend19. Program to convert ASCII to hexASCII codes 30 to 39 represent 0 to 9 in binary and 41 to 46 represent A to F. Thereforeif the ASCII code is between 30-39h then 30h is subtracted from the code. If the numberlies between A to F then 37h is subtracted from the code to get its binary equivalentmov dptr, #9000h ;load dptr with address 9000hmovx a,@dptr ; move data from external memory location to aclr c ;clear carry bitmov r1,a ;move data from a to r1subb a,#40h ;subtract 40h from ajc l2 ;jump to location l2, if carrymov a,r1 ;move data from r1 to asubb a,#37h ;subtract with borrow, 37h from asjmp here ;jump to location herel2:mov a,r1 ;move data from r1 to aclr c ;clear carry bitsubb a,#30h ;subtract with borrow, 30h from ahere:inc dptr ;increment dptrmovx @dptr,a ;move data from a to external memory locationrep:sjmp repend20. Program to check whether a 4th bit of a byte is 1, Store FFh if 1, else store 00 inthe same locationmov dptr,#9000hmovx a,@dptr ;move data from external memory location to ajnb 0e3h,down ;jump to location down, if 4th bit of a is setinc dptr ;increment dptrmov a,#0�h ;move 0�h to amovx @dptr,a ;move data from a to external memory locationsjmp last ;jump to location lastdown:mov a,#0h ;move 00 to ainc dptr ;increment dptrmovx @dptr,a ; move data from a to external memory locationlast:sjmp last

http://cnx.org/content/m32822/1.1/

Page 13: ALP for 8051

Connexions module: m32822 13

end21. Program to add two BCD numbersmov dptr,#9000h ;move dptr with 9000hmovx a,@dptr ; move data from external memory location to amov b,a ;move data from a to binc dptr ;increment dptrmovx a,@dptr ; move data from external memory location to aadd a,b ;add a and bda a ;decimal adjust accumulator after additionjc down ;if carry, jump to label downinc dptr ;increment dptrmovx @dptr,a ;move data from a to external memory locationsjmp last ;jump to label lastdown:mov r2,a ;move data from a to r2mov a,#01h ;move data 01h to amovx @dptr,a ;move data from a to external memory locationinc dptr ;increment dptrmov a,r2 ;move data from r2 to amovx @dptr,a ;move data from external memory location to alast:sjmp lastend22.Program to �nd the square of an 8 bit number

mov dptr,#9000h ;load dptr with 9000hmovx a,@dptr ;move data from external memory location to amov b,a ;move data from a to bmul ab ;multiply and binc dptr ;increment dptrmov r0,a ;move data from a to r0mov a,b ;move data from b to amovx @dptr,a ;move data from a to external memory locationinc dptr ;increment dptrmov a,r0 ;move data from r0 to amovx @dptr,a ;move data from a to external memory locationhere:sjmp hereend23. Program to count from 0-9here:mov a,#0h ;move 0 to aup:mov p0,a ;move data from a to port0acall delay ;call delay routineadd a,#01 ;increment acjne a,#010,up ;compare a with 10, if not equal, jump to location upsjmp here ;jump to location here;delay routinedelay:mov r1,#0�h ;move 0�h to r1l3:mov r2,#0�h ;move 0�h to r2l2:mov r3,#0�h ;move 0�h to r3l1:djnz r1,l1 ;decrement r1, repeat till r1=0djnz r3,l2 ;decrement r3,repeat l2 till r3=0djnz r2,l3 ;decrement r2, repeat l3 till r2=0ret ;returnend

http://cnx.org/content/m32822/1.1/

Page 14: ALP for 8051

Connexions module: m32822 14

24. Program to implement BCD counter to count from 0-99

here:mov a,#0h ;move 0 to aup:mov p0,a ;move data fracall delay ;call delay programinc a ;increment ada a ;decimal adjust accumulator after additioncjne a,#100,up ;compare a with 100, if not equal, jump to location upsjmp here ;jump to location heredelay routinedelay:mov r1,#0�h ;move 0�h to r1l3:mov r2,#0�h ;move 0�h to r2l2:mov r3,#0�h ;move 0�h to r3l1:djnz r1,l1 ;decrement r1, repeat till r1=0djnz r3,l2 ;decrement r3,repeat l2 till r3=0djnz r2,l3 ;decrement r2, repeat l3 till r2=0ret ;returnend25;Program to implement BCD counter to count from 99-0

here:mov b,#99h ;move 99h to bup:mov a,b ;move data from b to aadd a,#99h ;add 99h to ada a ;decimal adjust accumulator after additionmov b,a ;move data from b to amov p0,a ;move data from a to p0acall delay ;call delay routinemov a,b ;move data from b to acjne a,#0�h,up ;compare a with 0�h, if not equal , jump to location upsjmp here ;jump to location heredelay routine:delay:mov r1,#25h ;move 25h to r1l3:mov r2,#0�h ;move 0�h to r2l2:mov r3,#0�h ;move 0�h to r3l1:djnz r3,l1 ;decrement r3, jump if not equal to 0 to l1djnz r2,l2 ;decrement r2, jump if not equal to 0 to l2djnz r1,l3 ; decrement r1, jump if not equal to 0 to l3retend26. Program to generate 50msec delay

mov tmod,#01 ;select timer 0here:mov tl0,#0fdh ;load tl0 with 0fdhmov th0,#4bh ;load th0 with 4bhcpl p1.5 ;compliment p1.5acall delay ;call delay routinesjmp here ;jump to heredelay:mov r1,#0c8h ;load r1 with data 0c8hl1:setb tr0 ;set bit tr0again:jnb tf0,again ;repeat till tf0=0clr tr0 ;clear tr0clr tf0 ;clear tf0djnz r1,l1 ;decrement r1, jump to l1, if not equal to 0ret

http://cnx.org/content/m32822/1.1/

Page 15: ALP for 8051

Connexions module: m32822 15

endPart B: Interfacing programs

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 ofscanline */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;elset+=0x30;WriteChar(t); /* write lower nibble */return(indx+j); /* Return index of the key pressed */}

http://cnx.org/content/m32822/1.1/

Page 16: ALP for 8051

Connexions module: m32822 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/

Page 17: ALP for 8051

Connexions module: m32822 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);elsedelay(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/

Page 18: ALP for 8051

Connexions module: m32822 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;elseP1=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/

Page 19: ALP for 8051

Connexions module: m32822 19

6. Dual DAC, square wave

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

Page 20: ALP for 8051

Connexions module: m32822 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. Write an ALP to add, subtract, multiply and divide 2 8 bit numbers2. Write an ALP to transfer a block of data from one location to another location3. Write an ALP to exchange two blocks of data stored in memory.4. Write an ALP to add/subtract 2 16 bit numbers5. Write a program to add two BCD numbers6. Write a program to count the number of 1's in a given data byte.7. Write 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. Write an ALP to multiply a 16 bit number by an 8 bit number2. Write a programto �nd the average of 10 numbers3. Write an ALP to �nd the square of a given number4. Write an ALP to �nd the cube of a given number5. Write an ALP to arrange a set of data in ascending/descending order6. Write a program to �nd the GCF of two numbers7. Write a program to generate Fibonacci series.8. Write a program to �nd LCM of two numbers9. Write a program to search an element in an array of N numbers10. Write an ALP to �nd the largest of N numbers.11. Write an ALP to implement BCD counter to count from 0-912. Write a program to implement BCD counter to count from 9-013. Write a program to implement BCD counter to count from 99-0014. Write a program to implement BCD counter to count from 00-9915. Write a program to to convert a hexadecimal number to ASCII number16. Write a program to convert hexadecimal number to a decimal number.17. Write a program to convert an ASCII number to hexadecimal number18. Write a program to convert a BCD to ASCII number.

http://cnx.org/content/m32822/1.1/

Page 21: ALP for 8051

Connexions module: m32822 21

19. Write a program to convert an ASCII number to BCD number20. Write a program to generate a delay of 50ms.21. Write a C program to interface Alphanumeric LCD panel and Hex keypad input to 805122. Write a program to generate Square wave using DAC interface to 8051.23. Write a program to generate Triangular wave using DAC interface to 8051.24. Write a program to generate Ramp wave using DAC interface to 8051.25. Write a program to generate Sine wave using DAC interface to 8051.26. Write a C program to rotate Stepper motor in clockwise direction.27. Write a C program to rotate stepper motor in anticlockwise direction.28. Write a C program to interface Elevator to 8051.29. Write a C program to display a string on seven segment display interface

http://cnx.org/content/m32822/1.1/