PIC Program Development:Program Editing

Program Editing

The program is written using the instruction set of the processor selected, as specified in the MCU data sheet. The instruction set is essentially the same for all 16 series PIC chips. The source code, that is, the assembly code program, must be entered into a suitable text editor, which is normally provided with the development system. We will not go into the details of using a text editor, as it is assumed that the reader is familiar with using a word processor.

The MPLAB programming window has limited editing features, because it is only used for creating plain text files. The typeface Courier is used because each character occupies the same space, unlike proportionally spaced typefaces such as Arial and Times Roman. Displayed in this way, the text lines up vertically as well as horizontally, so the program can be laid out consistently in columns using tab stops, making it easier to understand. The tab spacing should be set to eight characters for the programs in this book. When printing, also use Courier to maintain the correct layout.

When a new application is started, a separate folder should be created to contain the source code file, and all the other files that will be generated by the assembler. Name the folder with the application name, e.g. BIN3. When the source code file has been opened, enter the source code filename (e.g. BIN3.ASM) at the top of the file, and immediately save it in the folder. This ensures that the required filepath is checked for correct operation before any further source text is entered. Always keep a backup copy of your file on a different drive before quitting the development system.

Instruction Set

The PIC 16 data sheets and the mid-range manual show the instruction set arranged by byte, bit, literal and control operations. Table 4.2 shows the same instruction set organized by function with an example given with each instruction so that the typical syntax can be seen. A detailed description of each instruction is provided in the data sheet, with more information in Chapter 6.

The grouping of the instructions in Table 4.2 reflects the different types of instruction explained in Chapter 2: data movement, single and register pair arithmetic and logical operations, sequence control and miscellaneous instructions. In the example instructions, the register used is always 0C, the first general purpose register in the smaller PIC 16 chips. The register being operated upon will normally be referred to by label (see Program BIN3 below). Literals and register bit numbers may be referred to by number or label, depending on the context.

BIN3 Source Code

Program BIN3 uses the same instructions as BIN2 (Chapter 3), with additional statements to read the switches and control the output. Program 4.1 is the result.

First, note the general layout and punctuation. The program header block contains as much information as is necessary at this stage. These comments are preceded by a semicolon on each line to indicate to the assembler that this text is not part of the program. Assembler

PIC Microcontrollers-1157

PIC Microcontrollers-1158

directives such as EQU and END are also not part of the program proper, but used to define labels and the end of the program source code. The labels ‘porta’, ‘portb’ and ‘timer’ refer to file registers 05, 06 and 0C, respectively; ‘inres’ and ‘inrun’ are input bit labels representing the push buttons. The program uses ‘Bit Test and Skip’ instructions followed by ‘GOTO label’ for conditional jumping.

At this stage, the learner can type the source code into the editor without full analysis in order to practice use of the editor. The instructions are placed in the first three columns, and the comments can be left out to save time. Labels go in the first column, instruction mnemonics in the second and the instruction operands in the third. The source code text file should be saved as BIN3.ASM in a suitably named directory or folder on disk. Alternatively, the source code file can be downloaded from the support website www.picmicros.org.uk. It can then be tested in the free MPLAB simulator (see Section 4.7) or in Proteus VSM (see Appendix E).

Syntax

‘Syntax’ refers to the way that words are put together to create meaningful statements, or a series of statements. In programming, the syntax rules are determined by the assembler,

which will be used to create the machine code. The assembler must be provided with source code that it can convert into the required machine code without any ambiguity, that is, only one meaning is possible. This is why the assembler syntax rules are very strict.

Layout

The program layout should be in four columns, as described in Table 4.3. Each character then occupies the same space, and the columns are correctly aligned. The label, command and operand columns are set to a width of eight characters, with the maximum label length of six characters, leaving a minimum of two clear spaces between columns (longer labels can be used, but a different form of the program layout must then be used). The tab key is normally used to place the text in columns, and the tab spacing can be adjusted if necessary.

PIC Microcontrollers-1159

a semicolon on any line of code helps the programmer and user to understand the program. It has no effect on the operation of the program. Full line comments may also be used between program blocks

Comments

Comments are not part of the actual program, but are included to help the programmer and user understand how the program works. Comments are preceded by a semicolon (;),

which can be placed at the beginning of a line to indicate a comment which relates to a whole program block (functional set of statements), or at the start of column 4 for line comment. The comment and line are terminated with a line return (‘Enter’ key).

A standard header block is recommended (see Program 4.1). For simple programs, the first line should at least contain the source code file name, the author and date, and/or version number. A program description should also be provided in the header, and for programs that are more complex, the processor type, target hardware details and other relevant information. In general, the bigger the program, the more information would be expected in the header comments.

Leave a comment

Your email address will not be published. Required fields are marked *