Go Down

Topic: I2C read problems (Read 6 times) previous topic - next topic

herctrap

#45
Jul 30, 2011, 12:12 am Last Edit: Jul 30, 2011, 12:21 am by herctrap Reason: 1
it gives me an error if i used your code in the compliler

is it gone work if i change the


#define BUFFER_LENGTH 32

from the wire.h?

and the TWI_BUFFER_LENGTH 32

from twi.h

?

Nick Gammon

What error? What code? I was just pointing out the 5 places it is used.

Quote
is it gone work if i change the


#define BUFFER_LENGTH 32

from the wire.h?

and the TWI_BUFFER_LENGTH 32

from twi.h


Yes, that was what I was suggesting. Although "work" may be a loose term. If you run out of memory it won't work.  I don't know if there is some hardware limit, I don't think there is.

You will of course need to change both ends, the sending end and the receiving end.
http://www.gammon.com.au/electronics

herctrap

Code: [Select]
#include <Wire.h>

#define I2C_Address 2
#define UartBaudRate 57600

int Position;

byte OS4000Data [128];

char command;

void setup()
{
 Serial.begin(UartBaudRate);  
 Wire.begin(I2C_Address);  
 Wire.onRequest(requestEvent);
 Wire.onReceive(ReceiveCommand);
}

void loop()
{
 if(command == 'C')
 {
   Capture_OS4000();
   command = 'N';
 }
}


void ReceiveCommand(int howMany)
{
 while(!Wire.available());
 command = Wire.receive();
}

void requestEvent()
{
   Wire.send ("kffkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkdd");
   //Wire.send (OS4000Data, 127);
}

void  Capture_OS4000()
{
 Serial.flush();
 while ( Serial.read () != '$' );
 Serial.flush();
 while( Serial.available() < 127 );
 OS4000Data[Position] = Serial.read();
}


Code: [Select]
#include <Wire.h>
#define requestdelay 25


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

void loop(){

 Wire.beginTransmission(2);
 Wire.send('C');
 Wire.endTransmission();
delay(200);

Wire.beginTransmission(2);
 
 Wire.requestFrom(2,40);
 for ( int i=0; i<40; i++ ) Serial.print(Wire.receive());
 Serial.println();
}


not working with the above code

i am reading kffkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
and not         kffkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkdd

Nick Gammon

Well I can't reproduce your problem. I changed the two defines I mentioned, and then amended your code slightly. This is the slave:

Code: [Select]
#include <Wire.h>

#define I2C_Address 2

char command;

char buf [131] = "";

void setup()
{
  Wire.begin(I2C_Address); 
  Wire.onRequest(requestEvent);
  Wire.onReceive(ReceiveCommand);
 
  for (int i = 0; i < 13; i++)
    strcat (buf, "0123456789");
   
}

void loop()
{
  if(command == 'C')
  {
    command = 'N';
  }
}


void ReceiveCommand(int howMany)
{
  command = Wire.receive();
}


void requestEvent()
{
    Wire.send ((uint8_t*) buf, 128);
}



And this is the master:

Code: [Select]
#include <Wire.h>

#define I2C_Address 2
#define AMOUNT 128

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

void loop(){

  Wire.beginTransmission(I2C_Address);
  Wire.send('C');
  Wire.endTransmission();

  delay(200);


  Wire.requestFrom(I2C_Address, AMOUNT);
  if (Wire.available () == AMOUNT)
    {
    for ( int i=0; i<AMOUNT; i++ )
      Serial.print(Wire.receive(), BYTE);
    }
  else
    Serial.println (Wire.available (), DEC);
  Serial.println();
}


And this is the debug output:

Code: [Select]
01234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567
01234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567
01234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567
01234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567
01234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567


As you can see, 128 bytes of the correct data.

From your code:

Quote
Code: [Select]
void loop(){

  Wire.beginTransmission(2);
  Wire.send('C');
  Wire.endTransmission();
delay(200);

Wire.beginTransmission(2);
 
  Wire.requestFrom(2,40);
  for ( int i=0; i<40; i++ ) Serial.print(Wire.receive());
  Serial.println();
}


The second Wire.beginTransmission is not correct - you don't do another beginTransmission before a requestFrom.
http://www.gammon.com.au/electronics

Nick Gammon

Make sure you upload both sender and receiver. The slave does not send the extra data if you try to send more than the maximum in the define.
http://www.gammon.com.au/electronics

herctrap

thanks it worked

for some reason the buffer size into the wire.h was 32 and not 128

herctrap

hallo again
i also tried to change the values in anrduino 1.0

but it didn't worked

???

Nick Gammon

What files did you change exactly?
http://www.gammon.com.au/electronics

herctrap

wire.h and twi.h into the library folder

Nick Gammon


but it didn't worked


Can you be a bit more specific? What didn't work? Under version 1.0 you would have had to change other things too.
http://www.gammon.com.au/electronics

herctrap

The master

asks for data

Code: [Select]

void Read_Data()
{
 Wire.beginTransmission(2);
 Wire.write('H');
 Wire.endTransmission();

 delay(200);

 Wire.beginTransmission(2);
 Wire.requestFrom(2,127);
 Wire.endTransmission();
 for ( int i=0; i<128; i++ ) Serial.write(Wire.read());
 Serial.println("");
}


and on the slave

Code: [Select]

 Wire.begin(I2C_Address);  
 Wire.onRequest(requestEvent);
 Wire.onReceive(ReceiveCommand);


Code: [Select]

void ReceiveCommand(int howMany)
{
 command = Wire.read();
}


Code: [Select]

void loop()
{
 if (command == 'H')
 {
   Capture_OS4000();
   command = 'N';
 }
}


Code: [Select]

void  Capture_OS4000()
{
 //Serial.println("mpika capture"); // debug
 //Serial.flush();
 while ( Serial.read () != '$' );
 //Serial.flush();
 while( Serial.available() < 127 );
 OS4000Data[0] = '$';
 for(Position=1; Position<128; Position++){
   buf = Serial.read();
   if ( buf == '*' ) EOL = Position + 2;
   if ( Position <= EOL ) OS4000Data[Position] = buf;
   else OS4000Data[Position] = B0;
 }
}


Code: [Select]

void requestEvent()
{
 //Wire.write (OS4000Data, 127);
  Wire.write ("AbcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789000987654321");
}


but i am receiving

AbcdefghijklmnopqrstuvwxyzABCDEFÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ

thanks


Nick Gammon


wire.h and twi.h into the library folder


In my copy they are in the Wire and Wire/utility folder.
http://www.gammon.com.au/electronics

herctrap


herctrap

if you make it work can you send me the files please
because I edit it with wordpad

thanks a lot

Nick Gammon

I'm not going to make it work and send it to you. Edit two files and change 2 numbers. At least show here what changes you made.
http://www.gammon.com.au/electronics

Go Up