The document provides assembly language programs to demonstrate data transfer instructions, exchange a block of data, and sort numbers in ascending order using the bubble sort algorithm on the 8051 microcontroller. The programs initialize registers, transfer data between source and destination addresses, decrement a count register in a loop, and exchange data values to sort an array of numbers stored from address 4000h.
The document provides assembly language programs to demonstrate data transfer instructions, exchange a block of data, and sort numbers in ascending order using the bubble sort algorithm on the 8051 microcontroller. The programs initialize registers, transfer data between source and destination addresses, decrement a count register in a loop, and exchange data values to sort an array of numbers stored from address 4000h.
Channasandra, Banga!r"#$%&&%' MICROCONTROLLER LABORATORY MANUAL (&%ESL)*+ ,E-ARTMENT OF ELECTRONICS . COMMUNICATION ENGINEERING /&&0 8051 MANUAL RNSIT 1 MICROCONTROLLERS LAB IVth SEM EC VTU ,RAFT SYLLABUS SUB1ECT2 MICROCONTROLLERS LAB E3AM HOURS2 4 (C!55!n t! EE, EC, IT, TC, BM and ML+ E3AM MAR6S2 $& I. -ROGRAMMING '. ,ata Trans7"r # B!89 5!:", E;8hang", S!rt<ng, F<nd<ng arg"st ""5"nt <n an arra= /. Ar<th5"t<8 Instr>8t<!ns # Add<t<!n?s>@tra8t<!n, 5>t<A<8at<!n and d<:<s<!n, sB>ar", C>@" C ('% @<ts Ar<th5"t<8 !A"rat<!ns C @<t addr"ssa@"+ 4. C!>nt"rs ). B!!"an . L!g<8a Instr>8t<!ns (B<t 5an<A>at<!ns+ $. C!nd<t<!na CALL . RETURN %. C!d" 8!n:"rs<!n2 BC, C ASCIID ASCII C ,"8<5aD ,"8<5a # ASCIID HE3 # ,"8<5a and ,"8<5a # HE3 *. -r!gra5s t! g"n"rat" d"a=, -r!gra5s >s<ng s"r<a A!rt and !n#Ch<A t<5"r ?8!>nt"r II. INTERFACING Er<t" C Ar!gra5s t! <nt"r7a8" 0&$' 8h<A t! <nt"r7a8<ng 5!d>"s t! d":"!A s<ng" 8h<A s!>t<!ns 0. S<5A" Ca8>at!r >s<ng % d<g<t s":"n#s"g5"nt d<sAa= and H"; 6"=@!ard <nt"r7a8" t! 0&$' F. AAhan>5"r<8 LC, Aan" and H"; 9"=Aad <nA>t <nt"r7a8" t! 0&$' '&. E;t"rna A,C and T"5A"rat>r" 8!ntr! <nt"r7a8" t! 0&$' ''. G"n"rat" d<77"r"nt Ga:"7!r5s S<n", SB>ar", Tr<ang>ar, Ra5A "t8. >s<ng ,AC <nt"r7a8" t! 0&$'D 8hang" th" 7r"B>"n8= and a5A<t>d" '/. St"AA"r and ,C 5!t!r 8!ntr! <nt"r7a8" t! 0&$' '4. E":at!r <nt"r7a8" t! 0&$' Introduction 8051 MANUAL RNSIT 2 MICROCONTROLLERS LAB IVth SEM EC -ROCESSOR >s"d <s At5" AT0FC$'E,/ # micro controller that has 64Kbtes o! on" chi# #ro$ram memor% It is a &ersion o! 8051 'ith enhance( !eat)res% * AT 8+,51-.2 o#erates at 11%05+2 M/0 -ROCESSOR FEATURES ON#CHI- MEMORY2 CO,E MEMORY2 64K 1tes o! !lash% ,ATA MEMORY2 256 1tes o! RAM2 13+2 1tes o! 4RAM2 2K 1tes o! --5R6M% ON#CHI- -ERI-HERALS 7 16"bit Timers8,o)nters2*9atch .o$ Timer2*5ro$rammable ,o)nter Arra :5,A; on 5ort1 i%e% 59M an( ,a#t)re < ,om#are2 S5I :Serial 5eri#heral Inter!ace; on 5ort12*=)ll ()#le> enhance( UART% INTERRU-TS Nine so)rces o! interr)#t :both e>ternal an( internal;% T'o ->ternal interr)#ts INT0 an( INT1 are #ro&i(e( 'ith #)sh b)tton s'itches? these can also be )se( as $eneral"#)r#ose s'itches% I?O (-!rt+ L<n"s =o)r 10"#in connectors !or all the 72 I86 lines% 502 51 an( 52 5ort lines are a&ailable on a 26"#in connector2 '%3/ LC, . SERIAL I?O are also a&ailable% Cr"at<ng and 8!5A<<ng a HIV<s<!n/ Ar!J"8t 8051 MANUAL RNSIT 7 MICROCONTROLLERS LAB IVth SEM EC 1% .o)ble ,lic@ on the *Aision7 icon on the (es@to#% 2% ,lose an #re&io)s #roBects that 'ere o#ene( )sin$ C 5roBect"D,lose% 7% Start -r!J"8t C N"G -r!J"8t, an( select the ,5U !rom the (e&ice (atabase :.atabase" Atmel" AT8+,51-.2;% :Select AT8+,51-.2 or AT8+,51R.2 as #er the boar(;%6n clic@in$ E6KF2 the !ollo'in$ o#tion is (is#lae(% ,hoose Ges% 4% ,reate a so)rce !ile :)sin$ =ile"DNe';2 t#e in the assembl or , #ro$ram an( sa&e this :!ilename%asm8 !ilename%c; an( a(( this so)rce !ile to the #roBect )sin$ either one o! the !ollo'in$ t'o metho(s% :i; 5roBect",om#onents2-n&ironmentan( 1oo@s"Da((!iles"D bro'se to the reH)ire( !ile "D 6K I6RJ :ii; ri$ht clic@ on the So)rce Kro)# in the 5roBect 9in(o' an( the Add F<"s t! Gr!>A o#tion% 5% Set the Tar$et o#tions )sin$ "D -r!J"8t C OAt<!ns 7!r Targ"t o#ens the * Aision2 OAt<!ns 7!r Targ"t C Targ"t con!i$)ration (ialo$% Set the 4tal !reH)enc as 11%05+2 Mh02 an( also the OAt<!ns 7!r Targ"t C ,"@>g C >s" "<th"r S<5>at!r ? 6"< M!n<t!r# $' dr<:"r. I! 6"< M!n<t!r# $' dr<:"r <s >s"d 8<89 !n S"tt<ngs "D ,6M 5ort settin$s select the ,6M 5ort to 'hich the boar( is connecte( an( select the ba)( rate as 1+200 or +600 :recommen(e(;% -nable S"r<a Int"rr>At o#tion i! the )ser a##lication is not )sin$ on"chi# UART2 to sto# #ro$ram e>ec)tion% 6% 1)il( the #roBect? )sin$ 5roBect "D 1)il( 5roBect% *Aision translates all the )ser a##lication an( lin@s% An errors in the co(e are in(icate( b C ITar$et not create(J in the 1)il( 'in(o'2 alon$ 'ith the error line% .eb)$ the errors% A!ter an error !ree b)il(2 $oto .eb)$ mo(e 8051 MANUAL RNSIT 4 MICROCONTROLLERS LAB IVth SEM EC 3% No' )ser can enter into ,"@>g mo(e 'ith ,"@>g# Start ? St!A ,"@>g s"ss<!n (ialo$% 6r b clic@in$ in the icon% 8%The #ro$ram is r)n )sin$ the ,"@>g#R>n comman( < halte( )sin$ ,"@>g#St!A R>nn<ng. Also the :reset2 r)n2 halt; icons can be )se(% A((itional icons are :ste#2 ste# o&er2 ste# into2 r)n till c)rsor;% +% I! it is an inter!ace #ro$ram the o)t#)ts can be seen on the L,.2 ,R62 motor2 le( stat)s2 etc% I! it is a #art A #ro$ram2 the a##ro#riate memor 'in(o' is o#ene( )sin$ Aie' "D memor 'in(o' :!or (ata RAM < 4RAM locations;2 9atch 'in(o' :!or timer #ro$ram;2 serial 'in(o'2 etc% NoteL To access (ata RAM area t#e a((ress as .L0020h% Similarl to access the .5TR re$ion :4RAM"#resent on chi# in AT8+,51-.2; sa +000h location t#e in 4L0+000/% 8051 MANUAL RNSIT 5 MICROCONTROLLERS LAB IVth SEM EC '. ,ATA TRANSFER INSTRUCTIONS '+ Er<t" an ass"5@= ang>ag" Ar!gra5 t! trans7"r n K'& @=t"s !7 data 7r!5 !8at<!n 0&4$h t! !8at<!n 0&)'h (G<th!>t !:"raA+. 6RK 0000/ SMM5 70/ 6RK 70/ M6A .5/2N80/ M6A R02N75/ 88so)rce a((ress M6A R12N41/ 88(estination a((ress M6A R72N05/ 88co)nt 1A,KL M6A .5L2 r0 M6A4 A2O(#tr M6A .5L2 R1 M6A4 O(#tr2A IN, R0 IN, R1 .MNP R72 1A,K /-R-L SMM5 /-R- -N. RESULT2 1e!ore ->ec)tionL 10 locations 4L8075h are !ille( )# 'ith (ata% A!ter ->ec)tionL 10 locations 4L8041h are !ille( )# 'ith (ata !rom 8075h% Ag!r<th5 1% Initiali0e re$isters to hol( co)nt (ata < also the so)rce < (estination a((resses% 2% Ket (ata !rom so)rce location into acc)m)lator an( trans!er to the (estination location% 7% .ecrement the co)nt re$ister an( re#eat ste# 2 till co)nt is 0ero% NoteL =or (ata trans!er 'ith o&erla# start trans!errin$ (ata !rom the last location o! so)rce arra to the last location o! the (estination arra% 8051 MANUAL RNSIT 6 MICROCONTROLLERS LAB IVth SEM EC /+ ASSEMBLY LANGUAGE -ROGRAM TO E3CHANGE A BLOC6 OF ,ATA. Er<t" an ass"5@= ang>ag" Ar!gra5 t! ";8hang" n K $ @=t"s !7 data at !8at<!n &&/*h and at !8at<!n &&)'h. 6RK 00/ SMM5 70/ 6RK 70/ M6A R02N23/ 88so)rce a((ress M6A R12N41/ 88(estination a((ress M6A R72N05/ 88co)nt 1A,KL M6A4 A2Or0 M6A r22a M6A4 a2Or1 M6A4 Or02a M6A a2 r2 M6A4 Or12a IN, R0 IN, R1 .MNP R72 1A,K /-R-L SMM5 /-R- -N. A<t"r >s<ng 3CH 8!55and. 6RK 0000/ SMM5 70/ 6RK 70/ M6A R02N23/ 88so)rce a((ress M6A R12N41/ 88(estination a((ress M6A R72N05/ 88co)nt 1A,KL M6A4 A2Or0 M6A r22a M6A4 a2Or1 4,/ a2 r2 M6A4 Or12a 4,/ a2 r2 M6A4 Or02a IN, R0 IN, R1 .MNP R72 1A,K /-R-L SMM5 /-R- -N. RESULT2 1e!ore ->ec)tionL 5 locations at 4L0023h < 4L0041h are !ille( )# 'ith (ata% 8051 MANUAL RNSIT 3 MICROCONTROLLERS LAB IVth SEM EC A!ter ->ec)tionL The (ata at 4L8023h < 4L8041h are e>chan$e(% Ag!r<th5 1% Initiali0e re$isters to hol( co)nt (ata :arra si0e; < also the so)rce < (estination a((resses% 2% Ket (ata !rom so)rce location into acc)m)lator an( sa&e in a re$ister% 7% Ket (ata !rom the (estination location into acc)m)lator% 4% ->chan$e the (ata at the t'o memor locations% 5% .ecrement the co)nt re$ister an( re#eat !rom ste# 2 to 4 till co)nt is 0ero% 4+ ASSEMBLY LANGUAGE -ROGRAM TO SORT NUMBERS. ??BUBBLE SORT -ROGRAM Er<t" an ass"5@= ang>ag" Ar!gra5 t! s!rt an arra= !7 nK % @=t"s !7 data <n as8"nd<ng !rd"r st!r"d 7r!5 !8at<!n 0&4$h.(>s" @>@@" s!rt ag!r<th5+ 6RK 0000/ SMM5 70/ 6RK 70/ M6A R02N05 88co)nt n"1 "ARRAG SIP-"n" 5ass ,o)nter L1L M6A (#tr2 N+000h 88arra store( !rom a((ress +000h M6A A2R0 88initiali0e e>chan$e co)nter M6A R12A L2L M6A4 a2 O(#tr 88K-T NUM1-R =R6M ARRAG M6A 12 A 88< ST6R- IN 1 IN, (#tr M6A4 a2 O(#tr 88ne>t n)mber in the arra ,LR , 88reset borro' !la$ M6A R22 A 88ST6R- IN R2 SU11 A2 1 882n( " 1st no%Qno com#are instr)ction in 8051 M, N6-4,/K 88 MN, " =6R AS,-N.INK 6R.-R M6A A21 88-4/ANK- T/- 2 N6-S IN T/- ARRAG M6A4 O(#tr2a .-, .5L 88.-, (#tr"INSTRU,TI6N N6T 5TR-S-NT M6A a2R2 M6A4 O(#tr2a IN, .5TR N6-4,/KL .MNP R12L2 88(ecrement com#are co)nter .MNP R02L1 88(ecrement #ass co)nter hereL SMM5 here -N. Ag!r<th5 1% Store the elements o! the arra !rom the a((ress +000h 2% Initiali0e a #ass co)nter 'ith arra si0e"1 co)nt :!or n)mber o! #asses;% 8051 MANUAL RNSIT 8 MICROCONTROLLERS LAB IVth SEM EC 7% Loa( com#are co)nter 'ith #ass co)nter contents < initiali0e .5TR to #oint to the start a((ress o! the arra :here +000h;% 4% Store the c)rrent an( the ne>t arra elements #ointe( b .5TR in re$isters 1 an( r2 res#ecti&el% 5% S)btract the ne>t element !rom the c)rrent element% 6% I! the carr !la$ is set :!or ascen(in$ or(er; then e>chan$e the 2 n)mbers in the arra% 3% .ecrement the com#are co)nter an( re#eat thro)$h ste# 4 )ntil the co)nter becomes 0% 8% .ecrement the #ass co)nter an( re#eat thro)$h ste# 7 )ntil the co)nter becomes 0% RESULT 2 1e!ore ->ec)tionLUnsorte( Arra at +000h A!ter ->ec)tionL Sorte( Arra :.escen(in$ or(er; at +000h )+ Er<t" an ass"5@= ang>ag" Ar!gra5 t! 7<nd th" arg"st ""5"nt <n a g<:"n str<ng !7 n K % @=t"s at !8at<!n )&&&h. St!r" th" arg"st ""5"nt at !8at<!n )&%/h. 6RK 0000/ SMM5 70/ 6RK 70/ M6A R72N6 88len$th o! the arra M6A .5TR2N4000/ 88startin$ a((ress o! the arra M6A4 A2O.5TR M6A r12a N-4T1GT-L IN, .5TR M6A4 A2O.5TR ,LR , 88reset borro' !la$ M6A R22A 88ne>t n)mber in the arra SU11 A2R1 886T/-R N)m " 5R-AI6US LARK-ST no% M, s@i# 88 MN, !or smallest element M6A A2r2 88U5.AT- lar$er n)mber in r1 M6A R12A s@i#L.MNP R72N-4T1GT- M6A .5L2 N62/ 88L6,ATI6N 6= T/- R-SULT"4062/ M6A A2R1 88LARK-ST NUM1-R M6A4 O.5TR2A 88ST6R- AT N4062/ 6A-RL SMM5 6A-R -N. Ag!r<th5 1% Store the elements o! the arra !rom the a((ress 4000h 2% Store the len$th o! the arra in r7 an( set it as co)nter% 8051 MANUAL RNSIT + MICROCONTROLLERS LAB IVth SEM EC 7% .5TR is loa(e( 'ith startin$ a((ress o! the arra% 4% Store the !irst n)mber o! the arra in r1 :r1 is assi$ne( to hol( the lar$est n)mber;% 5% Increment .5TR% 6% S)btract the n)mber #ointe( b .5TR !rom the contents o! r1 :to com#are 'hether the ne>t arra element is lar$er than the one in r1;% 3% I! the element #ointe( b .5TR is lar$er then loa( the lar$er n)mber into r1% 8% .ecrement the co)nter an( re#eat ste#s thro)$h 5 )ntil the co)nter becomes 0% +% Store the lar$est n)mber in r1 in a((ress 4062h RESULT2 1e!ore ->ec)tionL A!ter ->ec)tionL Location 4062 has the lar$est element% /. ARITHMETIC INSTRUCTIONS ASSEMBLY LANGUAGE -ROGRAM ILLUSTRATING A,,ITION, SUBTRACTION, MULTI-LICATION AN, ,IVISION . 5; 9rite an AL5 to #er!orm the !ollo'in$L I! >R0"#er!orm ' S &? else i! >R1"#er!orm '"&? else i! >R2"#er!orm 'T&? elsei! >R7"#er!orm '8&2 'here ' < & are ei$ht bit n)mbers% 6RK 0000/ SMM5 70/ 6RK 70/ M6A R02 N40/ M6A4 A2OR0 M6A R12 A 88R1 /AS ,6N.ITI6N 4 IN, R0 M6A4 A2OR0 M6A 12 A 881 /AS 1ST NUM1-R"& IN, R0 M6A4 A2OR0 88A /AS 2N. NUM1-R"' ,MN- R12N002,KSU1 A.. A21 885-R=6RM A..ITI6N M6A 12N00 881 /AS ,ARRG MN, SKI5 M6A 12N01/ SKI5LSMM5 LAST ,KSU1L ,MN- R12N012,KMUL ,LR , 88R-S-T 16RR69 =LAK SU11 A21 M6A 12N00 881 IN.I,AT-S 16RR69 8051 MANUAL RNSIT 10 MICROCONTROLLERS LAB IVth SEM EC MN, SKI51 M6A 12N0==/ 88== IN.I,AT-S N-KATIA- NUM1-R SKI51LSMM5 LAST ,KMULL ,MN- R12N022,K.IA MUL A1 8816 bit #ro()ct in A1 'ith A ha&in$ lo'er bte SMM5 LAST ,K.IAL ,MN- R12N0726T/-R .IA A1 88U)otient in A < remain(er in 1 SMM5 LAST 6T/-RLM6A A2N00 M6A 12N00 LASTL IN, R0 M6A4 OR02A IN, R0 M6A A21 M6A4 OR02A /-R-LSMM5 /-R- -N. Ag!r<th5 1% Store the con(ition > in r1% 2% Loa( the !irst an( secon( n)mbers to A an( 1 re$isters res#ecti&el 7% ,om#are the contents o! r1 an( #er!orm the o#erations a((2 s)b2 etc accor(in$l% 4% Store the res)lt #resent in A an( 1 re$isters to the a##ro#riate memor locations% RESULT2 1e!ore ->ec)tionL A.. SU1
A!ter ->ec)tionL A.. A!ter ->ec)tionL SU1 1e!ore ->ec)tionL MUL A!ter ->ec)tionL MUL ASSEMBLY -ROGRAM ILLUSTRATING SLUARE AN, CUBE O-ERATIONS. 88c)be is an e>am#le o! 16"bit arithmetic o#eration 88(e#en(in$ on !la$ con(ition2 sH)are or c)be is #er!orme( 88 =la$ is a bit in the bit a((ressable RAM2 sa 1 st bit o! location 20h is )se(2 then bit a((ress is 01 6; An ei$ht bit n)mber 4 is store( in e>ternal memor location +000h% 9rite an AL5 to com#)te :i; the sH)are o! the n)mber 4 i! LS1 o! (ata RAM 20h :bit a((ress 01/; is set 8051 MANUAL RNSIT 11 MICROCONTROLLERS LAB IVth SEM EC :ii; the c)be o! the n)mber 4 i! LS1 o! (ata RAM 20h :bit a((ress 01/; is reset% Store o)r res)lt at locations +0012 +0022 +007h% 6RK 0000/ SMM5 70/ 6RK 70/ M6A .5TR2N+000/ M6A4 A2O.5TR 88K-T NUM1-R"4 M6A R02A 88ST6R- IN R0 M6A 12A MUL A1 88SUUAR- IT"4V2 ,LR , 88=6R ST6RINK R-SULT M1 012LAST 88I= 1IT 01 IS S-T T/-N -N.2 -LS- .6 ,U1- 5US/ 1 88ST6R- U55-R 5ART 6= SUUAR- M6A 12A 881"L69-R 5ART 6= 4V2 M6A A2R0 88A"4 MUL A1 884TL69-R 4V2 IN, .5TR M6A4 O.5TR2A 88ST6R- 5ARTIAL R-SULT M6A A21 M6A R22A 88U55-R 5ART 6= 4TL69-R 4V2 IN R2 565 1 88K-T 1A,K U55-R 5ART 6= SUUAR- M6A A2R0 88A"4 MUL A1 884TU55-R 4V2 A.. A2R2 88A.. T6 5ARTIAL R-SULT LASTLIN, .5TR M6A4 O.5TR2A M6A A21 A.., A2N00 88A.. ,ARRG T6 1:=6R SUUAR- R-SULT2 ,R0; IN, .5TR M6A4 O.5TR2A /-R-LSMM5 /-R- -N. RESULT2 ,U1- 6= 56/ IS +14+8 9/I,/ IS ST6R-. AS +82 142 0+ :L69-R 1GT- =IRST; To $et sH)are ma@e the . 1 bit o! (ata memor 20h hi$h2 sa ==2022062etc% The bit a((ress is 01% Similarl bit a((ress 38h corres#on( to .0 bit 0! (ata ram location 2=h% Ag!r<th5 1% Store the ei$ht bit n)mber > in A2 r0 < 1 re$isters% 2% M)lti#l A an( 1 re$isters to obtain the sH)are :sa SU/LSUL; o! the n)mber >% 8051 MANUAL RNSIT 12 MICROCONTROLLERS LAB IVth SEM EC 7% ,hec@ i! bit 01 is set% I! set $o to en( :storin$ the res)lt;2 else (o the c)be o#erations% 4% The hi$h #art o! the sH)are res)lt :SU/; is store( on the stac@% 5% M)lti#l the lo' #art o! the sH)are res)lt :SUL; 'ith > :#artial c)be res)lt;% 6% Store the lo' #art o! the abo&e res)lt at +001h < the hi$h #art in R2% 3% Retrie&e the hi$h #art o! the sH)are res)lt :SU/; store( on the stac@ < m)lti#l 'ith >% 8% A(( the lo' #art o! the abo&e res)lt :SU/T4; 'ith R2 an( store in +002h% +% A(( the hi$h #art :SU/T4; 'ith the res)ltin$ carr an( store in +007% 4. -ROGRAM ILLUSTRATING BIT MANI-ULATIONS 3; T'o ei$ht bit n)mbers NUM1 < NUM2 are store( in e>ternal memor locations 8000h < 80001h res#ecti&el% 9rite an AL5 to com#are the 2 nos% Re!lect o)r res)lt asL i! NUMIWNUM22 S-T LS1 o! (ata RAM 2= :bit a((ress 38/; I= NUM1DNUM22 S-T MS1 6= 2=:3=/;% i! NUM1 R NUM2",lear both LS1 < MS1 o! bit a((ressable memor location 2=h 6RK 0000/ SMM5 70/ 6RK 70/ M6A .5TR2N8000/ M6A4 A2O.5TR M6A R02A IN, .5TR M6A4 A2O.5TR ,LR , SU11 A2R0 MP -UUAL MN, 1IK S-T1 38/ SMM5 -N.1 1IKLS-T1 3=/ SMM5 -N.1 -UUALL,LR 33/ ,LR 3=/ -N.1LSMM5 -N.1 -N. Ag!r<th52 1% Store the elements o! the arra !rom the a((ress 4000h 2% Mo&e the !irst n)mber in r0 an( the secon( n)mber in re$ister A res#ecti&el 7% ,lear carr !la$ an( s)btract the t'o n)mbers2 i! the carr !la$ is 0:i! the nos are eH)al;2 ,lear both LS1 < MS1 o! bit a((ressable memor location 2=h 4% I! the carr bit is set then Set MS1 o! 2=:3=/;2 else LS1 o! (ata RAM 2= :bit a((ress 38/;% RESULT L 1; 1e!ore ->ec)tionL 4L08000h R 45 < 4L8001 R 75 A!ter ->ec)t)ionL .L02=/ R01 2; 1e!ore ->ec)tionL 4L08000h R 25 < 4L8001 R 75 A!ter ->ec)t)ionL .L02=/ R80 7; 1e!ore ->ec)tionL 4L08000h R 45 < 4L8001 R 45 8051 MANUAL RNSIT 17 MICROCONTROLLERS LAB IVth SEM EC A!ter ->ec)t)ionL .L02=/ R00 ). LOGICAL INSTRUCTIONS 0+ ASSEMBLY -ROGRAM ILLUSTRATING LOGICAL INSTRUCTIONS (BYTE LEVEL+ 7 ei$ht bit n)mbers 42 NUM1 < NUM2 are store( in internal (ata RAM locations 20h2 21h < 22/ res#ecti&el% 9rite an AL5 to com#)te the !ollo'in$% I= 4R0? T/-N NUM1 :AN.; NUM22 I= 4R1? T/-N NUM1 :6R; NUM22 I= 4R2? T/-N NUM1 :46R; NUM22 -LS- R-S R002 R-S IS 27/ L6,ATI6N 6RK 0000/ SMM5 70/ 6RK 70/ M6A A2 20h 88(onot )se N2 as (ata ram 20h is to be accesse( M6A R12A 884 IN R1 M6A A221/ 88A "NUM1 ,MN- R12N02,K6R ANL A2 22/ SMM5 -N.1 ,K6RL,MN- R12N012,K46R 6RL A2 22/ SMM5 -N.1 ,K46RL,MN- R12N0226T/-R 4RL A2 22/ SMM5 -N.1 6T/-RL ,LR A -N.1L M6A 27/2A 88ST6R- R-SULT /-R-L SMM5 /-R- -N. Ag!r<th52 1% 5oint to the (ata RAM re$ister 20h an( store the con(ition >% 2% 5oint to 21h an( 22h an( mo&e the !irst n)mber to A re$ister% 7% ,om#are the contents o! r1 an( #er!orm the o#erations accor(in$l% 4% The res)lt 'ill be store( in 27/ re$ister% RESULT2 1;1e!ore ->ec)tionL .L020/ R002 21R0!2 22 R 12 A!ter ->ec)tion .L027/ R 02 2;1e!ore ->ec)tionL .L020/ R012 21R0!2 22 R 12 A!ter ->ec)tion .L027/ R 1= 7;1e!ore ->ec)tionL .L020/ R022 21R0!2 22 R 12 A!ter ->ec)tion .L027/ R 1. 4;1e!ore ->ec)tionL .L020/ R742 21R0!2 22 R 12 A!ter ->ec)tion .L027/ R 00 The abo&e #ro$ram can also be 'ritten as sho'n belo' :)sin$ in(irect a((ressin$; 6RK 0000/ SMM5 70/ 6RK 70/ mo& r02N20h M6A A2OR0 886N ,/I5 .ATA RAM".6N6T US- M6A4 8051 MANUAL RNSIT 14 MICROCONTROLLERS LAB IVth SEM EC M6A R12A 884 IN R1 IN, R0 M6A A2OR0 88A "NUM1 IN, R0 88 R0 56INTS T6 NUM2 ,MN- R12N02,K6R ANL A2 OR0 SMM5 -N.1 ,K6RL,MN- R12N012,K46R 6RL A2 OR0 SMM5 -N.1 ,K46RL,MN- R12N0226T/-R 4RL A2 OR0 SMM5 -N.1 6T/-RL ,LR A -N.1LIN, R0 M6A OR02A 88ST6R- R-SULT /-R-LSMM5 /-R- -N. B!!"an :ar<a@" instr)ctions are also calle( as @<t ":" !g<8a instr)ctions +; 7 ei$ht bit n)mbers 42 NUM1 < NUM2 are store( in internal (ata RAM locations 20h2 21h < 22/ res#ecti&el% 9rite an AL5 to com#)te the !ollo'in$% I= 4R0? T/-N LS1 6= NUM1 :AN.; LS1 6= NUM22 I= 4R1? T/-N MS1 6= NUM1 :6R;MS1 6= NUM2 2 I= 4R2? T/-N ,6M5L-M-NT MS1 6= NUM1 ST6R- T/- 1IT R-SULT IN R-S2 9/-R- R-S IS MS1 6= 27/ L6,ATI6N 6RK 00/ SMM5 70h 6RK 70h M6A R0220/ 88R0"4 ,MN- R02N02,K1 M6A ,208/ 88LS1 6= NUM1 :21/; " 1IT A..R-SS "08 ANL ,210/ 88LS1 6= NUM2 :22/; " 1IT A..R-SS "10 SMM5 LAST ,K1L,MN- R02N12,K2 M6A ,20=/ 88MS1 6= NUM1 :21/; " 1IT A..R-SS "0= ANL ,213/ 88MS1 6= NUM2 :22/; " 1IT A..R-SS "13 SMM5 LAST ,K2L,MN- R02N22,K7 ,5L 0=/ M6A ,20=/ 88MS1 6= NUM1 :21/; " 1IT A..R-SS "0= SMM5 LAST ,K7L,LR , LASTLM6A 1=/2, 88R-S IS MS1 6= 27/ L6,ATI6N "1=/ /-R-LSMM5 /-R- -N. RESULT L 20h R 00 RD AN. 6= LS1sR1 :hence 80 in 27h location; 8051 MANUAL RNSIT 15 MICROCONTROLLERS LAB IVth SEM EC 20h R 01 RD 6R o! MS1s R 0 :hence 00 in 27h location; 20h R 01 RDcom#lement o! MS1 o! 21h location% /ence 21h is chan$e( to A1 an( 27h location has 80h 1e!ore ->ec)tion A!ter ->ec)tion
Ag!r<th5 L 1% Mo&e the con(ition 4 :!rom 20h location; into R0 re$ister% 2% I! 4R0? then mo&e LS1 bit o! 21h to carr !la$ an( EAN.F ,arr !la$ 'ith LS1 bit o! 22h% Koto ste#5 7% I! 4R1? then mo&e MS1 bit o! 21h to carr !la$ an( E6RF ,arr !la$ 'ith MS1 bit o! 22h% Koto ste#5 4% I! 4R0? then com#lement MS1 bit o! 21h an( mo&e it to carr !la$% Koto ste#5 5% Store ,arr !la$ at MS1 bit o! 27h location% $. COUNTERS ASSEMBLY -ROGRAM ILLUSTRATING HE3 U-?,OEN COUNTERS. 88co)nter #ro$ram " he>8binar co)nters 10; 9rite an AL5 to im#lement :(is#la; an ei$ht bit )#8(o'n binar :he>; co)nters on 'atch 'in(o'% N!t"2 to r)n this #ro$ram2 a!ter selectin$ .-1UK session in the main men) )se V<"G#M Eat8h. 8a Sta89 G<nd!G2 in the 9atches select 'atch 1:or 2; an( #ress =2 an( enter a :!or acc)m)lator A; 6RK 0/ SMM5 70/ 6RK 0/ M6A a2N00 1A,KL A,ALL .-LAG IN, a 88d"8 a !or binar (o'n co)nter MNP 1A,K /-R-LSMM5 /-R- .-LAGL M6A r12N0==/ .-,R1LM6A r22N0==/ .-,RL M6A r72N6==/ .MNP r72X .MNP r22.-,R .MNP r12.-,R1 R-T -N. 8051 MANUAL RNSIT 16 MICROCONTROLLERS LAB IVth SEM EC RESULT2 Acc)m)lator A is incremente( in binar !rom 002 01202Y0+20A2 012 Y20=2102112Y== Ag!r<th52 1% Mo&e 00 to A re$ister 2% ,all the (ela s)bro)tine !or 1 secon(2 in (ela #ro$ram mo&e ==/ to re$isters r12 r2 an( r72 loo# an( (ecrement )ntil 0% 7% Increment A re$ister:(ecremant !or (o'n co)nter; ASSEMBLY -ROGRAM ILLUSTRATING BC, U-?,OEN COUNTERS. 88co)nter #ro$ram C 1,. )#8(o'n co)nters 11; 9rite an AL5 to im#lement :(is#la; an ei$ht bit )#8(o'n 1,. co)nters on 'atch 'in(o'% 6RK 0/ SMM5 70/ 6RK 70/ M6A a2N00 1A,KLA,ALL .-LAG A.. a2N++/ 88A.. 01 !or 1,. )# co)nter ,A A 88!or bc( co)nter MNP 1A,K /-R-LSMM5 /-R- .-LAGLM6A r12N0==/ .-,R1LM6A r22N0==/ .-,RLM6A r72 N0==/ .MNP r72X .MNP r22 .-,R .MNP r12 .-,R1 R-T -N. Ag!r<th52 4% Mo&e 00 to A re$ister 5% ,all the (ela s)bro)tine !or 1 secon( :in (ela #ro$ram mo&e ==/ to re$isters r12 r2 an( r72 loo# an( (ecrement )ntil 0;% 6% Increment A re$ister:a(( ++h !or (o'n co)nter; 3% .ecimal a(B)st acc)m)lator !or the 1,. )#8(o'n co)nter% RESULT2 Acc)m)lator A is incremente( in 1,. !rom 002 012 02Y0+2 102 112Y++% 8051 MANUAL RNSIT 13 MICROCONTROLLERS LAB IVth SEM EC %. SERIAL ,ATA TRANSMISSION 5ro$ram ill)stratin$ serial ascii (ata transmission :(ata"-; N!t""to )se res)lt o! this #ro$ram2 a!ter selectin$ .-1UK session in the main men) )se V<"G#M s"r<a G<nd!G N'% 6n r)nnin$ < haltin$ the #ro$ram2 the (ata is seen in the serial 'in(o'% 12; ,on()ct an e>#eriment to con!i$)re 8051 microcontroller to transmit characters :-; to a 5, )sin$ the serial #ort an( (is#la on the serial 'in(o'% 6RK 0/ SMM5 70/ 6RK 70/ M6A TM6.2N20/ 88timer 1? mo(e 2 M6A T/12N"7 88"7R=. loa(e( into T/1 !or +600 ba)(2 11%05+2M/0% M6A S,6N2N50/ 888"bit2 1 sto# bit2 R-N enable( S-T1 TR1 88Start timer 1 AKAINLM6A A2NFF 88trans!er IJ A,ALL TRANS M6A a2NF-F 88trans!er I-J A,ALL TRANS AKAIN1LSMM5 AKAIN1 TRANSL M6A S1U=2a 88loa( S1U= /-R-LMN1 TI2/-R- 889ait !or last bit to trans!er ,LR TI 88$et rea( !or ne>t bte R-T -N. RESULT2 - is #rinte( on the serial 'in(o' each time the #ro$ram is e>ec)te(% Th"!r=2 In serial transmission as o##ose( to #arallel transmission2 one bit at a time is transmitte(% In serial asnchrono)s transmission2 the (ata consists o! a Start bit :hi$h;2 !ollo'e( b 8 bits o! (ata to be transmitte( an( !inall the sto# bit% The bte character to be transmitte( is 'ritten into the S1U= re$ister% It transmits the start bit% The 8"bit character is trans!erre( one bit at a time% The sto# bit is trans!erre(% A!ter the transmission2 the TI !la$ R 1 in(icatin$ the com#letion o! transmission% /ence in the s)bro)tine 'ait )ntil TI is set% Later clear the TI !la$ an( contin)e 'ith transmission o! the ne>t bte b 'ritin$ into the S1U= re$ister% :The #ro$ram can also be 'ritten in interr)#t mo(e;% The s#ee( o! the serial transmission is set b the ba)( rate 'hich is (one 'ith the hel# o! timer 1% :Re!er Aala;% Timer1 m)st be #ro$ramme( in mo(e 2 :that is2 8"bit2 a)to reloa(;% 1a)( rate ,alc)lationL ,rstal !reH8 :12T72; R :11%05+2M/0;8:12T72; R 28800% Serial comm)nication circ)itr (i&i(es the machine ccle !reH)enc:11%05+2M/0;8:12; b 72 be!ore it is bein$ )se( b the timer to set the ba)( rate% To $et +6002 2880087 is obtaine( b loa(in$ timer1 'ith "7 :i%e%2 == C 7 R =.; !or !)rther cloc@ (i&ision% =or 2400 ba)( rate2 28800812 RD "12 R =4 in T/1% Ag!r<th52 1% Initiali0e timer 1 to o#erate in mo(e 2 b loa(in$ TM6. re$ister% 2% loa( T/1 'ith "7 to obtain +600 ba)(% 7% Initiali0e the asnchrono)s serial comm)nication transmission :S,6N; re$ister% 4% Start timer1 to $enerate the ba)( rate cloc@% 8051 MANUAL RNSIT 18 MICROCONTROLLERS LAB IVth SEM EC 5% Transmit the characters IJ < I-J b 'ritin$ into the S1U= re$ister an( 'aitin$ !or the TI !la$% *+ TIMER ,ELAY -ROGRAM 5ro$ram ill)stratin$ timer (ela 17; Kenerate a 1secon( (ela contin)o)sl )sin$ the on chi# timer in interr)#t mo(e% 6RK 0/ 88Reset Aector SMM5 70/ 6RK 01/ 88T=0 &ector SMM5 ISR 6RK 70/ M6A a2N00 M6A R02N0 M6A R12N0 M6A TM6.2N02/ 8800000010"R)n timer0 in mo(e 2 M6A T/02N118 88Set )# timer 0 to o&er!lo' in 0%05msec M6A I-2N82/ 88Z10000010 C -nable timer0 interr)#t S-T1 T,6N%4 88Start the timer0 /-R-LSMM5 /-R- ISRL ,LR T,6N%4 88.isable timer0 IN, r1 88r1Tr2 R 100T200 R 20000 T 0%05msec R 1sec ,MN- r12N1002SKI5 M6A r12N00 IN, r0 ,MN- r02N2002SKI5 M6A r02N00/ IN, a SKI5L S-T1 T,6N%4 88-nable Timer R-TI 88Ret)rn !rom interr)#t s)bro)tine -N. RESULT2 Acc)m)lator A is incremente( in binar !rom 002 01202Y0+20A2 012 Y2 0=2 102 112 Y== e&er 1 secon( :!or 77M/0 cloc@ settin$ < e&er 7 secon(s !or 11%05+8M/0; Al$orithmL 1% Set )# timer0 in mo(e 2 o#eration 2% Loa( T/1 'ith 118 to $enerate an interr)#t e&er 0%05msec% 7% Reset re$isters a2 r1 < r0% 4% Re#eat ste# 4 contin)o)sl 5% 6n interr)#t? ISR at 0001 loaction $oes to ste# 6 6% (isable timer0 3% U#(ate r1 < r0 8% ,hec@ i! 20000 interr)#ts :R1 sec; o&er% Ges Cincrement acc)m)lator a% +% enable timer < ret)rn !rom ISR% 8051 MANUAL RNSIT 1+ MICROCONTROLLERS LAB IVth SEM EC Timer(ela R 12T:253"(ela;8!reH)enc Timer(elaR0%05msec .elaR256"::timer(ela T !reH)enc;812; R256":0%05T10 "7 T 77T10 6 ;812 R256"173%5 R118%5 88loa(e( in T/0 To $et 1sec (ela 180%05msec R 200T100 in the ISR :ass)min$ 77 M/P crstal !reH)enc% =or 11 M/02 the calc)lations chan$e;% 8051 MANUAL RNSIT 20 MICROCONTROLLERS LAB IVth SEM EC 0. CONVERSION -ROGRAMS ')+ Er<t" an AL- t! <5A"5"nt d"8<5a t! h"; 8!n:"rs<!n 6RK 0000/ SMM5 70h 6RK 70h M6A .5TR2N40/ 882"(i$it (ecimal n)mber to be con&erte( is $i&en in (ata memor 40h M6A4 A2 O.5TR ANL A2 N0=0/ 88obtain )##er (ecimal (i$it S9A5 A 88brin$ to the )nits #lace M6A 12N0A/ 88MULTI5LG tens (i$it 'ith N0A"to$et tens in he> MUL A1 M6A r12a 88tem#oraril store the con&erte( tens &al)e M6A4 A2O.5TR 88$et the (ecimal n)mber a$ain ANL A2N0=/ 88obtain the )nits (i$it A.. A2R1 88a(( to the con&erte( tens &al)e IN, .5TR 88increment (ata a((ress M6A4 O.5TR2A 88con&erte( he>a(ecimal n)mber in ne>t location /-R-LSMM5 /-R- -N. RESULT L be!ore e>ec)tion" 4L0040/ R 45 :.ecimal81,.; A!ter ->ec)tionL 4L0041h R 2. :he> &al)e; Ag!r<th5 1% Mo&e the (ecimal (ata to be con&erte( !rom e>ternal memor 40h to acc)m)lator% 2% AN. A re$ 'ith 0!0h an( obtain the )##er MS1 o! the (ecimal (i$it an( s'a# the LS1 an( MS1 o! acc)m)lator to brin$ the same to )nits #lace% 7% Mo&e 0ah to 1 re$ister an( m)lti#l 'ith A re$ to con&ert to he> &al)e2 store the con&erte( tens &al)e in r1 4% Ket the LS1 o! the (ecimal n)mber an( a(( to the con&erte( tens &al)e 5% #oint to the ne>t memor location an( store the res)lt :he>a(ecimal;% '$+ Er<t" an AL- t! <5A"5"nt h"; t! d"8<5a 8!n:"rs<!n 6RK 0000/ SMM5 70h 6RK 70h M6A .5TR2N+000/ M6A4 A2O.5TR 88Ket he> n)mber M6A 12N10 .IA A1 88(i&i(e b 10 :0A/; IN, .5TR 4,/ A21 M6A4 O.5TR2A 88Store the remain(er :in 1; In )nits #lace 4,/ A21 M6A 12N10 88.i&i(e the H)otient in A b 10 .IA A1 IN, .5TR 8051 MANUAL RNSIT 21 MICROCONTROLLERS LAB IVth SEM EC 4,/ A21 M6A4 O.5TR2A 88Store the remain(er :in 1; In tens #lace 4,/ A21 IN, .5TR M6A4 O.5TR2A 88Store the H)otient :in A; in h)n(re(s #lace /-R-LSMM5 /-R- -n( RESULT L +000/ C == :/-4 NUM1-R; +001 to +007 C )n#ac@e( 1,. n)mber :(ecimal;" 52522 :i%e%2 255 store( Lo'er (i$it !irst; Ag!r<th5 1% Mo&e the he> (ata to be con&erte( to acc)m)lator% 2% Mo&e 10 to 1 re$ister an( (i&i(e 'ith A re$ to con&ert to ascii &al)e 7% Store the con&erte( LS1 &al)e in r3 4% Re#eat the ste# 2 to obtain the con&erte( MS1 &al)e 5% Store the same in r6 16; 9rite an AL5 to im#lement 1,. to AS,II con&ersion 6RK 0000/ SMM5 70h 6RK 70h M6A R12N50/ M6A A2OR1 88$et 1,. (ata bte !rom RAM location 50h M6A R22A 88Store in R2 ANL A2N0=/ 88Ket the lo'er nibble 6RL A2N70/ 88A((8or 'ith 70h i%e%2 0"+ con&erte( to 70"7+h IN, R1 M6A OR12A 88Store the lo'er (i$it[s AS,II co(e M6A A2R2 88Ket bac@ the n)mber S9A5 A 88S'a# nibbles in A ANL A2N0=/ 88Ket the )##er 1,. (i$it 6RL A2N70/ 88,on&ert to AS,II IN, R1 M6A OR12A 88Store the )##er (i$it[s AS,II co(e hereL sBm# here -N. RESULT L The 1,. co(e 28 at .L0050h is con&erte( to 2 AS,II co(es"78h 72h Ag!r<th5 L 8051 MANUAL RNSIT 22 MICROCONTROLLERS LAB IVth SEM EC 88,on&erts the 1,. bte in A into t'o AS,II characters% 1% Mo&e the 1,. (ata to be con&erte( to acc)m)lator% 2% Ket the lo'er nibble:1,. (i$it; < A.. :or 6RL; 'ith 70h 7% Store the con&erte( AS,II &al)e 4% Ket the hi$her nibble:tens 1,. (i$it; < A.. :or 6RL; 'ith 70h 5% Store the con&erte( AS,II &al)e '*+ Er<t" an AL- t! <5A"5"nt h";ad"8<5a t! ASCII 8!n:"rs<!n ??Th<s Ar!gra5 as! <>strat"s 8!nd<t<!na @ran8h<ng (1NC+, 8a and r"t>rn <nstr>8t<!ns. 6RK 0000/ SMM5 70h 6RK 70h M6A R12N50/ M6A A2OR1 88$et he>a(ecimal (ata bte !rom RAM location 50h M6A R22A 88Store in R2 ANL A2N0=/ 88Ket the lo'er nibble A,ALL AS,II 88,on&ert to AS,II IN, R1 M6A OR12A 88Store the lo'er (i$it[s AS,II co(e M6A A2R2 88Ket bac@ the n)mber S9A5 A 88S'a# nibbles in A ANL A2N0=/ 88Ket the )##er 1,. (i$it A,ALL AS,II IN, R1 M6A OR12A 88Store the )##er (i$it[s AS,II co(e hereL sBm# here AS,IILM6A R42A 88Store a ,LR , SU11 A2N0A/ 88,hec@ i! (i$it DR0A M6A A2R4 MN, SKI5 A.. A2N03/ 88A(( 03 i! D0+ SKI5LA.. A2N70/ 88-lse a(( onl 70h !or 0"+ R-T -N. RESULT L The 1,. co(e 2, at .L0050h is con&erte( to 2 AS,II co(es"47h:!or 01; < 72h :!or 02; Another ->am#le"1A
Ag!r<th5 L 88,on&erts the he>a(ecimal bte in A into t'o AS,II characters% 1% Mo&e the he>a(ecimal (ata to be con&erte( to acc)m)lator% 2% Ket the lo'er nibble < call AS,II ro)tine 7% Store the con&erte( AS,II &al)e 8051 MANUAL RNSIT 27 8 0 5 P0 1 P1
HC CRO Xout
MICROCONTROLLERS LAB IVth SEM EC 4% Ket the hi$her nibble < call AS,II ro)tine 5% Store the con&erte( AS,II &al)e AS,II s)bro)tine 1% I! (i$it $reater than 0+2:!or A"=; a(( 03h < 70h 2% -lse :i%e%2 !or 0"+; a(( onl 70h 7% ret)rn '0+ Er<t" an AL- t! <5A"5"nt ASCII t! h";ad"8<5a 8!n:"rs<!n 6RK 0000/ SMM5 70h 6RK 70h M6A R12N50/ M6A A2OR1 88$et ascii bte !rom RAM location 50h ,LR , SU11 A2N41/ M6A A2OR1 M, SKI5 ,LR , SU11 A2N03/ SKI5L,LR , SU11 A2N70/ IN, R1 M6A OR12A 88Store the he> co(e hereL sBm# here -N. RESULT L The AS,II co(e 45 at .L0050h is con&erte( to he>a(ecimal "0- at 51h N!t"2 =or this #ro$ram the in#)t (ata sho)l( be onl in the ran$e 4&h#4Fh < )'h t! )%h% Ag!r<th5 L 88,on&erts the AS,II characters into he>a(ecimal n)mber% 1% Mo&e the AS,II character to be con&erte( to acc)m)lator% 2% I! character is $reater than 41h2:!or A"=;2 then s)btract 03h < 70h 7% -lse :i%e%2 !or 0"+; s)btract onl 70h 4% Store the con&erte( he>a(ecimal n)mber% 8051 MANUAL RNSIT 24 8 0 5 P0 1 P1
HC CRO Xout
MICROCONTROLLERS LAB IVth SEM EC INTERFACING -ROGRAMS HardGar" Int"r7a8<ng 1%9a&e!orm Keneration )sin$ .)al .A, 2%Ste##er Motor inter!ace% 7%444 he>a(ecimal Keboar( inter!ace 4%., motor inter!ace 5%,alc)lator )sin$ Keboar( an( Se&en se$ment (is#la% 6%-le&ator control% 3%Tem#erat)re sensor% F"at>r"s !7 E5@"dd"d C \ , is a sim#le #ro$rammin$ lan$)a$e an( so &er eas to co(e% \ -mbe((e( , has most !eat)res o! ,"lan$)a$e 'ith more stress on certain bit mani#)lati&e instr)ctions% \ This !eat)re ma@es it eas to 'rite #ro$ram !or ], an( ]5% \ Keil is a &ersatile so!t'are 'ith a cross com#iler that 'ill con&ert the , #ro$ram to assembl lan$)a$e an( th)s the #ro$ram can be e>ec)te( on the (esire( tar$et :sa 8051;% Some o! the bit mani#)lati&e instr)ctions )se( are Smbol 6#eration < 1it'ise AN. ^ 1it'ise 6R _ 1it'ise N6T DD Shi!t Ri$ht WW Shi!t Le!t V 50%0 '.,>a ,a8 Int"r7a8" t! g"n"rat" a%SH)are 'a&e!orm b%Trian$)lar 9a&e!orm c%Ram# 'a&e!orm (%Sine 'a&e!orm 8051 MANUAL RNSIT 25 8 0 5 P0 1 P1
HC Dual DAC CRO Xout
MICROCONTROLLERS LAB IVth SEM EC '.aAg!r<th5 7!r SB>ar" Ga:" g"n"rat<!n Let initial2 am#lit)(e o! the sH)are 'a&e be 2%5&:3=; an( !reH)enc co)nt 100% 6)t#)t the &al)es 00h:0!!; an( 3!h:on; Aal)es thro)$h 50% I! am#lit)(e @e is #resse( then increase the &olta$e in ste#s o! 0%15&:8;% I! the !reH)enc @e is #resse( increment the co)nt in ste#s o! 50% I! the co)nt e>cee(s 1000 reset it bac@ to 100% -&er time am#lit)(e an( !reH)enc chan$es o)t#)t the &al)e thro 50 an( note the 'a&e!orm on ,R6% -r!gra5 7!r sB>ar" Ga:" Nincl)(e WR-K51>.2%/D sbit Am# R 57V7? 8T 5ort line to chan$e am#lit)(e T8 sbit =re R 57V2? 8T 5ort line to chan$e !reH)enc T8 &oi( (ela:)nsi$ne( int >; 8T (ela ro)tine T8 ` !or:?>D0?>"";? a main:; ` )nsi$ne( char on R 0>3!2o!!R0>00? )nsi$ne( int !re R 100? 'hile:1; ` i!:bAm#; 8T i! )ser choice is to chan$e am#lit)(e T8 ` 'hile:bAm#;? 8T 'ait !or @e release T8 onSR0>08? 8T Increase the am#lit)(e T8 a i!:b=re; 8T i! )ser choice is to chan$e !reH)enc T8 ` i!:!re D 1000; 8T i! !reH)enc e>cee(s 1000 reset to (e!a)lt T8 !re R 100?
'hile:b=re;? 8T 'ait !or @e release T8 !re SR 50? a 8T Increase the !reH)enc T8 50Ron? 8T 'rite am#lit)(e to #ort T8 51Ron? (ela:!re;? 50 R o!!? 8T clear #ort T8 51 R o!!? (ela:!re;? aa '.@Ag!r<th5 7!r Tr<ang>ar Ga:" g"n"rat<!n 6)t#)t the initial &al)e 00 thro)$h 50% 8051 MANUAL RNSIT 26 MICROCONTROLLERS LAB IVth SEM EC Increment it in ste#s o! 1 )ntil a co)nt &al)e o! ==h :5A; is reache(% -&er time re#eat ste# 1% .ecrement it in ste#s o! 1 )ntil a 0ero &al)e is reache( an( re#eat ste# 1% -r!gra5 7!r tr<ang>ar Ga:"2 Nincl)(e WR-K51>.2%/D main:; ` )nsi$ne( char iR0? 50 R 0>00? 8T 50 as 6)t#)t #ort T8 'hile:1; ` !or:iR0?iW0>!!?iSS;` 8T Kenerate 6N #)lse T8 51 R i? 50 R i? a !or:iR0>!e?iD0>00?i""; 8T Kenerate 6== #)lse T8 `50 R i? 51 R i?a a a '.8.Ag!r<th5 7!r Ra5A Ga:" g"n"rat<!n 6)t#)t the initial &al)e 00 thro)$h 50% Increment it in ste#s o! 1 )ntil a co)nt &al)e o! ==h :5A; is reache(% -&er time re#eat ste# 1% Re#eat ste# 1 < 2 contin)o)sl% -r!gra5 7!r Ra5A Ga:"7!r5 Nincl)(e WR-K51>.2%/D main :; ` Unsi$ne( char iR0? 50 R 0>00? 8T 50 as 6)t#)t #ort T8 'hile :1; ` !or :iR0?iW0>!!?iSS; 8T Kenerate 6N #)lse T8 ` 51 R i? 50 R i? a a a 'd.Ag!r!th5 7!r S<n" Ga:" ,om#)te (i!!erent ste# &al)es :c R 20 o 215 o Y; o! sine )sin$ the eH)ation AR 2%5A S2%5Asinc% % 6)t#)t the &al)es thro 50% More the ste#s smoother 'ill be sine 'a&e% 8051 MANUAL RNSIT 5S 23 MICROCONTROLLERS LAB IVth SEM EC -%$%L c R 0 o
AR 2%5A S2%5Asinc R 2%5A The &al)e sent to .A, is 25%645AR 128% -r!gra5 7!r s<n" Ga:" Nincl)(e WR-K51>.2%/D main:; ` static int ad17eR`12821+222782255227821+22128264213202132642128a? )nsi$ne( char iR0? 50 R 0>00? 8T 50 as 6)t#)t #ort T8 'hile :1; ` !or:iR0?iW17?iSS; 8T 6)t#)t (i!!erent &al)es T8 ` 50 R adie? a a a /.St"AA"r M!t!r \ Ste##er motor )nli@e ., motor rotates in ste#s% \ Ste##er motor has 4 coils 'hich !orms the stator an( a central rotor% \ Rotation (e#en(s on e>citation o! stator coils% ste# coil A coil 1 coil , coil . 1 0 0 0 1 2 1 0 0 0 7 0 1 0 0 4 0 0 0 1 Anone o! these &al)es !orms the initial &al)e% To $et 760o re&ol)tion 200 ste#s are reH)ire(% Ste# an$leR 760 o 8200 R 1%8 o % :(i!!erence bet'een 2 teeth;% Ag!r<th5 7!r St"AA"r M!t!r \ ,on!i$)re 50 as o)t#)t% \ A##l the initial e>citation o! 11 to motor coils thro)$h 50% \ =or cloc@'ise motion "Rotate ri$ht once the e>citation an( re#eat ste# 2% \ =or anticloc@'ise motion "Rotate le!t once the e>citation an( re#eat ste# 2% 8051 MANUAL RNSIT 5S 5S 28 MICROCONTROLLERS LAB IVth SEM EC 88-r!gra5 7!r st"AA"r 5!t!r <nt"r7a8" Nincl)(e WR-K51>.2%/D &oi( (ela :)nsi$ne( int >; 8T .ela Ro)tine T8 ` !or:?>D0?>"";? ret)rn? a Main : ; ` )nsi$ne( char Aal2 i? 50R0>00? Aal R 0>11? !or :iR0?iW4?iSS; ` 50 R Aal? Aal R AalWW1? 8T AalR AalDD1? !or cloc@'ise (irectionT8 (ela :500;? a a 4. )3) HE3 6"=@!ard Ag!r<th5 7!r 6"=@!ard Int"r7a8" \ ,on!i$)re 51 as o)t#)t #ort to scan the ro's an( 50 as in#)t #ort to rea( the col)mn &al)es% \ =irst select the last ro' b $ro)n(in$ that ro'% Scan the col)mns o! entire ro' i! a @e is #resse( in that ro' then one o! the col)mn rea(s E0F% \ I! no' @e is #resse( in the selecte( ro' all 1Fs is ret)rne(% So scan ne>t ro'% Re#eat the action )ntil all ro's are scanne(% 8051 MANUAL RNSIT 8051f, 50 =R, 26#in ,able 5S Ste##er Motor Inter!ace ,ar( 5S Ste##er Motor 5S 2+ MICROCONTROLLERS LAB IVth SEM EC 885ro$ram !or 444 he> @e#a(% Nincl)(e W R-K51>.2%/D Nincl)(e Wintrins%hD Nincl)(e glc(%hg )nsi$ne( char ro's2col)mns2res)lt2abhi? )nsi$ne( char tem# R 0? &oi( (ela:; ` )nsi$ne( int i? !or:i R 0? i WR 20000? i SS;? a &oi( .is#la:; ` i!:res)lt D 0>0+; ` res)lt SR 0>73? 9rite,har:res)lt;? a else ` res)lt SR 0>70? 9rite,har:res)lt;? a a &oi( KeScan:; ` a$ainL col)mns R 0>33? ro's R 0>04? res)lt R 0>0c? ne>tL 51 R col)mns? col)mns DDR1? i!:,G;
col)mns R col)mns ^0>08 ? tem# R 50? tem# R :tem# < 0>0!;? 8051 MANUAL RNSIT 8051f, 50 =R, 26#in ,able 5S 444 Keboar( Inter!ace ,ar( 5S 444 /e> @e#a( 5S 70 MICROCONTROLLERS LAB IVth SEM EC i!:tem# bR 0>0!; ` rotL tem# DDR 1? i!:b,G; ` ,lrLc(:;? ret)rn? a else ` res)lt SR 1? $oto rot? a a else ` res)lt "R 0>04? ro's ""? i!:ro's RR 0; $oto a$ain? else ` $oto ne>t? a a a &oi( main:; ` 50 R 0>!!? 51 R 0>00? InitLc(:;? 9riteStrin$ :gK-G 5R-SS-.Rg;? 'hile:1; ` KeScan:;? 9riteStrin$ :gK-G 5R-SS-.Rg;? .is#la:;? a a ).,C M!t!r Ag!r<th5 7!r ,C 5!t!r <nt"r7a8" \ ,on!i$)re 50251 as o)t#)t #ort an( 57 as in#)t #ort% \ Let initiall the motor rotate 'ith hal! s#ee( co)nt 3!h% \ I! IINRJ b)tton is #resse( re()ce the co)nt beca)se the s#ee( is in&ersel #ro#ortional to co)nt% \ I! I.-,J b)tton is #resse( increase the co)nt% 8051 MANUAL RNSIT 5S 5S 71 MICROCONTROLLERS LAB IVth SEM EC -r!gra5 7!r ,C 5!t!r Nincl)(e WR-K51>.2%/D sbit inrR 57V2? 88s#ee( increment s'itch sbit (crR 57V7? 88s#ee( (ecrement s'itch main:; ` )nsi$ne( char iR0>80? 50 R 0>3!? 8TR)n the motor at hal! s#ee(%T8 'hile :1; ` i! :binr; `'hile :binr;? i!:iD10; iRi"10? 88increase the ., motor s#ee( a i!:b(cr; ` 'hile:b(cr;? i!:iW0>!0; iRiS10? 88(ecrease the ., motor s#ee( a 50Ri? a a $.Ca8>at!r >s<ng 6"=@!ard and *#s"g5"nt d<sAa= Ag!r<th5 \ Rea( the n)mbers n1 an( n2 !rom @eboar( an( (is#la them on se&en se$ment% \ Rea( the o#eran( !rom the @e#a( i! @e #resse( is 1 :S;2 ,:";2.:T;2-:8; then res#ecti&e o#eration is #er!orme(% \ Res)lt is (is#lae( on 2 (i$it se&en se$ment (is#la% \ I! an time the @e #resse( &al)e ret)rne( as 10h then clear the L,.% 8051 MANUAL RNSIT 8051f, 50 =R, 26#in ,able 57%2:inr; 57%7:(ec; 5S ., Motor Inter!ace ,ar( 5S ., Motor 5S 72 MICROCONTROLLERS LAB IVth SEM EC -r!gra5 7!r 8a8>at!r Nincl)(e WR-K51>.2%/D &oi( .is#,har:)nsi$ne( char ch;? &oi( ,lrL-.:;? )nsi$ne( char $et@e:;? )nsi$ne( char $etn)m:;? )nsi$ne( char $et6#:;? sbit ,l@ R 57V4? 8T ,loc@ line !or 3 se$ment (is#la T8 sbit .at R 50V0? 8T .ata line !or 3 se$ment (is#la T8 main:; ` )nsi$ne( char tm#R0>0!!2n1R02n226#2Res? )nsi$ne( char N)mTabd10e R ` 0>0c020>0!+20>0a420>b020>++20>+220>8220>0!820>8020>+0 a? )nsi$ne( char 6#Tabd4e R ` 0>8820>01!20>c820>0a1a? bit Ne$R0? ,lrL-.:;? 8T ,lear 3 se$ment (is#la T8 'hile:1; ` Ne$ R 0? 8T Ne$ati&e !la$ T8 n1R$etn)m:;? 8T Ket 1st n)mber T8 6# R $et6#:; " 0>01? 8T Ket 6#co(e% 0>0b is @eco(e o! [S[:see @eboar( schematics;T8 n2R$etn)m:;? 8T Ket 2n( n)mber T8 'hile:$et@e:;bR0>17;? 8T 'ait !or [R[ @e T8 ,lrL-.:;? s'itch:6#; 8T 5er!orm corres#on(in$ o#eration T8 ` case 0L Res R n1 S n2? brea@? case 1L i!:n2Dn1; 8T chec@ !or ne$ati&it T8 ` 8051 MANUAL RNSIT 8051f, 50 =R, 26#in ,able 5S Ke#a( 5S 3 Se$ .is#la 77 MICROCONTROLLERS LAB IVth SEM EC Ne$ R 1? Res R n2 " n1? brea@? a Res R n1 " n2? brea@? case 2L Res R n1 T n2? brea@? case 7L Res R n1 8 n2? brea@? a .is#,har:N)mTabdResZ10e;? 8T .is#la n)mber T8 .is#,har:N)mTabdRes810e;? i!:Ne$; 8T i! ne$ati&e res)lt (is#la ["[ T8 .is#,har:0>01!;? aa &oi( .is#,har:)nsi$ne( char ch; 8T Ro)tine to (is#la char on 3 se$ment T8 ` )nsi$ne( char i2tm#? 50R0>00? !or:iR0?iW8?iSS; 8T !or all bits T8 ` tm# R ch < 0>80? i!:tm#; 8T 'rite (ata (e#en(in$ on MS1 T8 .at R 1? else .at R 0? ,l@ R 0? 8T Ki&e ,l@ 5)lse !or snchroni0ation T8 ,l@ R 1? ch R ch WW 1? 8T Ket ne>t bit T8 a a &oi( ,lrL-.:; ` )nsi$ne( char i? !or:iR0?iW4?iSS; .is#,har:0>0!!;? 8T 0>!! !or clear se$ment : see 3 se$ment man)al !or more in!o; T8 a )nsi$ne( char $et@e:; ` )nsi$ne( char i2B2in(>2t? 52 R 0>00? 8T 52 as 6)t#)t #ort T8 50 R 0>0!!? in(> R 0>00? 8T In(e> !or storin$ the !irst &al)e o! scanline T8 'hile:1; ` !or:iR1?iWR4?iWWR1; 8T !or 4 scanlines T8 ` 52 R 0>0! < _i? 8T 'rite (ata to scanline T8 t R 50? 8T Rea( rea(lines connecte( to 50T8 t R _t? i!:tD0; 8T I! @e #ress is tr)e T8 ` 8051 MANUAL RNSIT 5S 74 MICROCONTROLLERS LAB IVth SEM EC !or:BR0?BWR3?BSS; 8T ,hec@ !or 8 lines T8 ` t DDR1? i!:tRR0; 8T i! $et #resse( @eT8 ` ret)rn:in(>SB;? 8T Ret)rn in(e> o! the @e #resse( T8 a a a in(> SR 8? 8T I! no @e #resse( increment in(e> T8 a aa )nsi$ne( char $etn)m:; 8T Metho( !or $ettin$ n)mber T8 ` )nsi$ne( char tm#? 'hile:1; ` tm# R $et@e:;? i!:tm# W 0>0a ^^ tm#RR0>10; 8T i! #resse( @e is n)mber2 ret)rn T8 ret)rn:tm#;? aa )nsi$ne( char $et6#:; 8T Metho( !or $ettin$ 6#erator T8 ` )nsi$ne( char tm#? 'hile:1; ` tm# R $et@e:;? i!::tm# D 0>0a << tm# W0>0!;^^ tm#RR0>10; 8T i! #resse( @e is a 6#erator2 ret)rn T8 ret)rn:tm#;? a a %.E":at!r Ag!r<th5 7!r "":at!r <nt"r7a8" \ Rea( the !loor reH)est thro)$h in#)t #ort 51% \ I! the c)rrent !loor an( reH)este( !loor are the same no chan$e li$ht )# the corres#on(in$ L-. thro)$h 50% \ I! the reH)este( !loor $reaterthan c)rrent mo&in$ )# o! the li!t is in(icate( b $lo'in$ o! L-.Fs !rom c)rrent !loor to the reH)este(% \ I! the reH)este( !loor lesserthan c)rrent mo&in$ (o'n o! the li!t is in(icate( b $lo'in$ o! L-.Fs !rom c)rrent !loor to the reH)este(% 8051 MANUAL RNSIT 5S 75 5S MICROCONTROLLERS LAB IVth SEM EC -r!gra5 7!r E":at!r Nincl)(e WR-K51=%/D &oi( (ela:)nsi$ne( int;? main:; ` )nsi$ne( char =lrd+e R `0>!!20>0020>0720>!!20>0620>!!20>!!20>!!20>0+a? )nsi$ne( char =,lrd+e R `0>!!20>0-020>0.720>!!20>01620>!!20>!!20>!!20>3+a? )nsi$ne( char ReH=lr2,)r=lr R 0>012i2B? 50 R 0>00? 50 R 0>0!0? 'hile:1; ` 51 R 0>0!? ReH=lr R 51 ^ 0>0!0? 'hile:ReH=lr RR 0>0!!; ReH=lr R 51 ^ 0>0!0? 8T Rea( ReH)est =loor !rom 51 T8 ReH=lr R _ReH=lr? i!:,)r=lr RR ReH=lr; 8T I! ReH)est !loor is eH)al to ,)rrent =loor T8 ` 50 R =,lrd,)r=lre? 8T ,lear =loor In(icator T8 contin)e? a 8T Ko )# to rea( a$ain T8 else i!:,)r=lr D ReH=lr; 8T I! ,)rrent !loor is D reH)est !loor T8 ` i R =lrd,)r=lre " =lrdReH=lre? 8T Ket the no o! !loors to tra&el T8 B R =lrd,)r=lre? !or:?iD0?i""; 8TMo&e the in(icator (o'n T8 ` (ela:25000;? aa else 8T I! ,)rrent !loor is W reH)est !loor T8 ` i R =lrdReH=lre " =lrd,)r=lre? 8T Ket the no o! !loors to tra&el T8 B R =lrd,)r=lre? !or:?iD0?i""; 8T Mo&e the in(icator U# T8 ` 50 R 0>0!0 ^ B? BSS? 8051 MANUAL RNSIT 8051f, 50 =R, 26#in ,able 5S -le&ator inter!ace 5S 76 5S MICROCONTROLLERS LAB IVth SEM EC (ela:25000;? a a ,)r=lr R ReH=lr? 8T U#(ate ,)rrent !loor T8 50 R =,lrd,)r=lre? 8T ,lear the in(icator T8 a a &oi( (ela:)nsi$ne( int >; ` !or:?>D0?>"";? a *.T"5A"rat>r" S"ns!r The inter!ace car( has a .A, to con&ert the act)al tem#erat)re to (i$ital this is com#are( 'ith re!erence tem#erart)re% Reala also a #art o! inter!ace car( 'ill t)rn on an( o!! to in(icate i! the act)al tem#erat)re is abo&e or belo' re!erence% Ag!r<th5 7!r T"5A"rat>r" s"ns!r 1% ,on!i$)re 50 an( 51 as o8#2 57 as in#)t #ort% 2% Set )# a co)nter 'ith intial &al)e 0>!! sen( it to (ac thro 50 a!ter a (ela chec@ i! com#arator o8# has $one lo'% 7% I! lo' com#are 'ith set &al)e i! act)al $reaterthan set t)rn on the rela else t)rn o!!% -r!gra5 7!r t"5A"rat>r" s"ns!r. Nincl)(e WR-K51>.2%/D sbit ,m#h6)t R 57V4? 8TIn#)t 1it !or ,om#arator o)t#)tT8 sbit Relh,on R 50V0? 8TRela controller 1it i%e /eater 5o'er s)##l controlT8 8T1" S)##l 6==2 0"S)##l 6NT8 N(e!ine .ach.ata 51 8T.A, in#)t .ata 56RT i%e% 51T8 &oi( (ela:; ` int l? !or :lR0?lWR0>8?lSS;? a main:; ` )nsi$ne( char .acI#? &oi( (ela:&oi(;? .ach.ata R0>00? 8TMo&e 00h to .ac in#)tT8 8051 MANUAL RNSIT 73 8051f, 50252257 =R, 26#in ,able 5S Tem# Sensor Inter!ace 5S /eat So)rce MICROCONTROLLERS LAB IVth SEM EC 50R0>00? 8Tma@e 50 as o)t#)tT8 'hile:1; ` .acI#R 0>!!? 8T.A, in#)t .ata co)nterT8 (o ` .acI#SS? 8TIncrement the .A, in#)t .ataT8 .ach.ata R .acI#? 8TMo&e the .A, (ata to .A,T8 (ela:;? a'hile:,m#h6)t;? 8T ,hec@ com#arator o)t#)t !or lo' T8 i!:.acI# D 0>20; 8T,om#are 'ith the set &al)e i%e%0>20T8 Relh,on R 1? else Relh,on R 0? 8T Rela 6N2 S)##l 6== T8 a a 8051 MANUAL RNSIT 78