Avr Studio Syntax cheat sheet

Is there a cheat sheet out there that explains the syntax of AVR studio? I have programmed in basic before. I just got an OX2 and can’t seem to get much going. I have been reading a few books on programming in C but none explain how to use it in an AVR. Where would you start if you had NO experience before? I have no idea on how to start header files or anything. I am looking just to make LED’s blink, no LCD, no SPI… Nothing but LED’s and maybe some analog and digital inputs. I would like to be able to start from scratch so I understand whats going on. All the external files for the OX2 are just so overwhelming that I have no clue on what I can touch and what has to stay. So any pointers? I have been looking through the net looking into hoe to program. There are lots of general sites but most give you what you know with another language. I am looking for stuff like:

Portc.2 = 1 “sets portc.2 high”

GetADC(1) “Gets adc channel 1”

Can this be done or does everything have to have some crazy header file with 12 other friends.

Thanks!

Hahaha! Well, I hate to say it but to some extent yeah, everything has to start with some crazy header file and twelve friends. Though they have been known to travel in smaller parties than that.

A good place to start with C programming on micros is Joe Pardue’s book, “C Programming for Microcontrollers”. It uses the AVR Butterfly for its examples, but the ATmega169 on the Butterfly isn’t TOO different from the ATmega644 on the OX2. Some of the stuff in the book won’t be of immediate use (like driving the Butterfly’s LCD, which is completely different from driving the OX2’s LCD), but a lot of it is of immediate utility (digital I/O, analog input, etc.)

I don’t have all the OX2’s stuff rolled in yet, but another place to start is Orangutan-lib. The digital I/O and the analog input stuff will work fine, in any case. (So will the LCD stuff, for that matter.) There are a bunch of example programs in there that you can at least get some ideas from.

Your PORTC.2 example is how the Imagecraft AVR C compiler handles general purpose I/O ports. With AVR Studio it’s a little more involved, but it’s also more standard C:

DDRC |= (1 << 2); // Set PORTC bit 2 as an output
PORTC |= (1 << 2); // Set PORTC bit 2 high
PORTC &= ~(1 << 2); // Set PORTC bit 2 low

But that needs:

#include <avr/io.h> // Include I/O port definitions

For ADC stuff I’ll refer you to the Orangutan-lib example. It amounts to:

#include <avr/io.h>
#include “analog.h”

analog_init();
input1 = analog(1);

But you also need to link in analog.c from the library.

If this is your first exposure to C, that probably won’t help much. The book would help, reading through the library stuff would help, and one other place to look would be avrfreaks.net which is a discussion forum for people doing AVR development. Lots and lots of good info there.

Hope this helps.

Tom

P.S. Best bet for the files on the OX2 is to download the interface code that’s posted on the OS2 page, include all the .h files, link in all the .c files, and all the routines in those files are available to you at that point. The 644 has enough memory you won’t even be putting a scratch in it to just include the whole shebang.

I did order the AVR butterfly book. One question… What is DDRD or DDRC. Are those assembly terms? I am interested in understanding the language so I don’t have to always refer to a cheatsheet or some one else’s code. I eventualy want to be able to write my own code from scratch to use on a project.

Those aren’t exactly assembly terms, those are the nice almost-engilsh names of registers within the AVR. When writing in C, The library <avr/io.h> maps these names to their corresponding addresses so you don’t have to!

As you probably already know, AVRs divide their IO pins into groups or “ports” of up to eight pins each. The ATMega644 on your OX2 has four such ports (A-D) and each port has three eight-bit control “registers”. A register is really just a location for storing a byte of data that controls or reports back on the behavior of the microcontroller. The numbers stored in these registers define the setup and state of the port. Lets talk about an arbitrary Port X, with registers DDRX, PORTX, and PINX:

DDRX stands for Data Direction Register X, and the eight bits of it control whether the pins are set as outputs or inputs. A zero bit defines the corresponding pin as an input (default) and a one bit defines that pin as an output. The line of Tom’s code DDRC|=(1<<2); sets the data direction register of port C to (1<<2), or binary 00000100, making pin 2 of Port C (PC2) an output. Another standard way of writing this in C (with AVR studio and winAVR at least) would be DDRC|=(1<<PC2);.

(By the way, Tom uses the bitwise OR operator “|=”, meaning that only the PC2 bit will definitely changed by this line of code. Other bits ORed with zeros will stay the same. Let me know if this makes sense to you, and the &=~ statement two lines below, because bitwise logic is very important with AVRs and microcontrollers in general. It’s also well covered in C Programming for Microcontrollers.)

PORTX is the register describing the output state of the pins of the port. If the particular pin of a port is defined (by DDRX) as an output, then setting the corresponding bit of PORTX to 1 sets the output high. In Tom’s code, the line PORTC|=(1<<2); sets the PC2 pin high, powering the LED connected to it. Neat trick: if a pin is set as an input and you set it’s PORTX bit high, you connect an internal pull-up resistor, which can be very useful in simplifying external circuits.

PINX is the register describing the input state of the pins of the port. Basically a bit of this register is 1 if the corresponding pin is high, and 0 if it is low, regardless of whether you set it high/low as an output, or if it is being externally pulled high/low as an input.

That and some basic C syntax is all you need for digital IO on an AVR. All of the hardware built into the AVR is controlled by setting bits of registers, while other registers report back on the state of the hardware.

Hope that clears up some things for you.

-Adam

There are some great tutorials on AVRFreaks, including one on doing basic digital I/O. It goes into all the terms Adam explained and then some. Highly recommended:

avrfreaks.net

Tom

I know about AVR Freaks. That was the first place I looked. The problem is when you don’t know what your looking for or at you remain clueless. I ordered the Smiley micros book as an E-book and have yet to get any email other than they took my money. Its been 2 days now I have no link yet… :frowning: I hope that book will build upon what I have learned here. Once I figure all this out I think I just might have to write a small tutorial for the TOTAL beginner and tell people the bare bones of a program so they have a blank sheet to start with.

What you say is true. Until you know what it means, there’s nothing to explain what it means. It’s a pretty steep learning curve, but it’s worth sticking it out.

Luckily, the OX2 has lots of LEDs on it that you can use to test out some general purpose I/O code. Try writing a program that just turns on an LED, then turns it off. Really seriously, the DDRx, PORTx, PINx registers and the idea of doing the:

PORTx |= (1 << BITNUM);
PORTx &= ~(1 << BITNUM);

is about 90% of what’s involved with getting up and running with an AVR. Once you’ve got those ideas down, almost everything else tends to fall out in turn. Using timers is a matter of bit fiddling to make them run at the rate you want. Same with using the analog inputs.

And do give the Orangutan-lib code a look-see. There is a “blink the LED” program in there, the comments SHOULD explain what the various registers are for and why they’re being set the way they are, and if they aren’t explicit enough for you please let me know and I’ll update them.

Tom

Casey, what Benedict says (“Until you know what it means, there’s nothing to explain what it means.”) is certainly true. Further, it’s hard to remember after you “know what it means” what specifically it was that you learned. My point is simple: when you learn something is the ideal time to write a primer and explain it to someone else. It reinforces what you have learned and you can explain how/why you learned it. When you “Aha” something you have a unique perspective; The guru’s commonly have already forgotten their innocence.