Hi I am trying to send data(1 byte) from one arduino to another using parallel communication(a bus of 8 connections) and not using any library. However, the problem is the data gets corrupt all the time and what I am supposed to receive at receiver arduino is not what I send. The protocol that I made up is similar to uart protocol. There is ES pin and when it is low, transmission is started and when it is high, the transmission is ended. I think the problem could be related to wires that I use to connect two arduinos. There could be some noise induced that could flip bits in wires or the code is problematic. I am posting my code. Maybe you can help me get through this and help me to resolve my problem.
Sender code:
#define ES 8 //enable sampling
#define B0 0 //Bus 0
#define B1 1
#define B2 2
#define B3 3
#define B4 4
#define B5 5
#define B6 6
#define B7 7
const char dataToBeSent = 'C';
bool data_bit;
void setup() {
pinMode(ES,OUTPUT);
pinMode(B0,OUTPUT);
pinMode(B1,OUTPUT);
pinMode(B2,OUTPUT);
pinMode(B3,OUTPUT);
pinMode(B4,OUTPUT);
pinMode(B5,OUTPUT);
pinMode(B6,OUTPUT);
pinMode(B7,OUTPUT);
digitalWrite(ES,HIGH);
delay(3000);
}
void loop() {
digitalWrite(ES,LOW); //transmission is started
delay(10);
for(int i = 0 ; i<8 ; i++){
data_bit = dataToBeSent & (0x80 >> i);
digitalWrite(i,data_bit);
}
delay(10);
digitalWrite(ES,HIGH); //end transmission
while(true){}
}
Receiver code:
#define ES 8 //enable sampling
#define B0 0 //Bus 0
#define B1 1
#define B2 2
#define B3 3
#define B4 4
#define B5 5
#define B6 6
#define B7 7
char rx_data;
byte rx_byte= 0;
bool rx_pin;
void setup() {
Serial.begin(9600);
pinMode(ES,INPUT);
pinMode(B0,INPUT);
pinMode(B1,INPUT);
pinMode(B2,INPUT);
pinMode(B3,INPUT);
pinMode(B4,INPUT);
pinMode(B5,INPUT);
pinMode(B6,INPUT);
pinMode(B7,INPUT);
delay(1000);
}
void loop() {
byte rx_byte= 0;
while(digitalRead(ES) == HIGH){}
delay(15);
for(int i = 0 ; i<8 ; i++){
rx_pin = digitalRead(i);
Serial.print(rx_pin);
if(rx_pin == 1){
rx_byte |= (0x80 >> i);
}
}
rx_data = rx_byte ;
Serial.print("Received data: ");
Serial.println(rx_data);
delay(15);
while(digitalRead(ES)== HIGH){}
}