2.5. I=ve Got Troubles
The most common problems with getting PICmicros running involve making sure the few external components are of the appropriate value and properly connected to the PICmicro MCU. Following are some hints to help get things up and running.
Make sure the /MCLR pin is connected to 5 volts either through some kind of voltage protected reset circuit or simply with a 4.7K resistor. If you leave the pin unconnected, its level floats around and sometimes the PICmicro MCU will work but usually it won=t. The PICmicro MCU has an on-chip power-on-reset circuit so in general just an external pull-up resistor is adequate. But in some cases the PICmicro MCU may not power up properly and an external circuit may be necessary. See the Microchip data books for more information.
Be sure you have a good crystal with the proper value capacitors connected to it. Capacitor values can be hard to read. If the values are off by too much, the oscillator won=t start and run properly. A 4MHz crystal with two 22pf (picofarad) ceramic disk capacitors is a good start for most PICmicros. Once again, check out the Microchip data books for additional thoughts on the matter.
Make sure your power supply is up to the task. While the PICmicro MCU itself consumes very little power, the power supply must be filtered fairly well. If the PICmicro MCU is controlling devices that pull a lot of current from your power supply, as they turn on and off they can put enough of a glitch on the supply lines to cause the PICmicro MCU to stop working properly. Even an LED display can create enough of an instantaneous drain to momentarily clobber a small power supply (like a 9-volt battery) and cause the PICmicro MCU to lose its mind.
Start small. Write short programs to test features you are unsure of or might be having trouble with. Once these smaller programs are working properly, you can build on them.
Try doing things a different way. Sometimes what you are trying to do looks like it should work but doesn=t, no matter how hard you pound on it. Usually there is more than one way to skin a program. Try approaching the problem from a different angle and maybe enlightenment will ensue.
2.5.1. PICmicro Specific Issues
It is imperative that you read the Microchip data sheet for thePICmicro MCU device you are using. Some devices have features that can interfere with expected pin operations. The PIC16C62x and 16F62x parts (the 16C620, 621, 622, 16F627 and 628) are a good example of this. These PICmicros have analog comparators on PORTA. When these chips start up, PORTA is set to analog mode. This makes the pin functions on PORTA work in an unexpected manner. To change the pins to digital, simply add the line:
CMCON = 7
near the front of your program.
Any PICmicro with analog inputs, such as the PIC16C7xx, PIC16F87x and PIC12C67x series devices, will come up in analog mode. You must set them to digital if that is how you intend to use them:
ADCON1 = 7
Another example of potential disaster is that PORTA, pin 4 exhibits unusual behavior when used as an output. This is because the pin has an open drain output rather then the usual bipolar stage of the rest of the output pins. This means it can pull to ground when set to 0, but it will simply float when set to a 1, instead of going high. To make this pin act in the expected manner, add a pull-up resistor between the pin and 5 volts. The value of the resistor may be between 1K and 33K, depending on the drive necessary for the connected input. This pin acts as any other pin when used as an input.
Some PICmicro MCUs, such as the PIC16F627, 628, 873, 874, 876 and 877, allow low-voltage programming. This function takes over one of the PORTB pins and can cause the device to act erratically if this pin is not pulled low. It is best to make sure that low-voltage programming is not enabled at the time the PICmicro MCU is programmed.
All of the PICmicro MCU pins are set to inputs on power-up. If you need a pin to be an output, set it to an output before you use it, or use a PICBASIC PRO™ command that does it for you. Once again, review the Microchip data sheets to become familiar with the idiosyncrasies of a particular part.
There is no data direction (TRIS) register for PORTA on PIC17Cxxx devices. Therefore, commands that rely on the TRIS register for their operation, such as I2CRead and I2CWrite, may not be used on PORTA.
The name of the port pins on the PIC12C5xx, 12CE5xx, 12C67x and 12CE67x devices is GPIO. The name for the TRIS register is TRISIO.
GPIO.0 = 1
TRISIO = %101010
On the PIC12C5xx and 12CE5xx devices, pin GPIO.2 is forced to an input regardless of the setting of the TRIS register. To allow this pin to be used as a standard I/O pin, add the following line to the beginning of the program:
OPTION_REG.5 = 0
As hinted at above, the name of the OPTION register on all PICmicro MCUs is OPTION_REG.
Certain PICmicros have on-chip non-volatile data storage implemented like an I2C interfaced serial EEPROM.READ and WRITE will not work on devices with on-chip I2C interfaced serial EEPROM like the 12CE67x and 16CE62x parts. Use the I2CREAD and I2CWRITE instructions instead.
Some PICmicro devices, notably the PIC12C671, 672, 12CE673 and 674, have on-chip RC oscillators. These devices contain an oscillator calibration factor in the last location of code space. The on-chip oscillator may be fine-tuned by acquiring the data from this location and moving it into the OSCCAL register. TwoDEFINEs have been created to perform this task automatically each time the program starts:
DEFINE OSCCAL_1K 1 ' Set OSCCAL for 1K device DEFINE OSCCAL_2K 1 ' Set OSCCAL for 2K device
Add one of these 2 DEFINEs near the beginning of the PICBASIC PRO™ program to perform the setting of OSCCAL.
If a UV erasable device has been erased, the value cannot be read from memory. If one of these DEFINEs is used on an erased part, it will cause the program to loop endlessly. To set the OSCCAL register on an erased part, near the beginning of the program, add the line:
OSCCAL = $a0 ' Set OSCCAL register to $a0
The $a0 is merely an example. The part would need to be read before it is erased to obtain the actual OSCCAL value for that particular device.
PICBASIC PRO™ will automatically load the OSCCAL value for the PIC12C5xx and 12CE51x devices , if it is available. It is unnecessary to use the above DEFINEs with these devices.
Please see the Microchip data sheets for more information on OSCCAL and any of the other PICmicro registers.
PicBasic and BASIC Stamp Compatiblity
There are some differences between the standard PicBasic Compiler, the BASIC Stamps and the PICBASIC PRO™ Compiler. See section 10 for information on these differences.
2.5.2. Code Crosses Page Boundary Messages
Many PICmicros contain code space memory that is segmented into 2K or 8K pages. As large files are compiled and then assembled, they may start to use more than the first page. As each page is used, PM, the assembler, will issue a message that the code is crossing a particular boundary. This is normal and no cause for alarm. PBP will make sure to take care of most of the issues for you.
The only thing that you must be aware of are theBRANCH instructions. If a BRANCH tries to access a label on the other side of a boundary, it will not work properly. BRANCHL should be used instead. It can address labels in any code page.
2.5.3. Out of Memory Errors
Compiling large PICBASIC PRO™ source code files can tax the memory of the PC running the compiler. If an Out of Memory error is issued and the FILES and BUFFERS are set as recommended, an alternate version of PBP can be used. PBPW.EXE has been compiled to make use of all of the memory available to Windows 95, 98, NT and 2000. You must, of course, be running in a DOS shell from one of these 32-bit Windows environments or be within Microchip's MPLAB. To execute the Windows version from the DOS command line, simply substitute PBPW for PBP.