Usage of portOutputRegister() and digitalPinToMask() on Mega2560

Hello everyone,

I’m trying to program an AD9762 TxDAC from Analog Devices and, being a 12bit DAC with parallel programming, it needs 12 pins. And i need to program those 12 pins all together & as fast as possible (i’ll have to handle some fast signals with it).

I’m actually trying to make some pins of an Arduino Mega2560 to move together to do the data drive.

For doing so, i tried to go with digitalPinToBitMask() and portOutputRegister() functions, expecting them to traduce correctly my pin numbers into their respective ports and port pins…but it doesn’t seem to work.

Here’s the code whole code i’m working on
(it’s not the DAC programming software, it’s a test to see the pins move correctly and the performances)

const int dataPin[12] = { 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53 };
const int clockPin = 22;

volatile uint8_t *portofpin[12], *clkPort;
uint8_t bitofpin[12];
uint8_t clkPin;

void setup()
{
	Serial.begin(115200);

	
	for (int x = 0; x < 12; x++)
	{
		*portofpin[x] = portOutputRegister(digitalPinToPort(dataPin[x]));
		Serial.print("PORT : ");
		Serial.println(*portofpin[x]);
	}

	Serial.println("");

	for (int x = 0; x < 12; x++)
	{
		bitofpin[x] = digitalPinToBitMask(dataPin[x]);
		Serial.print("PIN  : ");
		Serial.println(bitofpin[x]);
	}

	*clkPort = portOutputRegister(digitalPinToPort(clockPin));
	clkPin = digitalPinToBitMask(clockPin);

	

	pinMode(clockPin, OUTPUT);
	for (int x = 0; x < 12; x++)
	{
		pinMode(dataPin[x], OUTPUT);
	}

	for (int x = 0; x < 12; x++)
	{
		digitalWrite(dataPin[x], LOW);
	}

	digitalWrite(clockPin, LOW);

	Serial.println("");
	Serial.print("CLK PORT : ");
	Serial.println(*clkPort);
	Serial.print("CLK PIN  : ");
	Serial.println(clkPin);
	Serial.println("");
	Serial.println("");
	Serial.println("");	
}


void loop()
{

	for (unsigned int x = 0; x < 12; x++)
	{
		//PINA = 1;
		//digitalWrite(dataPin[x], HIGH);
		*clkPort = clkPin;
		*portofpin[x] = bitofpin[x];
		*clkPort = clkPin;
		//PINA = 1;
	}
	for (unsigned int x = 11; x >= 0; x--)
	{
		//PINA = 1;
		//digitalWrite(dataPin[x], LOW);
		*clkPort = clkPin;
		*portofpin[x] = bitofpin[x];
		*clkPort = clkPin;
		//PINA = 1;
	}

}

As you can see, i put the list of pins to use into an array, then i pass that array to the before mentioned functions and expect to see the traduced into port names and port pin numbers, but actually nothing moves on the gpio. I’m using an oscilloscope to see them move.

It’s the first time i use such functions and i’m not a code guru, so quite surely i didn’t undestand how to use them.
Some suggestions? :smiley:

Thanks for your time and help!

PS: I used this solution to set a buch of pins (of different ports) at a time, but maybe there are better solutions i didn’t know/see. So…if you have some suggestions about that, i’m very interested in learning something new.
And thanks again.