Books on C
February 11, 2009 5:09 AM   Subscribe

What books would you recommend for embedded C programming?

I'm involved in new group project (University) that will eventually evolve into an autonomous path-finding robot. FYI, it will probably use a PIC32 processor.

I have a background in programming, and a good (practical) understanding of Java and C# having used them a lot over the last four years. However, pure C is not my strongest point, and I'm needing a good brush-up! I've done a bit as part of a module a while back, but certainly not enough to dive into detailed algorithms.

What good books (or online resources) would you recommend to someone who has a reasonable set of experience with programming in general but needs a bit more to read on pure C?
posted by gkhewitt to Computers & Internet (6 answers total) 2 users marked this as a favorite
 
Best answer: K&R is still the book.

Embedded ("non-hosted") means you don't have the standard library, and that you don't get a program loaded (you just jump to an instruction) so you don't have main(), anything that happens before main (code relocation), or anything that happens after main (atexit, etc.).And you may not have dynamic memory allocation (malloc, etc.).

What you'll probably find is that some portion the the standard libs will be ported into the project, just because: everyone knows them, everyone uses them, and there are good public domain/BSD licensed implementations of them.

Probably the hardest thing to get your head around will be no dynamic memory; basically you'll either have to put together your own malloc, or get used to using static structures exclusively. For that (well, actually for either case), it's very useful to know that the C Standard guarantees that pointer to struct can be portably converted to a pointer to the first element of the struct and back. It's also important to know that the Standard makes few guarantees about alignment and padding, so you'll have consult you compiler's documentation for your target architecture to get this right (and right a bunch of cryptic headers to achieve it).

You may find perusing the Rockbox source code to be informative; that's embedded code that runs more-or-less portably (with lotsof header hacks) on numerous different architectures.
posted by orthogonality at 5:37 AM on February 11, 2009


I'd recommend the O'Reilly book "Programming Embedded Systems" for intro to embedded. For pure C, K&R's "C Programming Language" is still an oldie but goodie.
posted by RobotVoodooPower at 5:38 AM on February 11, 2009


Nthing K&R. If you can, use AVR instead of PIC.
posted by phrontist at 6:46 AM on February 11, 2009


You can download the source for Micrium's μC/OS II, an embedded RTOS. It runs on PIC32 and many other architectures. It has a corresponding book.
posted by mkb at 6:58 AM on February 11, 2009


The O'Reilly book Programming Embedded Systems mentioned above has a very clear layout, and I did find it useful. But please do note that it uses a specific dev kit for examples - an Arcom / Eurotech board I think. Might not be exactly what you are after.
posted by Tapioca at 9:08 AM on February 11, 2009


Best answer: Since you're already proficient in C# I suggest you read "C Programming Notes". This will give you information about pointers (which can be problematic to understand for some people.) You'll also want to read "Bit Twiddling Hacks" to better understand bit manipulation in C. Some of these may come in handy when you're dealing with I/O pins and setting configuration registers.

I also suggest you read the code examples provided by Microchip in their application notes. They can be very useful.

As an aside:

The path I took was to learn the Microchip instruction set (PIC16) from the data sheets. Then I wrote a few simple applications in assembler like blinking an LED and generating a tone from a speaker.

When translated to C the same applications took far fewer lines of code but the breakdown of branches and subroutines was almost an exact match to the assembly versions. With some tuning, the compiler emitted efficient code that compared favorably with the hand coded assembler.

So, really I learned it wasn't the language I was using so much as the difference in code style. The strategies used in writing C to run on a resource wealthy PC are different from the strategies used on a comparatively limited microcontroller.
posted by plainyogurt at 9:43 AM on February 14, 2009


« Older best websites for cheap weekend fares from my city...   |   Help me find this Chinese pop song! Newer »
This thread is closed to new comments.