AVR1000XMEGA的C語言代碼編寫入門_第1頁
AVR1000XMEGA的C語言代碼編寫入門_第2頁
AVR1000XMEGA的C語言代碼編寫入門_第3頁
AVR1000XMEGA的C語言代碼編寫入門_第4頁
AVR1000XMEGA的C語言代碼編寫入門_第5頁
已閱讀5頁,還剩29頁未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

1、 8-bit 8位Microcoontrolllers 微處理器Applicaation Note 應(yīng)用注釋 AVR10000: Geettingg Starrted WWritinng C-ccode ffor XMMEGAAVR10000 : XMEGGA的C語言代碼編寫寫入門 Featurres 特點(diǎn): Naminng connventiions 命名約定- Regisster nnames 寄存器名- Bit nnames 位名 C-codde nammes C-代碼名名- Bit aand grroup mmasks 位與組掩碼碼- Groupp conffiguraation ma

2、skss 組配置掩碼碼 Methoods foor acccessinng reggisterrs 訪問寄存器器的方法 Methoods foor wriiting reusaable mmodulee funcctionss 可多次使用用模塊功能的的寫入方法1 Introoductiion 引言Short ddeveloopmentt timees andd highh quallity rrequirrementts on electtronicc prodducts has mmade hhigh-llevel progrramminng lannguagees a rrequirrem

3、entt. Thee mainn reasson iss thatt Highh leveel lannguagees makke it easieer to mainttain aand reeuse ccode ddue too bettter poortabiility and rreadabbilityy. 由于電子產(chǎn)品的的開發(fā)時(shí)間短,質(zhì)量要求高高,因此需要要高層次的編編程語言。最最主要的原因因是,高級(jí)語語言具有更好的可可移植性和可可讀性,使其其更易于維護(hù)和重復(fù)使使用代碼。The chooice oof proogrammming llanguaage allone ddoes nno

4、t ennsure high readaabilitty andd reussabiliity; ggood ccodingg stylle doees. Thherefoore thhe XMEEGA pperiphheralss, heaader ffiles and ddriverrs aree desiigned with this in miind. 編程語言的選擇擇本身并非是能確保具有較高的可可讀性和可重復(fù)使用性的唯一條條件,還必須有良好好的編碼風(fēng)格格。因此 XMEGAA 的外圍設(shè)備備,頭文件和和驅(qū)動(dòng)程序的的設(shè)計(jì)都是基基于這方面考考慮的。 The moost wiidely use

5、d high-levell langguage for AAVR mmicroccontroollerss is CC, andd thiss appllicatiion noote thherefoore foocusess on CC proggrammiing. TTo suppport most of thhe AVRR C coompileers thhat arre avaailablle, the code exampples aare ass far as poossiblle wriitten in ANNSI C. A feew exaampless are speciif

6、ic tto IARR Embeedded Workbbench, butt the ideass and methoods caan be used for oother compiilers with minorr channges. IAR sspeciffic exxamplees aree cleaarly mmarkedd.AVR 微控控制器使用的的最廣泛的高高級(jí)語言就是 C 語言,因此本本應(yīng)用注釋的的重點(diǎn)是 C 語言編程。為為了支持大多多數(shù)可用的 AVR CC 語言編譯器,我們盡可能把代碼示例編寫寫在 ANSI C 語言的規(guī)范中。有些些例子是 IAR EEmbeddded Woo

7、rkbennch 專用的,但其思路和方法法可用于與其他變化不大的編譯器器。 IAR 專用的示例都有清晰的標(biāo)標(biāo)示。2 XMEGAA Moduules XMEGAA 模塊An AVR XMEGAA is ccompossed off seveeral bbuildiing bllocks: An AAVR CPPU corre, SRRAM, FFlash, EEPRROM annd a nnumberr of pperiphheral modulles. TThese buildding bblockss are calleed “moodule typess”. Ann XMEGGA cann

8、 havee one or moore innstancces off a giiven mmodulee typee. Alll insttancess of aa moduule tyype haave thhe samme feaaturess and functtions. 一個(gè) AVR XMEGAA 由數(shù)個(gè)結(jié)構(gòu)塊組成:一個(gè)AVRR CPU 芯片,一個(gè) SRAAM,一個(gè)閃存, 一個(gè) EEPROOM 以及若干外設(shè)設(shè)模塊。這些些結(jié)構(gòu)塊被稱為為“模塊類型”。XMEGAA可以有一個(gè)個(gè)或多個(gè)給定定模塊類型的的實(shí)例。一個(gè)模塊類型型的所有實(shí)例例都具有相同同的特性和功功能。Some moodule t

9、ypess can be a subseet of otherr moduule tyypes. Thesee inheerit aa subsset off the featuures (and rregistters) of thhe supper tyype, aall innheritted feeaturees aree fullly commpatibble. TThis aappliees to e.g. timerrs andd IO pports. The subseet of a moddule ttype ccan foor a ttimer mean that it h

10、aas fewwer coomparee and captuure chhannells thaan a ffull ttimer modulle. Siimilarrly, aan IO port may hhave lless tthan eeight pins. 有些模塊類型可可以是其他模模塊類型的子集。這些(模塊塊類型)繼承承了特大模塊塊類型的某個(gè)子集的特征征(和寄存器器),其繼承的所有有特征都完全兼兼容。這適用用于比如計(jì)時(shí)器和和IO端口。用于計(jì)計(jì)時(shí)器的模塊塊類型的子集集可能意味著著它所能得到到的比較和捕獲通通道比完整的的計(jì)時(shí)器模塊塊的要少。同樣,一一個(gè) IO 端口的管腳可能不足八個(gè)。

11、A modulle typpe cann be aa “USAART”, whilee the modulle insstancee is ee.g. “UUSARTCC0”, wwhere the “CC0” suuffix indiccates the iinstannce iss “USAART nuumber 0 on port C”. FFor siimpliccity, a moddule iinstannce wiill bee refeerred to ass a moodule throuughoutt thiss docuument, unleess thhere iis

12、a nneed tto diffferenntiatee. 一個(gè)模塊類型可可以是“USARTT”的,如果這個(gè)個(gè)模塊實(shí)例比如是“USARTTC0”,其中的“C00”后綴表示這個(gè)個(gè)實(shí)例是“端口 C 上的 USARTT 編號(hào)為 0”。為了簡(jiǎn)單起起見,一個(gè)模塊實(shí)實(shí)例將被稱為為這整個(gè)文件中的一個(gè)模塊,除除非是有必要要另作區(qū)分。Each moodule has aa numbber off regiisterss thatt conttain ccontrool or statuus bitts. Alll moddules of a givenn typee conttain tthe saame se

13、et (orr subsset) oof reggisterrs, annd alll thesse reggisterrs conntain the ssame sset (oor subbset) of coontroll and statuus bitts. 每個(gè)模塊都有若若干寄存器,這這些寄存器都都包含控制位位或狀態(tài)位。一一個(gè)給定類型型的所有模塊塊都包含相同同的寄存器集集合(或子集集),并且所有這些些寄存器包含含相同的控制制位和狀態(tài)位位的集合(或子子集) 。Figure 2-1. Modulle typpes, iinstannces, regissters and bbits. 圖2

14、-1. 模模塊類型,實(shí)實(shí)例,寄存器器和位Each moodule has aa fixeed basse adddress in thhe IO memorry mapp and all rregistters ccontaiined iin thee moduule haave fiixed ooffsett addrressess relaative to thhe moddule bbase aaddresss. Thhis waay eacch reggisterr willl not only have an abbsolutte adddress in thhe IO memorr

15、y spaace, bbut allso a relattive aaddresss deffined by itts offfset. The rregistter offfset addreesses are eequal for aall innstancces off a moodule type, simpplifyiing thhe tassk of writiing drriverss thatt can be ussed foor alll moduules oof a sspeciffic tyype. 在 IO 內(nèi)存存圖里,每個(gè)模模塊都有一個(gè)個(gè)固定的基礎(chǔ)地址,并且該模塊中的

16、所所有寄存器都都有固定的偏移移地址(這是是相對(duì)于模塊塊的基地址而而言)。如此這樣,每每個(gè)寄存器不不僅會(huì)在 IO 內(nèi)存空間有一一個(gè)絕對(duì)地址址,而且還有有一個(gè)根據(jù)其其偏移量定義義的相對(duì)地址。一一個(gè)模塊類型型的所有實(shí)例例中的寄存器偏偏移地址都是是平等的,這這樣就簡(jiǎn)化了編寫能能夠適用于某某個(gè)特定類型型的所有模塊塊的驅(qū)動(dòng)程序序的任務(wù)。2.1 Reggisterr Namiing Coonventtion2.1. 寄存存器命名約定定Registeer aree rougghly sspeakiing diividedd intoo conttrol, statuus andd dataa regiiste

17、rss and the nnamingg of rregistters rreflecct thiis. A generral-puurposee conttrol rregistter off the modulle is namedd CTRLL. If multiiple ggeneraal-purrpose contrrol reegisteers exxists in a modulle theey havve a ssuffixx charracterr. In this case the ccontrool reggisterrs wouuld bee nameed CTRRLA

18、, CCTRLB, CTRLLC andd so oon. Thhis allso apppliess to SSTATUSS regiisterss. 大致說來,寄存存器有控制寄寄存器、狀態(tài)寄存器器和數(shù)據(jù)寄存存器之分,這從寄存器器的命名就可可看出。模塊的一個(gè)通用的的控制寄存器器被命名為CCTRL。如如果在一個(gè)模模塊中存在多多個(gè)通用控制制寄存器,他他們都有一個(gè)后綴綴字符。在這這種情況下,控控制寄存器將將被命名為CCTRLA 、 CTRLLB 、 CTRLLC等。這方法也適用于于狀態(tài)寄存器器。For reggisterrs thaat havve a sspeciffic fuunctioon

19、thee namee refllects this functtionallity. For eexamplle, a contrrol reegisteer thaat conntrolss the interrrupt levell of aa moduule iss nameed INTTCTRL. 對(duì)于具有特定功功能的的寄存器,它們們的命名會(huì)反反映其功能性。例如,一一個(gè)控制寄存存器,控制一一個(gè)模塊的中中斷級(jí)別,它它就被命名為為 INTCTTRL 。Since tthe AVVR datta buss widtth is 8 bitt, larrger rregistters aare

20、immplemeented usingg seveeral 88-bit regissters. For a 16-bit rregistter, tthe hiigh annd loww bytees aree acceessed by apppendiing “HH” andd “L” respeectiveely too the regisster nname. For eexamplle, thhe 16-bit TTimer/Countter coount rregistter iss nameed CNTT. Thee two bytess are namedd CNTLL and

21、 CNTH. 由于 AVR 的數(shù)據(jù)總線寬寬度為 8 位,較大的寄存器器得使用幾個(gè) 8 位寄存器才能能生效。對(duì)于于一個(gè)16位寄存器器,要訪問其高8位元組和低8位元組,得在在這個(gè)寄存器器的命名上分別別附加 “H” 和 “L”。例如,166位計(jì)時(shí)器/計(jì)數(shù)器的計(jì)計(jì)數(shù)寄存器被被命名為 CNT ,其兩個(gè)8位元組就被命名為 CNTL和 CNTH 。For a rregistter laarger than 16 biit, thhe byttes arre nummberedd fromm the leastt signnificaant byyte. FFor exxamplee, thee 32-bbi

22、t ADDC callibrattion rregistter iss nameed CALL. Thee fourr bytees aree nameed CALL0, CAAL1, CCAL2 aand CAAL3 (ffrom lleast to moost siignifiicant byte).對(duì)于大于16位位的寄存器,其8位元組的編號(hào)是從最低有效的8位元組開始。例如,32位的 ADC 校準(zhǔn)寄存器被命名為 CAL,其四個(gè)8位元組(從最低到最高有效8位元組)就被分別命名為 CAL0 , CAL1 , CAL2 和 CAL3 。 Most C compiilers offerr auto

23、omaticc handdling of acccess to muulti-bbyte rregistters. In thhat caase thhe namme CNTT, witthout “H” oor “L” suffiix, coould bbe useed to perfoorm a 16-biit acccess tto thee Timeer/Couunter countt regiister. Thiss is aalso tthe caase foor 32-bit rregistters. 大多數(shù) C 語語言編譯器都提供了自動(dòng)動(dòng)處理多8位元組寄存器器的訪問。在在這種情

24、況下下, CNTT 的命名沒有加“ H”或“ L”后綴綴的,可以用用來執(zhí)行166位的計(jì)時(shí)器/計(jì)數(shù)器的計(jì)計(jì)數(shù)寄存器的的訪問。這方方法也同樣使用于于32位寄存器器。2.2 Bitt Namiing Coonventtion2.2 位命名名約定Registeer bitts cann havee an iindiviidual functtion oor be part of a bit ggroup that have a joiint fuunctioon: Ann indiividuaal bitt coulld be a bitt thatt enabbles aa moduule, ee.

25、g. tthe USSART EENABLEE bit. A biit grooup caan connsist of twwo of more bits that jointtly seelect a speecificc conffiguraation of thhe moddule tthat tthey bbelongg to. A bitt grouup offfers uup to 2n seleectionns, whhere nn is tthe nuumber of biits inn the bit ggroup. The two bbits tthat ccontrool

26、 thee USARRT Recceive Compllete iinterrrupt llevel, RXINNTLVL1:0, is aan exaample of a bit ggroup. Thesse twoo bitss offeer thee folllowingg seleectionns: 寄存器位可以有有一個(gè)單獨(dú)的的函數(shù),或是一個(gè)位組的的一部分,有有一個(gè)共同的的功能:一個(gè)個(gè)單獨(dú)的位可可以是啟用一個(gè)模塊塊的位,如 USARTT ENABLLE 位。一個(gè)位位組可以由兩兩個(gè)或兩個(gè)以以上的位組成,這些些位共同選擇了它們所所屬的模塊的特殊配置。一個(gè)個(gè)位組提供多多達(dá) 2n 個(gè)選項(xiàng),其中

27、的 n 是位組中位的數(shù)量。這這兩個(gè)位控制制的 USARTT 接收結(jié)束中中斷級(jí)別,RXINTTLVL 1:0 ,是一個(gè)個(gè)位組的一個(gè)個(gè)例子。這兩兩個(gè)位提供以以下選項(xiàng):Table 22-1. RXINTTLVL bbits aand coorresppondinng intterruppt levvel seelectiion. 表 2-1. RXINTTLVL 位和相應(yīng)的的中斷級(jí)別選選項(xiàng)RXINTLVVL1RXINTLVVL0Interruupt leevel sselecttion 中中斷級(jí)別選項(xiàng)項(xiàng)00Interruupt Offf 中斷關(guān)閉01Low levvel innterruupt 低級(jí)

28、中斷1 10Medium levell inteerruptt 中級(jí)中斷11High leevel iinterrrupt 高級(jí)中斷 Bits thhat arre parrt of a grooup wiill allways have a nummber ssuffixx. Bitts thaat aree not part of a bit ggroup will neverr havee a nuumber suffiix. A Timerr/Counnter ccontrool reggisterr D haas twoo bit groupps, EVVACT aand EVVSE

29、L. The bbits iin theese grroups have a nummber ssuffixx, whiile thhe EVDDLY biit, whhich iis nott partt of aa bit groupp has no nuumber suffiix.作為一個(gè)組的一一部分的位,將始終有有一個(gè)數(shù)字后后綴。不是一個(gè)位組組的一部分的的位,永遠(yuǎn)不會(huì)會(huì)有一個(gè)數(shù)字字后綴。計(jì)時(shí)時(shí)器/計(jì)數(shù)器控制制寄存器 D 有兩個(gè)位組: EVACCT 和 EVSELL 。在這些些組中的位都有一個(gè)數(shù)字字后綴,而 EVDLYY 位不是一個(gè)個(gè)位組的一部部分,因此它它沒有數(shù)字后綴。Table 22

30、-2. Bits groupps andd bit namess for bits in Tiimer/CCounteer Conntrol regisster DD CTTRLD.表 2-2. 適用于計(jì)時(shí)時(shí)器/計(jì)數(shù)器控制制寄存器 D - CCTRLD 中的位的位組和位名稱Bit Grooup 位組EVACT-EVSELBit namme 位名EVACT2 EVACT1EVACT0EVDLYEVSEL3EVSEL2EVSEL1EVSEL0Bit nummber 位編號(hào)765432103 Writiing C-code for XXMEGA 編寫 XMMEGA 的 C 語言代代碼The foll

31、lowinng secctionss focuus on how tto wriite C-code for tthe XMMEGA. The eexamplles shhow hoow to make the ccode hhighlyy readdable and pportabble beetweenn diffferentt XMEGGA devvices. The exampples ccan allso bee usedd as aa guiddelinee to wwrite code that is eaasy too veriify annd maiintainn. 以下各節(jié)

32、重點(diǎn)放放在如何編寫寫 XMEGAA 的 C 語言代碼。示示例顯示了如如何使代碼具具有很強(qiáng)的可可讀性,并能能在不同的XMEEGA 器件之間移移植。這些例子也可可以作為指導(dǎo)導(dǎo)編寫代碼的準(zhǔn)則,使其易于驗(yàn)證和維維護(hù)。XMEGA mmodulees aree locaated iin deddicateed andd conttinuouus bloocks iin thee memoory sppace aand caan be seen as enncapsuulatedd unitts. Thhis reeflectts on the wway thhat thhe moddules are aa

33、ccesssed whhen cooding C: mooduless are encappsulatted ussing CC struucts, in whhich aall moodule regissters are ccontaiined. Figurre 3-11 showws an illusstratiion off thiss.XMEGA的模模塊位于內(nèi)存存空間的專用的連續(xù)的塊區(qū)域中,我們可以把這些模模塊看作是被被封裝的單元。這反映了了編寫 C 語言代代碼時(shí)訪問模塊的路徑:模塊是用 C 語言結(jié)構(gòu)封裝的,其中包容了了所有的模塊寄存器器。圖3-11給出了這個(gè)圖解。Note thha

34、t soome reegisteers haave noo direect moodule assocciatioon. Thhese aare noot enccapsullated in sttructss, as the sstructt is uused tto asssociatte reggisterrs witth a mmodulee. 請(qǐng)注意,某些寄寄存器沒有直直接的模塊相相聯(lián),這些寄存器都不是是封裝在結(jié)構(gòu)里的,因?yàn)樵摻Y(jié)構(gòu)是是用來使寄存器與某個(gè)個(gè)模塊相關(guān)聯(lián)的。For larrger ccode pprojeccts thhe moddule sstructts proovid

35、e advanntagess, nott onlyy to rreadabbilityy, butt alsoo becaause tthe coompileers caan reuuse thhe moddule ddriverrs andd therreby mmake tthe coode veery coompactt. Thiis is descrribed in moore deetailss lateer. 模塊結(jié)構(gòu)為較大大的代碼項(xiàng)目目提供的優(yōu)勢(shì)勢(shì)不僅具有可讀性,而且且還因?yàn)榫幾g器器可以重復(fù)使使用模塊驅(qū)動(dòng)動(dòng)程序,從而而使代碼非常常緊湊。這在在后面會(huì)作更詳細(xì)的描述。This doo

36、cumennt inttroducces a naminng connventiion annd reggisterr acceess meethodss thatt are diffeerent from what AVR pprograammingg veteerans are uused tto, buut onee shouuld bee awarre thaat thee “claassic” way tto acccess rregistters iis stiill suupportted byy the headeer filles. TThis aalso aappliees

37、 on the bbit leevel. 本文檔介紹的命命名約定和寄寄存器訪問的的方法,不同同于 AVR 編程老手過去去常用的方法法,但是有一點(diǎn)點(diǎn)應(yīng)該知道:訪問寄存器器的“經(jīng)典”方法仍然是要得到到頭文件的支持。這方法法也同樣適用于對(duì)對(duì)位級(jí)別的訪訪問。Figure 3-1. Modulles pllaced in deedicatted bllocks in IOO memoory sppace. 圖3-1 .放放置在 IO 內(nèi)存空間里的的專用塊區(qū)的模塊。3.1 XMEEGA Heeader Filess3.1 XMEEGA的頭文文件A dediccated headeer fille is a

38、vaillable for eeach XXMEGA devicce. Iff the targeet devvice iis speecifieed in the pprojecct setttingss (asssumingg thatt one uses the IIDE foor IARR EWAVVR), tthe IAAR commpilerr willl autoomaticcally incluude thhe corrrect headeer fille if the ddevicee filee is iincludded ass showwn in Code Listii

39、ng 3-1. 為每個(gè)XMEGGA的設(shè)備提提供一個(gè)專用用的頭文件,如果目標(biāo)設(shè)設(shè)備是在項(xiàng)目目設(shè)置中指定定的(假設(shè)一個(gè)目標(biāo)設(shè)備備將 IDE 用于 IAR EEWAVR ),又如果該設(shè)備備文件是包含含在如代碼列表3-1中所示的文件,那么么 IAR 編譯器將自自動(dòng)包含這個(gè)個(gè)正確的頭文文件。Code Liistingg 3-1. IAR heeader file incluusion. 代碼列表3-11 . IARR的頭文件包包含The advvantagge is that if thhe tarrget ddevicee channges, theree is nno neeed to changg

40、e thee sourrce fiiles, only the pprojecct setttingss. 它的優(yōu)點(diǎn)是,如如果目標(biāo)設(shè)備備變了,沒有必要修修改源文件,唯有有以項(xiàng)目設(shè)置置的為準(zhǔn)。3.2 Moddules Regissters3.2. 模塊塊寄存器The IO map iis laiid outt so tthat aall reegisteers foor a ggiven perippherall moduule arre plaaced iin onee conttinuouus memmory bblock. Regiisterss beloongingg to ddiffe

41、rrent mmodulees aree not mixedd. Thiis makkes itt posssible to orrganizze alll periipheraal moddules in C struccts, wwhere the aaddresss of the sstructt defiines tthe baase adddresss, of the mmodulee. Alll regiisterss beloongingg to aa moduule arre eleementss in tthe moodule strucct. IO 地圖的設(shè)設(shè)計(jì)是使一個(gè)給定

42、的的外設(shè)模塊的的所有的寄存存器都放置在在一個(gè)連續(xù)的的內(nèi)存塊,屬于不同模模塊的寄存器器不得混合。這使使得IO 地圖可以將所有外設(shè)模模塊組織到 C 語言結(jié)構(gòu)中,其結(jié)構(gòu)的地址址定義模塊的的基礎(chǔ)地址,同屬于一個(gè)模模塊的所有的的寄存器都是是這個(gè)模塊結(jié)結(jié)構(gòu)中的成員員。An exammple iis thee Proggrammaable MMulti-levell Inteerruptt Conttrolleer (PMMIC) mmodulee. Thee struuct deeclaraation for tthis mmodulee is sshown in Coode Liistingg 3-2

43、and aan exaample of itts usee in CCode LListinng 3-33. Notte thaat thee exammple iin Codde Lissting 3-3 aassumees thaat theere iss an iinstannce off the PMIC_t typpe nammed PMMIC. TThis iis covvered laterr in tthis ddocumeent. 一個(gè)例子是可編編程的多級(jí)中中斷控制器( PMIC )模塊。申報(bào)此模塊的結(jié)構(gòu)由代碼列表 3-2 給出,其用途的例子由代碼列表 3-3 給出。請(qǐng)注意,

44、在代碼列表 3-3 中的例子是假設(shè)有一個(gè) PMIC_t 類型的實(shí)例被命名為PMIC。這在本文檔后面還有提及。Code Liistingg 3-2. Modulle strruct ddeclarrationn. 代碼列表 3-2. 模塊結(jié)構(gòu)的申申報(bào)Code Liistingg 3-3. Modulle strruct uusage. 代碼列表 3-3. 模塊塊結(jié)構(gòu)的用途途3.2.1 MMulti-word Regissters in Moodule Struccts 3.2.1. 模塊結(jié)構(gòu)中中的多詞寄存器Some reegisteers arre useed in conjuunctioon

45、witth othher reegisteers too reprresentt 16 oor 32 bit vvaluess. As exampple onne couuld loook att the ADC sstructt decllaratiion shhown iin Codde Lissting 3-4. 將一些寄存器與與其他寄存器器一起用來代表16位或32位的值。我們可以舉一個(gè)例子,看看看 ADC 結(jié)構(gòu)申報(bào)的一一個(gè)寄存器,見見代碼列表3-4:Code Liistingg 3-4. ADC sttruct declaaratioon. 代碼列表3-44. ADC 結(jié)構(gòu)申報(bào)In C

46、odee Listting 33-4, tthe ADDC chaannel resullt reggisterrs CH00RES, CH1REES, CHH2RES, CH3RRES annd thee comppare rregistter, CCMP, aare 166-bit valuees. Thhese aare deeclareed usiing thhe WORRDREGIISTER macroo showwn in Code Listiing 3-5. Thhe callibrattion rregistter, CCAL, iis a 332-bitt valuue, d

47、eeclareed usiing thhe DWOORDREGGISTERR showwn in Code Listiing 3-6. 在代碼列表3-4中, ADCC通道的結(jié)果果寄存器CHH0RES , CH1RRES , CH2RRES , CH3RRES和比較較寄存器CMMP都是16位值。這這些寄存器都都是用在代碼列表 3-5 中所示的 WORDRREGISTTER 宏申報(bào)的。校準(zhǔn)準(zhǔn)寄存器 CAL,是一一個(gè)32位值的,它是用在代碼碼列表3-6中所示的 DWORDDREGISSTER 宏來申報(bào)的。Code Liistingg 3-5. WORDRREGISTTER Maacro. 代碼列表

48、3-5. WORDDREGISSTER 宏Code Liistingg 3-6. DWORDDREGISSTER MMacro. 代碼列表3-66. DWOORDREGGISTERR 宏As seenn, thee WORDDREGISSTER mmacro uses “H” andd “L” sufffix ffor thhe higgh andd low bytess resppectivvely. The DDWORDRREGISTTER usses nuumber suffiix to indiccate tthe byyte orrder. Both the 116-bitt and

49、 32-biit reggisterrs cann be aaccesssed inn 16-bbit/322-bit mode, by uusing the rregistter naame wiithoutt sufffix ass showwn in Code Listiing 3-7. 如我們所看到的的, WORDDREGISSTER宏使使用后綴“H”和“L” 分別作為高8位元組和低8位元組, DWORRDREGIISTER使使用數(shù)字后綴綴來表示8位元組的順序。在166/32位模模式中, 都可以使用不不帶后綴的寄存器名訪問問16位和32位寄存器器,見代碼列表3-7。Code Liist

50、ingg 3-7. Accesssing regissters of vaaryingg sizee. 代碼清單3-77 。訪問不不同大小的寄寄存器。Code Liistingg 3-7 showss how the ssinglee bytee regiister CTRLAA is rread, how tthe twwo CH00RESHH:L rregistters aare reead ussing aa 16-bbit opperatiion, aand hoow thee fourr CAL3:0 regissters are rread iin a 332-bitt operr

51、ationn. C ccompillers hhandlee multti-bytte reggisterrs auttomatiicallyy. Notte howwever that in soome caases iit mayy be rrequirred too readd and writee multti-bytte reggisterrs in one aatomicc operrationn to aavoid corruuptionn. In this case, inteerruptts musst be disabbled dduringg the multii-byt

52、ee acceess too makee suree thatt an iinterrrupt sservicce rouutine does not iinterffere wwith tthe muulti-bbyte aaccesss. AVRR1306 incluudes eexamplles onn how atomiic acccess oof reggisterrs is done for tthe XMMEGA TTimer/Countter mooduless. 代碼列表3-77演示了如何何讀取單個(gè)8位元組寄存器器CTRLAA,如何使用一個(gè)個(gè)16位的操作作來讀取兩個(gè) CH0R

53、EES H:L 寄存器,以以及如何在332位的操作中讀取四個(gè) CCAL 33:0 寄存器。 CC 語言編譯器會(huì)會(huì)自動(dòng)處理多多個(gè)8位元組的寄存器。但但是請(qǐng)注意,在在某些情況下下可能要求在在一個(gè)原子操操作中讀寫多個(gè)8位元組的寄存器,以以避免訛誤。假如這樣的的話,在多個(gè)8位元組的訪問問期間必須禁止止中斷,以確確保一個(gè)中斷斷服務(wù)例程不不妨礙多個(gè)8位元組的訪問。 AVR13306中有些些例子是關(guān)于寄存器是如何對(duì)XMEGAA的計(jì)時(shí)器/計(jì)數(shù)器模塊塊進(jìn)行原子訪訪問的。3.3 Moddule AAddressses3.3 模塊訪訪問Definittions of alll perripherral moodul

54、ess are foundd in tthe deevice headeer filles avvailabble foor thee XMEGGA. Thhe adddress for tthe mooduless is sspeciffied iin ANSSI C tto makke it compaatiblee withh mostt avaiilablee C coompileers. CCode LListinng 3-88 showws howw ADC 0 on port A is definned. 在適用于XMEEGA設(shè)備的的頭文件中找找到所有外設(shè)設(shè)模塊的定義義。這些模塊

55、的地地址在ANSSI C 語言標(biāo)準(zhǔn)中作了明確的的規(guī)定,使其其與大多數(shù)可可用的 C 語言編譯器兼兼容。代碼列列表 3-8 顯示了端口口 A 上的 ADC 0 是如何定義的。Code Liistingg 3-8. Perippherall moduule deefinittion. 代碼列表 3-8. 外設(shè)模塊塊的定義Code Liistingg 3-8 showss how the mmodulee insttance definnitionn usess a deereferrencedd poinnter tto thee absoolute addreess inn the memorry

56、, cooincidding wwith tthe moodule instaance bbase aaddresss. Thhe moddule ppointeers arre pree-defiined iin thee XMEGGA heaader ffiles, it iis theereforre nott neceessaryy to aadd thhese ddefiniitionss in tthe soource code 代碼列表3-88顯示模塊實(shí)實(shí)例定義如何何使用一個(gè)被解除引引用的指針指指向內(nèi)存中的的絕對(duì)地址,正正好與模塊實(shí)實(shí)例的基礎(chǔ)地址重合。模塊指針是在在 XMEGGA

57、 的頭文件中中預(yù)先定義的的,因此沒有有必要在源代代碼中添加這這些定義。3.4 Bitt Maskks andd Bit Groupp Maskks3.4 位掩碼碼和位組掩碼碼Registeer bitts cann be mmanipuulatedd usinng pree-defiined mmasks, or aalternnativeely biit possitionns. Biit possitionns aree not recommmendeed forr mostt taskks. Thhe pree-defiined bbit maasks aare eiither rela

58、tted too indiividuaal bitts, caalled a bitt maskk or aa bit groupp, callled aa bit groupp maskk, or groupp maskk for shortt. 寄存器的位可以以使用預(yù)先定定義的掩碼,或另一個(gè)位的位位置,進(jìn)行操作,但大多數(shù)任任務(wù)中并不推薦使用位位置。預(yù)先先定義的位掩掩碼可以與獨(dú)獨(dú)立的位有關(guān),稱之為位掩碼;或與一個(gè)位組有關(guān),稱之為位組掩碼,或簡(jiǎn)稱組掩碼。A bit mmask iis useed botth wheen settting and ccleariing inndividdual

59、bbits. A bitt grouup massk is mainlly useed wheen cleearingg multtiple bits in a bit ggroup. Settting mmultipple biit thaat aree partt of aa bit groupp is ccovereed in sectiion 3.5. 一個(gè)位掩碼是在在設(shè)置和清除除獨(dú)立的位時(shí)使用,一個(gè)位位組掩碼主要是用在清除一個(gè)位位組中的復(fù)合位時(shí)時(shí)使用。在3.5節(jié)中,介紹了了復(fù)合位(作為一個(gè)個(gè)位組的一部分分)的設(shè)置。3.4.1 BBit Maask 3.4.1 位位掩碼Considee

60、r a TTimer Countter Coontroll Regiister D, CTTRLD. The bbit grroups, bit namess, bitt posiitionss and bit mmasks of thhis reegisteer cann be sseen iin Tabble 3-1. 細(xì)心觀察一個(gè)計(jì)計(jì)時(shí)器計(jì)數(shù)器器控制寄存器器D,CTRLDD,在表3-1中我們們可以看到該寄存器的的位組、位名稱、位位置以及位掩碼。Table 33-1. Bit ggroupss, bitt namees, biit possitionns andd bit maskss f

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

評(píng)論

0/150

提交評(píng)論