With a bit of preprocessor magic this can be made compiler independent but for the actual defines. It makes the code easier to port and arguably more readable.
Code:
#ifdef __C18 // these are not the actual compiler ID's
#define STATUS_LED PORTBbits.RB0
#endif
#ifdef __MIKROC
#define STATUS_LED PORTBbits.RB0
#endif
...
STATUS_LED = 1;
The masking solution I proposed has the advantage that you can use it with most/all data types and it can operate on several bits at once.
To use the compiler specific bit setting and testing with defines in a loop we could try.
Code:
// compiler specific defines/macros
#define PORTD_BIT(x) PORTDbits.PORTDx
#define PORTC_BIT(x) PORTCbits.PORTCx
for(i=0; i<_TOTAL_LIGHTS; i++)
{
if (PORTD_BIT(i) == 1) // when i is 3 this expands to "if (PORTDbits.PORTD3 == 1) "
{
PORTC_BIT(i) = 1;
}
else
{
PORTC_BIT(i) = 0;
}
}
or
for(i=0; i<_TOTAL_LIGHTS; i++)
{
PORTC_BIT(i) = PORTD_BIT(i);
}
I missed that he wanted to use the variable i to access a bit. I just can't remember ever needing to do that? It's definitely not a standard C type of thing to do.
Maybe if it must be done using bit tests he could do something like this (which would compile to very fast executing code);