I was thinking today about how to speed up the data transfer on a project I have been working on. I thought about connecting PortD of one arduino to PortD of the other and using the whole port to transfer data, like in LCDs etc. When I got home I wrote some pretty simple code and tested it out. I was pretty surprised at how excellent the speed is.
I consistently get ~1.928Mbit/s (10,000,000 bytes in 41.5s), timed over various time periods, which is pretty damn nice. So I thought I'd share
MASTER
#include <avr/io.h>
byte byte_to_send = B01010101;
void setup()
{
DDRD = B11111111;
PORTD = B00000000;
DDRB = (DDRB | B00000001);
PORTB = (PORTB ^ B00000001);
}
void loop()
{
if(!(PINB&B00000001))
{
PORTD = byte_to_send;
PORTB = (PORTB | B00000001);
while(PINB&B00000001){}
PORTB = (PORTB ^ B00000001);
}
}
SLAVE
[#include <avr/io.h>
long Byte_counter = 0L;
byte Data_read;
long Show_light_at = 10000000L;
void setup()
{
DDRD = B00000000;
PORTD = B00000000;
DDRB = (DDRB ^ B00000001);
PORTB = (PORTB ^ B00000001);
digitalWrite(9,LOW);
}
void loop()
{
if(PINB&B00000001)
{
Data_read = PIND;
Byte_counter++;
DDRB = (PORTB | B00000001);
__asm__("nop\n\t");__asm__("nop\n\t");
DDRB = (DDRB ^ B00000001);
}
if(Byte_counter > Show_light_at)
{ pinMode(9,OUTPUT);
digitalWrite(9,HIGH);
}
}
In terms of connections PortD of master goes to PortD of slave. Digital pin 8 is connected between arduinos and has a pull down resistor of about 11k Ohms. Also a led is connected to digital pin 9 so I can time it.
The way I wanted it to work was the master sets the port, sets the write bit high and goes into a while loop. The slave then sees the write bit high, saves the port data, and toggles the write bit low, throwing the master out of it's while loop to add another byte to the port.
Port D is obviously a bad option for arduinos, I thought I might turn it into 4 data pins (to bypass any important hardware), just sending nibbles, a chip select (so that you can address a bunch of slaves, if you use a 595 then for 8 pins you could address a ton of slaves) and a write pin.
I plan on optimizing it a bit over the next week or so to make it, you know, actually useful. Just thought I'd post up the preliminary to get some ideas