"requestFrom" --Wire.cpp

Hallo,
I am using "Arduino UNO" with "HDC1000" . And in "Adafruit_HDC1000.cpp" file , there is

" Wire.requestFrom((uint8_t)_i2caddr, (uint8_t)4);" .. I dont understand, what does it do ?


uint32_t Adafruit_HDC1000::read32(uint8_t a, uint8_t d) {


Wire.requestFrom((uint8_t)_i2caddr, (uint8_t)4);///wire.cpp ---> requestFrom


}
________________________________________________________--

is it taking the address of internal Register and reading the data from that internal Register ? ? ?

Thanks

This is the reference of the Wire.requestFrom : Wire - Arduino Reference

If you use something, please give links or photos.
This file ? Adafruit_HDC1000_Library/Adafruit_HDC1000.cpp at master · adafruit/Adafruit_HDC1000_Library · GitHub

They have put the I2C address of the HDC1000 in a variable _i2caddr.
They cast the _i2caddr to a uint8_t, and they cast the number 4 to a uint8_t. I don't know why, I think that is not needed.

The default for a number (for example number 4) is an integer. They cast it to a 8-bits unsigned byte.

uint32_t Adafruit_HDC1000::read32(uint8_t a, uint8_t d) {
  Wire.beginTransmission(_i2caddr);
  Wire.write(a);
  Wire.endTransmission();
  delay(50);
  Wire.requestFrom((uint8_t)_i2caddr, (uint8_t)4);
  uint32_t r = Wire.read();
  r <<= 8;
  r |= Wire.read();
  r <<= 8;
  r |= Wire.read();
  r <<= 8;
  r |= Wire.read();
  //Serial.println(r, HEX);
  return r;
}

Wire.requestFrom() tells it to read the specified number of bytes from the device (and it then gets them with Wire.read() one at a time, and reassembles them into a 32-bit unsigned integer).

Prior to the request from, it sends a single byte (a, the first argument to the function) - this probably sets the address (within the device's internal memory map) that the subsequent requestFrom() will read starting at. (while the I2C standard doesn't specify it, this convention is very common in I2C devices)

Koepel:
They have put the I2C address of the HDC1000 in a variable _i2caddr.
They cast the _i2caddr to a uint8_t, and they cast the number 4 to a uint8_t. I don't know why, I think that is not needed.

The default for a number (for example number 4) is an integer. They cast it to a 8-bits unsigned byte.

C++ treats int and uint8_t (unsigned char) as different so unless you do the cast, there is no function with the proper arguments if the function is declared with uint8_t arguments and the caller arguments are int

The int is too big for the uint8_t so it is not a match.

--- bill

The requestFrom is declared 5 times, also one with: uint8_t requestFrom ( int , int) ;
https://github.com/arduino/Arduino/blob/master/hardware/arduino/avr/libraries/Wire/src/Wire.h

Thanks Guys for the info.

I would like to ask you again, the HDC1000 sensor is sending data to the microcontroller as a "Temperature / Humidity".

But is HDC1000 sensor sending "hex value or Binary value" to the microcontroller ? or it is sending Decimal value ?

Thanks

The I2C communication is almost always binary data. But it depends on the sensor what it is sending.

"hex", "binary", and "decimal" are not encoding formats, but rather display formats.
that are determined by how you choose to display a value not by what the contents is.
For example, a byte in memory can be displayed as hex, binary, or decimal. The actual value never changes.

What you really may be asking, perhaps, is how is the data encoded.
The TI HDC1000 datasheet describes the data encoding in section 8.1

But since you seem to be using the Adafruit library, you don't have to concern yourself with such details.
You can use the API functions provided by the library to read the humidity and temperature and you can look in the library header file to see what those functions are and what they return.

--- bill

Thanks again.

I would like to ask again a small question regarding "Adafruit_HDC1000" . the both prog ( .cpp and .h) can be found here

Problem is in "Adafruit_HDC1000.cpp" file The Method " read32(uint8_t a, uint8_t d)" . the full Method is like this:


uint32_t Adafruit_HDC1000::read32(uint8_t a, uint8_t d) {

 
  Wire.beginTransmission(_i2caddr); ///uint8_t _i2caddr = addr;

   Wire.write(a);  ///uint32_t read32(uint8_t a, uint8_t d=0); -->
                          /// float hum = (read32(HDC1000_TEMP, 20) & 0xFFFF);
                     ///HDC1000_TEMP  0x00
                    /// SO its sending command/Internal Register address, write(a)... a = 0x00

  Wire.endTransmission();
  delay(50);


 
  Wire.requestFrom((uint8_t)_i2caddr, (uint8_t)4);///wire.cpp  


  uint32_t r = Wire.read(); ///wire.cpp ---> read(void)
                                       ///Lets say, r = Wire.read() = 2... decimal value

  r <<= 8;       ///r = r << 8;  --> r = 2 << 8= 512

  r |= Wire.read();     ///r = r | Wire.read();  --> r = 512 | 2 = 514


  r <<= 8;      /// r= 514 << 8 = 332800

  r |= Wire.read();    /// r = 332800 | 2 = 332802

  r <<= 8;             ///r = 332802 << 8 = 858259968

  r |= Wire.read();        ///r = 858259968 | 2 = 	858259970

  ///Serial.println(r, HEX);

  return r;
}

I really don't understand, why we need this :


r <<= 8;

r |= Wire.read();


and really what does it do ? ? ?

if possible try to look here also --> HDC1000 -->Humidity problem - Microcontrollers - Arduino Forum

I really appreciate your help....

Thanks

Again, don't confuse the human readable output formats of hex,binary,decimal with data encoding.
The two are different.
To say a value is "decimal" in this context doesn't mean anything as any value communicated is hex, decimal, octal, and binary all at the same since those are simply output formats not encoding/storage formats
It is the data encoding format that matters.

The shift <<=8 and the OR |= are being used to construct a value that is larger than 8 bits.
This because the values from the part being read that are used to calculate the final temperature and humidity values are larger than 8 bits but are read 8 bits at a time.

To understand how the part works and the size and encoding of the data values for humidity and temperature you will need to read the datasheet and focus on section 8.
Section 8.1 explains the data encoding.

As to why you may be getting incorrect results from the adafruit readTemperature() and readHumidity() functions goes, that is probably best discussed in your other thread:
http://forum.arduino.cc/index.php?topic=402026.0

--- bill

Thank you bperrybap .........