Add to Technorati Favorites Electronics Projects and Articles: April 2009

Monday, April 6, 2009


I've created this blog to write electronics projects and electronics articles which can be used by any person interested in the electronic design. Your questions, doubts, comments and suggestions are welcome providing that they are written in a courteous and respectful way.



Recently I have met many students and colleagues that ask me about how to program PIC microcontrollers. For this reason I have decided to write this article. In the present article you can find hints and tips to help you begin to program PIC microcontrollers.


  • Clarifying concepts in programming languages for PIC microcontrollers.
  • Comparing the current programming languages for PIC microcontrollers and establishing their advantages and disadvantages.
  • Doing simple examples of the most important programming languages.
  • Knowing several compiler provider companies.
  • Drawing conclusions and to define the best language for programming PIC microcontrollers in a professional way.


Commonly for many people the first obstacle to programming PICs is the language.
Actually when we program a PIC we do not use a specific language. We do not see when program is loaded into a PIC but when the PIC is loaded with our program the instructions are converted in OPCODES. The OPCODES are in a hexadecimal base. The type of PIC (low range, mid range or upper range) that you are programming always loads OPCODES.

For instance, the 286C it is an OPCODE for PIC microcontroller and the mnemonic for this OPCODE is GOTO 0X6C, that means go to the 6C program memory position.
When a program is compiled in a suitable compiler, no matter which language is used, it always generates an OPCODES list that has a file extension .hex.
If we create a TURNONLED routine, when we compile it a TURNONLED.hex will be generated and it will be the file that we will sent to PIC microcontroller.
In order to carry out the TURNONLED.hex file loading task we need to have an interface tool between the computer and the PIC microcontroller. This interface tool is called a programmer. We could talk about this subject on another occasion.

PIC Microcontrollers Languages

Among the several languages to program routines and subroutines I would like to mention the following:

- BASIC language
- C language
- Assembly language

You must please not confuse neither BASIC language or C language for PICs with Visual Basic, Visual C++ or similar IDEs because you can never program PIC routines using Object-Oriented Programming (OOP). We always use the instructions and structures of these languages with special editors and compilers for PIC microcontrollers.

The advantages, disadvantages, versions and examples for each language is detailed below.

1. BASIC Language


  • It is a simple language with easy reading instructions.


  • You will never have the control of the program. That means, You will never know either the time of instructions nor the running time.
  • There are difficulties when you want to use interrupts.
  • When the *.hex file is generated, it is not optimized, therefore you do not know the size of program memory used.
  • Most compilers for this language use windows environment.


There are several companies producing BASIC language compilers and editors, among them I would like to mention:

The most popular and which I think is the best is PICBASIC PRO by MicroEngineering Labs Inc. It is not free.

Example: This routine makes blinking a LED on port B 0 every 200 milliseconds. I use BASIC language designed in PICBASIC PRO.

2. C Language


  • This language is closer to machine language. Therefore, you can merge it with assembly language.
  • You can build math routines easily.
  • You can create macros to simplify your program.
  • Is accepted by Microchip, even it has some compilers to C language.


  • When the programs are compiled they can become somewhat extensive, so You must consider the program memory capacity. I recommend using this language with upper range PICs.


Among the several companies that produce compilers and editor for C language for PICs I would like to mention: It has C18 compiler for upper range and C30 compiler for dsPIC. Both C18 and C30 compilers can include them with MPLAB IDE. They are not free. It has PCW IDE, PCWH IDE and PCWHD IDE. Each one offers the possibility to select the range to work and to compile for both Windows and Linux environments. The PCWHD IDE has all ranges for PIC microcontrollers and dsPIC. Besides it has a debbuger and Wizard that can generates a lot of source codes to initialize the program. You have a lot of libraries and you can integrate it with MPLAB IDE as well. These editors/compilers are not free. (HI-TECH) it has PICC and PICC18 software tools. They are not free.

Example: This routine makes blinking a LED on port B 0 every 200 milliseconds. I use C language designed in CCS PCW IDE.

3. Assembly Language


  • It is the natural language for PIC in all ranges.
  • You have a total control of the program. You can take into account the instruction times and control all registers bit to bit.
  • You can build macros to simplify the source code.
  • There are no problem to handle simultaneous interrupts.
  • When the compiler generates the *.hex file it is entirely optimized.


  • I consider that this language does not have disadvantages, except when someone does not have experience in programming, this language could delay his developments.


PIC microcontrollers have the MPLAB IDE editor/compiler/simulator which is completely free on This compiler is usually updated with new versions, but it works on Windows environment.
For Linux there is an alternative version named PIKLAB.

Example: This routine makes blinking a LED on port B 0 every 200 milliseconds. I use Assembly language designed in MPLAB IDE.


Above, we saw the several options for programming PICs. I consider that If you want to program PICS in a professional way you can take into account the following advice:

1. It is ideal completely to know the Assembly language because you can take complete control of the program and all resources of the device. You also can build macros easily with it.

2. If you are going to begin to program PICs, you should begin with Assembly language. My advice is that you are not going to begin with BASIC language.

3. The C language is very useful for upper range (18xxxx family) and dsPICs, but this language can result very useful for other ranges if you combine it with assembly language.