©MicrochipTechnologyIncorporated.AllRightsReserved.701PICPIC®MCU-IntroductiontoProgramming1701PICPICmicro®MCU–介绍PIC16系列产品的汇编编程HANDS-ON©MicrochipTechnologyIncorporated.AllRightsReserved.701PICPIC®MCU-IntroductiontoProgramming2讲述90分钟休息20分钟2个实验60分钟休息60分钟104个实验2小时休息20分钟日程讲述部分实验部分1实验部分2©MicrochipTechnologyIncorporated.AllRightsReserved.701PICPIC®MCU-IntroductiontoProgramming3架构存储器指令集开发软件(MPLAB®IDE)讲述部分的内容©MicrochipTechnologyIncorporated.AllRightsReserved.701PICPIC®MCU-IntroductiontoProgramming4Labs在MPLABIDE创建项目调试和跳转控制PWMA/D中断演示(时间允许的话)PIC16F877A实现多任务实验和演示部分的内容©MicrochipTechnologyIncorporated.AllRightsReserved.701PICPIC®MCU-IntroductiontoProgramming5架构©MicrochipTechnologyIncorporated.AllRightsReserved.701PICPIC®MCU-IntroductiontoProgramming6MicrochipPIC®MCU金字塔PIC16C5XPIC12C5XXPIC16PIC12FPIC18dsPIC14bits16bits24bits12bits8bitMCU16bitMCU程序存储器宽度数据存储器宽度©MicrochipTechnologyIncorporated.AllRightsReserved.701PICPIC®MCU-IntroductiontoProgramming7PICmicroArchitectureRISC-likeFeatures内部为哈佛结构寄存器文档大多数单指令周期指令流水线操作长字型指令指令数很少指令实现的功能基本不重复PIC单片机之所以有很高的性能是因为其具备如下特性:©MicrochipTechnologyIncorporated.AllRightsReserved.701PICPIC®MCU-IntroductiontoProgramming8PIC单片机架构哈佛结构从同一存储器空间取指令和取操作数据.限制了数据流量程序和数据存储器冯-纽曼结构8-位程序存储器数据存储器8-位12/14/16-位哈佛结构从两个独立的存储空间分别取指令和存取操作数.数据流量增加针对程序区和数据区可以设计不同的数据线宽度©MicrochipTechnologyIncorporated.AllRightsReserved.701PICPIC®MCU-IntroductiontoProgramming9FlushFetch4FetchSUB_1大部分单片机,其取指和执行过程是顺序进行的.指令流水线的引入允许取指和执行可以同步进行.使得指令可以在一个指令周期内执行.程序分支例外(如GOTO,CALL或直接修改PC),这需两个指令周期.Tcy0Tcy1Tcy2Tcy3Tcy4PIC单片机架构指令流水线Execute1Fetch31.MOVLW55hFetch1Execute3Fetch2Execute2Fetch42.MOVWFPORTB3.CALLSUB_14.BSFPORTA,BIT3©MicrochipTechnologyIncorporated.AllRightsReserved.701PICPIC®MCU-IntroductiontoProgramming10PIC单片机架构寄存器文档概念GeneralPurposeRegisters(RAM)OtherSFRsPORTAFSRSTATUSPCLTMR0INDFWRegisterALU数据存储器直接数据地址7操作码714-位字长的指令范例:RAM被看作是一组通用的寄存器.周边模块(I/O)也作为寄存器.所有的指令操作都可针对所有的寄存器.长字指令使得在指令中直接寻址寄存器.©MicrochipTechnologyIncorporated.AllRightsReserved.701PICPIC®MCU-IntroductiontoProgramming11PICmicro架构指令实例PICMCU指令编码为操作码和参数编码用一个字完成OPCODEk立即数类指令kkkkkkk©MicrochipTechnologyIncorporated.AllRightsReserved.701PICPIC®MCU-IntroductiontoProgramming12PICMCU晶振PICMCU4MHz(250ns)1MHz(1us)foscTcy指令率是输入时钟的1/4输入晶振频率叫做fosc一个指令周期占用1Tcy©MicrochipTechnologyIncorporated.AllRightsReserved.701PICPIC®MCU-IntroductiontoProgramming13存储器©MicrochipTechnologyIncorporated.AllRightsReserved.701PICPIC®MCU-IntroductiontoProgramming14PICmicro架构存储器两种类型程序(RAM和EEPROM)组成Pages-页(程序存储器)Banks–组(数据存储器)©MicrochipTechnologyIncorporated.AllRightsReserved.701PICPIC®MCU-IntroductiontoProgramming15PICmicro架构存储器表FLASH程序存储器RAM数据存储器大小(words)GPR大小(bytes)1K64PIC12F675PIC16F877A8K368PICmicroMCU©MicrochipTechnologyIncorporated.AllRightsReserved.701PICPIC®MCU-IntroductiontoProgramming16分页的程序存储器(14bit核)OPCODEkkkkkkkkkkkCALLorGOTOinstruction3bitsforinstruction11bitsfordestination在14-bit核的器件中:11位访问211或2048(2k)空间访问更多的空间,需要更多的位程序存储器的分页提供这些位©MicrochipTechnologyIncorporated.AllRightsReserved.701PICPIC®MCU-IntroductiontoProgramming1714-bit核架构程序存储器程序存储器最大8K字四页,每页2k使用PCLATH4:3访问页复位地址0000h中断地址0004hPage00000h0004h07FFhInterruptResetPage10800h0FFFhPage21000h17FFhPage31800h1FFFhPCLATH4:3=00PCLATH4:3=01PCLATH4:3=10PCLATH4:3=11©MicrochipTechnologyIncorporated.AllRightsReserved.701PICPIC®MCU-IntroductiontoProgramming18PICmicro架构程序存储器:PC绝对寻址OPCODEkkkk14-bitInstructionforcallandgotokkkkkkk用CALL和GOTO指令修改PCkkkkkkkkkxxxkkxxxPCLATHRegister有效的13-bit程序存储器地址11-bitsFromInstruction2-bitsFromPCLATHkkkk©MicrochipTechnologyIncorporated.AllRightsReserved.701PICPIC®MCU-IntroductiontoProgramming19PICmicro架构分页的程序存储器当执行CALLorGOTO指令时,需考虑分页当跳转到不同的页时,修改页位调用页位:–GOTO地址–CALL地址–InstructionPCL,F;e.g.ADDWFPCL,F返回时不需考虑页©MicrochipTechnologyIncorporated.AllRightsReserved.701PICPIC®MCU-IntroductiontoProgramming20PICmicro架构PC相对寻址(14-bitcore)PCH5PCL813-BitProgramCounterPCLATH5InternalDataBus8558高字节写入PCLATH.然后低字节写入PCL,将装入13-bit值到PC.movlwHIGHDelaymovwfPCLATHmovlwLOWDelaymovwfPCLNote:PCHcannotberead©MicrochipTechnologyIncorporated.AllRightsReserved.701PICPIC®MCU-IntroductiontoProgramming21分组的数据存储器(14bitcore)DataModifyingInstruction7位指令编码7位目标地址在14-bit核的器件中:7位访问27或128空间访问更多的空间,需要更多的位程序存储器的分组提供这些位OPCODEfffffff©MicrochipTechnologyIncorporated.AllRightsReserved.701PICPIC®MCU-IntroductiontoProgramming22PICmicro架构数据存储器:直接寻址低7位的数据地址直接在指令中描述高2位在STATUS寄存器中(又称寄存器组号BANK)指向目标寄存器的9位有效地址IRPRP1RP0OPCODETOPDZDCCSTATUS寄存器14位长指令fffffffRP1RP0fffffff7位来自指令2位来自STATUS寄存器©MicrochipTechnologyIncorporated.AllRightsReserved.701PICPIC®MCU-IntroductiontoProgramming23PICmicro架构数据存储器组成分成4个组SFR映射到最顶端32位置通过RP0,1和IRP选择组GPRsSFRsBank0000h01Fh020h07FhGPRsSFRsBank1080h09Fh0A0h0FFh0F0hGPRsSFRsBank2100h11Fh120h17Fh170hGPRsSFRsBank3180h19Fh1A0h1FFh1F0hRP1:0RP1:0RP1:0RP1:0=00=01=10=11©MicrochipTechnologyIncorporated.AllRightsReserved.701PICPIC®MCU-IntroductiontoProgramming24FileAddress(hex)Bank0Unimplementeddatamemorylocation;readas'0'.寄存器图PIC12F675PICmicro架构数据存储器PCLATHPCLATHINTCONINTCONFSRFSRG