Tom's Main Menu

Physical Computing Home

Intro to Physical Computing Syllabus

Networked Objects

Sustainable Practices



code, circuits, & construction

my links


Programming the PIC: PicBasic Pro

There are a number of programming environments for PICs. The lowest level is to program it in the Microchip assembly language. It's not the best way for non-technical programmers, however. Above that, there are a number of C compilers available for the PIC. C is perhaps the most common environment for PIC programming, so you will find many code examples available on the web in C. For these examples, however, we'll be using PicBasic Pro, from MicroEngineering Labs. PB Pro is based on the variant of the BASIC language used to program the BASIC stamp, so code written for the stamp can be easily adapted. It's got a few other advantages over stamp BASIC too, like the ability to write real if-then-else blocks.

With any PIC programming environment, there are some essential components. The first part you'll see is a text editor, into which you enter your code. The second element is a compiler, which converts your code to machine language. The third element is a downloader or programmer interface, which, in conjunction with some hardware, loads the compiled program into your chip. In the PicBasic Pro environment, we'll be using Microcode Studio Lite as the editor, PicBasic Pro as the compiler, and the ePIC programmer as the programmer/downloader.

These notes are intended as a quick guide to getting started on the PIC using PicBasic Pro. For more detailed instruction in PB Pro, including the full command library, see the PB Pro manual. It's best to download this manual and read it in conjunction with these notes.


Most programming environments have a text editor with a few extra features, such as a code checker, or a tool to link the editor to the compiler, so you can compile easily. Some, like Microcode Studio, also change the color of words in your code to show you which words are keywords, and so forth. Many also come with libraries of pre-written code, or code snippets.

Microcode Studio allows you to choose a program to compile your code, to choose a program to load code to your chip, to choose the processor you're compiling for, and so forth. When you make a new project, it creates a .bas file containing the text of your code.


The picBasic Pro compiler, pbpw.exe, takes your text code and compiles it into machine language code. A few files are generated: a .asm file, which contains the assembly language 'translation' of your Basic code; a .mac file, which contains some special assembly language macros generated by the compiler for its own use; and a .hex file, which contains the binary representation of your program, for download to the chip.


The ePIC programmer software (epicwin.exe), is a program that, used in conjunction with the ePIC programmer hardware, downloads your program to the PIC. The programmer software allows you to set various options on the chip, such as the particular PIC you are using, type of clock you are using, what timers you're enabling on the chip, and so forth. If the programmer hardware is not attached, the software will let you know. In this case, quit the program and attach the hardware to your PC.

The programmer hardware attaches to the parallel port of your PC, and to a chip adaptor appropriate to your particular PIC. It needs its own power supply as well. Once you've attached it and powered it, choose the adaptor you need (28/40 pin for the 16F876/77, 18 pin for the 16F84), and put your PIC in the adaptor.

Once everything is set, you open the .hex file of the program, choose "download", and the programmer will program your chip. If the chip already has a program, the software will warn you of this before erasing it. Likewise, if the chip isn't present, or isn't placed in the adaptor properly, the software will inform you with an error message.

PicBasic Pro Basics

PicBasic Pro is based on the BASIC stamp version of BASIC. Its syntax is a little more terse than other languages, as it was designed for writing very simple programs only. Like other languages, features the standard programming structures: variables, control structures like if-then-else blocks and repeat loops. Although there is no specific language for defining subroutines and functions, there are goto and gosub commands that allow you to control the flow of the program.

Comments in PB Pro are preceded with a ' character, as in BX-BASIC.


Identifiers are names for parts of your program. When you put a word in your code that's not a reserved word in BASIC, like "main" or "mySub" or "foo", it's interpreted as an identifier. You can use these to jump to different parts of the program; they are similar to subroutine names. For example, the simplest program that you might write would need an infinite loop. To do this, you'd use a label:

     ' put your code here
goto main

This loop would do the same thing as the infinite do loop that we write in BX BASIC.

If you wanted to jump to a label and then come back to the place you jumped to (i.e. execute a subroutine), you use the command GOSUB to get there, and RETURN to get back:

    gosub sensorSub
goto main

    ' do sensor stuff

RETURN tells the program to go back to the next line after the GOSUB that called the subroutine.


Like BX-BASIC, PB Pro is a typed language, meaning that you have to declare your variables and their data types before you use them. If you want a variable to hold a byte (0-255), you declare it as a byte. If you want it to hold two bytes, you declare it as a word. The syntax for declaring a variable is as follows:

variableName var size

for example:

byteVar var byte  ' a byte-size variable
wordVar var word  ' a word-size variable
bitVar var bit    ' a bit-size variable

These are the only three variable types. You can, however declare arrays of variables, like so:

byteArray var byte[10]   ' a 10 byte array

There are no string variables, or string-handling routines in PB pro, though some commands can deal with them in limited ways.

If-Then Statements

The if-then structure in PB Pro is similar to other languages, with a little catch. If you declare an if statement on one line, with no end-if, you can only follow it with a label to jump to. The best way to work around this is to always use an end-if, even if you only have one line in between, like so:

if x = 10 then


For-next loops in PB Pro are just like in other forms of BASIC:

for x = 1 to 10
     ' do something 
I/O Ports

The input/output ports on the PIC are addressed in PB Pro using their port name followed by the pin you want. the states of these pins are stored in special memory registers, so when you ask for PORTB.0, for example, you're actually reading the first bit of that byte of memory.

There two important memory registers for addressing the pins: The data direction register, or TRIS, which tells you what the state of the pin is (input or output). The PORT register then tells what the state of the pin is. So, for example, to set pin 0 of port B (RB0) to an output and set it high, you would do this:

' setting it to 0 makes the pin an output:
TRISB.0 = 0

' this sets the pin high:
PORTB.0 = 1 

Alternately, you could use the OUTPUT and HIGH/LOW commands. This small program makes RB0 an output and makes it blink:

' make RB0 an output:
output PORTB.0

    high PORTB.0
    pause 250    ' pause 250 msec.
    low PORTB.0
    pause 250
goto main


There are some parameters of the PIC that you must define at the beginning of the program. These are properties of the chip that remain constant throughout the program. For example, the speed of the clock, the I/O pins that you use for certain special functions, and so forth. Define statements are used to do this. These are placed at the top of the program in all capital letters.

For example, the default clock speed in PicBasic Pro is 4 MHz. To change it, you use a define like so:


This defines the oscillator speed as 20 MHz.

For more on using PB Pro, see the PB Pro manual. Here's a step-by-step page on programming the PIC for the first time.

You can use the dot form with any variable, actually; myVar.bit0 or myVar.0 would give you the first bit of a byte or word variable called myVar, and wordVar.byte0 or wordVar.lowbyte would give you the first byte of a word variable called wordVar.