0 ratings0% found this document useful (0 votes) 3 views22 pagesMicrocontroller Programs
Programs for microcontroller 8051
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content,
claim it here.
Available Formats
Download as PDF or read online on Scribd
a ———
AIM: aa ae the source block starting with address 20h, (internal RAM)
) bytes of data with destination block starting with addr a
can ‘ ing with address 40h
SOFTWARE REQUIRED: Keilu vision 5
ALGORITHM:
‘Step 1: Start
Step 2: Initialize Count
etch address of first memory location
Fetch address of second memory location
ey eu all the data’s from first memory location to second memory location byte by byte
Set program start address to 00H
Initialize loop counter R2 to 5 (number of bytes to swap)
RO, #201 Set source poinier RO to memory address 20H
| MOV RI, #408 Set destination pointer R1 to memory address 40H
UP: ‘A, GRO Load value from source address (RO) into accumulatorA:
Exchange accumulator A with value at destination address (R1)
‘Store the original destination value (now in A) back to source address
Increment source pointer to next byte
Increment destination pointer to next byte
‘Decrement loop counter R2; if not zero, repeat the loop.
End of program
AFTER EXECUTION:
Qe 89 99 00.00 46 90.90 6
00 00 9B 00/00 00.96 06.68
Deoweps 00-00 8 4
power 90-00 6008AIM: Write an ALP to exchange the source block starting with address 10h (Internal memon
containing n (06) bytes of data with destination block starting at location 00h (Externa
memory).
SOFTWARE REQUIRED: Keil visions
ALGORITHM:
Step 1: Start
Step 2: Initialize Count
Step 3: Fetch address of internal memory location
Step 4: Fetch address of external memory location
Step 5S: Exchange all the data’s from internal memory location to extemal memory location & vice
versa byte by byte
Step 6: Stop
CODE:
ORG 00H Set program start address to OOH
MOV RO, #10H Initialize RO as pointer to intemal memory starting at 10H
MOV DPTR, #0000H | initialize DPTR to external memory starting at OOOOH
MOV R2, #06H Set loop counter R2 to 6 (number of bytes to swap)
UP: MOVX A, @DPTR | Read abyte from extemal memory at addtess DPTR into accumulator A
MOV B,@RO Load byte from internal memory at address RO into register B
XCH A,B Exchange accumulator A with B (swap exiemal and internal data)
MOVX @DPTR, A Write swapped value (original internal byte) back to external memory
MOV @R0,B Write swapped value (original external byte) back to intemal memory
INC RO Increment internal memory pointer to next location
INC DPTR Increment DPTR to next external memory address
DINZ R2, UP Decrement loop counter R2 and repeat until all 6 bytes are swapped
| END End of program
OUTPUT:
BEFORE EXECUTION AFTER EXECUTION:
03 00 03 90
8 00 09 09 06
30 00 oH 09
13°80 00 40 94 90.
‘Adee DOTEXPERIMENT: 6
AIM: Write an ALP to subtract the bytes in Internal RAM 34h &3Sh store the resull in rewister RS
(LSB) & R6 (MSB)
SOFTWARE REQUIRED: Keil u vision 5
ALGORITHM:
Step 1: Start
Step 2: Fetch the address of first data
Step 3: Fetch address of second data
Step 4: Subtract second data from first data
Step 5 : Store remainder in register r5 borrow in register r6
Step 6: Stop
CODE:
ORG OOH Set origin (start address) at 0000H
MOV RO, #34H Load register RO with address 34H
MOV A,@RO Move the data from memory location 34H into accumulator A.
INC RO Increment RO to point to next memory location (35H)
MOV B,@RO Move the data from memory location 35H into register B
SUBB A,B Subtract B from A (A= A - B with borrow)
MOVRS,A Store the result of subtraction in register RS
INC EXIT Jump to EXIT ifnto borrow (A> B)
DEC R6 Decrement register R6 if borrow occurs (A < B)
EXIT:SIMP EXIT Infinite loop (program stops here)
END End of program
BEFORE EXECUTION AFTER EXECUTION
Reiter valve Peaset Vabe ian
Memory 1 oF fee aren paw
a, nv 05 2 mas 4
it on a oa pot
Aes: ML ee he no
| aise 8 02 8 aD y
# os % a0 wv 0
< C 5 ay fs
Ds0x34s FF 80 00 € ooo is 5 se 0
f 7 oo | mn OA
DebetCi: 09 00 00 | = 2 ae
| at a 0
: b O80 :
oyaT
AIM: Write an ALP to multiply two 8-bit numbers. stored
it 30h a it
esult in 32h and 33h of Internal RAM. 3 30h and. 31h and storet6- bit
soFTWARE REQUIRED: Keilu vision s
RITHM:
step 1: Start
Siep2: Fetch the address of first data
Step: Fetch address of second data
siep 4: Multiply first data with second data
Step 5: Store LSB of the product to location 32h and MSB to location 33h
Siep 6: Stop
CODE:
‘ORG 00H Set program starting address to 0000H
MOV RO, #30H Load RO with address 30H
Move first number from memory location 30H into accumulator A
Increment RO to point to next location (31H)
Moye second number from memory location 31H into register B
Multiply A and B (result; low byte in A, high byte in B)
Increment RO to point to 32H
Store lower byte of result at memory location 32H
Increment RO to point fo 33H
Store higher byte of result at memory lovation 33H
End of program
nue
= “gor 3LK Zor 3
Address: [O:CxSO my et ea
BEFORE EXECUTION:
Memory Location D:30H,
0X30; 15[ 10| 00, 00] 00] 0
_AFTER EXECUTIO
emory Location D30H
0x30: | 15| 10| S0| 01] 00 00—
AIM: Write an ALP to separate positive and negative in a given array.
‘SOFTWARE REQUIRED: Keil u vision 5
Initialize the array length
Initialize the intemal RAM address for array
Jnitialize the array address to store positive numbers
,5: Initialize the array address to store negative numbers
: Check the number is whether positive ot negative
Store the number to corresponding location
Program start at address 0O00H
RO = address of source start
RI = address of internal destination
DPTR = base address of external memory,
Loop Count =5
Load value from source (indirect)
IfMSB = 1 ? negative
Store positive in internal RAM. (indirect)
Next source
‘Next internal destination
Store negative in external RAM
Point DPTR to next external cell
‘Next source
Repeat 5 times
Halt (infinite loop)EXPERIMENT: 10
AIM: Write an ALP to separate even or odd elements ina given array.
SOFTWARE REQUIRED: Keil u vision 5
ALGORITHM:
Step 1: Start
Step 2: Initialize the array length
Step 3: Initialize the internal RAM address for array
Step 4: Initialize the array address to store even numbers
Initialize the array address to store odd numbers
Step 5: Check the number is whether even ot odd
Step 6: Store the number to corresponding location
Step 7: Stop
CODE:
ORG 0000b Program start at address OUOOH
| MOV RO, #30h RO = address of source start
MOV R1, #50h RI =address of internal RAM destination (EVEN Nos)
MOV DPTR, #2000h DPTR = base address of external memory (ODD Nos)
MOV R3, #05 Loop Count =$
LOOP:
MOY A, @RO. Load yalue from source (indirect)
JB ACC.0, STORE_ODD. IfLSB =1?0DD No.
STORE_EVEN:
MOV @R1, A Store EVEN in internal RAM (indirect)
INCRL Next source
INC RO Next internal destination
SJMP NEXT
STORE_ODD:
MOVX @DPTR, A
INC DPTR
INC RO Store ODD in external RAM Point DPTR to next external cell
NEXT: Next source
DJNZ R3, LOOP
END_LOOP: Repeat 5 times
SJMP END LOOP
END Halt (infinite loop)
RESULT:
Memory Mempy2 Memory 3
Address: [D: 0X030 | Address: [Dsaxnoso, Address: R200
D;0%80% 22.98 PE Pp On| || |BYOMS01) 34 Pe 007199) 05) ca, csonos aa Se FE 0 tol
AE 2 FA 98 2 FA O© O&© OOO Be 5 3 oc212
agiven array Starting from 20h &
an ALP to, find Largest & Smallest umber fr
asm Wee jn Internal Memory location 40h. a
JARE REQUIRED: Keil u vision 5
ire the current element with the next element
e current element is greater than the next element, then current element is larger
‘current element is less than the next element, then the next element is larger
neat till all the elements are compared
Origin — start of program memory
Load RO with starting address 20H (array base address)
Load RI with count = $ (number of elements)
Clear register B (assume minimum value initially OOH)
Move data from memory location pointed by RO into ACC
Compare A with B, ifnot equal jump to NEXT
IfA B, move A into B (update min/max. value)
Store the updated value at memory location 40H
Increment RO to point to next memory location
Decrement R1, if not zero repeat loop
End of program
AFTER EXECUTION
Memory 2TION PROGRAM
an ALP for Decimal UP-Coumer,
op RIMENT? 13
ave Wie
sormwane REQUIRED: Keil u vision S
Start :
Move 00 to A register
call the delay subroutine for 1 second delay
Increment A register
‘Convert the value into decimal for the BCD up counter
till last value
Set program start address to OOOOH
Initialize accumulator A to 00H
Call subroutine DELAY to create a time delay
Send the value of A to port PO (output on LEDs:
Add 1 to the accumulator A
Decimal Adjust Accumulator (for BCD addition)
TEA is not zero, jump back to BACK label (loop)
SJMP HERE
MOV R1, #0FFH Initialize RI to 255 (outer loop counter)
MOY R2,#0FFH | {nitialize R2 to 255 (middle loop counter)
Initialize R3 to 255 (inner loop counter)
Decrement R3, repeat inner loop until R3 = 0
Decrement R2, repeat middle loop
Decrement RJ, repeat outer loop
Return from subroutine
End of the program
SVEs _winaow
+) ome
Sent Port
‘ume ay Rene
——F__rors
periphedls Toolk
interrupt
overs
Infinite loop here after counter overflows (stop program here)EXPERIMENT: 14 eae
AIM: Write an ALP for Decimal DOWN-Counter.
SOFTWARE REQUIRED: Keil u vision 5
ALGORITHM;
Step i: Start
Step 2: Move 00 to A register
Step 3: Call the delay subroutine for 1 second delay
Step 4: Add 99h for down counter
Step 5: Convert the value into decimal forthe BCD down counter
Step 6; Countttill last value
Step 7; Stop
Cope:
ORG 0008 Set program start address to OOOOH
MOY A, #00 Initialize accumulator A to 00H
BACK: ACALL DELAY
MOV PO, A
ADD A, #99H
DAA
INZ BACK
HERE: SJMP HERE
DELAY: MOY RI, #0PFH
DELAY1: MOV R2, #0FFH
DELAY2: MOV R3, #0FEH
AGAIN: DJINZ R3, AGAIN
DJNZ R2, DELAY2
DJNZ R1, DELAY1
Call subroutine DELAY to create atime delay
Send the value of A to port PO (output on LEDs
Add immediate value 9911 to accumulator A
Decimal Adjust Accumulator (for BCD addition)
IfA is not zero, jump back to BACK label (loap)
Infinite loop here after counter overflows (stop program here)
Initialize R1 to 255 (outer loop counter)
Initialize R2 to 255 (middle loop counter)
Initialize R3 to 255 (inner loop counter)
Decrement R3, repeat inner Joop until R3 = 0
Decrement R2, repeat middle loop
Decrement Ri, repeat outer loop
RET Return from subroutine
END
End of the program
STEPS TO BE FOLLLOWED
(elm em ar Desa aL ele) 20S weve
WesPons O17) pono
serial Pony
Thee Pes ne
ie ¥ DBFE Pen>
RESULT a
| Pe: foroe— ySq—p- Ry - POR a
} Pine: FOE rr [opie IRR my1S)
ALP for Hexadecimal UP-Counter,
EQUIRED: Keil y vision 5
accumulator with 00h
delay subroutine for delay
i
i sae Step 4) Increment Ace Muliior
Set program start address to DOOOH
Initialize accumulator A to OOM
Call DELAY subjoutine to eyeate atime delay
Output the current value of. Ato por Pl (e.g, LEDs)
Increment the accumulator A byl
ILA is not zero, Jump back to BACK (loop until A overflows
thom OxFF to 0x00)
Infinite loop fo stop program execution here
Initialize outer loop counter RI to 255
Initialize middle loop counter R2 1 255
Initialize inner loop counter R3 to 255
Decrement R3 and loop until R3 = 0
Deerement R2 and loop middle loop
Decrement RI and loop outer Joop
Retum from subroutine to the calling location,
End of the program
Redphetsis Fools SVES Wondow
5 interrupt | dea
Wvorpoits > |¥| poito
Setisi ror)
Toes Ports
Port a
Ppolel Port 4
| jp Pont T__ Bes cH
Pa: [RFF VR
pre [aFF PR RRREXPERIME
AIM: Write an ALP for Hi
SOFTWARE REQUIRE
ALGORITHM:
Step 1: Start
exadecimal DOWN-Counter.
: Keil u vision 5
Step 2: Initialize accumulator sith 00h
Step 3: Call the delay subroutine for delay
Step 4: Increment Accumulator
Step 5: Count till last value
Step 6: Stop
ORG 0008
MOY A, #0FFH
BACK: ACALL DELAY
MOV PI,A
DEC A
JNZ BACK
HERE: SJMP HERE
DELAY:MOV Ri, #0FFH
DELAY1: MOV R2, #0FFH
DELAY2: MOV R3, #0FFH
AGAIN: DJNZ R3, AGAIN
Set program start address to OOOOH —
Initialize accumulator A to FFH (255 decimal)
Call DELAY subroutine to create a time delay
Quiput the current value of A to port PI (¢.g., LEDs)
Deerement the accumulator Abyl
If A is not zero, jump. back to BACK (loop until A reaches 00H) \
Infinite loop to stop program execution
Initialize outer loop counter Ri to 255
Initialize middle loop counter R2 to 255
Initialize inner loop counter R3 to 255
Decrement R3 and loop until R3 = 0
Decrement R2 and loop middle loop
DJNZ R2, DELAY2
DJNZ Ri, DELAY1 Decrement R1 and loop outer loop
RET Return from subroutine to the calling location
END End of the program é
STEPS TO BE FOLLLOWED
ie o| m Oe oh Berea ae oe
ters [fe]. Run ESI wopens |v | Porto
Rl ka ee 5
ster Start code execution F Ha . ee
fe z= ‘ DSFE G Pots
RESULT. i
Parallel Part! Tess aa Pavelte
(Pott A i ‘Pot
} age Bits o |} Z it 9
rr eee | enieor eererece
| Pine: [OxF i }) Fi
mye: OK plete lela i \| Pins: [xO CTP Ke