Register defintion

For C and ASSEMBLY users to post questions and code snippets for programming in C and ASSEMBLY. And for any other C or ASM course related questions.

Moderators: Benj, Mods

Post Reply
User avatar
Jan Lichtenbelt
Posts: 797
Joined: Tue Feb 17, 2009 8:35 pm
Location: Haren GN, the Netherlands
Has thanked: 128 times
Been thanked: 264 times
Contact:

Register defintion

Post by Jan Lichtenbelt »

I want to make a Flowcode program for different microcontrollers. I use also C-codes for some special registers. But different microcontrollers can have different registers. Using C-code with registers, which are not defined, will give an error.

Is it possible to define regsiters/variables in the following way?

if
{
char REGISTER_NAME;
....
}
else
{
.....
}

Kind regards

Jan Lichtenbelt

User avatar
JonnyW
Posts: 1230
Joined: Fri Oct 29, 2010 9:13 am
Location: Matrix Multimedia Ltd
Has thanked: 63 times
Been thanked: 290 times
Contact:

Re: Register defintion

Post by JonnyW »

Hi Jan. You would typically use the pre-processor for this:

Code: Select all

#ifdef MY_CHIP
  char REGISTER_NAME;
#else
  char YOUR_REGISTER_NAME;
#endif
This means that the C compiler would never see the inappropriate definition. For portability it may be an idea to define a 'generic' register name that you can use anywhere you want:

Code: Select all

#ifdef MY_CHIP
  char MY_REGISTER_NAME;
  #define ANY_REGISTER_NAME       MY_REGISTER_NAME
#else
  char YOUR_REGISTER_NAME;
  #define ANY_REGISTER_NAME       YOUR_REGISTER_NAME
#endif
You will then be able to use 'ANY_REGISTER_NAME' anywhere in your code and not have to change it.

Cheers,

Jonny

Post Reply