Assining PORTX to a variable and writing byte to it

Hi all,

(System: Mega2560R3)
I need to perform fast write on-off actions to ports (2µs). digitalWrite() is too slow for this as it perfoms several look-ups and calls. So i write to the port pointers directly via sending the byte mask. For example:

PORTA = PORTA | B10000000; //Turns Pin29 on leaving all other pins as they are
delayMicroseconds(2);
PORTA = PORTA & (~B10000000); //Turns Pin29 off leaving all other pins as they are

That works fine and fast enough.

The problem: At some point before the fast read and write actions start, I need to allocate the right PORT because I use many pins which are distributed between PORTA, PORTB, PORTC, PORTD, PORTE.

This sound trivial but appreas to be beyond my intellectual horizon:

byte currentPort = PORTA;
currentPort = currentPort | B10000000; //Nothing happens. This makes sense because it just assings the byte currently in PORTA to a new vaiable. So lets try with pointers:

volatile uint8_t **currentPort ;
*currentPort = &PORTA;
**currentPort = **currentPort | B10000000; //This also does nothing. Why??

Can anyone tell me whats wrong in my thinking?

You have your pointer types and dereferencing kind of balled-up:

  volatile uint8_t *currentPort;

  currentPort = &PORTB;
  *currentPort  = *currentPort | B10000000;

Thank you very much gfvalvo, big shouts going out to you.
I have no idea how I even got the idea that it must be a pointer to a pointer. Anyway, lets call it a week, thanks!

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.