SBAA588 April   2024 LM73 , LM75B , LM95071 , TMP100 , TMP101 , TMP102 , TMP103 , TMP104 , TMP107 , TMP1075 , TMP108 , TMP112 , TMP114 , TMP116 , TMP117 , TMP121 , TMP122 , TMP123 , TMP124 , TMP126 , TMP144 , TMP175 , TMP1826 , TMP1827 , TMP275 , TMP400 , TMP401 , TMP411 , TMP421 , TMP422 , TMP423 , TMP431 , TMP432 , TMP435 , TMP451 , TMP461 , TMP464 , TMP468 , TMP4718 , TMP75 , TMP75B , TMP75C

 

  1.   1
  2.   Abstract
  3.   Trademarks
  4. 1Introduction
    1. 1.1 2's Complement
      1. 1.1.1 2's Complement Traits
    2. 1.2 Q Format
    3. 1.3 Common Temperature Data Format
    4. 1.4 High Accuracy Temperature Data Format
  5. 2Code Examples
    1. 2.1  16 Bits With Q7 Notation
      1. 2.1.1 Properties
      2. 2.1.2 C Code
    2. 2.2  12-bits With Q4 Notation
      1. 2.2.1 Properties
      2. 2.2.2 C Code
    3. 2.3  13-bits With Q4 Notation (EM=1)
      1. 2.3.1 Properties
      2. 2.3.2 C Code
    4. 2.4  13-bits With Q4 Notation
      1. 2.4.1 Properties
      2. 2.4.2 C Code
    5. 2.5  14-bits With Q6 Notation
      1. 2.5.1 Properties
      2. 2.5.2 C Code
    6. 2.6  TMP182x Formats
      1. 2.6.1 Properties
      2. 2.6.2 C Code
    7. 2.7  14-bits with Q5 Notation
      1. 2.7.1 Properties
      2. 2.7.2 C Code
    8. 2.8  8-bits With No Q Notation
      1. 2.8.1 Properties
      2. 2.8.2 C Code
    9. 2.9  11-bits With Q3 Notation
      1. 2.9.1 Properties
      2. 2.9.2 C Code
    10. 2.10 Devices Without 2's Complement
      1. 2.10.1 Properties
      2. 2.10.2 C Code
  6. 3Other Programming Languages
    1. 3.1 Parsing
    2. 3.2 2's Complement
    3. 3.3 Discard Unused Bits
    4. 3.4 Apply Q format
  7. 4Summary
  8. 5References
  9. 6Appendix: Q App Source Code
  10. 7Appendix: Device Summary Table

Parsing

  • When programming hardware, the data is often returned as two 8-bit bytes.
    • If the language has explicit type conversions, initially store these bytes as unsigned to better control the conversion to signed bytes. The behavior that must be avoided is called sign-extend of the lower byte.
/* C Signed Types */
unsigned char x = 0xFF;
signed char y = 0xFF;
/* x is treated as 255 and y as -1 */

/* C99 fixed width integer types */
uint8_t x = 0xFF;
int8_t y = 0xFF;
/* x is treated as 255 and y as -1 */
  • In C, sscanf() parses strings (char*) including numbers that have 0x prefix. It's counterpart, printf(), uses the same format strings. The length modifier is a less commonly used feature of the format string, but this modifier can help to clean up our small data types. Here is a table of data types and format strings with relevant length modifiers. Note that %i used in scanf detects and correctly decodes the 0x prefix while %d and others do not.
Table 3-1 C Format Strings With Length Modifier
Bits Data Type Fixed Width Type Format String
8 char int8_t / uint8_t %hhi
16 short int int16_t / uint16_t %hi
32 int int32_t / uint32_t %i
/* C Parsing and Outputting Hex */
char *s = "0xFF";
uint8_t x;
sscanf(s, "%hhi", &x);
/* x is 255 */

/* printf without length modifier */
printf("%i, %d, %u, %x\n", x, x, x, x);
/* "-1, -1, 4294967295, ffffffff" is printed due to coercion into 32 bit types and sign-extend */

/* printf with length modifier */

printf("%hhu, 0x%hhX, %#hhX\n", x, x, x);
/* the desired "255, 0xFF, 0XFF" is printed */
  • Hex is convenient to use. Excel has HEX2DEC and related functions. This function does not allow prepending of "0x." HEX2DEC turns "FFFFFFFFFF" (ten Fs) into -1, so this function is internally behaving as a 40-bit signed data type. This is plenty of bits for temperature data.
  • Excel's binary functions, such as DEC2BIN, are limited to 9 bit functionality.
  • While HEX2DEC is desired for decoding of temperature data, the complementary function DEC2HEX can help when encoding temperature data.
  • In the following table, the result of the B column calculations are 15 and 0xA.
Table 3-2 Excel Example for Parsing
A B
1 F =HEX2DEC(A1)
2 10 =DEC2HEX(A2)
  • JavaScript, Python and C all accept numeral constants pre-fixed with 0x.
  • JavaScript has the function parseInt() that parses "0x" notation for hex. For example, the parseInt() function correctly converts "0xA" into 10.
  • JavaScript has the method toString() for string data, which creates a hex value if provided the argument 16.
/* JavaScript Parsing and Outputting Hex */
let x = 0xA
let y = parseInt("0xA")
let z = (10).toString(16)
let s = "0x" + x.toString(16).toUpperCase().padStart(2,'0')
/* x and y are 10, z is 'a' and s is '0x0A' */
  • Python's int() converts strings if the base argument is provided.
  • Python's hex() converts numbers to hexadecimal strings.
# Python Parsing and Outputting Hex
x = 0xA
y = int("A",base=16)
z = hex(10)
# x and y are 10, z is '0xa'