PIC + SDCC + CodeBlocks + Windows 7

Using the SDCC (Small Device C Compiler) is a greate way to fit more code into smaller Microchip PIC parts (Such as the PIC12F/PIC16F series) of microcontrollers.

Having used the XC8 compiler for these parts in free mode, taking a look at the generated assembly will reveal alot of wasted moving of variables to the w register. This is simply to encourage people to upgrade and purchase the compiler. Having used the Pro version of the compiler at work it does indeed optimise these wasted statements away, and further optimises the code in some often interesting ways.

By using SDCC however you always get clean and consistent code that is a pleasure to work with.

My IDE of choice is Code::Blocks, having orginally used it for some simple SDL games engines I have found it invaluable when working on console programs, ARM procjects, AVR projects and now PIC projects.

To setup Code::Blocks to work with the SDCC compiler, we need to download and install the following:

Code::Blocks IDE: We don’t need the integrated MingW compiler, but you may find it usefull for other programming tasks

SDCC: You can grab the latest Binary from http://sourceforge.net/projects/sdcc/files/

GPUTILS: You can grab the latest binary from http://sourceforge.net/projects/gputils/files/

Install each of the above programs making a note of where each program has been installed to.

Once Complete, run the code::blocks IDE:

CodeBlocks1Select the compiler settings window from the tools menu:

CodeBlocks2Select the Small Device C Compiler from the drop down menu:

CodeBlocks3Select other options and type –use-non-free:

CodeBlocks4Select the Toolchain executables tab and navigate to your SDCC installation:

CodeBlocks5Setup the search directories as follows:

CodeBlocks6This completes the generic settings that need to be set for SDCC to compile for the PIC architecture.

The next set of settings are specific to the processor you will be using.

SDCC splits the microchip PIC series into PIC14 (PIC12F/16F) and PIC16(PIC18F).

Click File > New > Project and select Empty Project then follow the instructions. Make sure to deselect Debug

CodeBlocks7CodeBlocks8CodeBlocks9You are now presented with a Empty Project. Next up is to add the device specific settings. Right Click on the Project Title in the Management Window and select Build Options:

CodeBlocks10Select Optimize generated code (for size) and [CPU] PIC16 as we will be targeting the PIC18F452:

CodeBlocks11Select Other options and type the name of part (-p 18f452) as follows:

CodeBlocks12This completes all the settings that we need to add. Now leds add some code!

CodeBlocks13Press Build and you are done. If you go into the bin/Release directory you will find both the generated assembly code (.lst) and Programming file (.hex).

Using your programmer (I have the PICKIT3 and the MPLAB IPE) program the hex file to your PIC and you are done.


One thought on “PIC + SDCC + CodeBlocks + Windows 7

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.