Use C for PIC Programming!

From OpenCircuits
Jump to: navigation, search

Contents

Use C for PIC Programming!

( see history for author information )

This can be part of a religious war, but when beginning programming one has to choose a language, choosing the right one can be an important. Most of these remarks apply to other microprocessors and to other higher level languages like Basic..... ( but see remarks later on not choosing other HHL's )

Almost all the heavy duty guys at places like the piclist.com seem to think assembler it the way to go with PICs. I beg to differ, perhaps because I am just a hobbiest, perhaps because I have a lot of experience in high level languages and have trouble programming like it was the 1950's. I particurlarly like BoostC for info on it see BoostC tiny Wiki, for other versions of C or even other high level languages ( for which many of the arguments are similiar to the ones for C ) see the compiler section in PIC Links

Generally

Using a higher level language brings your thinking closer the problem you are trying to solve and less about the hardware you are using. The syntax is much more like english with formatting that helps you understand what you ( or someone else ) has written. This and some of the reasons below mean that I program more easily, faster, and with better results. I may not write the fastest, smallest program, but they are fast enough and fit in the memory that I can easliy afford. I may not have the most hair on my chest, but I am proud of my projects.

Local variables

Easy in C, not even sure how to do it in assembler. Saves memory, hides information that should be hidden.

Memory

Assignment of memory for variables much easier in C.

Types

Type checking is stronger in C than assembler ( if it even exists ) and I am a believer in strongly typed languages.

Interrupts

Do not have to know much esp about state save and restore, the compiler does it. Where is the interrupt in memory? who cares the compiler takes care of it.

Call Stack

On the 16F877 for example I can call 8 levels deep with its stack. Need to go deeper? This is a big deal in assembler, in BoostC just change the linker options to add a software stack and call away.

Data types

Assembler is close to data type free, there is memory and registers they mean pretty much what you want. If for example I have a working program counting something and I realize I need signed number instead of unsigned I can pretty much change it with a few changed declarations, in assembler no ( easy ) way.

Portability

Much easier in C to move from one PIC to another. In a simple case just change the target of the compiler and recompile. If it does not compile tweak and then recompile. There is also more hope of moving code to a different type of microprocessor without total recoding.

What I think about when I code

In assembler I think about the PIC and how it works. In C I think about the problem I am trying to solve. I get a solution much quicker with C.

Bank Switching

In C pretty much forget ( or never learn ) about it.

Can use assembler if I want to

At least BoostC lets you insert assembler where you want to. Assembler does not let you insert a higher level language here and there in its code ( please do not say that macros are a lot like a higher level language, they certainly are not for the beginner ).

Libraries

Most C implementations include libraries for some common tasks like RS232 communications. You can build this sort of thing for assembler, but it is not so easy to find a package that is ready to go and understandable.


Some may agree with me

and some not, this is for the pro side.

4. Compilers

The case for assembler

Smallest fastest code and no time debugging subtle compiler bugs.

Why not another High Level Language

Another high level language may be a choice if you are in a special circumstance. It probabably will be better than assembler, but not better than C.

Not better because:

  • Not portable across as many environments.
  • Not as wide a literature particurlarrly on the Web.

Further References

the part on assembler is in at about: 12:15 ( is assembler really for dick heads? )


Personal tools
Namespaces

Variants
Actions
Navigation
Toolbox