Using alias names for registers

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

Hi,
I want to call TCNT0 with a more understandable name like Timer0Count. So I did the following declarationin my code

#define Timer0Count               _SFR_IO8(0x26);

But it doesn't work. I also used

#define Timer0Count               TCNT0

Can any body help, how can I define these registers in my code, without modifying the WINAVR headers.

Thanks in Advance
smartsarath2003

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

What exactly do you mean by 'not working'?
Does the code sit and wait for a member of the labour union to show up? Does it not compile? Does the chip explode? Please be more specific.

The code you posted looks OK for me, but I would strongly suggest to use the second variant, for clarity and portability.

Thomas

(edit: spelling)

pycrc -- a free CRC calculator and C source code generator

Last Edited: Mon. Jul 28, 2008 - 05:36 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:
But it doesn't work.

You'd still need a:

#include 

or at least a:

#include 

But like Thomas says "doesn't work" is not a lot to go on!

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

Hi,
To clear, my code does compile with the above defines. I am getting errors when I assign any values to it like:

Timer0Count = 64;

The Exact error I am receiving is

Quote:
Error: expected expression before '=' token

When I replaced Timer0Count with TCNT0, the error gets cleared and I can compile.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0
#define Timer0Count               _SFR_IO8(0x26);

Don't put a semi-colon at the end of the #define.

Your second snippet of code looks OK, though. If in doubt, post a piece of code that gives you the error.

pycrc -- a free CRC calculator and C source code generator

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

tpircher wrote:

#define Timer0Count               _SFR_IO8(0x26);

Don't put a semi-colon at the end of the #define.

To explain further, the #define is (in simple terms) an instruction to the pre-processor to replace one symbol with a different string. So your code:
Timer0Count = 64; 

was replaced by:

_SFR_IO8(0x26); = 64; 

See? :D

Of course, this gets more complicated when you get to #define ("macros") with arguments - "macro functions".

I found the following tutorials on C macros:

Macro tutorial and Macro Functions

Stu

Engineering seems to boil down to: Cheap. Fast. Good. Choose two. Sometimes choose only one.

Newbie? Be sure to read the thread Newbie? Start here!