Beginners Tips
When I started to use PICs (in March 2008, with use of the K8048 programmer from Velleman) I stumbled into a number of "beginners" problems.
For my first experiments I was trying to use the 16F627 supplied with the programmer.
I learned the following:
1. in the compiler (MikroPascal)
1. Do not forget to set the PIC Config word, its type and its clock frequency. In microPascal this is done in the "Edit Project" menu.
The config word mostly used by me is (for 12Fxxx and 16Fxxx Pics)
_Boden (or _Boren) ON --> this is about detecting bad power conditions and reset accordingly
_CP_OFF --> Code protection, I always keep it "off"
_DATA_CP_OFF --> Eeprom data protection, I always keep it "off"
_PWRTE_ON --> Power on timer, I always enable it
_WDT_OFF --> Watchdog timer, do not enable it unless you "clear" (ClrWdt) it accordingly in your SW regurarly
_LVP_OFF --> Low voltage programming, I always keep it "off" for use with the PicFlash2
_MCLRE_OFF --> Get rid of the "reset" circuit and the MCLRE pin can be used for input (*)
_INTOSC_NOCLKOUT --> Get rid of the oscillator circuit and the pin(s) can be used for other things (*)
(*): for some PIC types this selection is not possible, so there will always be external components required.
For some Pics or PIC ranges the number of configuration bits is a lot bigger, because of the higher number of chip capabilities.
All the explanation of the config word(s) You can find in the datasheet (available on the MicroChip website) of the PIC at hand, section "Special features of the CPU".
2. In the source code
1. If necessary, set the OPTION_REGister in your code to a certain value, depending on what you want to do. The value that I normally use as a "standard" for simple programs (no timer usage) is D7 hex. So:
OPTION_REG = $D7
Also here You can find info about the option register in the datasheet.
2. If you intend to use pins of the PIC as I/O pin then do not forget to e.g. disable comparators (or other functions) that use the same pins.
This commonly can be done with the statement
CMCON = 7
in the code of your application.
Also here You can find in the datasheet which pins are e.g. also used by the comparator in the PIC.
3. Do, in general, not forget to make in your code an initialisation part (like the things described above) and a "main loop" part that does the actual work for you. If the latter is not present, then your code will be executed only once, and that would be it. In the example below, the "main loop" is only one statement (the "while true").
In some cases the main loop can be empty because all the work is done in interrupt service routines.
See also Code Setup for more details.
3. In the programmer
Do not forget to select the correct PIC (should be no problem using the "mE" tool or control F11 from within MikroPascal) and the correct options, and load the correct hex file.
Finally, here is an example of a ver small program, intended for the 16F627, which copies the PortA pins into the PORTB pins:
program Test2;
begin
CMCON := 7; // Disable Comparator module's
OPTION_REG := $D7; // Write the OPTION register.
TRISA := $FF; // configure pins of portA as input
TRISB := 0; // configure pins of portb as output
while true do // beginning of a repeat loop
PortB := PortA; // here the actual work is done...
end.
Have fun!
-------------------------------------------