Introduction
The purpose of this technical note is to provide a review of two's complement notation for ease of design in the LPS22HB pressure sensor and to guide the user in the interpretation of pressure and temperature values in the device registers. MEMS pressure sensors measure absolute pressure ( P ) and temperature ( t ) and store the values in two's complement registers which can be read via the I 2 C or SPI host interface. The pressure sensor stores pressure as a 24-bit word. Both values are stored as two's complement integers as illustrated below.

1
Why we use two's complement
Measuring analog values and storing the data in a digital domain creates an interesting dilemma. How do we represent negative analog values in a digital system? How do we handle the number zero? A computing system uses two's complement to simplify the processing logic required to handle negative numbers and subtractions. A circuit designed for addition can handle negative operands by using two's complement. This minimizes the need for additional circuitry capable of subtraction or additional switching circuitry based on the sign. Most modern computers, MCU's, and DSP's use two's complement notation. If a register is defined as two's complement, typically the most significant bit (msb) of the most significant byte (MSB) indicates the sign as shown in the figure below. If the msb of the register is 1, the number is negative and we use two’s complement. If the bit is 0, the integer is positive and no translation is necessary.

2
How to obtain pressure values in mbar
The pressure sensor stores the pressure value in raw counts in 3 registers: PressOut_H, PressOut  L, and PressOut  XL. The most significant bit of the PressOut  H register indicates the polarity. If the sign bit is zero, then the value is positive and the pressure in mbar is determined by dividing the decimal value by the scaling factor of 4096. A sign bit of 1 indicates a negative value, so we first take the two's complement of the complete word and then divide by 4096. Keep in mind that the sign bit determines whether we should perform the two's complement operation or not. The 3 bytes are concatenated to form a 24-bit word and the complete word is represented in two's complement (not the single bytes). When reading the pressure value, it is important to note the byte ordering. This is especially important in auto-increment mode when the address is incremented automatically. The designer should assemble the proper 24-bit endian format (pressure count = 2Ah & 29h & 28h). The device may be configured to report a delta pressure by using the auto-zero feature. The actual pressure output value is copied in the REF_P_H, REF_P_L, and REF_P_XL registers and the PressOut() registers contain the difference between the current pressure and the reference pressure. So the PressOut() registers could contain a negative value. Negative values can also occur when a device is defective. Hence it can be used for self- test during a boot-up operation and when the auto-zero feature is not used. An example of a pressure calculation when the sign bit is 0 is shown in the following figure. The auto-zero feature can be enabled by setting bit 1 in CTRL_REG2 (0x21h).
3
How to obtain temperature values in °C
The LPS22HB also contains a temperature sensor which is used for compensation of the pressure sensor. The temperature data is also available for outside applications. The temperature raw count is stored in registers Temp  Out  H (2Ch) and Temp  Out  L (2Bh). The temperature in °C can be determined by calculating the two's complement, if necessary, and perform the scaling. The temperature in °C is calculated as follows:
An example of a temperature calculation for LPS22HB when the sign bit is 0 is shown below.
Temperature Counts =  2Ch & 2Bh  = 2710 =  10000 counts = 100 °C Likewise, a negative value is indicated when the MSB in the temperature word is equal to 1. To obtain the temperature, we first take the two's complement of the complete word and then perform the scaling. When reading the temperature values, it is also important to note the byte ordering. This is especially true in auto-increment mode in which case address 2Bh is read first, followed by address 2Ch. The designer should assemble the proper 16-bit endian format (temperature count = 2Ch & 2Bh).
How to obtain temperature values in  C
The following registers in the device are two's complement. All other registers in the device are either unsigned or bitwise representations (not negative).

4
Support material

5
Revision history


STMicroelectronics NV and its subsidiaries ("ST") reserve the right to make changes, corrections, enhancements, modifications, and improvements to ST products and/or to this document at any time without notice. Purchasers should obtain the latest relevant information on ST products before placing orders. ST products are sold pursuant to ST's terms and conditions of sale in place at the time of order acknowledgement. Purchasers are solely responsible for the choice, selection, and use of ST products and ST assumes no liability for application assistance or the design of Purchasers' products. No license, express or implied, to any intellectual property right is granted by ST herein. Resale of ST products with provisions different from the information set forth herein shall void any warranty granted by ST for such product. ST and the ST logo are trademarks of ST. All other product or service names are the property of their respective owners. Information in this document supersedes and replaces information previously supplied in any prior versions of this document. © 2015 STMicroelectronics – All rights reserved