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

ujurmsde
Posts: 38
Joined: Tue Jan 19, 2021 6:37 am

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

Postby ujurmsde » 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;
}



Eusebiodirlin
Posts: 1
Joined: Fri Dec 03, 2021 7:30 am

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

Postby Eusebiodirlin » Fri Dec 03, 2021 7:32 am

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?

Who is online

Users browsing this forum: Google [Bot] and 40 guests