## melabs Loader HTML Help

#### Copyright (c) 2002 microEngineering Labs, Inc.

This software and accompanying documentation is copyright (c) microEngineering Labs, Inc. Please contact microEngineering Labs, Inc. to license this software. Contact information is listed at the end of this file.

### Contents:

####

- Introduction
- Target Software
- Serial Connection
- Operation
- Controls
- Menus
- PicBasic Compiler / PicBasic Pro Compiler Information
- Background Information
- Contact Information

Please see the README.TXT file on the included diskette for the latest information on the melabs Loader. For the latest, latest information, go to the microEngineering Labs, Inc. web site. Also, refer to the Microchip Technology PICmicro microcontroller data sheet for specific information about the devices used by the melabs Loader.

### Introduction

The melabs Loader allows programming and verifying PIC 16F81x, PIC16F87x, PIC16F87xA and PIC18Fxxx devices over a serial connection to a PC. No PICmicro microcontroller (MCU) programmer is necessary. The PICmicro MCU on the target may be programmed, verified and read in-circuit. To accomplish this, special software must be resident on the PICmicro MCU.

A PICmicro MCU may be purchased with the required melabs Loader software already programmed into it or the special software may be programmed into it by any PICmicro MCU programmer, such as the microEngineering Labs, Inc. EPIC Programmer. The melabs Loader software running on the PICmicro MCU is specific to the device, as well as to the pins used for serial communication, the serial mode (true or inverted) and the oscillator frequency.

The PICmicro MCU must be installed on a PCB or proto-board target that has circuitry to power the device and provide an RS232 serial port. A RESET button is also recommended. The microEngineering Labs, Inc. LAB-X1 (40-pin PICmicro MCU only), LAB-X2 (40- or 28-pin PICmicro MCU) and LAB-X3 (18-pin PICmicro MCU) experimenter boards are compatible with the melabs Loader.

A program on the PC communicates serially with the software running on the PICmicro MCU on the target board. The melabs Loader will work under Windows 95, 98, ME, NT, 2000 and XP.

##### Back to contents

### Target Software

As mentioned above, the melabs Loader software running on the PICmicro MCU (target software) is specific to the device, as well as to the pins used for serial communication, the serial mode (true or inverted) and the oscillator frequency. Several target (.HEX) files are included with the default configuration (TX = PORTC.6, RX = PORTC.7, true mode, 4MHz for 28- and 40-pin devices and TX = PORTB.2, RX = PORTB.1, true mode, 4MHz for 18-pin devices). Target files for other devices or configurations may be created on the Build Your Own Loader page on the melabs web site.

##### Back to contents

### Serial Connection

The melabs Loader communicates with the target PICmicro MCU over a serial link. The serial pins, serial mode (true or inverted), start pin and oscillator frequency are fixed at the time the loader code is assembled and programmed into the PICmicro MCU. The standard settings (as provided and used by the LAB-X1 and LAB-X2) are TX on PORTC.6, RX and start pin on PORTC.7, true serial mode and a 4MHz oscillator. For the LAB-X3, the standard serial connections are TX on PORTB.2, RX and start pin on PORTB.1. A standard 9-pin serial cable can be used to connect the LAB-X1's, LAB-X2's or LAB-X3's serial port to the PC serial port.

The following drawings show how to connect the PICmicro MCU for either inverted or true (default) mode using the default communications pins.

##### Back to contents

### Operation

Connect a serial cable between the target board with the melabs Loader's PICmicro MCU and one of the PC COM ports.

Power up the target board.

Start MELOADER.EXE on the PC.

Select the COM port that is connected to the target board under the File menu.

Open the .HEX file to be programmed into the target (File|Open).

Press the Program button (or select Load|Program) on the melabs Loader screen. The melabs Loader may ask you to press reset on the target board to establish communications. Once communications are established, the open .HEX file will be programmed into the target.

The programming can be verified by pressing the Verify button (or select Load|Verify).

Press the Go (Run) button (or Load|Go (Run)) to start the program running. Optionally, press reset on the target board to start the program running.

The current setup is saved to the file MELOADER.INI when you exit the melabs Loader. It is reloaded the next time the melabs Loader is started.

##### Back to contents

### Controls

The Open speed button opens a .HEX file for programming. The name of the open file appears in the melabs Loader title bar.

The Program speed button will program (load) the current code and data into the target. It will optionally read the latest version of the .HEX file before programming (see the Options menu). The melabs Loader cannot program the ID or configuration.

The Verify speed button will compare the current code and data to the programmed target. If the information does not match, an error message is displayed. A verify is also done as the target is being programmed.

The Read speed button will read the current code and data from the target.

The Erase speed button will erase the code memory on a PIC16F81x or PIC18Fxxx target. It has no effect on PIC16F87x or PIC16F87xA targets.

The Go (Run) speed button will start the loaded program running on the target.

All of the speed buttons, along with many other settings, are also available using the drop-down menus.

##### Back to contents

### Menus

#### File Menu

File|Open opens a .HEX file for programming. The name of the open file appears in the melabs Loader title bar.

File|Comm Port allows setting the PC comm port on which the melabs Loader is to communicate with the target.

File|Baud Rate allows selecting the speed at which the PC communicates with the target. This baud rate must match the baud rate selected when the software resident on the target device was created. This speed is normally 19,200 baud.

File|Exit exits the program.

#### View Menu

View|Code displays the code window. This will show the open .HEX file or recently read code information in hexadecimal or ASCII.

View|Data displays the data window. This will show the open .HEX file or recently read data information in hexadecimal or ASCII.

View|Close All Windows will close any open Code, Data or Debug window.

View|Stay On Top causes the melabs Loader bar to float on top of any other windows, when checked.

#### Load Menu

Load|Program will program (load) the current code and data into the target. It will optionally read the latest version of the .HEX file before programming (see the Options menu). The melabs Loader cannot program the ID or configuration.

Load|Verify will compare the current code and data to the programmed target. If the information does not match, an error message is displayed. A verify is also done as the target is being programmed.

Load|Read will read the current code and data from the target.

Load|Erase will erase the code memory on a PIC16F81x or PIC18Fxxx target. It has no effect on PIC16F87x or PIC16F87xA targets.

Load|Go (Run) will start the loaded program running on the target.

Load|Get Target Information will report the current firmware revision, target device name and user program status.

#### Options Menu

Options|Program/Verify Code allows the target's code space to be programmed and verified when checked. If it is not checked, the target's code space will not be programmed or verified. It will always be read, regardless of the state of this option.

Options|Program/Verify Data allows the target's data space to be programmed and verified when checked. If it is not checked, the target's data space will not be programmed or verified. It will always be read, regardless of the state of this option.

Options|Reread File Before Programming, when checked, will cause the latest version of the .HEX file to be read before the target is programmed.

Options|Erase Before Programming, when checked, will erase the code space before the target is programmed. It is only useful on a PIC16F81x or PIC18Fxxx target. It has no effect on PIC16F87x or PIC16F87xA targets.

Options|Verify After Programming, when checked, will cause the target to be verified as an additional step after programming.

Options|Go (Run) After Programming, when checked, will cause the program loaded into the target to be executed after successful programming.

Options|Disable Completion Messages will prevent the displaying of operation complete messages at the end of a program or verify operation.

#### Help Menu

Help|Help opens the default browser and displays the HTML help file.

Help|Readme opens the default text editor and displays the README file.

Help|About... displays the program version number and copyright.

##### Back to contents

### PicBasic Compiler / PicBasic Pro Compiler Information

The default configuration of the target PICmicro MCU has the Watchdog Timer turned off. This will cause PicBasic and PicBasic Pro programs that use the Nap or Sleep command to simply hang as it is the Watchdog timer that wakes them up. Be sure to use a target device with the Watchdog Timer turned on if you are using the Nap or Sleep commands.

PicBasic Pro programs must be modified slightly to work properly with the melabs Loader. The following line must be added to a PicBasic Pro program:

        Define LOADER_USED 1

On versions of the PicBasic Pro Compiler earlier than 2.33, a different Define must be used:

        Define ONINT_USED 1

Standard PicBasic Compiler programs may be used as is, with no modification made for the melabs Loader.

##### Back to contents

### Background Information

The PIC16F81x, PIC16F87x, PIC16F87xA and 18Fxxx devices have a special feature not available on other PICmicro MCUs: they can program their own code space while running. All of the other PICmicro MCUs must be programmed by some kind of PICmicro MCU device programmer, like the EPIC Programmer. This self-programming feature allows one of these devices to run a special kind of program called a bootloader.

A bootloader is a program that resides in the code space of the target MCU. It can be activated to allow additional program code to be written to and read from that same target MCU. The melabs Loader includes such a bootloader program. The melabs Loader consists of 2 elements, connected by a serial cable.

The first part of the melabs Loader is a program resident on the PICmicro MCU. This program occupies the upper 256 words of the code space (264 words for PIC16F81x and PIC18Fxxx devices). This small program must be put into the PICmicro MCU using a conventional programmer, or the MCU may be purchased with this program already resident. A pre-programmed PICmicro MCU is included as part of the melabs Loader package.

The program resident on the PICmicro MCU communicates with the second element of the loader over a serial connection. This second program is a 32-bit Windows PC program that is the user interface for the loader. It allows selection of the file to be programmed, as well as has the capability to read and verify the downloaded user program on the target MCU. This is the program you see as the melabs Loader.

Only the code space and data space may be read and programmed on the target MCU. The ID space and configuration fuses are not accessible to the melabs Loader. The configuration fuses must be set at the time the actual loader program is programmed into the PICmicro MCU. Once they are set, they cannot be changed by the melabs Loader.

The melabs Loader software resident on the PICmicro MCU intercepts the reset vector. When the PICmicro MCU powers up, it executes the melabs Loader boot manager. The boot manager watches the start pin for .2 seconds. Usually this is the serial input pin but may be set to any other pin, including the TX pin, when the target firmware is created. If it sees the start state during this period, it enters the melabs Loader communications software to download or read a progam. If it does not see the start state during the .2 seconds, it starts the user program in the PICmicro MCU, if one has been previously loaded.

The interception of the reset vector is accomplished by automatically relocating the first 4 user program locations from 0 to 3 to a special place in the melabs Loader's code space (within the top 256 or 264 words). A jump to the boot manager is then placed at locations 0 to 3. When the melabs Loader software running on the PC reads or write these addresses, the values shown are as expected (as if the melabs Loader was not resident and the reset code had not been moved).

The serial pins used by the melabs Loader are only used when the loader is actually programming, verifying or reading. They are uncommitted while the downloaded user program is running on the target and may be assigned to any other task or serial baud rate with one exception.

As noted above, the melabs Loader checks the start pin at power up and reset to determine if it should run or if the user (dowloaded) progam should run. Therefore, for the user program to run, this pin must not be in the start state when the device is powered up (unless it is desired for the melabs Loader to start). Make sure the pin, which is usually the RX pin, has a pullup or pulldown resistor on it steering it to the idle state.

The serial communication speed is normally 19,200 baud. The loader program resident on the PICmicro MCU can communicate at this speed with an oscillator frequency from 1.8432MHz to 40MHz. This oscillator frequeny is determined at the time the loader program is created and programmed into the target PICmicro MCU. The target MCU must then only be run at this frequency in order to be able to communicate with its counterpart running on the PC.

The serial mode, true or inverted, as well as the serial TX, RX and start pins, are also fixed at the time the loader program is assembled and programmed into the PICmicro MCU. None of these parameters can be changed by the loader.

The melabs Loader uses no PICmicro MCU resources while the user program is running. All the data memory, RAM and I/O pins are available to the user program.

However, there are a few special considerations that should be observed when writing programs that will be loaded by the melabs Loader. The first is that the loader's boot manager takes over at power up and any subsequent resets. Any time the program vectors through the reset vector, the loader's boot manager becomes active and watches the loader's start pin for the start state. If this level is present on the start pin, which is normally the RX pin, the loader will start and the user program will not execute. Even if there is no activity, the start of the user program will be delayed by the 200 milliseconds that the loader's boot manager is watching the start pin.

Another consideration is the fact that the configuration fuses are not alterable by the melabs Loader. If some programs require the use of the Watchdog Timer, for example, and others don't, separate PICmicro MCUs will be necessary. One MCU that was programmed with the WDT on and one with it off may be necessary as the state cannot be changed. The same is true for the Powerup Timer, Brownout Detect Enable and oscillator type, though these timer states are generally not critical. The standard programmed defaults are Watchdog Timer off, Powerup Timer on and Brownout Detect Enable on.

The configuration fuses for code protection cannot be on. The melabs Loader needs to be able to freely read and write to the PICmicro MCU code and data space. Therefore, the device cannot be code protected. Flash program writes must also be enabled so that the melabs Loader can write to the flash code space.

There is also the fact that the loader occupies the last 256 (or 264) words of code space. Usually a program is written starting at location 0 and grows upward so the loader's position in memory is not noticable. The loader will not allow itself to be overwritten. However, it is a good idea not to allow program code to attempt to enter the upper 256 words of code space.

As stated earlier, the melabs Loader inserts its own code at the reset vector and automatically relocates the users reset code to an area reserved within the loader's 256 (or 264) words of memory. Usually, these locations contain a jump to the startup routine for the user program. Since the user code is no longer located at these locations, the user program should not attempt to jump to or call any routine within the area between 0 and 3. Either code that is only executed once at startup or a jump to the startup code, located above location 3, should reside in this area.

The standard PicBasic Compiler puts some startup code at this location that has no effect on and is not affected by the melabs Loader. The PicBasic Pro Compiler may put library routines at this location. The "PicBasic Pro Information" section above describes how to prevent the PicBasic Pro Compiler from doing this. Assembler programs should use reset code similar to the following:

            org     0
            goto    startup

            org     5
    startup

##### Back to contents

### Contact Information

microEngineering Labs, Inc.
Box 60039
Colorado Springs CO 80960-0039
USA
(719) 520-5323
(719) 520-1867 fax
http://www.melabs.com
email: support@melabs.com
Microchip Technology Inc.
2355 W. Chandler Blvd.
Chandler AZ 85224-6199
USA
(480) 792-7200
(480) 792-7277 fax
http://www.microchip.com
email: literature@microchip.com