Program Development
The primary development system toolset for PIC applications is Microchip MPLAB IDE (integrated development environment). At the time of writing, MPLAB version 8.60 is the most recently released version. The basic principles of use do not change greatly as it is updated, rather extra facilities and support for an ever-expanding range of devices are added, and the reader will need to refer to the manufacturer’s documentation for details concerning the use of any particular version. The intention at this stage is to outline how to assemble and test demonstration programs BIN3 and BIN4. More advice on debugging programs is provided in Chapter 9.
The flowchart in Figure 4.1 gives an overview of the program development process.
The starting point is the specification for the program, which describes how the application will function when complete. This must then be analyzed by the software designer so that the required program can be derived from it, taking into account the features of the instruction set of the microcontroller (MCU). The program algorithm describes the process whereby the required outputs are obtained from the given inputs. Various software design techniques are available to outline the program, including flowcharts and pseudocode, which we will use here. These represent the program processes and their sequence in
The source code is developed from the program algorithm by filling in the details and converting each program block to assembler code. The program must be saved regularly as it is developed; it is a good idea to always have backup copies on different disks (memory stick, hard disk or network drive) in case of disk, memory or network failure. The source code text file is called PROGNAME.ASM, where PROGNAME represents the application name, such as BIN1. Successive versions of a program can be numbered BIN1, BIN2, etc., so that we can revert to an earlier version if new code does not work properly.
After initial text entry, the program can be assembled by calling up the assembler utility, MPASMWIN.EXE. It converts the source code into machine code, and creates additional files to help with debugging (fault finding) the program. If a mistake has been made in the individual instruction (e.g. misspelling a mnemonic), it will be reported in an error message window and an entry added to the error file on disk. This must then be corrected in the source code and the program reassembled until it is free of syntax errors.
The program can then be tested for correct operation by simulation, using the Microchip MPLAB simulator MPSIM or Proteus VSM (ISIS). This means running the program in the host computer as though it were running in the chip itself. The program is loaded and executed on screen, and the outputs are observed. It can then be checked step by step for the correct logical operation, by monitoring the changes in the registers, and checking the timing if necessary. Simulated inputs are needed to represent all the likely input sequences and combinations. If a logical error is found, the source code must be re-edited and reassembled, and the simulation repeated.
This process is simpler in ISIS, since inputs can be generated interactively on the schematic (see Figure 3.3) from animated manual inputs or simulated signals. Outputs can be observed directly on light-emitting diodes (LEDs), seven-segment displays or liquid crystal displays (LCDs), or on virtual instruments such as oscilloscopes or logic analyzers. However, MPLAB has the advantage that inputs and outputs can be generated from a predefined stimulus file and outputs recorded in a trace file, which provides a form of automated testing using standard sequences and a permanent record of the test results. Thus, MPSIM has a more extensive range of debugging tools, but Proteus can test the whole circuit and is more intuitive.
When the logical errors have been removed, the program can be downloaded to the chip. Final testing can then allow the finished circuit function to be compared with the original specification. If necessary, in-circuit debugging can be used to detect any remaining errors that may arise from interaction with the real peripheral circuits. Most current PIC chips have
on-board circuitry to support this option, while smaller chips need a dedicated header connector (see Chapter 7). Only MPSIM provides this final debugging stage.
The main software tools and the files created and used by MPLAB during the development process are listed in Table 4.1. The most significant ones are the source code (.ASM) and
machine code (.HEX) for simulation and downloading. The error file (.ERR) stores the assembler error messages that are displayed automatically when generated. The list file (.LST) contains the source text, machine code, memory allocation and label values in one text file. In more complex applications, multiple relocatable machine code object files (.O) that have been assembled separately as application components are combined by a linker utility to produce a COF file which contains the hex and list files (see Microchip ‘MPASM User’s Guide’).