PROGRAMMING AND CUSTOMIZING THE PIC MICROCONTROLLER 3rd phần 2 potx

130 441 0
PROGRAMMING AND CUSTOMIZING THE PIC MICROCONTROLLER 3rd phần 2 potx

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

MICROCHIP MPLAB IDE 103 probably make every day with respect to selecting which source code files are to be used in a new version of your application—or trying to go back to a previous version of the code, only to find that it was overwritten—will become a thing of the past. MPLAB helps facilitate version control through its use of projects. Later in this chapter I will spend some time explaining how to set up projects in MPLAB and how they can simplify your application development as well as ensure that you are only working with the code spe- cific to the current version of the application. Microchip MPLAB IDE Although there are a number of companies that have created IDEs and development tools for the PIC microcontroller, I believe that Microchip’s MPLAB IDE is the best, both for beginners as well as experienced developers. MPLAB IDE offers the basic devel- opment functions, including an editor, assembler/linker/builder interface, and a simu- lator, but it also includes more advanced features such as programmer, emulator and debugger interfaces that eliminate the need for learning new tools. MPLAB IDE also provides the facilities for cross-referencing machine code and hard addresses back to the source files to allow the editor to indicate the current line or update the data watch windows. The software is professionally written and you will probably not experience any problems with it, which will make the process of learning the tool and how to develop applications for the PIC microcontroller more efficient. MPLAB is fairly unique because Microchip provides this tool free of charge to download off the web. Not having this type of development tool would make learning the PIC microcontroller much more difficult. MPLAB IDE is a very complete integrated development environment for all the PIC microcontroller families and runs under Microsoft’s Windows operating system. The tool is regularly updated, so you should check Microchip’s web site periodically to see if new versions of the tool are available for download and if there are new chip support, features, or bug fixes that you will require. Along with this tool, Microchip provides a set of forums where you can post questions or help others learn about PIC MCU appli- cations or MPLAB IDE operation. Microchip, like many other vendors, has embraced the Internet and provides software and datasheets on their web site, which you can access to help you develop your PIC microcontroller applications. INSTALLING MPLAB IDE In the previous editions of this book, I included a diskette or CD-ROM with copies of the Microchip development tools (the first edition included MS-DOS command-line tools while the second edition included a copy of the Windows version). The problem with doing this is how quickly the copies became out of date; Microchip continually updates MPLAB IDE with new features and new chip support, which is why I recommend that you download the code from the web and install it directly on your PC. In this section, I will guide you through the process of installing MPLAB IDE, followed by showing Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com 104 SOFTWARE DEVELOPMENT TOOLS you the process of creating a project and installing an introductory C compiler that will give you quite a bit of flexibility in creating your own applications. Before starting the installation process, there are a few things for you to do to ensure a trouble-free installation. First, you must be prepared to spend an hour for the installation—the process is definitely not “fire and forget,” and you will have to be present throughout the download and installation. The installation requires a lot of your PC’s resources, so all other applications, except for folder windows and an Internet browser, should be closed before you start downloading and definitely must be closed when you start the installation process. Finally, some antivirus tools may attempt to block the download; if necessary you should temporarily enable this capability if it is disabled. Downloading and installing MPLAB IDE is a significant operation and one that you should be prepared for to ensure that it goes smoothly. The first step is to go to the Microchip web site (Fig. 3.10) and click on MPLAB® IDE in the Design box. This will bring you to a new page devoted to the MPLAB IDE from which you can review the features of the software and download the application Figure 3.10 Microchip’s web site can be found at www.microchip.com. Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com MICROCHIP MPLAB IDE 105 or datasheets. Scroll down to the bottom of the page (shown in Fig. 3.11) and click on the appropriate Download option. I usually select the Full Zipped Installation rather than the Interim Release to ensure that the software and its features are fully tested. When you are given the option of where to put the .zip file, select a temporary folder such as C:\temp. Once the program has downloaded (depending on your Internet con- nection, this can range from five minutes to over an hour; at the time this was written, the full MPLAB IDE package is greater than 30 megabytes), run an unzip program, and store (extract) the contents into the C:\temp folder. Figure 3.12 shows the .zip file with the extracted contents; the application installation files ready to load MPLAB IDE onto your PC. With the full .zip program downloaded and the MPLAB IDE setup files extracted from it, double-click on MPLAB IDE v#.## Install (where “#.##” is the version number of Figure 3.11 Click on Download Full Zipped Installation to get the most recent fully tested release of MPLAB IDE. Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com 106 SOFTWARE DEVELOPMENT TOOLS MPLAB IDE) and follow the instructions to install the program onto your PC (Fig. 3.13). Before the installation completes, you should have the opportunity to display the readme files for the different functions. I recommend that you do so, to help familiarize your- self with the program and its capabilities. When the installation is complete, you can delete the contents of the C:\temp folder. MPLAB IDE is constantly updated and improved. As I noted at the start of this section, I didn’t feel that it would be helpful to provide you with a copy of the tool on a CD- ROM and I do not believe there is a reason for you to save a copy either—if you have a disk crash or want to migrate to a new PC, you should download a new copy of MPLAB IDE and install it (using the procedures outlined in this section). Similarly, every couple of months or so, you may want to check to see if there is a new version of the program available for download. If there is, you will want to install it to take advantage of the latest changes. You do not have to remove the current copy of MPLAB IDE, the installation program will do that for you. There really is no reason to save the installa- tion file that you have downloaded and unzipped. Figure 3.12 Extract the MPLAB IDE installation files from the downloaded .zip file and store in a temporary folder. Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com MICROCHIP MPLAB IDE 107 Now you should have a desktop icon which you can click to start up MPLAB IDE. The first time you click on the icon, don’t be surprised if it takes up to five minutes for the program to configure itself and do final setups before you can use it. Later program invocations will be much faster. Once the MPLAB IDE desktop is up and running (Fig. 3.14) you are able to create your first application. MPLAB IDE FILE TYPES While MPLAB IDE does a lot to shield you from the various files that are required and produced when you are creating an application, you should be aware of the different files involved in the creation of an application. When you are creating an application, along with the source code you should be aware of the need for include files as well as linker control files. When assembly or compilation is complete, the process will pro- duce a number of files which, along with the .hex file (which is programmed into a PIC microcontroller) are resources for the linker and the debugger or emulator tools. You Figure 3.13 After unzipping the MPLAB File, click on MPLAB IDE v#.## Install and follow the instructions to install MPLAB IDE for a complete installation. Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com 108 SOFTWARE DEVELOPMENT TOOLS do not need to concern yourself with the various MPLAB IDE project management files (such as the files that end in .mcp, .mcs, .mcw, etc.) because these files will change with different versions of MPLAB IDE as well as the project and the tools used to build the application. I realize that when you are starting out with a new device, the number of things to learn is overwhelming; the purpose of reviewing the file types is to help you understand the process and choose the path that will allow you to most efficiently learn how to program the PIC microcontroller. In Fig. 3.15, I have drawn the build process as a large box encompassing the assembler/ compiler and the linker process step as well as showing the files required and produced for the two process steps. For your first application, you should be aware of the process and the purpose of the ten file types shown here. As you become more familiar with the PIC microcontroller, MPLAB IDE and the development tools, you will be able to customize these files to make your application development more sophisticated. The need for the source files should be self-explanatory—the .asm, .bas, and .c files that are created as the program is run through the assembler/compiler are entered into Figure 3.14 Finished installing MPLAB IDE—the desktop is up and running. Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com the MPLAB IDE editor and then passed through the build process. Later in the book, I will comment on how these files should be written and give basic templates for your use, but for now, I want to identify them as files that are the basis of the application. Often, one of the first things I see when new PIC microcontroller programmers have problems with their initial applications is that they have defined the hardware regis- ters they are going to use in their applications. The programmer is mystified why the code doesn’t work or, in some cases, assemble correctly. Almost invariably, the prob- lems with the application are a result of a typo or transposition of a register address or label. To fix the problems, I will tell them to delete the hardware register declara- tions in their source code and use the include directive to load the Microchip or compiler vendor written .inc register definition files into their applications. These files were written to provide the application developer with the addresses of the PIC microcontroller hardware registers, along with some other parameters, in the same format as the documentation. Usually, when the programmer-defined hardware reg- ister declarations are deleted and the .inc file added to the source, the application prob- lems disappear. When working with the MPASM assembler, there is an .inc file for every PIC micro- controller part number in the format: p<I>PIC_MCU_P/N</I>.inc where <I>PIC_MCU_P/N</I> is the PIC microcontroller part number. For example, the include file for the PIC16F84A is p16f84a.inc and the include file for the PIC12C508 is p12c508.inc. This is true for all the PIC microcontroller devices except for the origi- nal, low-end (PIC16C5x) parts. For these devices, the include file is p16c5x.inc. The file below is p12c508.inc, which is relatively small, but has all the elements that you should look for in the include files. MICROCHIP MPLAB IDE 109 Assembler/ Compiler Linker .asm/.c (Source) Files .inc (Include) Files .lib (Library) Files .hex File .cod/.cof Files .o (Object) Files .o (Object) Files .lkr (Linker) Files .lst (Listing) Files .map File .err (Error) Files Figure 3.15 The application build process requires and produces the various files shown here. Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com 110 SOFTWARE DEVELOPMENT TOOLS LIST ; P12C508.INC Standard Header File, Version 1.02 Microchip Technology, Inc. NOLIST ; This header file defines configurations, registers, and other useful bits of ; information for the PIC12C508 microcontroller. These names are taken to match ; the data sheets as closely as possible. ; Note that the processor must be selected before this file is ; included. The processor may be selected the following ways: ; 1. Command line switch: ; C:\ MPASM MYFILE.ASM /P12C508 ; 2. LIST directive in the source file ; LIST P=12C508 ; 3. Processor Type entry in the MPASM full-screen interface ;=============================================================== ; ; Revision History ; ;============================================================== ;Rev: Date: Reason: ;1.02 05/12/97 Correct STATUS and OPTION register bits ;1.01 08/21/96 Removed VCLMP fuse, corrected oscillators ;1.00 04/10/96 Initial Release ;============================================================== ; ; Verify Processor ; ;============================================================== IFNDEF __12C508 MESSG “Processor-header file mismatch. Verify selected processor.” ENDIF ;=============================================================== ; ; Register Definitions ; ;=============================================================== Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com MICROCHIP MPLAB IDE 111 W EQU H’0000’ F EQU H’0001’ ; Register Files ––––––––––––––––––––––––––––––––––––––––––– INDF EQU H’0000’ TMR0 EQU H’0001’ PCL EQU H’0002’ STATUS EQU H’0003’ FSR EQU H’0004’ OSCCAL EQU H’0005’ GPIO EQU H’0006’ ; STATUS Bits –––––––––––––––––––––––––––––––––––––––––––––– GPWUF EQU H’0007’ PA0 EQU H’0005’ NOT_TO EQU H’0004’ NOT_PD EQU H’0003’ Z EQU H’0002’ DC EQU H’0001’ C EQU H’0000’ ; OPTION Bits –––––––––––––––––––––––––––––––––––––––––––––– NOT_GPWU EQU H’0007’ NOT_GPPU EQU H’0006’ T0CS EQU H’0005’ T0SE EQU H’0004’ PSA EQU H’0003’ PS2 EQU H’0002’ PS1 EQU H’0001’ PS0 EQU H’0000’ ;=============================================================== ; ; RAM Definition ; ;=============================================================== __MAXRAM H’1F’ ;=============================================================== ; ; Configuration Bits ; ;=============================================================== Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com 112 SOFTWARE DEVELOPMENT TOOLS _MCLRE_ON EQU H’0FFF’ _MCLRE_OFF EQU H’0FEF’ _CP_ON EQU H’0FF7’ _CP_OFF EQU H’0FFF’ _WDT_ON EQU H’0FFF’ _WDT_OFF EQU H’0FFB’ _LP_OSC EQU H’0FFC’ _XT_OSC EQU H’0FFD’ _IntRC_OSC EQU H’0FFE’ _ExtRC_OSC EQU H’0FFF’ LIST At the start of the file, the PIC microcontroller specified within MPLAB is checked against the file to make sure they match. When MPLAB has a PIC microcontroller selected, the part number label with two underscore characters (__) is defined when the assembler is invoked. For the PIC12C508, this label is __12C508, for the PIC16F84, it is __16F84, and so on. This label can be used in conditionally assembled code to access hardware appropriately instead of having to define multiple source files for dif- ferent devices. Once the PIC microcontroller type is verified, then the hardware regis- ter addresses (under Register Files) are defined. The registers are given the same labels as the Microchip documentation and have their addresses specified with them. Following the hardware register address definitions, the bit definitions for hardware registers that have unique, accessible bits are defined. After the hardware register files are defined, then the file registers are defined. The __MAXRAM and __BADRAM directives are used to indicate the valid addresses for vari- ables. One thing lacking with these directives is that the addresses are not given labels (a label indicating the start of the file registers would be useful) and the registers “shad- owed” across banks are not defined. This information could make application develop- ment somewhat easier and avoid the necessity of looking up the file register address ranges from the data books. Lastly, the configuration fuse bits are defined. When I start working with a new PIC microcontroller, one of the first things I always do is to open up the .inc file and look at the configuration fuses. As I will discuss in other areas of the book, a very common mistake is to forget one of the configuration fuse options, which causes your PIC micro- controller application to not work as expected. I want to make sure I access each con- figuration fuse option, either enabling or disabling it to make sure I don’t have any unexpected problems. The .inc values are defined with the NOLIST parameter specified. This means the actual definitions will not be seen in the listing file, but will show up in the symbol table at the end of the listing file. Compiler include files can provide the same register and address information for an application as the assembly language .inc files and they can also be used to define lan- guage functions and features. When you are working with C, to properly access spe- cific library subroutines and functions there are include files that provide function prototypes, constants, and structures required for proper operation of the language. Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com [...]... Merge and Split Unregistered Version - http://www.simpopdf.com MICROCHIP MPLAB IDE 137 TABLE 3.11 PIC MICROCONTROLLERS SUPPORTED BY PICC-LITE AND ANY PROGRAM SIZE LIMITATIONS PIC MICROCONTROLLER PART NUMBER LIMITATIONS PIC1 2F 629 None PIC1 2F675 None PIC1 6C84 None PIC1 6F 627 2 RAM banks PIC1 6F 627 A 2 RAM banks PIC1 6F684 1 RAM bank, 1K program memory PIC1 6F690 2 RAM banks, 2K program memory PIC1 6F84A None PIC1 6F877... through the various aspects of the source file and explain what the statements are and why you might like to use them The two statement types that are required for an application are the PIC microcontroller instructions and the directives The instructions are the application itself, and the end directive is a command to stop the assembler The only requirement of these two statements is that they cannot... 0009 0009 000A 000B 000C 3003 3EFF 1D03 28 0A 000D 1508 000E 1908 000F 28 0E 0010 0011 00 12 0013 1683 0909 128 3 0086 0014 28 09 00016 00001 000 02 00151 00017 00018 00019 00 020 00 021 00 022 00 023 00 024 00 025 00 026 00 027 00 028 00 029 00030 00031 000 32 00033 00034 00035 00036 00037 00038 00039 00040 00041 000 42 00043 00044 00045 00046 00047 00048 00049 00050 00051 000 52 00053 00054 00055 00056 00057 00058 00059... always available with the PIC part number selected, the current program counter, the editor operating mode, and the WREG and STATUS register contents The other windows are discussed in more detail throughout the book and provide you with the ability to monitor the changes in the registers of the chip as well as change their contents In Fig 3.16, I have arranged the various windows the way that I feel... considered assembly language to be the basic method of PIC microcontroller programming Over time, I have seen the efficiency of high level compilers improve and I would say that the need for understanding and using the assembler has lessened considerably That said, a good basic understanding of the various PIC microcontroller processor architectures and their configuration fuses and other features is necessary... enable These fuses are specified in the source file using the CONFIG directive This directive takes the bit value of its single parameter and stores it at the configuration fuse register address For the mid-range devices, this is address 0x 020 07 So the statement: CONFIG 0x0 123 4 stores the value 0x0 123 4 at address 0x 020 07 This statement is equivalent to: org dw 0x 020 07 0x0 123 4 The fuse values and states... None PIC1 6F877 2 RAM banks, 2K program memory PIC1 6F877A 2 RAM banks, 2K program memory PIC1 6F887 2 RAM banks, 2K program memory PIC1 6F917 2 RAM banks, 2K program memory microEngineering Labs’ PICBASIC For something less than C18 or PICC, microEngineering Labs’ (also known as melabs) PICBASIC PRO compiler is an excellent tool for new developers to become introduced to PIC microcontrollers The language... number in the source file listed With this information, instructions Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com MICROCHIP MPLAB IDE 117 can be found either by their address within the PIC microcontroller s program memory space or by the line they are found on in the source code The next section in the listing file is a list of all the labels in the application and what their... to the starting address (the parameter of the CBLOCK statement) is incremented by the number of bytes of the variables For the example above, i is at address 0x 020 , j is at address 0x 021 , and k is at address 0x 023 Accessing multibyte variables is accomplished by creating small structures using the CBLOCK directive and using the offsets of the structure elements to access the different bytes of the. .. own PIC microcontroller applications MPLAB IDE APPLICATION BUILD TOOLS The build tools (assembler, compilers, and linker) that I discuss in this book are probably the most popular tools available for the PIC microcontrollers and each of them integrate well with MPLAB IDE The nuts and bolts of this integration were discussed earlier in the chapter with the discussion of the files used or produced in the . For example, the include file for the PIC1 6F84A is p16f84a.inc and the include file for the PIC1 2C508 is p12c508.inc. This is true for all the PIC microcontroller devices except for the origi- nal,. found either by their address within the PIC microcontroller s program memory space or by the line they are found on in the source code. The next section in the listing file is a list of all the. microcontroller instructions and the directives. The instructions are the application itself, and the end directive is a command to stop the assembler. The only requirement of these two state- ments is that they cannot

Ngày đăng: 12/08/2014, 23:21

Từ khóa liên quan

Mục lục

  • Contents

  • Introduction

  • Acknowledgments

  • Chapter 1 Embedded Microcontrollers

    • Microcontroller Types

    • Internal Hardware

    • Applications

    • Processor Architectures

    • Instructions and Software

    • Peripheral Functions

    • Memory Types

    • Microcontroller Communication

    • Device Packaging

    • Application Development Tools

    • Chapter 2 The Microchip PIC Microcontroller

      • Accessing the Microchip Web Site

      • PIC Microcontroller Feature Summary

      • Features Unique to the PIC Microcontroller

      • PIC Microcontroller Families

      • Chapter 3 Software Development Tools

        • Tools Overview

        • High Level Languages

        • Microchip MPLAB IDE

Tài liệu cùng người dùng

  • Đang cập nhật ...

Tài liệu liên quan