Intro to Programming PIC Microcontrollers in Linux
April 19, 2005
I first tried switching to Linux back in 1996-- however, grew frustrated after 2 agonizing weeks of trying to configure ppp to connect to the internet on my speedy 28.8 modem. I continued using, and developting software for, Windows. I grew more and more frustrated with Windows until I reached the breaking point with the new spyware/virus epidemic. I once again attempted the switch to Linux, and to my delight, found it quite painless. I found the install to be VERYeasy and all the drivers and applications I would need were free and readily available-- if not already built into my Linux distribution (at the time that was Fedora Core 1, and is now Fedora Core 3).
Everything was working. I was developing my web applications with Oracle and JSP on my local system, I had office, all the messaging clients, an image editor with all the functionality I could ever want-- and this was all on a laptop! But there was one, small thing missing. I didn't have MPLAB and all my great software for electronics and PIC programming. Eh! However, after quite some time I was able to get up an running with my PIC programming. I haven't booted into my Windows XP system in over 6 months. Now I pass on the information to you.
Before going any further, I would like to point out that PIC developement on Linux is, as of yet, nowhere near as intuitive and thorough as it is on Windows using MPLAB. However, for the hobbyist such as myself, it's more than sufficient. And you don't have to rely on Windows!
Target Audience
This article is inteded for people who are familiar with PIC programming. It's not a beginners guide to programming PICs, but not far from it. It's intention is to get you setup and started in programming, compiling, and simulating your PICs in a Linux environment. Ideally you have a basic knowledge of PIC programming. Ideally you have a basic understanding of using Linux and installing from source-- but if not, I do go over it step-by-step. You shouldn't be afraid of issuing commands on a command-line. But then again, if you're someone who's reading an article on PIC programmin in Linux, I doubt you'll have any problems with that :) I am no expert in Linux nor PIC programming. I just wrote this article to compile what I've learned in the last few months as a result of my own endevours in PIC programming and Linux. I hope it helps. I have done all the steps of this article on a Fedora Core 2 installation with KDE and also with a Fedora Core 3 installation and Gnome.Resources
Here are some of the sources from which I obtained my information, and I reccomend you get familiar with if you're going to be developing your PIC applications in Linux.:- GNUPIC Project - Project dedicated to conolidating PIC Linux websites. Most of the other sites are derived from this one site. I'm not going to list all the PIC Linux projects separately as they can all be accesed through the GNUPIC Project. The mailing list is also a great resource for PIC and Linux information.
- LinuxQuestions.org - Forums for Linux. I ask TONS of questions here, especially when getting strange errors. Great resource when you're stumped on installation and/or configuration issues.
- Programming & Customizing PICmicro Microcontrollers - This is a book about programming PIC micro's written by Myke Predko. It is truely a great book on programming the PICmicros. I've read about 4 different books and nobody handles the subject better than Mr. Predko.
Installing GPUTILS - The Linux PIC Assembler and Linker
GPUTILS is a collection of cross-platform PIC utilities designed to emulate the syntax and functionality of Microchip's MPLAB software for Windows. It includes gpasm, gplink, and gplib which are compatable with mpasm, mplink, and mplib respectively. These utilities are the essentials for programming PICs on Linux. You should download and read through the manual. Many readers are already quite familiar with installing software on Linux and can therefore skip down to the section where we write our first PIC program, toggle_led.asm. However, if you have never installed linux software by "compiling from source", then read on for step-by-step instructions on installing the software.Downloading GPUTILS
You can download the latest version of GPUTILS at from Sourceforge. At the time of writing, the current file was gputils-0.13.1.tar.gz. Note that I am using the tar.gz file and not the .rpm file. Make sure you know where you downloaded the file to.Extract the Tarball
You'll need to extract the files in the archive. To do this, open up a terminal, navigate to the directory where you downloaded the file to, and issue the following commands (assuming your file was version 0.13.1):tar -xzf gputils-0.13.1.tar.gz cd gputils-0.13.1That will extract the files into a new directory named gputils-0.13.1 and then set gputils-0.13.1 as the current working directory.
Configure, Make, and Make Install
As with most software for Linux, it's installed by performing a configure, a make, and a make install. This is pretty much the same for all the software packages we'll be installing. Each of these steps can take a few minutes, during which a bunch of information scrolls across the screen. Unless you see some error messages you should be okay. If you do see some error messages, either paste those messages into google or post a message on Linuxquestions.org for quick answers and help. The installation will install the GPUTILS to /usr/local/share/gputils. Also, when issuing the su command, you will need to know the root password. To perform the installation, issue the following commands (assuming your file was version 0.12.4):./configure make su make install exitTest the InstallationBefore we go an delete the files we no longer need, let's test that the installation went okay. Issue the following commands and ensure each one outputs the version:
gpasm -v gplink -v gplib -vIf each of those commands echo the version of the utility as opposed to outputing an error about a command not being found, then the installation was most likely a success. You can then return to the direcory where the original file was downloaded and remove both it and the make files by issuing the following commands:
cd .. rm -dfr gputils*
Installing GPSIM - A PIC Simulator for Linux
GPSIM is a simulator for the PIC microcontrollers. That means, you can debug the programs you write, and actually "step" through the code one instruction at a time. This allows you to view the changes in registers, ports, memory locations, etc. after each instruction is executed. GPSIM also includes a realtime stopwatch and a breadboard window so you can watch the output pins change based on the code you write. Most of this is very similar to what you get with MPLAB, the IDE from Microchip for Windows. Downloading GPSIMYou can download gpsim from the gpsim website. At the time of writing, the current version was gpsim-0.21.2 and the current version of gtk+extra was 0.99.17. The two files are gpsim-0.21.2.tar.gz and gtk+extra-0.99.17.tar.gz respectively. Download both of these files. The gtk+extra is needed for the GUI for the simulator. Once the files are downloaded, you'll need to open up your terminal program to navigate to the directory where the files were downloaded. Extract the Tarball, Configure, Make, Make Install. Make sure you install gtk+extra first! Since we already stepped through the installation process in the last section, I'm going to combine the steps in this section. Also, keep in mind we're installing both gtk+extra and then gpsim. For gtk+extra, issue the commands:tar -xzf gtk+extra-0.99.17.tar.gz cd gtk+extra-0.99.17 ./configure make su make install exit cd ..And for gpsim issue these commands:
tar -xvzf gpsim-0.21.2.tar.gz cd gpsim-0.21.2 ./configure make su make install exit cd ..Test the InstallationWe can test the installation by executing the command:
gpsimThe GUI should launch and echo a bunch of feedback to the command prompt window. You can ignore that feedback for now, however, you should get the GUI window which looks like the one below.

rm -dfr gtk+extra* rm -dfr gpsim*
Compiling a Simple PIC Program
Now, as with any linux command, executing the command 'gpasm' without any parameters is going to give you a listing of the options available. There are quite a few. I'm only going to talk about a couple of basic ones. You'll have to resort to the manual if you need more details. What we're going to do is compile a simple little PIC program. This program just toggles a pin on Port B, Pin 1. There is no delay, so in actuality it would be blinking at a rate too quickly to detect. However, for our purposes, and to see a simultion working, it'll do just fine. The file is for a PIC16F627. If you do not have this PIC, don't worry. We're just going to simulate it. Actually programming the PIC and testing it physically is beyond the scope of this article. So, first download my little sample PIC program: toggle_led.tar.gz Make sure you download the above file. Do not copy and paste from this webpage or your code may not compile! In a command prompt, much like we did when installing the utilities, navigate to the folder where you downloaded the file and execute:tar -xzf toggle_led.tar.gz cd toggle_ledThat will put you in the directory containing toggle_led.asm. To be sure, issue the ls command and verify that toggle_led.asm is there:
[user@localhost toggle_led]$ ls toggle_led.asm [user@localhost toggle_led]$Below is the listing for toggle_led.asm. I'm not going to explain this code, as that's another article at another time. It's assumed you know the very basics of PIC programs.
;****************************************************************************** ; * ; Filename: toggle_led.asm * ; Date: 04.03.2004 * ; File Version: 1.0.0 * ; * ; Author: Micah Carrick * ; * ;****************************************************************************** ; NOTES: * ; * ; * ;****************************************************************************** ; CHANGE LOG: * ; * ; * ;******************************************************************************list p=16f627 ; list directive to define processor #includeNow execute the following command to complie the ASM file:; processor specific variable definitions __CONFIG _CP_OFF & _WDT_ON & _BODEN_ON & _PWRTE_ON & _ER_OSC_CLKOUT & _MCLRE_ON & _LVP_ON ; '__CONFIG' directive is used to embed configuration data within .asm file. ; The lables following the directive are located in the respective .inc file. ; See respective data sheet for additional information on configuration word. ;****************************************************************************** ; VARIABLE DEFINITIONS * ;****************************************************************************** w_temp EQU 0x70 ; variable used for context saving status_temp EQU 0x71 ; variable used for context saving ;****************************************************************************** ; RESET VECTOR * ;****************************************************************************** ORG 0x000 ; processor reset vector goto main ; go to beginning of program ;****************************************************************************** ; INTERRUPT VECTOR * ;****************************************************************************** ORG 0x004 ; interrupt vector location movwf w_temp ; save off current W register contents movf STATUS,w ; move status register into W register movwf status_temp ; save off contents of STATUS register ; TODO: Place ISR (Interrupt Service Routine) here... movf status_temp,w ; retrieve copy of STATUS register movwf STATUS ; restore pre-isr STATUS register contents swapf w_temp,f swapf w_temp,w ; restore pre-isr W register contents retfie ; return from interrupt ;****************************************************************************** ; MAINLINE CODE * ;****************************************************************************** main bsf STATUS, RP0 ; Select Bank 1 bcf TRISB ^ 0x080, 1 ; Enable RB1 for Output bcf STATUS, RP0 ; Select Bank 0 toggle bsf PORTB, 1 bcf PORTB, 1 goto toggle END ; directive 'end of program'
gpasm toggle_led.asmAfter compiling, type ls again to ensure you have 3 new files:
[user@localhost toggle_led]$ ls toggle_led.asm toggle_led.cod toggle_led.hex toggle_led.lstThe toggle_led.hex is our actual program which would be "burned" into our PIC. This is the file we are going to have GPSIM simulate. However, toggle_led.cod is also important. We're going to pass that to GPSIM so that we can have a nice "Source Window" where we can step through our code line-by-line. We'll go over that in the next section.
Simulating a Simple PIC Program
Now we're going to use GPSIM to "Simulate" the program running on the PIC. This is the cool part. This is an incredibly valuable tool in debugging your PIC programs. It alows you to slow it all down and actually step through your program and watch it run. So, using our toggle_led.asm program, let's get simulating. Execute the command:gpsim -pp16f627 -s toggle_led.cod toggle_led.hexThis command has a couple of important arguments. First, -pp16f627 tells gpsim what PIC it's simulating. Next, -s toggle_led.cod tells gpsim which file to use for the debug info. This is extremely important as it gives us our Source Window. Not only will the gpsim GUI show up, but you'll also get a bunch of text output to your command window. You can just ignore that text for now. We're going to use the GUI. What we want to do is arrange some windows on our desktop so we can watch things happen as we step through the code. Make sure you have the 'Source Browser', 'Symbol Viewer', and 'Breadboard' windows in addition to the main window and arrange them on your desktop as best you can so that you can see them all (may not be possible depending on your screen resolution). You can hide/display these windows from the 'Windows' menu item in the main windows (The main window is titled '0.21.2' which is the version). My layout looked like the display below (click the image to see full size):
I'm not going to talk about the Symbol Viewer in this article, I just had you open it so that you can see the various symbols change as we step through the code. The main thing to notice, when we first start gpsim, is the little green arrow in the Source Browser window. This little arrow shows us the next instruction in our source code that will be executed when we press the 'Step' button in the main window. Press the 'Step' button in the main window 3 times. This advances the green arrow to the line:
bcf STATUS,RP0 ; Select Bank 0However, this is the next instruction to be executed. The actual current state of the simulation is a result of the prior instruction:
bcf TRISB ^ 0x080, 1 ; Enable RB1 for Output


bsf PORTB, 1That instruction sets bit 1 of portb. Again, this is going to be notated in our Breadboard window. This time, to denote the pin being set, the pin goes red:

From Here...
Well, from here you're on your own. Read the documentation, experiment, and ask questions. You may want to join the GNUPIC mailing list. Once you've got your PIC program working, and you're ready to program the chip, you're going to have to find the software for Linux that works for your particular programmer. If you're using the KDE Desktop, there's a great IDE (Integrated Development Environment) for the PIC including a generic programmer called PikDev. Personally, I'm now using Gnome exclusively, but I have used PikDev in the past and it's built in programmer worked with my Epic Plus programmer after making the modification discussed on the PikDev website.
Categories:
Linux, Robotics & Electronics
Copyright © 2004 - 2010 Micah Carrick. All Rights Reserved.

30 Comments about "Intro to Programming PIC Microcontrollers in Linux"
RSS Feed
I encountered a few problems installing gpsim on ubuntu and in the end all you have to do is
execute this two commands :
sudo apt-get install gputils
sudo apt-get install gpsim
:D
Cheers!
1/ In a dumy directory :
apt-get source gpsim
apt-get build-dep gpsim
2/ then use the procedure above (configure, make install) ...
Bye
Lolo
Good work.. a gr8 article.
gpsim: symbol lookup error: /usr/lib/libgpsimgui.so.0: undefined symbol: _ZTI14address_symbol
Any solution ?
sudo apt-get install gputils
sudo apt-get install gpsim
At the time of this (comment's) writing gputils installs in version 0.13.6-1 and gpsim 0.22.0-5
No need to compile yourself and very useful for someone on a 64-bit AMD platform like myself - no need to hunt down the right .deb package. I have all pieces running on Ubuntu 8.10 AMD64 and will try soon on a 32-bit 9.04 laptop
Thanks!
but i got a problem when i tried to run gpsim. i used the command line as you said:
$ gpsim -pp16f627 -s toggle_led.cod toggle_led.hex
then, i got error:
---------------------------------------------
gpsim - the GNUPIC simulator
version: Release 0.22.0
type help for help
**gpsim> SimulationMode:52
WARNING: command line processor named "p16f627" is being ignored
since the .cod file specifies the processor
WARNING: Ignoring the hex file "toggle_led.hex"
since the .cod file specifies the hex code
FIXME: HLL files are not supported at the moment
**gpsim>
----------------------------------------------
it looks like the p16f627 is not supported by gpsim. however, i checked this website
(http://gpsim.sourceforge.net/gpsim.html), the p16f627 is fully supported.
by the way, i installed gpsim by using $sudo apt-get install gpsim
thanks, i am looking forward your kind reply.
Thanks.
In pic programming part I got many things I didn't know so thank you very much!
try installing from here:
http://gtkextra.sourceforge.net/
I have it working on slack
configure: error: Cannot find gtkextra-2.0 package
Tried to find gtkextra in SourceForge without result.
Can you explain why it is looking for gtkextra-2.0 and not gtk+extra-0....
Had some problems installing the gtk-extra so i used apt-get install gpsim.
Thanks for this guide.
Christopher
-BaC
-BaC
Many thanks for your work here. I had similar problems to SW under xubuntu but synaptic seemed to take it in its stride.
Once again - Thank you for getting me started
Stuart
sudo apt-get install gpsim
Worked for me (Ubuntu 7.10)
Thanks for the tutorial,
Justin
It's possible that the version you have is simply too old.
If you continue having throuble, you may want to post the error on a Linux forum website as it's pretty common compatibility issues that any experience Linux user should be able to help with.
[sw@localhost gpsim-0.22.0]$ ./configure
checking for a BSD-compatible install... /usr/bin/install -c
checking whether build environment is sane... yes
checking for gawk... gawk
checking whether make sets $(MAKE)... yes
checking whether to enable maintainer-specific portions of Makefiles... no
checking build system type... i686-redhat-linux-gnu
checking host system type... i686-redhat-linux-gnu
enabling gui support
disabling gpsim socket interface
checking for pkg-config... /usr/bin/pkg-config
configure: error: Cannot find gtkextra-2.0 package
[sw@localhost gpsim-0.22.0]$
I am loading the RAM window instead of the SYMBOL window, I can watch the W register, and the file registers change values. This is so important to me. I am glad I found it, or I would move to MPLAB in Windows, moving to the Dark Side.
Ed in Eastpointe
Leave a Comment about "Intro to Programming PIC Microcontrollers in Linux"