How to program PIC microcontrollers
- 1 Tools
- 2 Assembling & compiling code
- 3 Typical software workflow
- 4 Software setup
- 5 What links here
Why not use professional development tools?
Microchip MPLab ICD
Microchip's MPLab ICD2, a.k.a the hockey puck, and the newer ICD3 are all-in-one debugger/programmers. ICD stands for in-circuit debugger. They install the code you write on your computer into microcontrollers, such as Microchip's PIC microprocessors.
The ICD's just happen to integrate perfectly with MPLab's IDE (integrated development environment), where you can write your code.
The ICD2 and ICD3 cost about $200 as of this writing.
There are other ICDs made by other people that may or may not be compatible with your projects.
Microchip MPLab IDE
So you write your code in the IDE and once youre done, you install it into the PIC microcontroller using the ICD. The combination of one of the ICDs and the IDE is all you need to develop and run applications for Microchip's amazing PIC microcontrollers.
Microchip gives away the MPLab IDE for free.
There are other IDEs made by other people that may or may not be compatible with your projects.
Microchip PIC Microcontrollers
Since our goal here is to program PIC microcontroller, not to spend time coercing incompatible tools into work together, getting the MPLab IDE and one of the MPLab ICDs are a good option because they are created by a single company and built to work together and to work with PIC Microcontrollers.
You can order samples of PIC microcontroller for free. They are a few dollars to buy.
There are other microcontrollers made by other people that may or may not be compatible with your projects.
PicBasic Pro Compiler
By default, the MPLab IDE comes ready for you to program in assembly language, which is then converted into hexadecimal machine code, the native language of the PIC microcontrollers.
Assembly code is very raw and brutal. For most purposes, youll probably want to use a higher-level language, like Basic or C, which are more intuitive.
So you need a compiler to convert your high-level code into low-level assembly before it gets programmed into the microcontroller.
microEngineering Labs PicBasic Pro is just such a compiler for Basic. It has been around for a long time, so is very compatible with most things you are likely to try to do.
PicBasic Pro costs $250 as of today.
There are other comparable compilers for a variety of other languages made by other people that may or may not be compatible with your projects.
Don't have Windows? Get VMWare Fusion.
The software we are discussing, the MPLab IDE and PicBasic Pro compiler, are both built for Windows computers. But there's no need to buy a PC.
You can have a virtual Windows computer inside of your Mac or Linux by using virtualization software like VMWare Fusion. Windows shows up like any other application it is contained within the VMWare Fusion window.
VMWare Fusion for Mac costs about $80 today.
Your best bet, if you want to use virtualization, and you choose foolishly to break the law and use hacked pirated illegal software, is to find a copy of TinyXP Beast Edition to run in VMWare Fusion.
TinyXP is a hacked, stripped down, pirated copy of the Windows XP operating system. The Beast Edition is the most brutally scaled down version. The hackers have removed many of the bloated applications and processes that can slow down a typical Windows installation. TinyXP Beast Edition is therefore lean and fast, and perfect for microcontroller development.
But any Windows XP installation will be just fine if you choose not to run TinyXP.
Beware: if you use Windows versions later than XP, you may get some compatibility problems with various softwares.
Assembling & compiling code
PIC Microcontrollers are programmed with hex code. Hex code is the native machine code that the microcontrollers understand - it is the lowest-level type of code. However, most humans do not understand hex code, so you will want to write your code in another higher-level language which you will then convert into hex code.
MPLab IDE comes with the MPASM assembler. On Windows, this program is named MPASMWIN. This assembler converts assembly code into the hex code suitable for programming into the microcontroller.
Assembly code is a slightly higher-level language than hex code. A problem is that only the most well-educated electronics engineers know how to read and write assembly code. The average Renaissance man or woman dabbling in the art of electronics will not want to learn it because it is slightly too obscure and doesn't make for interesting conversation.
Not coincidentally, there therefore are a variety of compilers used to convert code written in higher-level languages into assembly code. For example, the PicBasic Pro compiler converts BASIC code into assembly. The CSS C Compiler converts C code into assembly. The Pyastra translator converts Python code into assembly, and so on.
Typical software workflow
So, generally, developers use the following work flow process:
- Write code written in C, BASIC, Python, or another high-level language. We will use MPLab IDE as our code editor.
- Compile/translate this code into assembly. We will use PicBasic Pro as our compiler.
- Assemble the assembly code into hex code. We will use MPASMWIN as our assembler.
- Program this hex code into the microcontroller. We will use the MPLab ICD2 as our programming hardware.
With the proper setup, most of these steps happen automatically. You will write your code, click a few buttons, and the microcontroller will magically start working.
Install MPLab IDE
This can be downloaded from the Microchip site. We happen to have an older version of the PicBasic Pro compiler (version 2.44) which necessitates that we install an older version of the MPLab IDE, as per the instructions on the microEngineering Labs site. So we are using MPLab IDE version 8.15a, not the latest.
Install drivers for MPLab ICD2
Make sure to follow the instructions that come with the MPLab ICD2 for installing the correct USB drivers. Do not install the drivers that Windows will try to install automatically when you plug in the device they don't work.
Install PicBasic Pro
Install PicBasic Pro into a folder named C:\pbp\. Longer folder names will cause problems. Once the basic installation is complete, youll need to copy two DLL files, which are code libraries, named Suite_PicBasic.dll and Suite_PicBasicPro.dll into the C:\Windows\System32\ folder. DLL files (dynamic link libraries) are code libraries necessary for PicBasicPro to integrate with other programs, like MPLab IDE.
Once these two DLLs are in the proper place, well have to register them with Windows, so that Windows knows to load them. To do so, open the DOS command prompt by clicking the Windows start button, then typing cmd into the Run field.
Now type the following to navigate into the Sytem32 directory:
Then type the follwing two lines:
regsvr32 Suite_PicBasic.dll regsvr32 Suit_PicBasicPro.dll
You should see a notification pop up when you hit enter after each line that says you have successfully registered this library.
If all goes well, these two DLL libraries will be automatically loaded whenever the MPLab IDE needs them.