Go Down

Topic: I2C help needed (Read 1 time) previous topic - next topic

michinyon

Your explanation of which two bits are not used seems implausible.   Are you sure it is the most
significant 2 bits of the low byte which are unused ?

You may need to be shifting the first byte you read by 8 bits,  not 6 bits,  if that is the case.
If you only shift the first byte you read by six bits,   then the second byte you read will overwrite
the least significant two bits of the first byte,  which is probably not what you intended.

I'd suggest printing the actual hex value of the two bytes which you read to the serial monitor
and examining them closely,  to check.

Lsnyman


Code: [Select]

You may need to be shifting the first byte you read by 8 bits,  not 6 bits,

If i do that then the output goes to 16-bit (0 - 65535) which is clearly incorrect.

Code: [Select]
I'm a bit confused about the relationship between the numbers 66 and 112.
As I said above, the 112 is part of the comments from the arduino code example, I changed the address to 66

Nick Gammon

So I gather you are getting valid readings, and your question is "why am I getting valid readings?"

I've used this device using SPI and you ask for address 0xFFFF and then read 2 bytes, which looks like what is happening for you.

Lsnyman


Quote
So I gather you are getting valid readings, and your question is "why am I getting valid readings?"


Hi Nick. Yes exactly. At least it appears i am getting valid readings. I just dont understand that if the data is contained in 254 and 255, why it only works when I call 255? I am not calling 254 at all but it seems to be getting the data from it.

Here is the HEX output with the bits shifted 6
0, E
0, 34
1, 6C
3, C7
3, F8
5, 153
6, 1B6
9, 242
C, 312
F, 3DD
12, 483
15, 543
16, 5A1
17, 5D6
1A, 699
1B, 6F7
1F, 7C9
23, 8D4
25, 964
28, A29
2A, AA7
2D, B7C
33, CCC
35, D70
39, E46
3D, F4E
41, 1065
45, 1144
4A, 1292
4E, 139F
55, 1575
5E, 17BF
63, 18CA
6C, 1B06
72, 1C93
78, 1E2B
78, 1E2B
78, 1E17
82, 2083
8D, 2378
96, 259B
A1, 2844
AA, 2A82
B0, 2C00
B5, 2D58
BD, 2F40
C8, 322F
D4, 3518
E1, 386C
ED, 3B4A
F3, 3CD7
F4, 3D10
F4, 3D0E
F9, 3E4D
'
Here is the decimal output
0, 18
2, 130
8, 522
15, 975
24, 1582
34, 2237
45, 2936
58, 3723
72, 4640
89, 5730
98, 6315
112, 7223
128, 8231
132, 8470
132, 8471
139, 8936
153, 9849
170, 10894
181, 11597
190, 12190
200, 12846
215, 13766
225, 14435
228, 14642
233, 14968
240, 15399
247, 15836
251, 16116
255, 16347

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

void setup()
{
 Wire.begin();                // join i2c bus (address optional for master)
 Serial.begin(9600);          // start serial communication at 9600bps
}

unsigned int reading = 0;
int reading1 = 0;
void loop()
{

Wire.beginTransmission(66); // transmit to device #66
Wire.write(byte(255));      // sets register pointer to echo #1 register
Wire.endTransmission();      // stop transmitting

 // step 4: request reading from sensor
 Wire.requestFrom(66, 2);    // request 2 bytes from slave device #112

 // step 5: receive reading from sensor
 if(2 <= Wire.available())    // if two bytes were received
 {
   reading = Wire.read();  // receive high byte (overwrites previous reading)
   Serial.print(reading);   // print the reading
   Serial.print(", ");
   reading = reading << 6;    // shift high byte to be high 8 bits
   reading |= Wire.read(); // receive low byte as lower 8 bits
   Serial.println(reading);   // print the reading
 }

 delay(300);                  // wait a bit since people have to read the output :)
}

Nick Gammon

Their documentation is pretty obscure, and your results agree, as I say, with the way that the SPI interface works. This is my SPI code, and it reads two bytes:

Code: [Select]

// for parity checks
const unsigned int masks [16] = {
      0x01, 0x02, 0x04, 0x08,
      0x10, 0x20, 0x40, 0x80,
      0x100, 0x200, 0x400, 0x800,
      0x1000, 0x2000, 0x4000, 0x8000 };

unsigned int AS5048A::Transfer (const unsigned int command)
  {
  byte hi, lo;

  SPI.setDataMode (SPI_MODE1);
  digitalWrite (chipSelect_, LOW);
  hi = SPI.transfer (highByte (command));
  lo = SPI.transfer (lowByte (command));
  digitalWrite (chipSelect_, HIGH);
   
  unsigned int result = makeWord (hi, lo);
 
  // calculate parity
  byte parity = 0;
  for (byte i = 0; i < 16; i++)
    {
    if (result & masks [i])
      parity++;
    }
  // even parity, so low-order bit should be zero
  if (parity & 1)
    {
    parityFlag_ = true;
    errorFlag_ = false;  // we actually don't know the error flag state
    }
  else
    {
    parityFlag_ = false;
    errorFlag_ = (result & EF) != 0;
    }
  return result;
   
  } // end of AS5048A::Transfer


Go Up