This article is currently under construction, so it's not nicely done.


Things you need to program PICs using C in Linux:


First step: Install programmer

In my case, the programmer is a Velleman K8048, which is still not packaged with any of my OSes (Archlinux and Gentoo), so I had to download and build the sources. This was rather painless. All I had to do was download the software, extract it, hit "./configure", "make" then "make install" and I was done. For more information browse through their site.

If you have compiled k8048 successfully, then running "k14 i" as root with your programmer plugged in should give you some information on your programmer, in my case I got the following:

[2000] [USERID0]  d4 .
[2001] [USERID1]  ff .
[2002] [USERID2]  ff .
[2003] [USERID3]  ff .
[2006] [DEVICEID] 07a0 DEV:7a0 (3d) REV:0 PIC16F627

This makes sense and the programmer has 5 slots for PIC chips (you can only use one at a time though), four for various PIC's and one ICSP connector. So all is well from the programming perspective.

To test the programmer, you can enter the "velleman" folder in the k8048 source, and try writing the demos to your PIC. You can use the following command to do this:

k14  -p ./demo2.hex

With any luck it will program your PIC and when successful, it will print out the total amount written (e.g. "TOTAL: 131"). Flim the switch to "RUN" and your PIC should start displaying LED patterns.

Second step: Install C compiler

Well, we have a functioning programmer and PIC, now we need a compiler.  The C compiler I chose is SDCC, which was in portage. This just involved me emerging it. Gentoo was nice enough to provide the include files for the PIC16/18, so no extra work was required on my part. For a more detailed explanation on building sdcc and required libraries, have a look here.

Third step: Putting it all together

Well, now we have a compiler and programmer, lets try to make a simple C program. Open up your favorite text editor and type in the following:

char test;
 void main(void) {

This simple program does nothing, it just tests to see if sdcc is compiled and functioning. Save it (I called it "sdcc_test.c") and compile it with the following command:

sdcc ./sdcc_test.c

It should compile with no errors, when done if you look the directory you should see quite a few files, like in my case:

sdcc_test.asm  sdcc_test.ihx  sdcc_test.rst
sdcc_test.c    sdcc_test.lnk  sdcc_test.mem  sdcc_test.sym
sdcc_test.cod  sdcc_test.lst  sdcc_test.rel

So far so good, now lets see if we can make our microcontroller do something useful. Fire up your editor and paste in the following:

 Micah Carrick
 Toggles an LED on Pin 1 of PORTB on a PIC16F627. Written
 as a sample for the article on using SDCC and GPSIM in
 Compile: sdcc --debug -mpic14 -p16f627 toggle_led.c
 Simulate: gpsim -pp16f627 -s toggle_led.cod toggle_led.asm
/* Define processor and include header file. */
#define __16f627
/* Setup chip configuration */
typedef unsigned int config;
config at 0x2007 __CONFIG = _CP_OFF&
#define b1 0x02 /* pin 1 on PORTB */
#define B_OUTPUTS 0xFD /* value used to setup TRISB */
void main(void) {
 /* PORTB.1 is an output pin */ 
 while(1) { /* Loop forever */
 /* toggle bit 1 */
 PORTB = (PORTB ^ b1); 

This code turns on a LED on the k8048 board. When compiled with the flags specified, I got the following:

sdcc --debug -mpic14 -p16f627 ./led_test1.c
led_test1.asm:163:Warning [230] directive ignored when debug info is disabled
led_test1.asm:184:Warning [230] directive ignored when debug info is disabled
message: using default linker script "/usr/share/gputils/lkr/16f627.lkr"

Which seems ok. This time if you list the contents of the directory you will get some interesting additions:

led_test1.adb  led_test1.c   led_test1.cod  led_test1.lst  led_test1.p
led_test1.asm  led_test1.c~  led_test1.hex  led_test1.o

We have a hex file, which we can program directly into our PIC.

k14 p ./led_test1.hex

And if successful, you should have k14 print you a total number of lines supported.  That's it, run your microcontroller to see if it all works as expected, and have fun coding :-)