0% found this document useful (0 votes)
78 views7 pages

Microcontroller: Unit 2

The document contains 14 example programs demonstrating various operations on microcontrollers including: 1. Moving a block of data within internal RAM 2. Moving a block of data from external to internal RAM 3. Exchanging data between internal RAM locations 4. Sorting an array stored in internal RAM 5. Finding the largest element in an internal RAM array 6. Adding two 16-bit numbers 7. Subtracting two 16-bit numbers 8. Finding the square of a number in a given range 9. Converting a BCD number to ASCII 10. Finding the cube of a number in a given range 11. Additional programs for conversion between decimal, hexadecimal, and BCD formats

Uploaded by

Vijay Singh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
78 views7 pages

Microcontroller: Unit 2

The document contains 14 example programs demonstrating various operations on microcontrollers including: 1. Moving a block of data within internal RAM 2. Moving a block of data from external to internal RAM 3. Exchanging data between internal RAM locations 4. Sorting an array stored in internal RAM 5. Finding the largest element in an internal RAM array 6. Adding two 16-bit numbers 7. Subtracting two 16-bit numbers 8. Finding the square of a number in a given range 9. Converting a BCD number to ASCII 10. Finding the cube of a number in a given range 11. Additional programs for conversion between decimal, hexadecimal, and BCD formats

Uploaded by

Vijay Singh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

MICROCONTROLLER

UNIT 2: Example Programs.

1. Write A Program to move a block of data within the internal RAM Org 0h mov r0,#40h mov r1,#30h mov r2,#5 mov a,@r0 mov @r1,a inc r0 inc r1 djnz r2,start Sjmp Start1 end

start1:

;r0 pointed to internal RAM 40h ;r1 pointing to internal RAM 030h ;r2 loaded with no. of elements in the array ;data transfer

Start:

;decrement r2,if not equal to 0,continue with data ;transfer process.

2. WAP to move a block of data external RAM to internal RAM program for block data transfer from external RAM to internal RAM enter the elements from location 0500h(ext.RAM) Org 0h start1: mov dptr,#0500h //data pointer ppointed to external memory //0500h mov r1,#30h //r1 pointing to internal RAM 030h mov r2,#5 //r2 loaded with no. of elements in the array Start: movx A,@dptr mov @r1,a inc dptr inc r1 djnz r2,start Sjmp Start1 end

3. WAP to exchange data between internal RAM locations org 0000h mov r0,#30h

;r0 pointing to 030h(int. RAM) Page 1

Veena Hegde, BMSCE, Bangalore

start:

here:

mov r1,#40h mov r2,#0ah mov a,@r0 mov r3,a mov a,@r1 mov @r0,a mov a,r3 mov @r1,a inc r0 inc r1 djnz r2,start nop sjmp here end

MICROCONTROLLER

;r1 pointing to 040h(int. RAM) ;r2 loaded with no. of elements to be exchanged ;data @r0 is stored in temporary reg r3 ;data @r1 is moved to @r0 ;data from r3 is moved to @r1 ;increment data pointers ;decrement counter r2,repeat the process if r2 is not zero

4. WAP to sort an array stored in the internal RAM org 0000h num equ 040h back1: mov r0,#50h ;store n elements(say n=5) from 50h mov a,r0 ;r0 and r1 are used as pointers mov r1,a mov r3,#04h ;load (n-1) to r3 (no. of passes) mov a,r3 mov r2,a ;load r3 to r2(no. of comparison in each pass) back: mov a,@r0 ;compare no. pointed to by r0 with no. pointed to by r1 inc r1 mov num,@r1 cjne a,num,loop sjmp next loop: jc next ;if num at r0<no. at r1 continue with comparison process //jnc next for descending order mov r4,a ;else exchange the two numbers mov a,@r1 mov @r0,a mov a,r4 mov @r1,a next: inc r0 djnz r2,back ;decrement no. of comparison djnz r3,back1 ;decrement no. of passes end

5. WAP to find the largest element in ar array stored in the internal RAM. Veena Hegde, BMSCE, Bangalore Page 2

org 0000h lar equ 040h start1: mov r0,#50h mov r2,#4h mov lar,@r0 inc r0 mov a,@r0 cjne a,lar,big sjmp next jc next mov lar,@r0 djnz r2,start sjmp here end

MICROCONTROLLER

;location lar stores the largest element in the array ;array location is 50h pointed to by r0 ;no. of elements in the array ;no. pointed to by r0 loaded to lar ;no. pointed to by r0+1 loaded to acc. ;compare the two no.s ;jump to next if a<lar ;else acc loaded to lar ;decrement count

start:

big: next: here:

6. WAP to add two 16 bit numbers // program to add two 16 bit numbers ,result available in 40h,41h and 42h(40 lsb;41 msb;42 carry) org 0h start: mov r0,#20h ;r0 pointing to lsb of src1 mov r1,#30h ;r1 pointing to lsb of src2 mov a,@r0 ;add lsb of src1 and src2 add a,@r1 mov 40h,a ;result stored at 40h inc r0 ;r0 pointing to msb of src1 inc r1 ;r0 pointing to msb of src2 mov a,@r0 addc a,@r1 ;add msb of src1 and src2 with carry mov 41h,a ;result stored at 41h mov a,#0h addc a,#0h mov 42h,a ;carry stored at 42h sjmp start end

7. WAP to subtract two 16 bit numbers // program to subtract two 16 bit numbers ,result available in 40h and 41h(40 lsb;41 msb) org 0h here: clr c ;clear carry bit Veena Hegde, BMSCE, Bangalore Page 3

mov r0,#20h mov r1,#30h mov a,@r0 subb a,@r1 mov 40h,a inc r0 inc r1 mov a,@r0 subb a,@r1 mov 41h,a sjmp here end

MICROCONTROLLER

;r0 pointing to lsb of src1 ;r1 pointing to lsb of src2 ;sub lsb of src2 from lsb of src1 ;result stored at 40h ;r0 pointing to msb of src1 ;r0 pointing to msb of src2 ;sub msbs with borrow ;result stored at 41h

8. WAP to find the square of the number in the range 0h to ffh //Finds the square of the number in data RAM 20h and store the square in location30h and31h org 0h mov r0,#20H mov a,@r0 ;num in 20h is loaded into acc and b reg mov 0f0h,a mul ab mov 030h,a ;lower order product in 30h mov 031h,0f0h ;higher order product in 31h here: sjmp here end

9. Program to convert BCD to ASCII // The equivalent ASCII code will be found in Registers R2 and R6

10. WAP to find the cube of the number in the range 0h to fh //WAP to find the cube of the number in data RAM 20h and store the result in 30h org 0h mov r0,#20H mov a,@r0 ;num in 20h is loaded into acc and b reg mov 0f0h,a mul ab ;find the square of the no. mov 0f0h,@r0 mul ab ;multiply square of the no. by the no.to get the cube. mov 030h,a ;lower order product in 30h mov 031h,0f0h ;higher order product in 31h here: sjmp here end Veena Hegde, BMSCE, Bangalore Page 4

MICROCONTROLLER
11. WAP to convert BCD to ASCII org 0h start: mov r0,#20h ;r0 pointing to src location,loaded with a BCD no. mov a,@r0 ;no. moved to accumulator and added 30h to get equivalent ;ASCII add a,#30h mov 40h,a ;result stored at 40h sjmp start end

12. WAP to convert given decimal no. to equivalent ASCII org 0h start: mov r0,#30h ;r0 pointing to src locn. mov a,@r0 ;acc. loaded with decimal no. ;process of seperating the lower nibble and higher nibble of decimal no. anl a,#0fh mov r1,a mov a,@r0 anl a,#0f0h swap a mov r2,a ;mask higher nibble ;lower nibble stored at r1 ;mask lower nibble ;exchange higher and lower nibble positions ;higher nibble stored in r2

;process of converting decimal to ASCII mov a,r1 add a,#30h mov 40h,a mov a,r2 add a,#30h mov 41h,a sjmp start end

;40h has ASCII value for lower nibble of decimal no.

;41h has ASCII value for higher nibble of decimal no.

Veena Hegde, BMSCE, Bangalore

Page 5

MICROCONTROLLER
13. WAP to convert given hex no. to equivalent decimal no. //Hex number has to be store at location x:5fffh and result to be stored in //next consecutive locations org 0h start: mov dptr,#5fffh movx a,@dptr mov 0f0h,#064h ;Load B reg with 100d or 64h div ab ;Hundreds inc dptr movx @dptr,a ;store in external ram mov a,0f0h ;remainder from b reg to acc mov 0f0h,#0ah ; Load B reg with 10d or 0ah div ab; inc dptr; movx @dptr,a ;store tens in external ram inc dptr mov a,0f0h movx @dptr,a ;store units in ext ram here: sjmp here end

14. WAP to convert given decimal no. to equivalent hexadecimal no. org 0h start: mov r0,#30h mov a,@r0 ;process of seperating the lower nibble and higher nibble of decimal no. anl a,#0fh mov r1,a mov a,@r0 swap a anl a,#0fh mov r2,a Veena Hegde, BMSCE, Bangalore ;mask higher nibble ;lower nibble stored at r1 ;exchange higher and lower nibble position ;mask lower nibble ;higher nibble stored in r2 Page 6

;process of conversion ;dec no.=higher nibble*10d (or 0ah)+lower nibble*1 mov a,r1 mov 0f0h,#01h mul ab mov r3,a mov a,r2 mov 0f0h,#0ah mul ab add a,r3 mov 40h,a sjmp start end ;lower nibble to acc ;reg b=01 ;product in r3 ;higher nibble to acc ;reg b=10d ;product in acc. ;compute hex no. ;result at 40h

MICROCONTROLLER

Veena Hegde, BMSCE, Bangalore

Page 7

You might also like