PDA

View Full Version : Discussion AVRs and C


cowana
Dec 10, 2008, 03:46 PM
Hi All.

I am an old hand with PICs programmed in BASIC (PICAXE), and I want to move up to AVRs programmed in C.

I am looking for a good book to learn C with - I am slightly confused by the difference between C for AVRs and C for computers. Does the book need to be specifically for AVR programming? Do you know any good books / how did you learn C?

I plan to write the programs in AVR Studio 4.

Thanks for any help or contributions.

cowana

spinup
Dec 10, 2008, 05:57 PM
The big difference in C on different platforms is the libraries. Here is a good "book" covering the C language on all platforms:
http://cslibrary.stanford.edu/101/EssentialC.pdf

And here is the reference for a standard C library for AVR (avr-libc):
http://www.nongnu.org/avr-libc/user-manual/modules.html

If you're totally new to AVRs and C, I suggest starting with Arduino:
http://arduino.cc/en/Reference/HomePage

The Arduino language is actually C, but it gives you high-level interfaces for lots of common tasks so you can focus on learning to program at first, instead of learning how to do the low-level stuff with the micro. One step at a time.

-Jeff

cowana
Dec 12, 2008, 02:46 AM
That second link looks useful.

Anyone else going to share how they learnt C?

cowana

vintage1
Dec 12, 2008, 05:18 AM
Well i got a book, a C compiler for a CP/M machine, and the kernighan and ritchie.

I realised it was just a faster way to write assembler.

as Spinup says, teh language is one thing, the libraries that come with teh comiler are quite another.

On one project, by job was to take aan 8086 board, and write the libraries, and most of the drivers for an operating system, and port FORTH to it.

Great fun. Starting with hardware access to a floppy disk controller, and making it able to read standard formatted disks..

cowana
Dec 12, 2008, 05:01 PM
Right - so a book on 'learning C' won't be library specific?

cowana

cstratton
Dec 12, 2008, 05:38 PM
Right - so a book on 'learning C' won't be library specific?


There's a sort of standard concept of the "standard C library" but then everybody adds extensions. For example, if you are on a desktop computer and you want to get keystrokes without waiting for the return/newline, there's the windows way of doing it and the unix way of doing it, and similar for every system ever made... And anything GUI-related is of course specific to that gui.

In an embedded system, you end up having to give the compiler and linker a lot of "hints" about the nature of the real hardware, where you want to put parts of your code in memory, etc - all using not truly standard extensions.

But the core content of a decent book on C should be portable.

cstratton
Dec 12, 2008, 05:41 PM
Anyone else going to share how they learnt C?

By modifying other people's code to add additional things that were now needed.

Sort of learning by example - find something that does something related to what you need, and extend it until it is what you need. With reference material, or someone to ask when you occasionally really get stumped, if you are generally able to think algorithmically about what you want the computer to do, you can work in many "foreign" languages this way...

Generally you don't have to understand an entire language to accomplish things with it, you just have to figure out the fraction of its mechanisms that are needed to accomplish your problem. Then you pick up a few more doing something else.

mrdunk
Dec 12, 2008, 06:33 PM
if you want a step by step tutorial on how to set up an AVR programing environment try the SOR $50 robot project: http://www.societyofrobots.com/step_by_step_robot.shtml
the whole project walks you through building an AVR based, servo driven light following robot.
all useful stuff for anyone who wants to go on to interface an AVR with RC equipment.

the tutorial is somewhat thin on C programming advice though but it does give you a good starting point from which you can easily get an AVR running.
for most people getting that far is more of a problem than the actual programming.

i'd recommend the $50 robot as a first step soon followed by a generic book on C.


dunk.

AleG
Dec 12, 2008, 09:29 PM
This is a good book to get started (http://www.smileymicros.com/) you can buy and download it online so you can get started right away.
It's very centered about a particular piece of hardware, the AVR Butterfly but it will take you around all the relevant parts of AVR programming as applied to most micros on the family line.

Also, very useful is the AVR Freaks forum (www.avrfreaks.net). They have an excellent tutorials section and you can ask for help if you get stuck somewhere. On the other hand expect a little smacking at the beggining if you don't seem to be doing your homework, many "Whh don't you read the datasheep, huh?" and the like. ;) But the truth is that you shuld read the datasheet from cover to cover, there you will know what makes a particular AVR tick, with examples in many cases.

Razor_Racer
Dec 13, 2008, 11:26 AM
I agree with mr dunk, the $50 robot tutorial on SoR is the best place to go! The tutorial provides some nice code that anyone can understand and learn from.

village_idiot
Dec 13, 2008, 01:47 PM
I'll second the kit from Smiley Micros. Just a note that the software described in the book has`changed a lot, so you might want to load the software from the included CD while you go through the book, then update it after you finish.

obor
Dec 16, 2008, 07:28 AM
You can also find pretty good tutorial on C/C++ here:
http://cplus.about.com/od/thecden/Welcome_to_the_C_Area_with_Articles_about_C_Develo pment.htm

Olivier