I need to enable a timer interrupt for a chip, and in the mean time, I am testing the code on Arduino Nano rather than (feed the chip, test the chip)++.

So as part of the code I have to TIMSK0 |= (1 << OCIE0A); which works great on the Arduino Nano, but has to be changed to TIMSK (without the 0) for the ATtiny45.

I was planning to go for something along the line of

#define NANO//Put // in front for the AtTiny45
#ifdef NANO
  const char in  = A0;
  const char out = 3;
  const char pwr = 10;
  char *timer = &TIMSK0;
  const char in  = 3;
  const char out = 0;
  const char pwr = 1;
  char *timer = &TIMSK;
*timer  |= (1 << OCIE0A);

So here are my three questions:

  • Do #these commands take space on a chip?
  • Is a pointer the way to go for this kind of things?
  • Am, am I doing it right?

Just as a complement to Delta_G's answer:

  1. The names TIMSK0, OCIE0A, etc. are already preprocessor macros so, for consistency, it makes sense to define your own preprocessor macros for them. I tend to be lazy and use one of the already defined names, like

    #ifdef TIMSK0  // support both ATtiny{25,45,85} and ATmega328P
    # define TIMSK TIMSK0

    A const pointer for &TIMSK0 is fine though.

  2. The macros identifying the MCU are defined to the value 1. Thus you have the choice between these three idioms:

    #ifdef __AVR_ATtiny45__
    #if defined(__AVR_ATtiny45__)
    #if __AVR_ATtiny45__

    The third one is convenient when you want to address several MCU models with the same code:

    #if __AVR_ATtiny25__ || __AVR_ATtiny45__ || __AVR_ATtiny85__
| improve this answer | |
  1. They take no space, the preprocessor looks at those and will determine what part of the code to include. The rest is "deleted" before compilation.

  2. I would probably use a define for that. Others may disagree.

  3. There are actually a set of defines you can get depending on which chip is selected. Check out some of the hardware headers in the core like wiring_analog.c to see some of them in action. For instance for the Tiny 45 you could use #if defined(AVR_ATtiny45). Or simply an #ifdef TIMSK0 to see if that is defined, it won't be for the 45.

| improve this answer | |
  • Thank you for your insight! Helped me figure things out :) – B7th Jul 13 at 5:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.