Specifications
mikroElektronika | Free Online Book | PIC Microcontrollers | Appendix A: Programming a Microcontroller
enough to use command "goto Main" in order to direct the microcontroller to the beginning of the program. The next
command selects memory bank 1 in order to enable access to the TRISB register to configure port B as output (banksel
TRISB). The main program ends by selecting memory bank 0 and setting all port B pins to logic one (1)(movlw 0xFF,
movwf PORTB).
It is necessary to create a loop to keep program from "getting lost" in case an error occurs. For this purpose, there is an
endless loop executed all the time while the microcontroller is switched on.
"end" is required at the end of every program to inform the assembler that there are no more commands to be compiled.
DATA FILES RESULTING FROM PROGRAM COMPILING
The result of compiling a program written in assembly language are data files. The most important and most commonly
used data files are:
● Executive data file (Program_Name.HEX);
● Error data file (Program_Name.ERR); and
● List data file (Program_Name.LST).
The first file contains compiled program which is loaded into the microcontroller. Its contents give no information of
importance to the programmer so it will not be discussed here. The second file contains errors made in writing process and
detected by the compiler during compiling process. Errors can be detected in list data file, which takes more time, so the
error data file is more suitable for long programs.
The third file is the most useful for the programmer. It contains lots of information on commands and variables locations in
on-chip memory as well as error signalization. There is a symbol table at the end of each data file list containing all the
symbols used in a program. Other useful elements of list data file are memory usage maps and error statistics provided at
the very end of the file list.
MACROS AND SUBROUTINES
The same sequence of computing instructions is usually used repeatedly within a program. Assembly language is very
demanding. The programmer is required to take care of the last little detail when writing a program, because only one
wrong command or label name may cause the program to not work properly or it may not work at all. Therefore, it is less
tedious and less error-prone to use a sequence of instructions as a single program statement which works properly for sure.
To implement this idea, macros and subroutines are used.
MACROS
A macro contains programmer-defined symbols that stand for a sequence of text lines. It is defined by using directive
macro which names macro and arguments if needed. Macro must be defined prior it is used. Once a macro has been
defined, its name may be used in the program.When the assembler encounters macro’s name, it replaces it by the
appropriate sequence of instructions and processes them just as though they have appeared in the program. Many
different macro-instructions are available for various purposes, eliminating some of the repetitiveness of the
programming, as well as simplifying the writing, reading and understanding of the program. The simplest use of macros
may be giving a name to an instruction sequence being repeated. Let us take, for example, global interrupt enable
procedure, SFRs' bank selection.
macro_name macro arg1, arg2...
...
sequence of instructions
...
endm
The following example shows four macros. The first two macros select banks, the third one enables interrupt, whereas the
fourth one disables interrupt.
bank0 macro ; Macro bank0
bcf STATUS, RP0 ; Reset RP0 bit
bcf STATUS, RP1 ; Reset RP1 bit
endm ; End of macro
http://www.mikroe.com/en/books/picmcubook/appa/ (7 of 21)5/3/2009 11:35:35 AM