PreProcessor defines

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

Hi everyone.
I am using a library that wants you to tell it what pins buttons etc are on using its own macros:

P('A', 1) 

is PA1.
However I am using the mega2560 which has a PORTJ that my peripherals are on, and the library wasnt set up for PORTJ.
I found the file where all this is defined using the preprocessor:

#ifdef DDRA
  &DDRA,
#else
  0,
#endif
  &DDRB,
#ifdef DDRC
  &DDRC,
#ifdef DDRD
  &DDRD,
#ifdef DDRE
  &DDRE,
#ifdef DDRF
  &DDRF,
#ifdef DDRG
  &DDRG,
#ifdef DDRH
  &DDRH,
#ifdef DDRI
  &DDRI,
#else
  0,
#ifdef DDRJ
  &DDRJ
#endif
#endif
#endif
#endif
#endif
#endif
#endif
#endif
};

I added in

#ifdef DDRI
  &DDRI,
#else
  0,
#ifdef DDRJ
  &DDRJ
#endif
#endif

and there is one of those for PORTx's and PINx's aswell.
Basically if I just add in DDRJ, then it doesnt work - I dont know why, perhaps it thinks it has to have a DDRI.
If I add in DDRI it doesnt work either, because DDRI doesnt exist I assume.
Lastly if I put in the #else 0,
then it slightly works, in that my rotary encoder, when turned produces values on my terminal...
So someone with more preprocessor experience than me, looking at the above, do you see any issues?
Thanks

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

But surely the 0 terminates the list and you've added J *after* it??

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

Quote:
Lastly if I put in the #else 0,
then it slightly works

What does not work here yet?

Basically, what the preprocessor does in this piece of code is creating an array of the ddr's. When your code is looking up the right DDR, it will (i assume) look up the Nth position in the array, where N equals the index of the letter you give to the function (the 'A' in P('A', 1) ).

Now, if PORTI does not exist, and you don't have the "else 0" clause in your code, you miss an element in the array. If you then try to look up PORTJ it will try to read the 11th element in the array which, because of the missing PORTI element, does not exist.

I hope this made things a little more clear for you