PIC compilers: a love-hate relationship


One of the reasons Arduino is so successful lies in its IDE. Free, simple to use, comes loaded with a lot of high-level functions. Furthermore there is a great community of Arduino coders, with thousands of code libraries available for free.

This article is more than two years old and might contain obsolete information; it is still kept here for informational purposes.

Things are not so great for PIC microcontroller fans. I used C18 for some time, with the old MPLAB IDE. I could do the work with it, but it wasn’t fun. Compared to coding with Arduino, it’s a pain in the  well-you-know. Did someone ever made the LCD library that comes with C18 work on PICDEM2 plus? Considering also the lack of a community willing to share the written code, it’s no wonder that I soon started to hate this compiler.

In February 2009 Microchip bought HITEC, a company that was making one of the best compilers for 8-bit PIC microcontrollers. After this, XC8 and MPLABX were born. While the idea of making an IDE that is platform independent, the implementation sucks. On Windows x64 systems, with both 32 and 64 bit Java Virtual Machines installed it crashes, and it also has big issues when working with USB hardware. Making it work is cumbersome, and it only adds to the frustration level. Once I made it work I only found it so different compared to MPLAB, it’s so complicated that I soon started to hate it even more than MPLAB. The migration from C18 to XC8 is not easy either. And there is also the issue that the free version of XC8 bloating the code. The pro version costs too much for me (about 900 USD).

Things changed when I discovered the MikroElektronika range of Click boards. I got a bunch of them, together with an Arduino adapter shield. Those boards are not just hardware, but code examples for MikroElektronika  compilers are also provided on www.libstock.com. This has reopened my interest towards PIC microcontrollers. So, here I am, downloading ans installing the MikroC compiler for PIC.

WOW. Love at first sight! A clean and easy to use interface, loads of working libraries, a good community willing to share the code. Time to say goodbye to C18 and XC8. Some will say this is only a compiler, not a full IDE. I don’t care. I don’t write code for dsPIC’s or PIC32’s. I haven’t written code in assembly language for more than 6 years. For me, this compiler is great and easy to use. The only issues are that the free version creates only 2kb of code, and it works natively only with the mikroPROG programmer/debugger.

I solved the first issue by buying a USB dongle license. Only 249$, and I can take it wherever I want. I work in my office, than I plug it in my PC. I do field work – it goes with the laptop. I lose it – I will have to buy a new license (not funny).

The second issue is solved using the PICkit 3 stand alone programmer app from Microchip website. If you don’t find it, it’s well hidden in the download archive page. There will be no debug mode though, but I can live with this until I will buy a mikroPROG.

The test: LED blink

To show you how easy is to work with this compiler, let’s take the blinking LED example.

One you start mikroC the first screen, you can choose to create a new project, open an existing one or open the examples folder. There are also shortcuts for the most recent projects.

mikroC start screen

mikroC start screen

Once you click the New Project icon, a wizard opens. The first step is to choose the project name, path, microcontroller type and clock frequency. Here I use a pic18F45K22, running at 4MHz.

The next step allows adding of files to the project. There are no files to be added, so I clicked next.

Now comes the fun part: no more #include directives. I just went into “lazy mode” and selected all libraries, leaving mikroC to do all the library management (this can be changed later using the library manager).  In the next screen I clicked on “Open Edit Project to set Configuration bits”, than clicked on Finish.

Another fun part of mikroC: no more #pragma config directives. All the configuration bits are set in the Edit project window.

Finally, the code window looks like this:

mikroC code window

mikroC code window

The compiler messages are:

Available RAM: 1515 [bytes], Available ROM: 32768 [bytes] 
Compilation Started Blinking LED.c
Compiled Successfully Blinking LED.c
All files Compiled in 78 ms  
Used RAM (bytes): 0 (1%)  Free RAM (bytes): 1515 (99%) Used RAM (bytes): 0 (1%)  Free RAM (bytes): 1515 (99%)
Used ROM (bytes): 112 (1%)  Free ROM (bytes): 32656 (99%) Used ROM (bytes): 112 (1%)  Free ROM (bytes): 32656 (99%)
Project Linked Successfully Blinking LED.mcppi
Linked in 281 ms  
Project ‘Blinking LED.mcppi’ completed: 468 ms  

Code listing is:

Assembly file generated by mikroC looks like this:

In the assembly listing I noticed that the code for Delay_ms(1000) appears twice. Not the thing that I would do when writing assembly myself: it would be enough to write this routine only once, than use the CALL instruction twice. Maybe I just ask too much…

Programming the code

One the PICkit 3 standalone app is started, I selected the PIC18K45K22 microcontroller, turned on the 5V power supply and then selected “Auto import HEX + Write device”. After selecting the correct .hex file, the PICkit 3 application monitors it constant;y, and reprograms it every time it detects a change. That is, whenever I recompile it inside mikroC the microcontroller is reprogrammed automatically.

PICkit 3 blink example

PICkit 3 programming the “LED blink” example





  1. Avatar

    I’m not a big fan of the Arduino IDE at all and instead use a licenced copy of mikroPascal Pro for PIC. I agree that the compiler interface and functionality is a breath of fresh air.

    However any AVR fans thinking of moving away from the Arduino IDE to mikroC / mikroPascal or mikroBASIC for AVR may like to think twice, as the persistent lack of a native USB library is a serious omission IMO and a great handicap when programming the more recent USB-capable AVR chips.

    Perhaps it’s why mikroe made the Arduino adapter shield available – to concentrate on selling click hardware instead of adding new libraries to their AVR compilers.

    For PIC chips mikroe compilers are hard to beat. However things are not so great for fans of the mikroe compilers and USB-ready AVR chips.

  2. Avatar

    Hi Teodor. I bought my MikroBasicPro as a standard lic and put it on a flash drive. I then cloned it with
    HP USB Disk Storage Format Tool or similar so I always have an extra or three just in case.
    Since I bought my Mikrobasic as a one user copy I can get a new key if I were to have a hard drive crash etc.
    but this works just the same as yours.
    You may be able to clone the dongle you have for a safe copy too. Just a thought.

    • Teodor

      Of course, if I lose the USB dongle I will have to buy a new licence. This is something I can live with. But the main advantage in using the dongle version is that I can move it betweeen the desktop PC and the laptop. I can do field work with the laptop, and I can benefit from the convenience of the desktop PC when I’m working from the office.
      Regarding the software itself I don’t worry much. I can always download the latest version from the mikroe.com website – no need to keep backups myself.

    • Teodor

      And one more thing. The USB dongle is not a mass storage device. It’s a sort of HID device, so don’t even think of using something like the USB cloner. Besides this, any attempt to clone the USB dongle will violate the licence terms.