Pages: 1 2 3 [4] 5   Go Down
Author Topic: I2C read problems  (Read 6657 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Full Member
***
Karma: 0
Posts: 146
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

?
« Last Edit: July 29, 2011, 05:21:37 pm by herctrap » Logged

Global Moderator
Melbourne, Australia
Offline Offline
Brattain Member
*****
Karma: 511
Posts: 19306
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

http://www.gammon.com.au/electronics

Please post technical questions on the forum - not to me by personal message. Thanks a lot.

0
Offline Offline
Full Member
***
Karma: 0
Posts: 146
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
#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:
#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
Logged

Global Moderator
Melbourne, Australia
Offline Offline
Brattain Member
*****
Karma: 511
Posts: 19306
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
#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:
#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:
01234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567
01234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567
01234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567
01234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567
01234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567

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

From your code:

Quote
Code:
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.
Logged

http://www.gammon.com.au/electronics

Please post technical questions on the forum - not to me by personal message. Thanks a lot.

Global Moderator
Melbourne, Australia
Offline Offline
Brattain Member
*****
Karma: 511
Posts: 19306
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

http://www.gammon.com.au/electronics

Please post technical questions on the forum - not to me by personal message. Thanks a lot.

0
Offline Offline
Full Member
***
Karma: 0
Posts: 146
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

thanks it worked

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

0
Offline Offline
Full Member
***
Karma: 0
Posts: 146
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

but it didn't worked

???
Logged

Global Moderator
Melbourne, Australia
Offline Offline
Brattain Member
*****
Karma: 511
Posts: 19306
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

What files did you change exactly?
Logged

http://www.gammon.com.au/electronics

Please post technical questions on the forum - not to me by personal message. Thanks a lot.

0
Offline Offline
Full Member
***
Karma: 0
Posts: 146
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

wire.h and twi.h into the library folder
Logged

Global Moderator
Melbourne, Australia
Offline Offline
Brattain Member
*****
Karma: 511
Posts: 19306
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

http://www.gammon.com.au/electronics

Please post technical questions on the forum - not to me by personal message. Thanks a lot.

0
Offline Offline
Full Member
***
Karma: 0
Posts: 146
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The master

asks for data

Code:
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:
 Wire.begin(I2C_Address);  
  Wire.onRequest(requestEvent);
  Wire.onReceive(ReceiveCommand);

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

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

Code:
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:
void requestEvent()
{
  //Wire.write (OS4000Data, 127);
   Wire.write ("AbcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789000987654321");
}

but i am receiving

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

thanks

Logged

Global Moderator
Melbourne, Australia
Offline Offline
Brattain Member
*****
Karma: 511
Posts: 19306
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

wire.h and twi.h into the library folder

In my copy they are in the Wire and Wire/utility folder.
Logged

http://www.gammon.com.au/electronics

Please post technical questions on the forum - not to me by personal message. Thanks a lot.

0
Offline Offline
Full Member
***
Karma: 0
Posts: 146
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

yes in these folders
Logged

0
Offline Offline
Full Member
***
Karma: 0
Posts: 146
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

thanks a lot
Logged

Global Moderator
Melbourne, Australia
Offline Offline
Brattain Member
*****
Karma: 511
Posts: 19306
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

http://www.gammon.com.au/electronics

Please post technical questions on the forum - not to me by personal message. Thanks a lot.

Pages: 1 2 3 [4] 5   Go Up
Jump to: