Tom's Main Menu

Physical Computing Home

Intro to Physical Computing Syllabus

Networked Objects

Sustainable Practices

blog

Resources

code, circuits, & construction

my del.icio.us links

 
PIC:

Programming the PIC using PicBasic Pro

PIC memory registers

Digital in and out

Analog in

Serial in and out (using serin2 and serout2)

PIC resources page

Microengineering Labs' site

Using MELabs Loader with the PIC 16F87x

 
The PIC 16F87x family of chips are capable of reprogramming themselves while running. In other words, a program resident in part of the memory of the chip can reprogram other parts of the memory. Thanks to this, it is possible to program the 16F87x PICs without a hardware programmer.

Microengineering labs makes a tool to do this called MELabs loader. Here's how it happens:

You make a small program to configure the chip (set the timers, the oscillator frequency, etc.) and download it into the chip using a regular programmer like the ePIC. This program is called a bootloader, and it resides in the first 256 words of code memory. Then, you attach a serial cable to two pins of the chip (preset by the bootloader), and open up the MELabs loader program on the PC. Using MELabs loader, you load in any program you want to the chip. You can change the program, put in a new program, and so forth, as long as the bootloader is on the chip. To erase or reconfigure the bootloader, you have to go back to the ePIC and download again.

For more details, see the MELabs loader page on MicroEngineering Labs' site.

So how do I write this bootloader program?

Actually, you don't. Microengineering Labs does. Go to their make loader page and configure it for your chip. Set the following:

  • chip type
  • the speed of your crystal
  • the pins you plan to connect your serial cable to (best to use RC6 and RC7, the hardware serial port on the chip)
  • the serial mode (inverted, unless you are using a hex inverter between your chip and the serial cable)
  • the watchdog timer and other timers (on or off as you need)

Enter your email address and the form will mail you a hex file. Load this hex file into your chip using the ePIC or another programmer. When programming, make sure your options are identical to those you chose when you made the hex file on MElabs' site, and make sure you enable "Flash Program Write" in the configuration menu. This allows the bootloader to rewrite the rest of the flash program memory.

Now your chip no longer needs a programmer.

So how do I program the chip without a programmer?

Once you've loaded the bootloader into your chip, attach a serial connector to the pins you specified when making the bootloader (RC6 for transmit to PC, RC7 for receive from PC, most likely). Put a 22K resistor in series with the receive pin, as you normally do for serial communication to the PC:

Connect the PIC to a serial port on your PC. .

Before you can download a PBPro program using the bootloader, you have to make one change. Add the following to the top of your program:

DEFINE ONINT_USED 1

Save this, and compile your program as usual.

Now, open MELabs loader on the PC. The interface looks much like the ePIC programmer software. Make sure the serial port is set appropriately, and check "Go (run) after program" in the Options Menu. Open the hex file, and click "Program". If you get a message asking you to reset the chip, check your power and serial connections and ground the reset pin on your chip (pin 1). Then click "Program" again, and you're done.

So where's the catch?

Well, you lose some of the memory available for code, because the bootloader's taking it up. And you can't change the bootloader without a hardware programmer. But that's about it. In return, you gain the ability to program without a hardware programmer. It's a tradeoff that is worth making if you don't have access to a hardware programmer all the time.