1

I am using Arduino Nano to test a code that will be ported on an ATTiny chip, which will need to perform operations at a frequency near clock maximum, calling for a very succinct coding.

My goal: to streamline coding for two different chips in one file with the least repetitions and operations possible.

Specifically, I have a pin

#ifdef TIMSK0 //Nano vs atTiny
const byte  wavePin  = 5,
            waveBit  = B100000;
      byte *wavePort = &PORTD;
#define NANO
#else
const byte  wavePin  = 0,
            waveBit  = PB1;
      byte *wavePort = &PORTB;
#endif

Notice the pointer? It works, but I have the impression this may not be the most efficient option, as it adds an operation (by looking up the reference). I have also planned to do constantly check which chip we are loading it in this fashion

#ifdef NANO
  PORTD
#else
  PORTB
#endif
  |=   waveBit *  waveOn);
#ifdef NANO
  PORTD
#else
  PORTB
#endif
  &= ~(waveBit * !waveOn));

But again, while this may be perfectly fine in operations, it hurts the eyes and is rather repetitive.

Is there a way to create a direct access to a port?

5
  • 1
    Are you familiar with C++ Macros?
    – StarCat
    Aug 10, 2020 at 7:20
  • Not yet? Tell me more :D
    – B7th
    Aug 10, 2020 at 7:30
  • 1
    Do you mean to do something like #define WAVEPORT PORTD?
    – B7th
    Aug 10, 2020 at 7:33
  • StarCat you opened me a can of worms and I love it :D Thanks it actually works!
    – B7th
    Aug 10, 2020 at 7:39
  • 1
    Great! That is actually more or less what I meant.
    – StarCat
    Aug 10, 2020 at 8:18

0

Your Answer

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