Arduino Mega - Arduino Mega parallel communication using PORT manipulation

This is my first post. So, I am not entirely sure if this is the correct forum.

I am trying to write a parallel communication protocol between 2 Arduino Mega. I am planning to use PORTA for data and a few other pins for control.

So here is my test code -

Master transmits -

byte i = 0;
int intpin = 44;
int rcv_ready = 45;
void setup(){
  DDRA = B11111111;
  pinMode(intpin, OUTPUT);
  pinMode(rcv_ready, INPUT);
}
void loop(){
  digitalWrite(intpin, HIGH);
  while(rcv_ready == LOW){}
  PINA = i++;
  digitalWrite(intpin, LOW);
  
  delay(299);
}

Slave receives

int rcv_ready = 45;
void setup()
{
  DDRA = B00000000; //input
  Serial.begin(19200);
  attachInterrupt(5, blink, RISING);
  pinMode(rcv_ready, OUTPUT);
  digitalWrite(rcv_ready, LOW);
}

void loop()
{
    
}

void blink()
{
  digitalWrite(rcv_ready, HIGH);
  Serial.println(PINA);
  digitalWrite(rcv_ready, LOW);

}

The transfer is mostly working, except for some errors.

84
1
87
0
88
1
91
0
92
1
95
0
96
1
99
0
100
1
103
0
104
1
107
0
108
1
111
0
112
1
115
0
116
1
119
0
120

Error # -

  1. I have random 0s and 1s in alternate lines.
  2. It is printing 2 numbers and then skipping the next two numbers.

I am have no clue what the problem might be.

I have connected Pin 22-37 and 45 of master to Pin 22-37 and 45 of the slave. Pin 18 of Slave is connected Master's Pin 44. I plan to use PORTC (Pin 30-37) in the future but currently i am not sending any signals.

I will appreciate any help.

Thanks in advance.

PINA = i++;

Should that not be

PORTA = i++;


Rob

digitalWrite(rcv_ready, HIGH);
Serial.println(PINA);

You are handshaking then immediately reading PINA, try a short delay, you may be reading too fast.


Rob

Thanks Graynomad. Those fixed my problems.

Now, my final objective is to transfer 4-bytes of data (double or float) from one Arduino to another. It will have 8-bit data bus, 8-bit address bus, and 4-bit control bus.

The first part fixed my issue with capturing the address. Now I am trying to pick up the first byte of data form PORTC. I have gone over the code and handshake timing over and over again. I do not see any problem but I am capturing incorrect data from PORTC.

Master code -

byte i = 0;
int intpin = 44;
int rcv_ready = 45;
void setup(){
  DDRA = B11111111; // Output
  DDRC = B11111111; // Output
  pinMode(intpin, OUTPUT);
  pinMode(rcv_ready, INPUT);
}
void loop(){
  transmit();
}

void transmit(){
  PORTA = i++;
  PORTC = i;
  digitalWrite(intpin, HIGH);
  while(rcv_ready == LOW){}
  digitalWrite(intpin, LOW);
 
  delay(299);
}

Slave Code

byte x = 0;
byte y = 0;
int rcv_ready = 45;
void setup()
{
  DDRA = B00000000; //input
  DDRC = B00000000; //input
  Serial.begin(19200);
  attachInterrupt(5, blink, RISING);
  pinMode(rcv_ready, OUTPUT);
  digitalWrite(rcv_ready, LOW);
}

void loop()
{
 
}

void blink()
{  
  x = PINA;
  y = PINC;
  delay(100);
  digitalWrite(rcv_ready, HIGH);
  delay(100);
  digitalWrite(rcv_ready, LOW);
   Serial.print(x);
  Serial.print(" , ");
  Serial.println(y);

}

So here is my problem. The first column is progressing as it should. But the second column should be column 1 + 1.

11 , 12
12 , 12
13 , 14
14 , 14
15 , 16
16 , 16
17 , 18
18 , 18
19 , 20
20 , 20
21 , 22
22 , 22
23 , 16
24 , 16
25 , 26
26 , 26
27 , 28

Any help is appreciated.
Thanks.

A few updates...

There was was an error in line 18, it should be -

while(digitalRead(rcv_ready) == LOW){}

The other issue was a bad connection on LSB of PORTC.

Hi, I'm trying to get your code to work but it appears to be giving me very weird results.
Rather than printing numbers its printing out gibberish like this
"ĵˆJĵˆJĵˆJĵˆŠÄµˆŠÄµˆŠÄµ…
Ľˆ
Ľˆ
ĽEJĽ!JĽ!JĽ!
Ľˆ
Ľˆ
ĽˆJÄJ ½ÄJ ½ÄJˆŠÄJ ½�þJ ½�þJˆ ÄJ
¥�þJ
"
i have two ports being used i have port A as 22-26 port C as 23-37 i have pins 45 and 44 connected across the boards and ground and 5v connected.
Any ideas what I've got wrong?
I've just copied your code and ran it.