View Full Version : [Discussion] C Programming Recommendations

06-30-2008, 09:49 AM
I know alot of us here in the TRC are C programmers, or at least have dabbled in C a bit, so this thread is meant to recommend some good resources for those of us (including myself) that do not know C, but wish to learn it. Now, this isn't C++, C# or anything, but straight up C.

Whether it be a book, a link, a PDF or what, post it here:D

06-30-2008, 06:01 PM
The K&R (Kernighan & Ritchie) book "The C Programming Language" was called the C Bible in college. It is one of the few college books I still use often.

06-30-2008, 07:19 PM
Yuppers - K&R is the bible. Back with my first edition it was all so much simpler, it's actually nice. Very little to learn, but still the absolute foundation to anything you'll do today.

06-30-2008, 08:53 PM
Great to know! I'll pick up this book asap:) What about C and microcontroller programming, anyone?

06-30-2008, 11:31 PM
Well, today's microcontrollers basically ARE yesterday's PCs... So really, once you learn your basic C, you're set to start digging into the particulars of your chosen microcontroller. I'm a huge fan of the AVR chips. They have a microcontroller for every occassion, from the miniscule widdle ATTINY's on up through the ATMEGA128 and beyond. The tool chain is free and standard (GCC under WinAVR), and if you get a good programmer (I like the STK500) the DVD comes with full documentation, spec-sheets, app-notes, etc.

This is the reason I argue that today's microcontrollers may be the absolute best way to learn C programming. They don't overkill it as today's modern libraries and environments do. It's just good clean ANSI C programming. Compile, Link, Load the chip, and hit the reset. Boom. It runs or it doesn't. Immediate feedback with the advantage of being able to talk to the world outside of your computer.

The Arduino is arguably better for this, but it's expensive to be dropping it on a project and adding another one to your collection every few days. The smaller ATTINYs cost pennies. I buy the ATTINY45's and 2313's by the tube full. They require almost no glue hardware for smaller projects. You can do real work with a couple resistors, whatever sensors, LEDs, etc you have laying around, and a marginally clean power source.

The STK500 can be used as a learning board too, with 8 switches and 8 LEDs, and comes with a reprogrammable ATMEGA 8515 pre-installed.

07-02-2008, 01:00 PM
Hey Alex. I've been programming in C for a little while, having come from Java. I can't provide a specific book for you, as I mostly taught myself through looking at other people's code, but if you have a specific question perhaps I can answer it with an example :) You'll definitely want to have this: http://www.acm.uiuc.edu/webmonkeys/book/c_guide/. It is the basic C API. And as you can see, there's not much there!

I don't use too many outside libraries myself, but I think this is a must have for all C programmers: http://bstring.sourceforge.net/. It's a library for manipulating strings of characters that, while still fairly minimalistic, is world's better than manipulating your characters directly. It's probably not necessary for real small scale microprocessor stuff, but if your robot is running on a small computer and needs to parse sentences or large amounts of text data, I can't recommend this enough.

I agree with Adrenalynn on AVR being awesome for programming with C, which I started messing with after using the Arduino. When you program, you're basically just using C with an extra library filled with micro processor specific functions, so once you know C, it's almost nothing to jump into AVR programming.

07-02-2008, 01:31 PM
Awesome wireframewolf! That C guide is definitely going to help me! Straight and to the point, just the way that I like to learn:D I don't have any directly C related questions yet, but I'm sure eventually I will.

So, as far as boards and programming in C go, do you think the Arduino is a good start? How exactly do you program the Arduino in C? I knew there was a way to program it in C (outside of it's own proprietary language), but I haven't had the time to look into it yet:(

07-02-2008, 02:11 PM
Well, the arduino board centers around an AVR chip that is just like any other AVR chip. The difference is the bootloader that comes preprogrammed on the chip, which allows for programming the microprocessor through serial commands rather than needing a hardware programmer.

The board contains some other things, like a timing crystal, capacitors, leds, etc, as well as an FTDI232 chip which, with the help of a driver, lets your computer talk with the chip (through usb, but the driver really makes it act like a simulated com port). This allows you not only to program the AVR with the arduino software, but if you'd need a chip similar to this for any kind of communication between a microchip and your computer.

As for the arduino language, it can be thought of as a simplified 'subset' of C. Basically they took C and made it easier to write (simpler syntax) and included some libraries that have functions very specific to the board's AVR. What's really neat though is that all the libraries and backends for the arduino language are just straight C and C++ code, so you can write your own C/C++ stuff, include your header file in your arduino program, and then call functions from your arduino code.

Now, if you decide at some point that you want to delve into AVR C programming and skip the Arduino language/IDE altogether, you can get yourself a hardware programmer (I use this one: http://www.ladyada.net/make/usbtinyisp/) and an AVR toolchain (winAVR if you're a windows user, AVR MacPack for a Mac user), and program the AVR on the arduino without use of the FTDI chip.

Geeze, I know this all seems confusing reading it here, but I promise it will become clear once you get into it. Is the arduino a good starting point? Definitely. It'll get you going from 0 to 60 in 3 seconds. And it's not an underpowered beginners toy either (*cough*Basic Stamp*cough*). As far as microcontrollers go it's the real deal. I'm using it in my robot now :)

07-02-2008, 02:55 PM
110% behind you, 'Wolf. The Arduino is more limited than I'd like, but not a bad place to start. If you're looking for more power, you can pick up any of the more advanced Atmel (AVR) chips for dirt-cheap-nothing from Mouser or Digikey. I use the STK500 programmer from Atmel directly, and it'll program any of the through-hole chips, or with just a little magic, the surface-mount chips. Mouser has a good price on them and Digikey just a bit less good price.

Here's a photo programming an ATTINY45: 376

Programming them in C is identical to programming C in Linux. The tool chain is Gnu GCC with all the microcontroller libraries and port already worked out for you. It's called "cross compiling" - compiling for a platform other than what it will run on. Then you just load the AVR Tools GUI and point it at the compile HEX file and send it to the chip with a couple mouse clicks.

The Arduino insulates you a bit from that, either by using its WIRE language, or by using it as a programmer (for the ATMEGA168 only, pretty much). I have a couple Arduinos 'cause they're handy, but you can build a microcontroller project with nothing but a breadboard and a couple resistors. I tend to prototype my code and circuit on the Arduino, then ship it off to a dedicated "right-sized" microcontroller that is nearly free. (Less than a dollar for a lot of the TINY series in quantity ten)

Let's see if I can give you an example:

* this function transmits a data byte to the I2C bus (polling)
int8_t i2c-tx-data(uint8_t data, uint8_t expected_status, uint8_t verbose)
uint8_t status;
millisec_count = 0;

/* send a data byte */
TWDR = data;
while (!(TWCR & _BV(TWINT)) && (millisec_count < I2C_TIMEOUT));
if (ms_count >= I2C_TIMEOUT)
if (verbose)
i2c_error(s_i2c_data_tx_error, TWCR, TWSR);
i2c_error(s_i2c_timeout, TWCR, TWSR);
return -1;
status = TWSR;
if ((status & 0xf8) != expected_status)
if (verbose)
i2c_error(s_i2c_data_tx_error, TWCR, status);
return -1; //error
return 0; //success

07-20-2008, 04:34 PM
I've just jumped back into the C programming with the bioloid kit, and turns out 4 years seems like forever in coding..

Although we only scratched the surface of C in school, I find it very hard to 'get back on the horse' with the coding.

That being said, I really appreciate the tons of info that's readily available on this forum, this thread is just what I was looking for..

Oh well, at least I got the damn interrupts going :wink:

07-26-2008, 11:00 PM
I have programmed in C for about 25 years now. 'straight up C', I like that one, but the standard indeed pretty much is based on K&R nowadays, but nothing much really changed over the years. (There used to be ANSI C, IBM C (EBCDIC C ? *lol* ) and a few other exotic ones (like Watcom). The nice thing about C is that it is compact and fast. (it's also easy to incorporate ASM). basically, if you can do it in 'a language' you can do it in C too, and probably with faster more compact code. It might be a hassle to do things that tend to be done with objects etc. Programming windows boxes is just no fun and too much hassle nowadays. (Better use something like visual studio or so, but bigger etc.)
For robotics, when you need to send data to specific ports or want to move data around from one port to another or so, you'll find that it is pretty easy to do in C

There are tons of code examples on the web. If you need to write some function/routine, someone probably did something similar and a lot, if not most, is free or open source.

Books that I always had around:
The books from Richard Stevens (Unix network programming series and advanced programming in the unix environment (those are classics)
Than there is 'born to code in C) and of course 'numerical recipes in C'

If you get used to using structures, pointers and know how to do some pointer arithmetic you'll see that the 'structure' of C has some familiarity to java, java script, php etc. *ducking*
. And of course you can always ask around.


07-26-2008, 11:43 PM
Ahh, Watcom. Super fast register math. Nothing else needs to be said.

07-27-2008, 01:07 AM
oh yes, and the crosscompiler and optimizations were out of this world

Ahh, Watcom. Super fast register math. Nothing else needs to be said.

07-27-2008, 01:11 AM
Yeah, it generated 80286 objects that I couldn't hand-optimize more than a couple percent better. :)

Man could you get DES running fast on it. Almost 700&#37; faster than the best Borland could muster.