Raspberry Pi 上的 C# TMP102。疯狂的温度回来了

C# TMP102 on Raspberry Pi. Crazy Temps Returned

我正在尝试将我从 Sparkfun.com 购买的 TMP102 与我的 Raspberry Pi 一起使用。

我在 Pi 上使用 Mono 并从这个 site 复制了代码以进行跳跃。

这是我的:

using System;
using System.Diagnostics;
using System.Threading;

namespace NDBC_Keg_Manager
{
    public class Tmp102
    {
        private string i2cgetExe = "/usr/sbin/i2cget";
        private string i2cgetCmdArgs = "-y 1 0x48 0 b";

        private Process p;

        public Tmp102()
        {
            p = new Process();
        }

        public double tempC
        {
            get { return readRawTempData(); }
        }
        public double tempF
        {
            get { return this.tempC * 1.8 + 32; }
        }

        private int readRawTempData()
        {
            // Don't raise event when process exits
            p.EnableRaisingEvents = false;
            // We're using an executable not document, so UseShellExecute false
            p.StartInfo.UseShellExecute = false;
            // Redirect StandardError
            p.StartInfo.RedirectStandardError = true;
            // Redirect StandardOutput so we can capture it
            p.StartInfo.RedirectStandardOutput = true;
            // i2cgetExe has full path to executable
            // Need full path because UseShellExecute is false

            p.StartInfo.FileName = i2cgetExe;
            // Pass arguments as a single string
            p.StartInfo.Arguments = i2cgetCmdArgs;
            // Now run i2cget & wait for it to finish
            p.Start();
            p.WaitForExit();
            // Data returned in format 0xa017
            // Last 2 digits are actually most significant byte (MSB)
            // 2 digits right after 0x are really least significant byte (LSB)
            string data = p.StandardOutput.ReadToEnd();
            // Get LSB & parse as integer
            Console.WriteLine();
            Console.WriteLine("Raw Hex: " + data.ToString());
            data = data.Substring(4, 2);
            int hexData = Int32.Parse(data, System.Globalization.NumberStyles.AllowHexSpecifier);
            Console.WriteLine("Pre-Dec: " + data.ToString());
            Console.WriteLine("Dec: " + hexData.ToString());

            return hexData;

        }

        public static void Main()
        {
            Tmp102 t = new Tmp102();
            while (true)
            {
                // Print temp in degrees C and F to console
                Console.WriteLine("{0}°C  {1}°F", t.tempC, t.tempF);
                Thread.Sleep(1000);
            }
        }
    }
}

我在 Raspberry Pi 上得到的是一个传感器的温度为 119c,另一个传感器的温度为 90c。两个传感器都太不正常了,这让我想知道它的代码是否在某种程度上是错误的??

原始代码读取两个字节并翻转它们,因为 i2cget 反转输出并将 12 位转换为 dec 值。 TI 的文档说 MSB 是整个温度。我通过在 Pi 上使用 b 标志而不是带有 i2cget 的 w 标志直接获得该值。

建议??

来自该芯片的 documentation 中的第 7.3.1 节:

Byte 1 is the most significant byte (MSB), followed by byte 2, the least significant byte (LSB). The first 12 bits (13 bits in extended mode) are used to indicate temperature. The least significant byte does not have to be read if that information is not needed. The data format for temperature is summarized in Table 2 and Table 3. One LSB equals 0.0625°C. Negative numbers are represented in binary twos-complement format ... Bit D0 of byte 2 indicates normal mode (EM bit = 0) or extended mode (EM bit = 1) , and can be used to distinguish between the two temperature register data formats.

所以,似乎需要交换字节,你需要去掉包含其他信息的其他位。然后你需要按 0.0625 缩放。这样的事情应该可以解决问题:

ushort raw = 0xA017;
int swapped = ( ( raw & 0x00FF ) << 8 ) | ( raw >> 8 );
int trimmed = swapped & 0xFFF; // 12 bits
double temperature = trimmed * 0.0625; // 122.0

这适用于正温度。对于否定,请参阅手册第 8 页的说明。