Peter's electronic projects

About the PIC microcontroller

PIC is the name for the Microchip microcontroller (MCU) family, consisting of a microprocessor, I/O ports, timer(s) and other internal, integrated hardware. The main advantages of using the PIC are low external part count, a wide range of chip sizes (now from 5-pin up!) available, nice choice of compilers (assembly, C, BASIC, etc.) good wealth of example/tutorial source code and easy programming. Once bought, the PIC's program memory is empty, and needs to be programmed with code (usually HEX files) to be usable in a circuit.

FAQ

Q: What can I do with the assembly source code for the PIC microcontroller?

A: First, you need a compiler, which takes the source code in the form of asm (assembly), inc (include), and lkr (linker script) files. You can compile microchip assembly files with gputils or mpasm, part of MPLAB.
(Gputils are command line utilities, mplab is a graphical development environment)

The compiler will output a hex file, suitable for programming into the device. You need a programmer device for that. It is an interface between the computer and the microcontroller. I recommend using the Pickit 2 or 3 this purpose.

Please read the pcbheaven pic pages to get started with compiling code and programming the PIC microcontrollers.

Q: How do I compile a HEX file from multiple source files (linked project or relocatable code) ?

A: Start MPLAB-X, and select file/new project. Select "Microchip Embedded" in Categories and "Standalone Project" in Projects. Click next, select the target microcontroller you see in the circuit diagram (for example, PIC16F628). Click next, ignore Select Header, and click next again. Select "Simulator" in "Hardware Tools" as Tool. Click next, and select "mpasm (version) for Compiler. Click next and enter a project name. Click Finish. Select Window/Projects. In the projects window, use the actual project, and ignore others (if there are any). Right-click "Linker files", select "Add existing item" and add the linker script (*.lkr) to the project. In the projects window, right-click "Source files", select "Add existing item" and add all assembly (*.asm) files to the project. Right-click the actual project name and select "Build". You will see the compilation output in the "Output window".

A: Start MPLAB, and select project/project wizard. Choose the target microcontroller you see in the circuit diagram (for example, PIC16F628). Choose the MPASM toolsuite. Add all assembly (*.asm), header (*.inc) files and the linker script (*.lkr) to the project. Select project/build all. If asked, choose generating "relocatable code", and not "absolute code".

Explanation: There are two incompatible source types for one-file absolute and multiple-file linked projects, you can't write code working for both types. If you want to build modular code which can be combined with other libraries, code must be written in the linkable format, even if it is the only source file in the beginning. But linkable format source must be compiled differently, in two steps: first compiled into an intermediate object format, then linked into a HEX file.

Q: Where is all this assembly language documented?

A:You can find the PIC microcontroller instruction set documentation in the microcontroller datasheets at microchip.com. The PIC12, PIC16, PIC18, PIC24, PIC32 families of microcontrollers have different instruction sets. In addition, the general syntax of the code (labels, variables, macros, etc.) is described in the mpasm documentation.