<<Previous

Next>>

4. PICBASIC™ Programming

4.1. Comments

All well written programs contain adequate comments, unless you're Microsoft, in which case they're contained on three CD-ROMs. A PBC comment starts with either the REM keyword or the single quote (' ). All following characters on this line are ignored.

4.2. Numeric Constants

PBC allows numeric constants to be defined in the three bases: decimal, binary and hexadecimal. Binary values are defined using the prefix '%' and hexadecimal values using the prefix '$'. Decimal values are the default and require no prefix.

100 ' Decimal Value 100

%100 ' Binary Value for Decimal 4

$100 ' Hexadecimal Value for Decimal 256

For ease of programming, single characters are converted to their ASCII equivalents. Character constants must be quoted using double quotes and must contain only one character (otherwise, they are string constants).

"A" ' ASCII Value for Decimal 65

"d" ' ASCII Value for Decimal 100

4.3. String Constants

PBC doesn't provide string handling capabilities, but strings can be used with some commands. A string contains one or more characters and is delimited by double quotes. No escape sequences are supported for non-ASCII characters (although most PBC commands have this handling built-in).

"Hello" ' String (Short for "H","e","l","l","o")

Strings are usually treated as a list of individual character values.

4.4. Identifiers

An identifier is, quite simply, a name. Identifiers are used in PBC for line labels and symbol names. An identifier is any sequence of letters, digits, and underscores, although it must not start with a digit. Identifiers are not case sensitive, thus label, LABEL and Label are all treated as equivalent. And while labels might be any number of characters in length, PBC only recognizes the first 32.

4.5. Line Labels

In order to mark statements that the program might wish to reference with the GOTO or GOSUB commands, PBC uses line labels. Unlike many older BASICs, PBC doesn't allow line numbers and doesn't require that each line be labeled. Rather, any PBC line may start with a line label, which is simply an identifier followed by a colon ( : ).

here: serout 0,N2400,("Hello, World!",13,10)

    goto here

 

4.6. Variables

A number of variables have been predefined for temporary data storage in your PICBASIC™ program. Byte-sized variables are named B0, B1, B2 and so forth. Word-sized variables are named W0, W1, W2 and so forth. These word-sized variables are made up of two byte-sized variables. For example, W0 consists of B0 and B1; W1 is made up of B2 and B3 and so forth. Any of these variables can, in effect, be renamed using the SYMBOL command to have more meaning within your program.

These variables are actually stored in the PICmicro MCU's RAM registers. B0 is stored in the first available RAM location, $0C for the PIC16C84 and some of the smaller PICmicro MCUs, or $20 for the PIC16C74 and other larger PICmicro MCUs. Refer to the Microchip PICmicro MCU data books for the actual location of the start of the RAM registers for a given PICmicro MCU.

The variables are assigned to RAM sequentially up to and including B21 at which point variables internal to the compiler library subroutines are assigned. These assignments are done in the file PBH.INC in the INC subdirectory. You may refer to it for additional information.

For a PICmicro MCU with only 36 bytes of RAM, such as the PIC16C84, the 22 user variable bytes and the internal library variables use all of the RAM that is available. For larger PICmicro MCUs like the PIC16F84 with 68 bytes of RAM and the PIC16C74 with 192 bytes of RAM, additional user variables may be accessed. These variables continue at B22 and run through B79 (also W11 through W39.) A particular PICmicro MCU may not have actual RAM at all of these additional locations. If you try to use a variable with no RAM location, the compiler will not generate an error, but your program will not do what you expect.

This table lists the highest user variable names that should be used with each PICmicro MCU:

BASIC Stamp I

B13

W6

PIC16C61

B21

W10

PIC16C71

PIC16C710

PIC16F83

PIC16C84

PIC16C711

B51

W25

PIC16F84

PIC16C554

B63

W31

PIC16C556

PIC16C620(A)

PIC16C621(A)

PIC12C67x

B79

W39

PIC14C000

PIC16C558

PIC16C622(A)

PIC16C62(AB)

PIC16C63

PIC16C64(A)

PIC16C65(AB)

PIC16C72(A)

PIC16C73(AB)

PIC16C74(AB)

The first two bytes, B0 and B1, may also be used as bit variables: Bit0, Bit1, ..., Bit15.

Additionally, the variable names Port, Dirs and Pins are predefined. Pins references PICmicro hardware PORTB. Dirs references the data direction register for PICmicro hardware PORTB (TRISB). A Dir of 0 sets its associated Pin to an input and a Dir of 1 sets its associated Pin to an output. Most instructions set the Pin's direction automatically. Port is a word variable that combines Pins and Dirs. Like W0, these are overlaid and can be referenced as bits (Pin0, Pin1... and Dir0, Dir1...).

When powered up or reset, Dirs is set to $00 (all pins input) and all other variables are set to $00. All variable values are unsigned. Thus, bits have the value 0 or 1, bytes 0 to 255, and words 0 to 65535.

The following table lists the predefined variables:

Word Variables

Byte Variables

Bit Variables

W0

B0

Bit0,Bit1,... Bit7

 

B1

Bit8,Bit9,... Bit15

W1

B2

 

 

B3

 

W2

B4

 

 

B5

 

...

...

 

 

...

 

W39

B78

 

 

B79

 

Port

Pins

Pin0,Pin1,... Pin7

 

Dirs

Dir0,Dir1,... Dir7

While the use of fixed names might seem to be limiting and lead to ugly programs, these variables may be given more useful names via the SYMBOL statement.

 

4.7. Symbols

In order to make programs more readable, PBC allows the user to define his own symbols. These symbols may be used to represent constants, variables or other symbols. They may not be used for line labels. Only one symbol may be defined per SYMBOL statement.

Symbol Ten = 10 ' Symbolic Constants

Symbol Count = W3 ' Named Word Variable

Symbol BitVar = BIT0 ' Named Bit Variable

Symbol Alias = BitVar ' An Alias for BitVar

4.8. Multi-statement Lines

In order to allow more compact programs and logical grouping of related commands, PBC supports the use of the colon (:) to separate statements placed on the same line. Thus, the following two examples are equivalent:

W2 = W0

W0 = W1

W1 = W2

is the same as:

W2 = W0 : W0 = W1 : W1 = W2

This does not, however, change the size of the generated code.

<<Previous

Next>>