I2C writing to a 16bit register on TMP117

I have a TMP117 temp sensor connected to I2C.
I can read out the configuration on 0x48 as follows:

Wire.beginTransmission(0x48);
Wire.write(0x01);
Wire.endTransmission();
Wire.requestFrom(0x48, 2);
delay(5);
Wire.readBytes((uint8_t*)&buf3, 2);
Serial.print(" Config Reg "); Serial.println(buf3,HEX);

This works perfect!
I need to now change this register to a value of 0x0220 but whatever I try fails!
The register is 16bytes in length (Word)

Wire.beginTransmission(0x48);
Wire.write(0x01); 
Wire.write((word)0x0220);
Wire.endTransmission();

Can anyone help me?

That writes only one byte. Write two bytes in succession, using two calls to Wire.write() (if the device supports address autoincrement).

The data sheet has the details.

1 Like

Please put me out of my misery,

So --

  Wire.beginTransmission(0x48);
  Wire.write(0x01); 
  Wire.write((word)0x220);
  Wire.write((word)0x220);
  Wire.endTransmission();

Now returns a value of 2000, where am I going wrong?
It needs to be 0220.
My brain is fried!

Many Thanks
JL

Now you are writing the same byte twice. Wire.write() expects a byte argument.
Also, mind Endianess!

Try this, or reverse the order

  Wire.beginTransmission(0x48);
  Wire.write(0x01); 
  Wire.write(0x20);  //low byte
  Wire.write(0x02);  //high byte
  Wire.endTransmission();

so tried both ways

Wire.beginTransmission(0x48);
  Wire.write(0x01); 
  Wire.write(0x02);  //low byte
  Wire.write(0x20);  //high byte
  Wire.endTransmission();

gives a result of 2002 other way gives a result of A400

Maybe I'm losing the plot??
Any help appreciated.

This is what SparkFun does

/* WRITE REGISTER
    Wire data to a TMP117 register
*/
void TMP117::writeRegister(uint8_t reg, uint16_t data) // originally TMP117_Register reg
{
	_i2cPort->beginTransmission(_deviceAddress); // Originally cast uint8_t when a register value again
	_i2cPort->write(reg);
	_i2cPort->write(highByte(data)); // Write MSB (D15-D8)
	_i2cPort->write(lowByte(data));  // Write LSB (D7-D0)
	_i2cPort->endTransmission();	 // Stop transmitting data
}

See SparkFun_TMP117.cpp

Give it a try ...

Good Luck!
ec2021

1 Like

If you have fixed this, then also please remove the "delay(5);" after the "Wire.requestFrom()".

0xA400 can't be correct. There is a mistake in the code, so post the "other way" that produced this result.

Here is the full dirty code....

#include <Wire.h>

const byte  address           =0x48;      
const byte  configurationReg  =0x01;      
const byte  tempHighLimit     =0x02;      
const byte  tempLowLimit      =0x03;
const byte  Eeprom_UL         =0x04;     
const byte  Eeprom1           =0x05;     
const byte  Eeprom2           =0x06;      
const byte  tempOffset        =0x07;
const byte  Eeprom3           =0x08;      
const byte  deviceID          =0x0F;

uint16_t buf1;
uint16_t buf2;
uint16_t buf3;
uint16_t data = 0x220;

void setup()
{
  Serial.begin(115200);
  Wire.begin(); 
  Wire.setClock(400000);             
 
  //Wire.beginTransmission(0x48);
  //Wire.write(0x04); 
  //Wire.write((word)0b0000000000000001); 
  //Wire.endTransmission();
  //delay(15);
  
Wire.beginTransmission(0x48); // Originally cast uint8_t when a register value again
Wire.write(uint8_t(0x1));
Wire.write(highByte(data)); // Write MSB (D15-D8)
Wire.write(lowByte(data));  // Write LSB (D7-D0)
Wire.endTransmission();	 // Stop transmitting data
delay(500);
}


void loop()
{
  Wire.beginTransmission(0x48);
  Wire.write(0x05);
  Wire.endTransmission();
  Wire.requestFrom(0x48, 2); 
  Wire.readBytes((uint8_t*)&buf1,2);

  Wire.beginTransmission(0x48);
  Wire.write(0x08);
  Wire.endTransmission();
  Wire.requestFrom(0x48, 2); 
  Wire.readBytes((uint8_t*)&buf2,2);

  Wire.beginTransmission(0x48);
  Wire.write(0x01);
  Wire.endTransmission();
  Wire.requestFrom(0x48,2); 
  Wire.readBytes((uint8_t*)&buf3,2);
  
  Serial.print("I2C Address 0x48 S/N 0x"); Serial.print(buf1,HEX);Serial.print(buf2,HEX);
  Serial.print("  Config Reg "); Serial.print(buf3,HEX);Serial.print(" Bin "); Serial.println(buf3,BIN);
}

The result with device connected for config is 2002 if I swap order of high low I get A400 just don't understand??

I don't understand this comment. What code produced "2002" and what code produced "A400"?

Swapping order of ..

Wire.write(highByte(data)); // Write MSB (D15-D8)
Wire.write(lowByte(data));  // Write LSB (D7-D0)

And this is what SparkFun does to read a register

/* READ REGISTER
	This function reads the register bytes from the sensor when called upon.
	This reads 2 bytes of information from the 16-bit registers. 
*/
uint16_t TMP117::readRegister(uint8_t reg) // originally TMP117_Register reg
{
	_i2cPort->beginTransmission(_deviceAddress); // Originally cast (uint8_t)
	_i2cPort->write(reg);
	_i2cPort->endTransmission();					   // endTransmission but keep the connection active
	_i2cPort->requestFrom(_deviceAddress, (uint8_t)2); // Ask for 2 bytes, once done, bus is released by default

	uint8_t data[2] = {0};			// Declares an array of length 2 to be empty
	int16_t datac = 0;				// Declares the return variable to be 0
	if (_i2cPort->available() <= 2) // Won't read more than 2 bits
	{
		data[0] = _i2cPort->read();			// Reads the first set of bits (D15-D8)
		data[1] = _i2cPort->read();			// Reads the second set of bits (D7-D0)
		datac = ((data[0] << 8) | data[1]); // Swap the LSB and the MSB
	}
	return datac;
}

The same source as above ...

Analyze the difference ... and give it also a try ...

Good luck!
ec2021

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.