Page 227 - Mechatronics with Experiments
P. 227

MICROCONTROLLERS  213
                             linker, and debugger. The C18 compiler supports mixing assembly language and C language
                             instructions in the same file. A block of assembly code included in a C source file must be
                             labeled by

                             _asm

                             ....
                             _endasm
                             The basic sequences for programming the PIC, using a PC as the development tool, are as
                             follows:

                                1. On the PC, create a new project in the MPLAB IDE environment. Setup the project
                                  environment by selecting the target PIC microcontroller, specify directories to access
                                  libraries, add source codes to the project, and specify the project name. For each
                                  project, the following configurations must be made in the MPLAB IDE environment:
                                  (a) Create a project: MPLAB IDE > Project> New.
                                  (b) Select the target PIC microcontroller:  MPLAB IDE > Configure >
                                     Select Device: PIC18F452.
                                  (c) Configure project options:  MPLAB IDE > Project > Select Lan-
                                     guage Toolsuite: Microchip C18 Toolsuite.
                                  (d) Configure project options: MPLAB IDE > Project > Set Language
                                     Tool Locations: MPLAB C18 C Compiler and,
                                     ....... >Set Language Tool Locations: MPLINK Object
                                     Linker.
                                  (e) Configure  project  build  options:  MPLAB IDE > Project > Build
                                     Options...>Project, then setup the options in the window under differ-
                                     ent tabbed pages (options: General, Assembler, Compiler, Linker options).
                                   (f) Add source files to the project: in the project window, right click on “Source
                                     Files” and select “Add Files.”
                                  (g) Select a script file for the linker: in the project window, right click on “Linker
                                     Scripts,” and select ”Add Files,” and then select file “18f452.lkr” from the “lkr”
                                     directory.
                                2. Write the program source code in C language, using the built-in editor or any ASCII
                                  text editor, and save it as filename.c. Add other relevant files to your project.
                                  MPLAB IDE > File > New and MPLAB IDE > File > Save.
                                3. Build (compile and link) the project in the MPLAB IDE environment. This converts
                                  the high-level C code to the corresponding hex files which contain the binary coded
                                  machine instructions: MPLAB IDE > Project > Build All.
                                4. First debug the program using the software simulator (SIM, provided as part of
                                  MPLAB IDE) for the PIC chip on the PC. This is a non-real-time simulation of the
                                  PIC chip. Once the program is debugged using the non-real-time simulator (MPLAB
                                  SIM), then it can be transferred to the PIC microcontroller. If programming in C
                                  language, the program must be re-built using a different linker script file (18f452i.lkr
                                  file for building a program to run on the actual PIC 18F452 chip, instead of 18f452.lkr
                                  which is used for debugging the program on the PC). Then, transfer the program from
                                  the PC to the PIC board through the MPLAB IDE environment and communication
                                  cable (MPLAB ICD 3):
                                  MPLAB IDE > Programmer > Select Programmer and
                                  ...........>Settings and
                                  MPLAB > Debugger > Connect.
   222   223   224   225   226   227   228   229   230   231   232