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

ujurmsde
Posts: 6
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;
}


PamelaGary
Posts: 1
Joined: Tue Apr 06, 2021 9:43 am

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

Postby PamelaGary » Tue Apr 06, 2021 10:14 am

There really is no need for anything other than int and unsigned long: these types have at least as many bits as int16_t and uint32_t, and unlike int16_t and uint32_t, they must exist in any appropriate implementation. If you really want sexy new size types, at least go for portability with int_least16_t and uint_least32_t write my essay.


Who is online

Users browsing this forum: No registered users and 18 guests