First you say your I2C address is correct since you see ACK on the scope, good. But it does not write all data apparently, however do you see ACKs after each data byte on the scope? The most logical reason it does not send everything is because it does not get an ACK back (which would be weird, if the address is correct).
Then you have several times i2c.stop() commands, that is not needed, the i2c.write/read commands will send a stop command in the end (unless you add ",true" at the end of the command, then it will transmit a restart instead).
In your read function you first write three bytes, however only one is initialized, the other two are randomly what used to be in that register address. The next read it is better to just use the 'normal' read command: i2c.read(RAMaddr, [pointer to wherever you want to store results], [number of bytes to read]); How you used the i2c read command isn't valid, and will for sure not give you what you will expect.
Hopefully that will get you further.
Hi,
I'm having some problems writing to and reading from a RAM in an I2C device.
My code is as follows:
void RAMw() // I2C RAM write routine { pc.printf("I2C: - write to RAM\r\n"); char cmdw[3]; cmdw[0] = 0x8f; // Set cmd to HEX 8f to write to RAM address 0f cmdw[1] = 0x62; // Set cmd[1] to HEX 62 (=98 dec) as second data byte for 0f cmdw[2] = 0x4a; // Set cmd[2] to HEX 4a (=74 dec) as first data byte for 0f i2c.write(RAMaddw, cmdw, 3); // addr + 2 byte cmd = f08f624a i2c.stop(); wait(0.1); } void RAMr1() // I2C RAM read routine { char cmdr[3]; pc.printf("Read back from RAM\r\n"); cmdr[0] = 0x1F; // Set cmd to HEX 1F to read from RAM address 0f to SIF i2c.write(RAMaddr, cmdr, 3); // addr + 1 byte cmd = f01F i2c.stop(); wait(0.1); val = i2c.read(0x0f); // read from SIF to I2C val2 = val; pc.printf("RAM address 0f: %04x hex, ",val2); pc.printf("dec = %d\r\n",val2); if (val == data){pass4 = true;}; }When I look at the I2C with a scope, I see the write data is F08F - when I would expect to see F08F624A. I know the I2C address is correct otherwise I would not get an acknowledge back from the device. During the read stage I see F01F62 on the scope trace! But val in the above comes out as value of cmdw[1] x 2 (in this case c4 hex = 196 dec).
Are my write and/or read routines at fault? The device in question is a ZSC31050 sensor conditioner from ZMD.
I've looked through the forum post which have helped me get this far. Any pointers to help me make progress would be most useful. Thanks in advance.
Chris