8-bit8位Microcontrollers微处理器ApplicationNote应用注释AVR1000:GettingStartedWritingC-codeforXMEGAAVR1000:XMEGA的C语言代码编写入门Features特点:•Namingconventions命名约定-Registernames寄存器名-Bitnames位名•C-codenamesC-代码名-Bitandgroupmasks位与组掩码-Groupconfigurationmasks组配置掩码•Methodsforaccessingregisters访问寄存器的方法•Methodsforwritingreusablemodulefunctions可多次使用模块功能的写入方法1Introduction引言Shortdevelopmenttimesandhighqualityrequirementsonelectronicproductshasmadehigh-levelprogramminglanguagesarequirement.ThemainreasonisthatHighlevellanguagesmakeiteasiertomaintainandreusecodeduetobetterportabilityandreadability.由于电子产品的开发时间短,质量要求高,因此需要高层次的编程语言。最主要的原因是,高级语言具有更好的可移植性和可读性,使其更易于维护和重复使用代码。Thechoiceofprogramminglanguagealonedoesnotensurehighreadabilityandreusability;goodcodingstyledoes.ThereforetheXMEGA™peripherals,headerfilesanddriversaredesignedwiththisinmind.编程语言的选择本身并非是能确保具有较高的可读性和可重复使用性的唯一条件,还必须有良好的编码风格。因此XMEGA™的外围设备,头文件和驱动程序的设计都是基于这方面考虑的。Themostwidelyusedhigh-levellanguageforAVR®microcontrollersisC,andthisapplicationnotethereforefocusesonCprogramming.TosupportmostoftheAVRCcompilersthatareavailable,thecodeexamplesareasfaraspossiblewritteninANSIC.AfewexamplesarespecifictoIAREmbeddedWorkbench®,buttheideasandmethodscanbeusedforothercompilerswithminorchanges.IARspecificexamplesareclearlymarked.AVR®微控制器使用的最广泛的高级语言就是C语言,因此本应用注释的重点是C语言编程。为了支持大多数可用的AVRC语言编译器,我们尽可能把代码示例编写在ANSIC语言的规范中。有些例子是IAREmbeddedWorkbench®专用的,但其思路和方法可用于与其他变化不大的编译器。IAR专用的示例都有清晰的标示。2XMEGAModulesXMEGA模块AnAVRXMEGAiscomposedofseveralbuildingblocks:AnAVRCPUcore,SRAM,Flash,EEPROMandanumberofperipheralmodules.Thesebuildingblocksarecalled“moduletypes”.AnXMEGAcanhaveoneormoreinstancesofagivenmoduletype.Allinstancesofamoduletypehavethesamefeaturesandfunctions.一个AVRXMEGA由数个结构块组成:一个AVRCPU芯片,一个SRAM,一个闪存,一个EEPROM以及若干外设模块。这些结构块被称为“模块类型”。XMEGA可以有一个或多个给定模块类型的实例。一个模块类型的所有实例都具有相同的特性和功能。Somemoduletypescanbeasubsetofothermoduletypes.Theseinheritasubsetofthefeatures(andregisters)ofthesupertype,allinheritedfeaturesarefullycompatible.Thisappliestoe.g.timersandIOports.Thesubsetofamoduletypecanforatimermeanthatithasfewercompareandcapturechannelsthanafulltimermodule.Similarly,anIOportmayhavelessthaneightpins.有些模块类型可以是其他模块类型的子集。这些(模块类型)继承了特大模块类型的某个子集的特征(和寄存器),其继承的所有特征都完全兼容。这适用于比如计时器和IO端口。用于计时器的模块类型的子集可能意味着它所能得到的比较和捕获通道比完整的计时器模块的要少。同样,一个IO端口的管脚可能不足八个。Amoduletypecanbea“USART”,whilethemoduleinstanceise.g.“USARTC0”,wherethe“C0”suffixindicatestheinstanceis“USARTnumber0onportC”.Forsimplicity,amoduleinstancewillbereferredtoasamodulethroughoutthisdocument,unlessthereisaneedtodifferentiate.一个模块类型可以是“USART”的,如果这个模块实例比如是“USARTC0”,其中的“C0”后缀表示这个实例是“端口C上的USART编号为0”。为了简单起见,一个模块实例将被称为这整个文件中的一个模块,除非是有必要另作区分。Eachmodulehasanumberofregi...