gcc-avr ATmega16/32 Programming

Go To Last Post
8 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I have just entered into AVR MCU programming using gcc-avr, but when I see sample programs I am not able to make out much from the code:


DDRD |= (1 << PD7);
TCCR2 = (1 << WGM21) | (0 << WGM20);
TCCR2 |= (1 << COM20); 
TCCR2 |= (6 << CS20);

I do not see also any declarations variables : DDRD, PD7, TCCR2, WGM21, WGM20, COM20, CS20, but they are directly used. Please let me know how I can know all pre-defined variables and its usage? It becomes very difficult in understanding the code without knowing the same.

Thanks in advance.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

DDRD and the others are defined within the header file for the particular AVR chip you're using.

The |= operator does a bitwise OR together with an assignment in one step. And 1 << PD7 shifts a 1 x positions to the left, where x is given by the value of PD7, another defined constant in those header files.

Einstein was right: "Two things are unlimited: the universe and the human stupidity. But i'm not quite sure about the former..."

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I can understand the code that is not the issue - but the variables and its importance is the concern.

How do I know what DDRD and other variables stands for? Where can I find all such variables declarations and its meaning such that when I develop code I know what defined variable to make use for what purpose?

Last Edited: Fri. Oct 25, 2013 - 10:35 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

prakash_prasad wrote:
How do I what DDRD and other variables stands for? Where can I find all such variables declarations and its meaning such that when I develop code I know what defined variable to make use for what purpose?
Look into the data sheet.

Stefan Ernst

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:

Where can I find all such variables declarations

To see the declarations you follow the #include's in the code you wrote. I'm guessing you have a line that says:

#include 

at the top of your code. If using AS6 then put teh cursor somewhere inbetween < and > and right click then "Goto Implementation". That will load io.h into the editor. You will find that it's a long list of other #includes...

#if defined (__AVR_AT94K__)
#  include 
#elif defined (__AVR_AT43USB320__)
#  include 
#elif defined (__AVR_AT43USB355__)
#  include 
#elif defined (__AVR_AT76C711__)
#  include 
#elif defined (__AVR_AT86RF401__)
#  include 
etc.

Somewhere in that long list you will find the name of the AVR you are building for. Let's say it is mega32. In which case it will be this line that is actually used (because "__AVR_ATmega32__" is defined when you build for mega32):

#elif defined (__AVR_ATmega32__)
#  include 

Now open that file and you will find things such as:

/* Port D */
#define PIND    _SFR_IO8(0x10)
#define DDRD    _SFR_IO8(0x11)
#define PORTD   _SFR_IO8(0x12)

That is where "DDRD" is defined. If you check the datasheet it will tell you that DDRD, the register that sets direction for PORTD is at IO memory location 0x11 and this line in the header file is just defining a macro that allows you to read/write to IO location 0x11 (RAM location 0x31).

Similarly you will find:

/* Timer 2 */
#define OCR2    _SFR_IO8(0x23)
#define TCNT2   _SFR_IO8(0x24)
#define TCCR2   _SFR_IO8(0x25)

which is where TCCR2 is defined (to agree with the information in the datasheet).

The 8 bits in each of these registers are usually also defined in the .h file so you have for example:

/* PORTD */
#define PD7     7
#define PD6     6
#define PD5     5
#define PD4     4
#define PD3     3
#define PD2     2
#define PD1     1
#define PD0     0

and

/* TCCR2 */
#define FOC2    7
#define WGM20   6
#define COM21   5
#define COM20   4
#define WGM21   3
#define CS22    2
#define CS21    1
#define CS20    0

the fact that all these register and bit names agree with the datasheet usually means you don't actually need to look at this (or any other) .h file. Just use the names given in the datasheet as documented there and you can rely on them working as long as you get the names the same and ensure you stick to all upper case (which is a hint that they are macro definitions).

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thanks I got the pointer - but data-sheets are very exhaustive.

Is there just any reference document that states about these variables? It can just a couple of pages where I can get a brief of each variables like a coding reference manual. Later I can refer to data-sheets for its detailed concept.

For example:

Variable                     Meaning              Address
DDRD                   Port D Data Direction         

Further is there any diagram that shows registers, memory organization of the MUC. Like for 8051 on chip RAM-

http://www.8052.com/images/8051m...

It gives me details of special registers, general purpose registers, timers with the registers variable names as declared

Thanks once again in advance.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:

but data-sheets are very exhaustive.

I should bloody hope so! There's nothing worse than a manual that only tells you about 95% of the operation of something and leaves you wondering about the other 5%!!

Did you honestly believe it was going to be possible to program a microcontroller without reading it's manual?

BTW while a datasheet is 300-400 pages nothing says you have to read the whole thing no day one. Your code above only delas with IO ports and Timer2. That's just 2 chapters out of about 30 chapters in the book. So just start by reading those two chapters. (leave ADC, SPI, TWI, comparator, and all the rest for another day).

Quote:

Further is there any diagram that shows registers, memory organization of the MUC.

You really really do need to read the datasheet. Don't be afraid to open it and at least skim through the first few chapters. You will find things like this (taken from the chapter called "Memories"):

Attachment(s): 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

prakash_prasad wrote:

It can just a couple of pages where I can get a brief of each variables like a coding reference manual. Later I can refer to data-sheets for its detailed concept.

Every datasheet has a picture like the 8051 you linked to in it. In the 328 datasheet it is section 36 'Register Summary'.

#1 Hardware Problem? https://www.avrfreaks.net/forum/...

#2 Hardware Problem? Read AVR042.

#3 All grounds are not created equal

#4 Have you proved your chip is running at xxMHz?

#5 "If you think you need floating point to solve the problem then you don't understand the problem. If you really do need floating point then you have a problem you do not understand."