Go Down

Topic: I²C 2 bytes from slave to master (Read 663 times) previous topic - next topic

fenrig

Hi i'm having some trouble sending data from my slave to my master, the first byte is received correctly by the master but then the second byte consists of 1's so the receiving value is always 255. Now I can't imagine that I'm the only one having this problem that's why I'm asking it here: how do I send 2 bytes from a slave to master when the master requests bytes from the slave.

Code: [Select]

//master
//------------

#include <Wire.h>
#include <TFT.h>
#define SLAVE 0x02

void setup(void){
  Serial.begin(9600);
  Wire.begin();
  Tft.init();
}

void loop(void){
  if(decode == 1){
   photo_bytes();
   
  }else{
    change_photo();
    decode = 1;
  }
}

void photo_bytes(){
  Wire.requestFrom(SLAVE,3);
  delay(1);
  while(Wire.available() && decode){
    Serial.println(Wire.available());
    unsigned int color_low = Wire.read();
    unsigned int color_low = Wire.read();
    unsigned int color_high = Wire.read();
   /* Serial.print(" - ");
    Serial.print(color_high);*/
    Serial.print(" - ");
    Serial.println(color_low);
    //color_high = (color_high<<8);
    color_high += color_low;
    Tft.setPixel(x,y,color_high);
    x++;
    if(x == 241){
      x = 0;
      y++;
      if(y == 321){
        y = 0;
        decode = 0;
      }
    }
    //Wire.requestFrom(SLAVE,2); //maakt proces veeeeel sneller
    delay(1);
  }
}

//Slave
//---------
#include <SD.h>
#include <Wire.h>

#define SLAVE 0x02
#define RGB16(red, green, blue) ( ((red >> 3) << 11) | ((green >> 2) << 5) | (blue >> 3))

void(* resetFunc) (void) = 0;

unsigned char blue,green,red,color_high,color_low;
unsigned int color;
int no_pics; //aantal fotos
int no_off_pic = 0; //stand van de foto
File pic;

void setup(){
  Serial.begin(9600);
  Serial.println("begin");
  pinMode(13,OUTPUT);  //nodig voor SD kaart
  if (! SD.begin(10)){
    Serial.println("init SD failed\nREBOOT\n--------");
    delay(40);
    resetFunc();
  }
  Serial.println("jep");
  pinMode(13, OUTPUT); //COMMUNICATIE LED
  //picture
  no_pics = listpics(); //tel alle bmp files in /pictures en delete alle niet bmp's
  openpic();            //open eerste foto
  //i²c communicatie
  Wire.onRequest(requestEvent);
  Wire.onReceive(receiveEvent);
  Wire.begin(SLAVE);

}

void loop(void){
  int i = 0;
  i++;
  i = i + i;
  if(i > 200) i = 0;
  if(i < 0) Serial.println("jaja");
}

int readfile(){
  pic.seek(54);
}

int listpics(){
  File dir = SD.open("/pictures");
  int files = 0;
  int lengte;
  char name[255];
  while(1){
    File entry = dir.openNextFile(); //open volgende file
    if(! entry) break; //geen files meer
    strcpy(name," ");
    strcpy(name,entry.name());
    lengte = strlen(name);
    if(name[lengte-1] == 'P' && name[lengte-2] == 'M' && name[lengte-3] == 'B' && name[lengte-4] == '.'){
      files++;
    }
    else{ //delete niet ".bmp" files :D
      strcpy(name,"/pictures/");
      strcat(name,entry.name());
      SD.remove(name);
    }
  }
  return files;
}

void openpic(){
  File dir = SD.open("/pictures");
  char name[255] = "/pictures/";
  int i;
  for(i = 0; i < no_off_pic+1;i++){
    pic = dir.openNextFile();
  }
  strcat(name,pic.name());
  pic = SD.open(name, FILE_READ);
  pic.seek(54);
}

void requestEvent(){
 
  digitalWrite(13, HIGH);   // set the LED on
  if(pic.position() <= pic.size()){
    blue = pic.read();
    green = pic.read();
    red = pic.read();
    color = RGB16(red,green,blue);
    color=0x55AA;
    Wire.write(lowByte(color));
    Wire.write(lowByte(color));
    Wire.write(highByte(color));
    /*Wire.write(red);
    Wire.write(green);
    Wire.write(blue);*/
  }
  else{
    Serial.println("Error");
  }
  digitalWrite(13, LOW);
}

void receiveEvent(int numBytes){
  int c = Wire.read();
  Serial.write(c);
  if(c == 2){
    no_off_pic++;
    if(no_off_pic > no_pics) no_off_pic = 1;
  }else if(c == 3){
    no_off_pic--;
    if(no_off_pic < 1) no_off_pic = no_pics;
  }
  openpic();
}


fenrig

I kinda fixed it, actually I'm now using the one working byte :D

Go Up