Page 1 of 1

How to correctly process two's complement data with ESP-RTOS tasks for sensor

Posted: Tue Apr 06, 2021 8:38 am
by ujurmsde
I have a question regarding 2's complement values which I get from I2C sensor. I am new to ESP and maybe the question might look a bit strange but I am facing this problem.
I have a pressure sensor from Infineon DPS310. There are two's complement values in 12 bit and some are in 24 bits. I want to process them with the right datatype.

Is uint16_t/uint32_t are okey or int16_t/int32_t is a better choice?

Also, I don't understand the following code which I found online for two's complement conversion. Can someone tell is this the correct code?

Code: Select all

static int32_t twosComplement(int32_t val, uint8_t bits) {
  if (val & ((uint32_t)1 << (bits - 1))) {
    val -= (uint32_t)1 << bits;
  }
  return val;
}


Re: How to correctly process two's complement data with ESP-RTOS tasks for sensor

Posted: Wed Apr 07, 2021 9:24 pm
by chegewara

Re: How to correctly process two's complement data with ESP-RTOS tasks for sensor

Posted: Fri Dec 03, 2021 7:32 am
by Eusebiodirlin
ujurmsde wrote:
Tue Apr 06, 2021 8:38 am
I have a question regarding 2's complement values which I get from I2C sensor. I am new to ESP and maybe the question might look a bit strange but I am facing this problem.
I have a pressure sensor from Infineon DPS310. There are two's complement values in 12 bit and some are in 24 bits. I want to process them with the right datatype.

Is uint16_t/uint32_t are okey or int16_t/int32_t is a better choice?

Also, I don't understand the following code which I found online for two's complement conversion. Can someone tell is this the correct code?

Code: Select all

static int32_t twosComplement(int32_t val, uint8_t bits) {
  if (val & ((uint32_t)1 << (bits - 1))) {
    val -= (uint32_t)1 << bits;
  }
  return val;
}

Is uint16_t/uint32_t are okey or int16_t/int32_t is a better choice?