title: Lm74 thermometer bugs while on negative temperature using STM
body: <p>I want to measure temperature using lm74 temperature sensor:
  <a href="https://html.alldatasheet.com/html-pdf/9026/NSC/LM74/113/3/LM74.html" rel="nofollow noreferrer">https://html.alldatasheet.com/html-pdf/9026/NSC/LM74/113/3/LM74.html</a></p>
  
  <p>I have connected it to my stm32L476 by SPI as shown on diagram: 
  <a href="https://i.stack.imgur.com/1rHjs.png" rel="nofollow noreferrer">Conection stm temperature sensor</a></p>
  
  <p>Code to recive temperature (it is triggled by interuption every 1 s):</p>
  
  <pre><code>float TEMP_readTemp() {
      uint16_t temp_binary;
  
      HAL_GPIO_WritePin(CS_GPIO_Port, CS_Pin, GPIO_PIN_RESET);
      HAL_SPI_Receive(&amp;hspi1, &amp;temp_binary, 1, 10);
      HAL_GPIO_WritePin(CS_GPIO_Port, CS_Pin, GPIO_PIN_SET);
      float temp;
      temp = TEMP_ConvertTemp2(temp_binary);
      if (LOG_UART_ENAIBLE)
          TEST_UART_LogSTLink("Temp:  %s \n\r", FLOAT, &amp;temp);
      return temp;
  }
  </code></pre>
  
  <p>It shows a positive temperature ok. But when I have the temperature below 0 it shows -256.0625 every second record:</p>
  
  <pre><code>Temp:  -4.187500                                                                
  Temp:  -256.062500                                                              
  Temp:  -4.125000                                                                
  Temp:  -256.062500                                                              
  Temp:  -4.187500                                                                
  Temp:  -256.062500                                                              
  Temp:  -4.250000                                                                
  Temp:  -256.062500                                                              
  Temp:  -4.250000                                                                
  Temp:  -256.062500                                                              
  Temp:  -4.312500                                                                
  Temp:  -256.062500                                                              
  Temp:  -4.312500                                                                
  Temp:  -256.062500                                                              
  Temp:  -4.312500                                                                
  Temp:  -256.062500   
  </code></pre>
  
  <p>Any idea whats happening?</p>
  
  <p>converting function:</p>
  
  <pre><code>float TEMP_ConvertTemp2(uint16_t arrayBoth) {
      uint16_t bitShift;
      int bitShift_int;
      uint16_t division;
      float temperature;
      if (arrayBoth &amp; 0x8000) {
          division = arrayBoth - 1;
          division = ~division;
          bitShift = division &gt;&gt; 3;
          bitShift_int = (int) bitShift;
          temperature = (float) -1 * ((bitShift_int +1 )* LSB_CONST);
      } else {
          bitShift = arrayBoth &gt;&gt; 3;
          bitShift_int = (int) bitShift;
          temperature = bitShift_int * LSB_CONST;
      }
      return temperature;
  }
  </code></pre>
  
  <p>Update:
  Ok, I looked by oscilloscope to see it (today it was strange, every second record was wrong, no matter what temperature was (positive or negative)).</p>
  
  <p><a href="https://i.stack.imgur.com/zNGMB.jpg" rel="nofollow noreferrer">CS and CLOCK osciloscope</a></p>
  
  <p><a href="https://i.stack.imgur.com/1MQWr.jpg" rel="nofollow noreferrer">MISO and CLOCK osciloscope</a></p>
  
  <p>Those photos show two measurements done one right after the another.  The first one is -256.0625,  secend is correct and is about +31.</p>