1

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;
#else
  const char in  = 3;
  const char out = 0;
  const char pwr = 1;
  char *timer = &TIMSK;
#endif
//...
*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?
0

1 Answer 1

2

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
    #endif
    

    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__
    ...
    #endif
    
    #if defined(__AVR_ATtiny45__)
    ...
    #endif
    
    #if __AVR_ATtiny45__
    ...
    #endif
    

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

    #if __AVR_ATtiny25__ || __AVR_ATtiny45__ || __AVR_ATtiny85__
    ...
    #endif
    

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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