NOTE: This page is a little out of date now - I plan to revise it when I get a chance

How to Write Standard Code in C++. There are infinitely many ways to program computers. Ultimately, it is the choice of the programmer how to accomplish what they need. Hour 2 - Writing Your First C Program. Cut your own wood and it will warm you twice. —Chinese proverb. In Hour 1, 'Getting Started,' you learned that C is a high-level programming language and that you need a C compiler to translate your C programs into binary code that your computer can understand and execute. Programmer, you may well find that assembly language is simpler than BASIC or C. In many ways it’s more like designing a circuit than writing software. The trouble with assembly language is that it’s different for each kind of CPU. There’s one assembly language for Pentiums, another for PIC mi. Hour 2 - Writing Your First C Program. Cut your own wood and it will warm you twice. —Chinese proverb. In Hour 1, 'Getting Started,' you learned that C is a high-level programming language and that you need a C compiler to translate your C programs into binary code that your computer can understand and execute. Writing your program in a text-editor and saving it with correct extension(.CPP,.C,.CP) Compiling your program using a compiler or online IDE The “Hello World” program is the first step towards learning any programming language and also one of the simplest program you will learn. All you have.

Writing your microcontroller firmware in a high level language is so much easier than writing in assembler. Something that might take a week to write and debug in the C language would take months if you were forced to use assembler — and, the assembler code would be buggy and hard to maintain.

Free Paper Writing Program

Unfortunately, due to the compiler unfriendly architecture of the Microchip PIC series, there are few free or low cost compilers available for the low end chips. This is where the users of the Atmel AVR series have a clear advantage, they have access to the GNU C Compiler for that chip.

This article looks at three C Compilers for the PIC series of microcontrollers from the point of view of the cash strapped hobbyist and makes some recommendations as to the best way to go.

Why Use a C Compiler?

Firstly, a few words are necessary to explain why a C Compiler is such a good thing. If you are familiar with C you can skip to the next heading.

Assembler gives you tight control of every CPU cycle and very efficient use of both program memory and RAM. When you write in assembler you are intimate with every detail of your code and this intimacy allows you to cut corners and craft some especially neat solutions. This is essential when you are short of memory and must get the best speed from limited hardware.

This intimacy is also the downfall of assembler when a program gets large. Anything that is much greater than 2K or 3K bytes in size starts to become a nightmare. There are so many variables to keep track of, so many side effects from calling various subroutines, and so much complexity that in many cases it is impossible to continue using assembler. Even an application for the humble 16F88 chip with its 4K of memory would benefit from being developed in C, while complex applications written for the 32 bit chips are impossible to develop for in anything other than C.

What about Basic and Pascal? These languages are good for high level tasks like business software, but they hold you too far from the intimate details referred to above. C also lets you get down and dirty and easily twiddle registers and bits — It is designed for efficiently dealing with hardware.

Finally, all C Compilers give you a get out of jail free card. They allow you to embed assembler directly into the C code. So, when you do feel the need to really optimise the hell out of a routine you can indulge yourself without causing too much of a headache.

Before we start, all compilers listed below have free trial versions with a limited life and they all fully integrate into the Microchip MPLAB IDE (integrated development environment). So, you can always try them before you commit.

CCS C Compiler

CCS (http://www.ccsinfo.com) produce a range of compilers that cover all 8 bit PICs (PIC10, PIC12, PIC14, PIC16, and PIC18 ) and a few of the higher spec chips (PIC24 and dsPIC).

If you needed to summarise the CCS C Compiler in a word, that word would be “friendly”. The compiler quietly handles all sorts of complications and provides you with a range of functions that handle most of the peripherals that come integrated on a PIC chip.

For example, calling setup_timer_1() will do all the hard work in setting up timer 1. Sometimes the compiler can be too friendly, for example it will automatically set a I/O pin to input or output depending on how you use it in your code, but you can turn that off.

The best part of using this compiler is that you can install it, write some code, and the compiler will just do its job without you having to bother too much about the compiler (you might have problems with your code or the language, but that is another issue). The excellent support for the on chip peripherals and extensive libraries that CCS supply make the CCS Compiler by far the easiest to use.

The only downfall with the CCS C Compiler is that it is not ANSI C compliant. It is close, but there are some constructs that will not compile. This is not generally a problem if you are writing your own code, as these constructs are relatively obscure, but it is a problem if you want to use code created by someone else. For example, the Microchip USB stack just will not compile using the CCS Compiler.

It is a pity that the free version of the CCS Compiler is so limited that it is of no practical use, so the hobbyist is forced to shell out real money. It is the cheapest of the three compilers reviewed here but it still is not cheap. Typical prices are in the US$200 to $600 range although you might be able to wrangle an educational version at $50 for one chip.

Hi-Tech C Compiler

Hi-Tech (http://www.htsoft.com) have long been writing compilers for the PIC series for a long time and they have a reputation for producing the best compiled code (compact and fast). Their compilers cover all chips from the smallest 10 bit chip to the latest 32 bit chips.

I would summarise the Hi-Tech compiler as “sporty”. It is fast, efficient but just a little spartan.

The compiler generates tight code, in my tests it is about 8% more efficient than the CCS Compiler. It is fully ANSI C compliant and it handles the minutiae of port and register I/O in a logical and straight forward manner.

The Hi-Tech C Compiler comes with little support for the capabilities of the chip. This means that you must learn to write to the registers and twiddle the bits yourself to get peripherals such as timers, UART, comparators, etc to work. With the CCS C Compiler you could just call a library function. Many think that writing your own interfacing routines is a good thing, as you are fully in control, but it does make for a longer development time.

Hi-Tech produce a “Lite” version of their compilers which is free. Even better, when you read through the comparison charts the Lite version seems to do everything the full compiler does (at US$1,500) but just has some optimisations turned off.

You might think that this is a real godsend but, sad to say, it is not. Hi-Tech have crippled the Lite version far beyond turning off some optimisation - the Lite compiler produces code that is almost double the size that it should be. This is a huge problem if you are using chips with limited code space as it means that you are forced back to using assembler for anything that is of reasonable size.

As of writing (May 2009) Microchip has just bought Hi-Tech. Hopefully Microchip will re look at the Lite compiler and remove some of the penalty attached to it. With the GNU C Compiler available for free for the Atmel AVR series Microchip could be loosing the younger generation of engineers, the ones who are learning to specify and design microcontrollers into the gadgets of the future.

Microchip C Compiler

Writing

Microchip themselves also have a series of C Compilers (http://www.microchip.com). These cover the PIC18, PIC24, dsPIC and PIC32 series. There is no support for the standard 8 bit chips (PIC10, PIC12 and PIC16) so if you use the popular 16F88 chip you are out of luck.

Carrying on the theme of a one word summary, I would call this compiler the “Hummer” of C Compilers. Why compare a C compiler with a huge military vehicle? Because this is a large, clunky, difficult and powerful compiler.

Everything about the compiler seems to match this description. The documentation takes 20 pages to give you 5 pages worth of information, that is assuming that you can find any documentation on the feature that is causing you trouble. The compiler and linker require you to know the most intimate details of the chip and while you can place variables exactly where you want them in memory, you start to wish for the simplicity of the CCS compiler which “just did it”. The learning curve for the Microchip compiler is steep.

On the good side, the compiler is fully ANSI C compliant and the Microchip code libraries compile cleanly, as you would expect. The compiler is efficient and generates compact and fast code.

Microchip compilers also have a free “student mode” and the only difference between this and the full, paid mode is that two optimisations (procedural abstractions and extended instruction set) are turned off.

Writing C Programs For Pic

Procedural abstraction searches for repeated stretches of code and puts them into a subroutine which is called when required. This is quite aggressive and can reduce the code size to two thirds of normal, but constantly calling these small subroutines has a significant impact on performance. The extended instruction set is a feature of the PIC18 and higher series of chips which enable the compiler to generate more efficient code. Running in the extended instruction mode the Microchip compiler becomes even more difficult and picky to use so I have not noticed its loss.

As it turns out, these optimisations are not terribly useful anyway and the compiler does a great job in student mode, and is free.

Wrapping Up

Sadly to say, there is no simple solution for the hobbyist looking to get into C programming for the PIC series.

For

If you have no money at all, then your best choice is to use the Microchip compiler in student mode. This means that you are restricted to the PIC18 and higher series of chips, but this is no great loss as they are almost the same price as the PIC16 series and have greatly improved capabilities. You will have a struggle coming to grips with the compiler and linker but you will get a clean, fast and professional result.

Writing C Programs

If you are prepared to spend some money then the CCS compiler is a good choice for the low end 8 bit chips (PIC10, PIC12 and PIC16) . However, unless you are really rich, I would not buy the CCS compiler for the PIC18 and higher, the Microchip compiler, in student mode, is the best solution there.

If you are buying the CCS compiler, you only need to buy the cheaper command line version. The Windows versions of the compiler include an integrated development environment (IDE), which is not needed as the command line version will integrate into the Microchip MPLAB IDE, and that is an excellent system which is completely free.

Write A C Program For Pic16f877 To Add Two 16-bit Operands

More Reviews

Basic C Programs

Here are some more detailed reviews: