AVR1000XMEGA的C语言代码编写入门_第1页
AVR1000XMEGA的C语言代码编写入门_第2页
AVR1000XMEGA的C语言代码编写入门_第3页
AVR1000XMEGA的C语言代码编写入门_第4页
AVR1000XMEGA的C语言代码编写入门_第5页
已阅读5页,还剩29页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、 8-bit 8位Microcoontrolllers 微处理器Applicaation Note 应用注释 AVR10000: Geettingg Starrted WWritinng C-ccode ffor XMMEGAAVR10000 : XMEGGA的C语言代码编写写入门 Featurres 特点: 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. 由于电子产品的的开发时间短,质量要求高高,因此需要要高层次的编编程语言。最最主要的原因因是,高级语语言具有更好的可可移植性和可可读性,使其其更易于维护和重复使使用代码。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. 编程语言的选择择本身并非是能确保具有较高的可可读性和可重复使用性的唯一条条件,还必须有良好好的编码风格格。因此 XMEGAA 的外围设备备,头文件和和驱动程序的的设计都是基基于这方面考考虑的。 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 微控控制器使用的的最广泛的高高级语言就是 C 语言,因此本本应用注释的的重点是 C 语言编程。为为了支持大多多数可用的 AVR CC 语言编译器,我们尽可能把代码示例编写写在 ANSI C 语言的规范中。有些些例子是 IAR EEmbeddded Woo

7、rkbennch 专用的,但其思路和方法法可用于与其他变化不大的编译器器。 IAR 专用的示例都有清晰的标标示。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. 一个 AVR XMEGAA 由数个结构块组成:一个AVRR CPU 芯片,一个 SRAAM,一个闪存, 一个 EEPROOM 以及若干外设设模块。这些些结构块被称为为“模块类型”。XMEGAA可以有一个个或多个给定定模块类型的的实例。一个模块类型型的所有实例例都具有相同同的特性和功功能。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. 有些模块类型可可以是其他模模块类型的子集。这些(模块块类型)继承承了特大模块块类型的某个子集的特征征(和寄存器器),其继承的所有有特征都完全兼兼容。这适用用于比如计时器和和IO端口。用于计计时器的模块块类型的子集集可能意味着着它所能得到到的比较和捕获通通道比完整的的计时器模块块的要少。同样,一一个 IO 端口的管脚可能不足八个。

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. 一个模块类型可可以是“USARTT”的,如果这个个模块实例比如是“USARTTC0”,其中的“C00”后缀表示这个个实例是“端口 C 上的 USARTT 编号为 0”。为了简单起起见,一个模块实实例将被称为为这整个文件中的一个模块,除除非是有必要要另作区分。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. 每个模块都有若若干寄存器,这这些寄存器都都包含控制位位或状态位。一一个给定类型型的所有模块块都包含相同同的寄存器集集合(或子集集),并且所有这些些寄存器包含含相同的控制制位和状态位位的集合(或子子集) 。Figure 2-1. Modulle typpes, iinstannces, regissters and bbits. 图2

14、-1. 模模块类型,实实例,寄存器器和位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 内存存图里,每个模模块都有一个个固定的基础地址,并且该模块中的

16、所所有寄存器都都有固定的偏移移地址(这是是相对于模块块的基地址而而言)。如此这样,每每个寄存器不不仅会在 IO 内存空间有一一个绝对地址址,而且还有有一个根据其其偏移量定义义的相对地址。一一个模块类型型的所有实例例中的寄存器偏偏移地址都是是平等的,这这样就简化了编写能能够适用于某某个特定类型型的所有模块块的驱动程序序的任务。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. 大致说来,寄存存器有控制寄寄存器、状态寄存器器和数据寄存存器之分,这从寄存器器的命名就可可看出。模块的一个通用的的控制寄存器器被命名为CCTRL。如如果在一个模模块中存在多多个通用控制制寄存器,他他们都有一个后缀缀字符。在这这种情况下,控控制寄存器将将被命名为CCTRLA 、 CTRLLB 、 CTRLLC等。这方法也适用于于状态寄存器器。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. 对于具有特定功功能的的寄存器,它们们的命名会反反映其功能性。例如,一一个控制寄存存器,控制一一个模块的中中断级别,它它就被命名为为 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 的数据总线宽宽度为 8 位,较大的寄存器器得使用几个 8 位寄存器才能能生效。对于于一个16位寄存器器,要访问其高8位元组和低8位元组,得在在这个寄存器器的命名上分别别附加 “H” 和 “L”。例如,166位计时器/计数器的计计数寄存器被被命名为 CNT ,其两个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).对于大于16位位的寄存器,其8位元组的编号是从最低有效的8位元组开始。例如,32位的 ADC 校准寄存器被命名为 CAL,其四个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. 大多数 C 语语言编译器都提供了自动动处理多8位元组寄存器器的访问。在在这种情

24、况下下, CNTT 的命名没有加“ H”或“ L”后缀缀的,可以用用来执行166位的计时器/计数器的计计数寄存器的的访问。这方方法也同样使用于于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: 寄存器位可以有有一个单独的的函数,或是一个位组的的一部分,有有一个共同的的功能:一个个单独的位可可以是启用一个模块块的位,如 USARTT ENABLLE 位。一个位位组可以由两两个或两个以以上的位组成,这些些位共同选择了它们所所属的模块的特殊配置。一个个位组提供多多达 2n 个选项,其中

27、的 n 是位组中位的数量。这这两个位控制制的 USARTT 接收结束中中断级别,RXINTTLVL 1:0 ,是一个个位组的一个个例子。这两两个位提供以以下选项:Table 22-1. RXINTTLVL bbits aand coorresppondinng intterruppt levvel seelectiion. 表 2-1. RXINTTLVL 位和相应的的中断级别选选项RXINTLVVL1RXINTLVVL0Interruupt leevel sselecttion 中中断级别选项项00Interruupt Offf 中断关闭01Low levvel innterruupt 低级

28、中断1 10Medium levell inteerruptt 中级中断11High leevel iinterrrupt 高级中断 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.作为一个组的一一部分的位,将始终有有一个数字后后缀。不是一个位组组的一部分的的位,永远不会会有一个数字字后缀。计时时器/计数器控制制寄存器 D 有两个位组: EVACCT 和 EVSELL 。在这些些组中的位都有一个数字字后缀,而 EVDLYY 位不是一个个位组的一部部分,因此它它没有数字后缀。Table 22

30、-2. Bits groupps andd bit namess for bits in Tiimer/CCounteer Conntrol regisster DD CTTRLD.表 2-2. 适用于计时时器/计数器控制制寄存器 D - CCTRLD 中的位的位组和位名称Bit Grooup 位组EVACT-EVSELBit namme 位名EVACT2 EVACT1EVACT0EVDLYEVSEL3EVSEL2EVSEL1EVSEL0Bit nummber 位编号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. 以下各节

32、重点放放在如何编写写 XMEGAA 的 C 语言代码。示示例显示了如如何使代码具具有很强的可可读性,并能能在不同的XMEEGA 器件之间移移植。这些例子也可可以作为指导导编写代码的准则,使其易于验证和维维护。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的模模块位于内存存空间的专用的连续的块区域中,我们可以把这些模模块看作是被被封装的单元。这反映了了编写 C 语言代代码时访问模块的路径:模块是用 C 语言结构封装的,其中包容了了所有的模块寄存器器。图3-11给出了这个图解。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. 请注意,某些寄寄存器没有直直接的模块相相联,这些寄存器都不是是封装在结构里的,因为该结构是是用来使寄存器与某个个模块相关联的。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. 模块结构为较大大的代码项目目提供的优势势不仅具有可读性,而且且还因为编译器器可以重复使使用模块驱动动程序,从而而使代码非常常紧凑。这在在后面会作更详细的描述。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 编程老手过去去常用的方法法,但是有一点点应该知道:访问寄存器器的“经典”方法仍然是要得到到头文件的支持。这方法法也同样适用于对对位级别的访访问。Figure 3-1. Modulles pllaced in deedicatted bllocks in IOO memoory sppace. 图3-1 .放放置在 IO 内存空间里的的专用块区的模块。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. 为每个XMEGGA的设备提提供一个专用用的头文件,如果目标设设备是在项目目设置中指定定的(假设一个目标设备备将 IDE 用于 IAR EEWAVR ),又如果该设备备文件是包含含在如代码列表3-1中所示的文件,那么么 IAR 编译器将自自动包含这个个正确的头文文件。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. 它的优点是,如如果目标设备备变了,没有必要修修改源文件,唯有有以项目设置置的为准。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 地图的设设计是使一个给定

42、的的外设模块的的所有的寄存存器都放置在在一个连续的的内存块,属于不同模模块的寄存器器不得混合。这使使得IO 地图可以将所有外设模模块组织到 C 语言结构中,其结构的地址址定义模块的的基础地址,同属于一个模模块的所有的的寄存器都是是这个模块结结构中的成员员。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. 一个例子是可编编程的多级中中断控制器( PMIC )模块。申报此模块的结构由代码列表 3-2 给出,其用途的例子由代码列表 3-3 给出。请注意,

44、在代码列表 3-3 中的例子是假设有一个 PMIC_t 类型的实例被命名为PMIC。这在本文档后面还有提及。Code Liistingg 3-2. Modulle strruct ddeclarrationn. 代码列表 3-2. 模块结构的申申报Code Liistingg 3-3. Modulle strruct uusage. 代码列表 3-3. 模块块结构的用途途3.2.1 MMulti-word Regissters in Moodule Struccts 3.2.1. 模块结构中中的多词寄存器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位的值。我们可以举一个例子,看看看 ADC 结构申报的一一个寄存器,见见代码列表3-4:Code Liistingg 3-4. ADC sttruct declaaratioon. 代码列表3-44. ADC 结构申报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通道的结果果寄存器CHH0RES , CH1RRES , CH2RRES , CH3RRES和比较较寄存器CMMP都是16位值。这这些寄存器都都是用在代码列表 3-5 中所示的 WORDRREGISTTER 宏申报的。校准准寄存器 CAL,是一一个32位值的,它是用在代码码列表3-6中所示的 DWORDDREGISSTER 宏来申报的。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使使用数字后缀缀来表示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演示了如何何读取单个8位元组寄存器器CTRLAA,如何使用一个个16位的操作作来读取两个 CH0R

53、EES H:L 寄存器,以以及如何在332位的操作中读取四个 CCAL 33:0 寄存器。 CC 语言编译器会会自动处理多多个8位元组的寄存器。但但是请注意,在在某些情况下下可能要求在在一个原子操操作中读写多个8位元组的寄存器,以以避免讹误。假如这样的的话,在多个8位元组的访问问期间必须禁止止中断,以确确保一个中断断服务例程不不妨碍多个8位元组的访问。 AVR13306中有些些例子是关于寄存器是如何对XMEGAA的计时器/计数器模块块进行原子访访问的。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设备的的头文件中找找到所有外设设模块的定义义。这些模块

55、的地地址在ANSSI C 语言标准中作了明确的的规定,使其其与大多数可可用的 C 语言编译器兼兼容。代码列列表 3-8 显示了端口口 A 上的 ADC 0 是如何定义的。Code Liistingg 3-8. Perippherall moduule deefinittion. 代码列表 3-8. 外设模块块的定义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显示模块实实例定义如何何使用一个被解除引引用的指针指指向内存中的的绝对地址,正正好与模块实实例的基础地址重合。模块指针是在在 XMEGGA

57、 的头文件中中预先定义的的,因此没有有必要在源代代码中添加这这些定义。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. 寄存器的位可以以使用预先定定义的掩码,或另一个位的位位置,进行操作,但大多数任任务中并不推荐使用位位置。预先先定义的位掩掩码可以与独独立的位有关,称之为位掩码;或与一个位组有关,称之为位组掩码,或简称组掩码。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. 一个位掩码是在在设置和清除除独立的位时使用,一个位位组掩码主要是用在清除一个位位组中的复合位时时使用。在3.5节中,介绍了了复合位(作为一个个位组的一部分分)的设置。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. 细心观察一个计计时器计数器器控制寄存器器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等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论