MINIMUM COMMANDS

The absolute bare minimum program command requirements for any PICmicro program which is to be assembled under MPASM are shown in Listing 1.

In fact, none of the statements in this listing have anything directly to do with a functioning software program. Three are aimed directly at the MPASM assembler; the others are comments to the programmer, or other reader.

Such comments include program title and function, and notes about what particular program instructions within the list are intended to perform. In the full listings of this tutorial you will also see stated the various configuration settings required for each demonstration program. Comments must always be preceded by a semicolon (;) so that the assembler does not try to treat them as program commands.

Comments may appear anywhere within the program, and in any position where they do not interfere with a program command. It is convenient, though, to place them tabulated a short distance beyond the end of the program command lines.

To take Listing 1 in detail, you will see that it starts with two comments, identifying the listing and its function:

; TUTA1.ASM
; minimum requirement

Next come five commands which are aimed at the assembler and need not normally concern you; repeating them parrot-fashion in any software you write will normally suffice unless interrupts are involved (covered later):

ORG 0 ; Reset vector address
GOTO 5 ; Goto start of program
ORG 4 ; Interrupt vector address
GOTO 5 ; Goto start of program
ORG 5 ; Start of program memory

These commands give the memory address (position) within the PICmicro at which a particular set of subsequent commands is to be placed. They consist of the ORG (origin) statement followed by a number, which may be expressed in bases decimal, hexadecimal, binary or even octal (base 8)! Don't worry about octal numbers - we will assume you never need to use them and not discuss them further.

This raises a question - how would the assembler know which number base (or radix) the number '10' was expressed in? This number could represent a binary, hexadecimal or decimal number. Luckily MPASM will allow us to specify the radix of the number by preceding it with the letters B, H or D. Thus D'10' would represent the decimal number 10 and H'10' would represent the hexadecimal number 10 (i.e. 16).

Note that the values themselves are enclosed in "right-hand" quotes, the apostrophe symbol (normally below the @ symbol on your keyboard). Other forms of quote symbol cannot be used in this context.

If numbers are used without specifying the radix, the assembler will assume the radix is some default. This default may be different between assemblers, so we will make sure that numbers in the tutorials have their radix specified. Numbers such as 4 and 5 (as used in listing 1) are the same in decimal and hexadecimal, so we don't need to specify their radix. Note that assemblers other than MPASM may represent number base / radix differently.

The first ORG is the most important. When the PICmicro starts running, it is to the first location (known as the "Reset Vector"), at address zero, that it looks for information about what to do next. We must tell the PICmicro through this location where it is that the main program code is stored. Normally (but not always), the first address at which program code can be started is address 5.

As you become more advanced in your knowledge about PICmicros you will come to know that in fact the program can start at almost any location. However, it is conventional for it to start at location 5. Hence the command that follows the ORG 0 statement, GOTO 5. The assembler when it spots an ORG (origin) statement knows that it is to place the next command at the location indicated, and subsequent commands at consecutive addresses from that location, until it meets another ORG statement giving a different address. In this instance the command GOTO 5 is thus placed at location 0.

Following ORG 0 and GOTO 5, the next command is ORG 4. This points to an address known as the "Interrupt Vector". We shall discuss interrupts later and you need not concern yourself about them at the moment. Suffice to say that when an interrupt occurs, the command placed at location 4 indicates the action that needs to be taken at that moment. With nearly all the example programs used in this tutorial, placing the statement GOTO 5 is all that is needed, telling the PIC that it is to go to the same location as indicated through the original ORG 0 statement.

Next comes ORG 5, stating that address 5 is where the first command of the main program code itself will be placed.

The bracketed statement in the listing (and prefixed by a semicolon) is aimed at you, the reader: it tells you where your program is to be written. This will become evident as we progress through the example listings. The term Initialisation Block is often given to all the statements made prior to the start of the program itself.

The final statement (END) is one that some programmers require, while others do not. To be on the safe side, always place an END statement at the very end of the codes you have written. (Be aware that if writing a program in Windows Notepad, the ENTER key must be pressed after END has been keyed-in, to signify the end of the final line - if this is not done, the END statement may not be found correctly.)

So, of the nine lines in Listing 1, only six statements are actually needed. Everything else is up to you. (In fact the ORG 4 statement and its following command are not actually necessary, but it is safer to put a specific GOTO (start of program) command at location 4 in case an interrupt does occur as a result of an error in program code development.)

With TUTA1.HEX loaded into the PICmicro on the development board, briefly press the Reset switch to reset its "null program" running from the beginning.

Note, though, that pushing the push-switches will have an effect on the LEDs, but not because the PICmicro is causing a response, instead it is because current direct from the power rails is able to pass through each LED and its associated switch and resistors.

END OF TUTORIAL 1

Now use the contents tree to navigate to Tutorial 2.