Sei sulla pagina 1di 10

ELEC 3040/3050 Lab Manual

Lab 2 Revised 1/15/10

LAB 2: DEVELOPING C PROGRAMS IN CODEWARRIOR FOR THE HCS12 MICROCONTROLLER The objective of this laborator session is to beco!e fa!iliar "ith the #rocess for creatin$% e&ecutin$ and debu$$in$ a##lication #ro$ra!s% "ritten in the C lan$ua$e% for the 'reescale MC()12C32/12* +,C)12- !icrocontroller% usin$ the CodeWarrior .evelo#!ent )tudio/ 0ou are to desi$n a C #ro$ra!% containin$ a 1!ain2 #ro$ra! and t"o subroutines% to e&ercise various 3/4 #orts and ele!ents of the !icrocontroller/ 3n the lab% ou "ill e&ercise a nu!ber of the debu$ su##ort ele!ents of Code5arrior% so that ou "ill be better #re#ared for debu$$in$ lar$er #rojects later in the se!ester/ 'or reference% Cha#ter 10 of the ELEC 2220 te&t + Software and Hardware Engineering: Assembly and C Programming for the Freescale HCS12 Microcontroller 2 nd Ed! b 'rederic6 M/ Cad -% introduces #ro$ra! develo#!ent for the ,C)12 in C usin$ CodeWarrior% and other C #ro$ra! e&a!#les are #rovided in Cha#ters 1171*/ Interfacing T an! C ntr ""ing HCS12 In#$t%O$t#$t &I%O' Pin( The ,C)12 !icrocontroller bloc6 dia$ra! 1 is $iven in 'i$ure 1/ The !icrocontroller includes the ,C)12 C89% 32: b tes of flash !e!or % 2: b tes of R;M% analo$ to di$ital converter +;T.-% ti!er !odule +T3M-% 85M !odule% as nchronous +)C3- and s nchronous +)83- serial co!!unication interfaces% controller area net"or6 +C;<interface% cloc6 $enerator% s ste! inte$ration !odule +)3M-% and a nu!ber of #arallel 3/4 #orts/ )ho"n in 'i$ure 1 are =0 #ro$ra!!able si$nal #ins% #lus #o"er% $round% cloc6 and test #ins/ The chi# co!es in 4*7#in L>'8% 52 #in L>'8% and *07#in >'8 #ac6a$es/ 3n the *07#in #ac6a$e% all si$nals sho"n in 'i$ure 1 are bonded to #ins on the #ac6a$e/ 4n s!aller #ac6a$es% onl a subset of these si$nals are bonded to #ins/ The .ra$on'l a##lication !odule used in this lab contains the 4*7#in #ac6a$e% sho"n in 'i$ure 2/ T"o 207#in headers% arran$ed in .38 #ac6a$e for!at as sho"n in 'i$ure 3% are used to connect the !odule to the #roject board/ Therefore% the 40 #ins sho"n in 'i$ure 3 carr the si$nals available to users of the .ra$on'l !odule/ ;s discussed in ELEC 2220% !ost of the si$nal #ins have !ulti#le functions/ The default in !ost cases is si!#le #arallel 3/4% "ith s#ecial functions selected b soft"are/ Table 1 lists the functions of the #ins on the 4*7#in #ac6a$e/

Freescale "MC#S12C Family $eference Man%al

ELEC 3040/3050 Lab Manual

Lab 2 Revised 1/15/10

Fig$re 1) MC*S12C+Fa,i"- B" c. Diagra,2

Freescale MC9S12C Family Reference Manual

ELEC 3040/3050 Lab Manual

Lab 2 Revised 1/15/10

Fig$re 2) Pin a((ign,ent( n t/e 01+#in L2FP #ac.age3

Fig$re 3&a') 04+#in c nnect r 51 n t/e , !$"e0


3

Freescale MC9S12C Family Reference Manual Application Mo ule Stu ent !earnin" #it $sers %ui e featurin" t&e Freescale 'CS12

ELEC 3040/3050 Lab Manual

Lab 2 Revised 1/15/10

Fig$re 3&6') Drag nF"- , !$"e #in a((ign,ent( 8ort ; ? E 8arallel 3/4 8ins ;lternate 'unction 1 ;lternate 'unction 2 8;0 8?4 8E0 A3R>B +non7!as6able interru#t8E1 3R>B +!as6able interru#t8E4 ECL: +E7cloc6 out#ut8E@ ACL:)B +cloc6 in#utM 8M0%8M1 RAC;<%TAC;< +C;< control8M2 M3)4 +)83 controller8M3 ))B 8M4 M4)3 8M5 )C: 8 885 :585 +:e "a6eu#855 +85M $enerator) 8)0%8)1 RA.% TA. +)C3 controllerT 8T078T4 34C0734C4 +Ti!er 3/48507854 +85M $en/8T578T@ 34C5734C@ ;. 8;.0078;.0@ ;<007;<0@ +;/. Converter777 M4.C ?:CC +?.M in#utRE)ETB Ta6"e 1) Pin f$ncti n( n t/e 01+#in HCS12 ,icr c ntr ""er an! te(t , !$"e

ELEC 3040/3050 Lab Manual Micr c ntr ""er S ft7are Set$# in A((e,6"- Lang$age

Lab 2 Revised 1/15/10

9nli6e the $eneral7#ur#ose !icro#rocessors in !ost des6to# and #ortable co!#uters% ever !icrocontroller has its o"n uniDue set of fi&ed !e!or and 3/4 resource addresses/ Therefore% "hen creatin$ a ne" #roject in Code5arrior% a s#ecific !icrocontroller derivative !ust be identified so that the co!#iler/asse!bler/lin6er can be told "here it can #lace #ro$ra! !e!or % data% stac6% etc/ 3n the ,C)12% the follo"in$ usable addresses are available/ E00007E03'' F C89 and 3/4 !odule re$isters E0*007E0''' F 4: b tes R;M +data stora$eE40007E@''' 7 1=: b tes flash 1R4MG40002 fi&ed !e!or E*0007E?''' F *B1=: b tes flash !e!or +in1=: #a$e "indo"EC0007E'''' F 1=: b tes flash 1R4MGC0002 fi&ed !e!or +so!e used b ?.M3n an asse!bl lan$ua$e #roject% data and the stac6 !ust be e&#licitl forced into R;M and #ro$ra! code into flash !e!or via ori$in +4RC- asse!bler directives% as sho"n in E&a!#le 1/ <ote that the four labels $AMStart $AMend $&M'()))Start and $&M'()))End are defined in the 3<CL9.E file 1MC#S12C*2!inc2% "hich also #rovides s !bolic labels for all C89 and !odule re$ister addresses/
;********************************************************** ; include derivative specific macros INCLUDE 'MC9S1 C! "inc' ;#$MStart% e&u '((((()(( ;#$MEnd% e&u '(((((*** ;#+M,-(((Start% e&u '((((-((( ;#+M,-(((End% e&u '((((.*** +#/ #$MStart ; data definitions 0o 1ere ; code section 2e0ins 1ere +#/ #+M,-(((Start Entr3% LDS 4#$MEnd51 ; initiali6e t1e stac7 pointer

+ ,rogram instr%ctions go here


;********************************************************** ;* Interrupt 8ectors * ;********************************************************** +#/ '***E DC"9 Entr3 ; #eset 8ector

E8a,#"e 1) Sa,#"e a((e,6"- "ang$age #r gra, (et$# f r HCS12) C89 and !odule re$isters are accessed in asse!bl lan$ua$e b treatin$ the! as !e!or addresses/ E&a!#le 2 illustrates initialiHation of the data direction re$isters of #orts ; and ? as in#ut and out#ut% res#ectivel % readin$ a b te fro! #ort ;% and "ritin$ it to #ort ?/ ;s stated above% 3<CL9.E file ' MC9S1 C! "inc' contains s !bolic label
definitions for all re$isters/ Therefore% ou !a either include that file or else define the! in our

ELEC 3040/3050 Lab Manual

Lab 2 Revised 1/15/10

#ro$ra!/ ;s sho"n in the last instruction of E&a!#le 2% s !bolic labels are not absolutel necessar % but the !a6e the #ro$ra! !uch !ore readable% and therefore easier to "rite% understand% and debu$/ Therefore% the use of s !bolic na!es for all re$isters is $ood #ro$ra!!in$ #ractice% and hi$hl reco!!ended/
INCLUDE ' MC9S1 C! "inc' ;#+M,-(((Start E:U '-((( ;;+#<$ E:U '(((( ;;+#<= E:U '(((1 ;DD#$ E:U '((( ;DD#= E:U '(((! +#/ mov2 mov2 ldaa staa ldaa #+M,-(((Start 4'((>DD#$ ;confi0ure ;+#<$ as input 4'ff>DD#= ;confi0ure ;+#<= as output ;+#<$ ;input 23te from ;+#<$ ;+#<= ;output 23te to ;+#<= '(((( ;also inputs 23te from ;+#<$

E8a,#"e 2) Sa,#"e a((e,6"- "ang$age #r gra, t acce(( I%O # rt( Micr c ntr ""er S ft7are Set$# in C The #ur#ose of a hi$h level lan$ua$e% such as C% is to allo" a #ro$ra!!er to concentrate on the al$orith! to be #erfor!ed% inde#endent of the #articular #rocessor bein$ used/ This is done b !a6in$ #rocessor7s#ecific details% such as !e!or addresses% C89 re$isters% and asse!bl lan$ua$e instructions% trans#arent to the #ro$ra!!er/ )uch lo" level details are !ana$ed b the lan$ua$e co!#iler/ Therefore% the co!#iler !ust be #rovided "ith R4M and R;M address ran$es so that it can #osition code% data% and stac6 accordin$l / 3n Code5arrior% this is done auto!aticall "hen a #rocessor derivative +e&/ ,C)12- is selected at #roject creatin$ ti!e/ 3n asse!bl lan$ua$e% the #ro$ra!!er !ust e&#licitl initialiHe the stac6 #ointer% bac6$round ti!er% interru#t vectors% etc/ To ta6e care of these thin$s% Code5arrior allo"s the user to include 'reescale7su##lied 1startu# code2 in a C #roject/ 'or this lab% select 1!ini!al startu# code2 on 8a$e = of the <e" 8roject 5iHard to include file 1startu#/c2 in our #roject/ 4n reset% the instructions in this file "ill be e&ecuted to set u# the !icrocontroller% #rior to enterin$ the 1!ain2 #ro$ra! defined b the #ro$ra!!er/ 9se the follo"in$ <e" 8roject 5iHard selections for C #rojects in this lab/ Page 2 + Se"ect - $r #r ce(( r !eri9ati9e &MC*S12C32 r 121' Page 3 : Se"ect t/e #r gra,,ing "ang$age &C' Page 0 : C/ec. NO t ,it t/e Pr ce(( r E8#ert Page ; : C/ec. NO t ,it t/e (et$# f r PC+"int Page < : C/ec. ,ini,a" (tart$# c !e Page = : C/ec. N ne> t ,it t/e "i6rar- f f" ating # int arit/,etic r $tine( Page 1 : C/ec. S,a"" ,e, r- , !e" Page * : Se"ect TBDML &- $ ,a- a"( (e"ect F$"" C/i# Si,$"ati n if - $ 7i(/'

ELEC 3040/3050 Lab Manual

Lab 2 Revised 1/15/10

;ll 3/4 #orts and functions in the !icrocontroller are accessed and controlled via 1re$isters2% each of "hich is assi$ned a !e!or address in the ran$e E00007E03''% as defined in the ,C)12 .ata )heet% available on the class "eb #a$e% and in the ELEC 2220 te&t% Software and Hardware Engineering: Assembly and C Programming for the Freescale HCS12 Microcontroller 2 nd Ed! b 'rederic6 M/ Cad / Cha#ter 10 of the te&t discusses #ro$ra! develo#!ent usin$ C and Code5arrior/ 5hen usin$ C for a !icrocontroller% one !ust be a"are of ho" data t #es are defined b the co!#iler used for each #roject/ 'or the data t #es needed in this lab% the CodeWarrior ,C)12 C co!#iler uses the definitions in Table 2/ 0ou should al"a s select a data t #e a##ro#riate for each variable/ .ata t #e declaration <u!ber of bits Ran$e of values unsi$ned char 6 * 0//255 si$ned char 6 * 712*//I12@ unsi$ned int +short- 6 1= 0//=5535 si$ned int +short- 6 1= 732@=*//I32@=@ Ta6"e 2) Data t-#e !efiniti n( f r a 9aria6"e . in t/e C !e?arri r C c ,#i"er) Jariables in C can be auto!atic% static% or volatile/ ;n a%tomatic -ariable is declared "ithin a #rocedure% and is visible +has 1sco#e2- onl "ithin that #rocedure/ )#ace for the variable is allocated on the s ste! stac6 "hen the #rocedure is entered% and deallocated "hen the #rocedure is e&ited/ Therefore% values are not retained fro! one #rocedure call to the ne&t/ 3f there are onl one or t"o variables% the co!#iler !a choose to allocate the! to re$isters in that #rocedure% instead of allocatin$ !e!or / ; static -ariable !a be declared either inside or outside a function/ These are retained for use throu$hout the #ro$ra!% and are allocated fi&ed s#ace in R;M that is not reallocated durin$ the #ro$ra!/ ; static variable is defined b insertin$ the "ord 1static2 in front of the variable definition% as in the follo"in$ character variable declaration/ static char bob+ ; -olatile -ariable is one "hose value can be chan$ed b outside influences% i/e/ b factors other than #ro$ra! instructions% such as the value of a ti!er re$ister% or the out#ut of an analo$ to di$ital converter/ The !ost co!!on use of volatile variable definitions in this lab "ill be for the various 3/4 #orts and !odule re$isters of the !icrocontroller/ 'or e&a!#le% the follo"in$ defines the addresses of 3/4 #orts ; and ?/ .define P&$/A 0100-olatile %nsigned char120)3))))222 .define P&$/4 0100-olatile %nsigned char120)3)))1222 ;s an *7bit in#ut #ort% values read via #ort ; or ? are su##lied b sources e&ternal to the !icrocontroller% and therefore such #orts are declared as data t #e 1volatile unsi$ned char2 at addresses E0000 and E0001% "ith identifiers 84RT; and 84RT?% res#ectivel % defined as #ointers to these addresses/ This allo"s these identifiers to be used as "ould

ELEC 3040/3050 Lab Manual

Lab 2 Revised 1/15/10

an other variable/ The follo"in$ C state!ents read the *7bit value fro! 84RT?% assi$ns it to character variable c% and then "rites the value to 84RT;/ c 5 P&$/4+ P&$/A 5 c+ 61 read -al%e from P&$/4 into -ariable c 16 61 write -al%e to P&$/A from -ariable c 16

CodeWarrior #rovides a derivative7s#ecific include file for each !icrocontroller "hich #rovides s !bolic label definitions for all C89 and !odule re$isters/ E&a!#le 3 sho"s the default !ain/c file created b Code5arrior for a ne" #roject/ <ote the inclusion of the derivative infor!ation file 1 MC9S1 C! /h2/
4include ?1idef"1@ A* common defines and macros *A 4include ?MC9S1 C! "1@ A* derivative information *A 4pra0ma LINB,IN*+ DE#I8$<I8E CMC9S1 C! C void mainDvoidE F A* put 3our oGn code 1ere *A Ena2leInterrupts; forD;;E FH A* Gait forever *A A* please ma7e sure t1at 3ou never leave t1is function *A H

E8a,#"e 3) S.e"et n ,ain)c fi"e generate! 6- C !e?arri r) Pr gra, De6$gging in C !e?arri r )ource "indo" +C state!ents;sse!bl "indo" .ata "indo" Re$ister "indo" Me!or "indo" )in$le ste# ?rea6#oints +#ause #ro$ra! e&ecution at selected #ointsMar6#oints +ca#ture variable values at selected #ointsLa6 rat r- E8erci(e To e&#eri!ent "ith C #ro$ra! creation and debu$$in$% ou are to set u# the ,C)12 to create a decade u#/do"n counter% i/e/ count u# fro! 0 to ( and re#eat% or count do"n fro! ( to 0 and re#eat/ The #ro$ra! is to test t"o to$$le s"itches on the #roject boardK start/sto# s"itch )1 and direction s"itch )2% connected to 3/4 #ort #ins 8;0 and 8?4% res#ectivel / Connect these to the !odule header "ith "ires/ Countin$ is to occur "hen )1 L 1% other"ise the count should 1freeHe2/ 5hile countin$ is enabled% the count should be u# if )2 L 0% and do"n if )2 L 1/ Chan$in$ )2 "hile countin$ is enabled should chan$e the count direction on the ne&t/ The count is to be dis#la ed on LE.17LE.4 +!ost si$nificant bit on LE.1- on the #roject board b "ritin$ to 8ort T bits 073/ Connect

ELEC 3040/3050 Lab Manual

Lab 2 Revised 1/15/10

the LE.s to the 8ort T #ins on the !odule header "ith "ires/ The count is to chan$e a##ro&i!atel once ever half second/ This #ro$ra! is to be desi$ned as a !ain #ro$ra! and t"o se#arate 1functions2 as follo"s/ ; elay function is to i!#le!ent a half7second ti!e dela +1do nothin$2 for about half a second-/ ; countin" function is to incre!ent or decre!ent the count% accordin$ to the settin$ of a direction variable #assed as a #ara!eter fro! main% and dis#la the ne" value on the LE.s/ The count value is to be a static $lobal variable/ The main #ro$ra! is to initialiHe #ort directions and variables% and then e&ecute in an endless loo#% callin$ the elay function% settin$ the direction variable based on #osition of s"itch )2% and callin$ the countin" function if countin$ is enabled b s"itch )1/ 3n debu$$in$ the #ro$ra!% ou "ill be e&#ected to de!onstrate the use of brea6#oints% !ar6#oints% sin$le7ste#% and other debu$ features in Code5arrior/ These are described in the Code5arrior hel# !essa$es/ )#ecificall % do the follo"in$ in the debu$$er/ 1/ )et brea6#oints in the !ain #ro$ra! and in each of the t"o functions% and verif that the #ro$ra! reaches these brea6#oints as e&#ected/ 2/ 5hile sto##ed at a brea6#oint% record the values of our different #ro$ra! variables +fro! the .ata "indo" at the left/- <ote that the onl data values dis#la ed are $lobal variables% and those in the current 1sco#e2% i/e/ in the currentl 7e&ecutin$ function/#rocedure/ )o% if ou "ish to e&a!ine a variable in a function '% then ouMll need a brea6#oint in '/ 3/ ;fter sto##in$ at a brea6#oint% sin$le7ste# throu$h a fe" instructions% and record an chan$es to the variables/ Then ou !a clic6 1run2 to continue the #ro$ra!/ 4/ 9sin$ the disasse!bled #ro$ra! "indo" +u##er ri$ht-% find and record the !e!or addresses assi$ned to t"o of our #ro$ra! variables/ Then% in the !e!or "indo" +botto! ri$ht-% dis#la the !e!or area containin$ these t"o addresses and verif that the values !atch those in the variables "indo"/ 5/ Re!ove the brea6#oints and run the #ro$ra! to verif correct o#eration/ 8rior to lab ou should "rite a first draft of the #ro$ra! and desi$n a 1test #rocedure2 +record these in our noteboo6- so that ou "ill be read to co!#ile the #ro$ra! and be$in testin$ "hen the lab #eriod be$ins/

ELEC 3040/3050 Lab Manual

Lab 2 Revised 1/15/10

La6 2 De"i9era6"e(: ;ll students are to sub!it their lab noteboo6s in the bo& in the lab b 3K00 #/!/ 'rida / These noteboo6s are e&#ected to contain our lecture notes% #re7lab "or6 +#ro$ra! desi$n and test #rocedure-% and notes !ade durin$ the lab session of tests #erfor!ed% observations% desi$n chan$es% etc/

Potrebbero piacerti anche