将温度从3个字节2的补充中转换

发布于 2025-02-04 17:34:24 字数 1967 浏览 6 评论 0 原文

我正在尝试使用Raspberry Pi Pico运行Micropython读取ADC的内部温度(Texas仪器的ADS1235)。 Raspberry Pi Pico和ADC工作之间的SPI通信(我使用示波器检查)。 3个数据字节收到的表格ADC我必须变成一个数字:

“

数据在两者的补充中,首先是MSB。我试图从24位两位的补充二进制字符串变成负数或正数。一个正数计算可行,但对于负数(最重要的位是1),则没有。转换有一些功能吗?转换器函数的代码在其中模拟ADC发送3个字节以[0x81,0x00,0x00]的顺序发送3个字节:

import string

def twos_comp_to_decimal(adcreading):
    """compute the int value of 2's complement 24-bit number"""
    """https://www.exploringbinary.com/twos-complement-converter/ look at "implementation" section"""
    """https://note.nkmk.me/en/python-bit-operation/#:~:text=Bitwise%20operations%20in%20Python%20%28AND%2C%20OR%2C%2
    0XOR%2C%20NOT%2C,NOT%2C%20invert%3A%20~%206%20Bit%20shifts%3A%20%3C%3C%2C%20%3E%3E"""

    signbit = False  # Assume adc-reading is positive from the beginning

    if adcreading >= 0b100000000000000000000000:
        signbit = True
        print("negative signbit")

    if signbit:
        print("inv string")
        negativval = bin(~adcreading & 0b011111111111111111111111)
        negativval = int(negativval)
        negativval += 0b000000000000000000000001

        negativval *= -1
        return negativval

    return adcreading

if __name__ == '__main__':
    # tempdata = [0x80, 0xFF, 0x80]
    tempdata = [0x81, 0x00, 0x00]

    print("Slicing 3 databytes into one 24-bit number")
    adc24bit = int.from_bytes(bytearray(tempdata), "big")
    print(adc24bit)
    print(hex(adc24bit))
    print(bin(adc24bit))

    print(twos_comp_to_decimal(adc24bit))
    # print("Integer value: {}".format(adc24bit))

    #temperatureC = ((adc24bit - 122.400) / 420) + 25
    #print("Temp in celcius: {}".format(temperatureC))

I'm trying to read internal temperature of an ADC (ADS1235 from Texas Instrument) using the Raspberry Pi Pico running MicroPython. SPI communication between Raspberry Pi Pico and ADC works (I used an oscilloscope to check). 3 data bytes received form the ADC I have to turn into a number:

ADC response to Read Data command

The data is in two's complement, MSB first. I tried to go from a 24-bit two's complement binary string to a negative or positive number. A positive number calculation works, but for a negative number (where most significant bit is 1) it doesn't. Is there some function for the conversion? Code of converter function where I simulate ADC sent 3 bytes in the order [0x81, 0x00, 0x00]:

import string

def twos_comp_to_decimal(adcreading):
    """compute the int value of 2's complement 24-bit number"""
    """https://www.exploringbinary.com/twos-complement-converter/ look at "implementation" section"""
    """https://note.nkmk.me/en/python-bit-operation/#:~:text=Bitwise%20operations%20in%20Python%20%28AND%2C%20OR%2C%2
    0XOR%2C%20NOT%2C,NOT%2C%20invert%3A%20~%206%20Bit%20shifts%3A%20%3C%3C%2C%20%3E%3E"""

    signbit = False  # Assume adc-reading is positive from the beginning

    if adcreading >= 0b100000000000000000000000:
        signbit = True
        print("negative signbit")

    if signbit:
        print("inv string")
        negativval = bin(~adcreading & 0b011111111111111111111111)
        negativval = int(negativval)
        negativval += 0b000000000000000000000001

        negativval *= -1
        return negativval

    return adcreading

if __name__ == '__main__':
    # tempdata = [0x80, 0xFF, 0x80]
    tempdata = [0x81, 0x00, 0x00]

    print("Slicing 3 databytes into one 24-bit number")
    adc24bit = int.from_bytes(bytearray(tempdata), "big")
    print(adc24bit)
    print(hex(adc24bit))
    print(bin(adc24bit))

    print(twos_comp_to_decimal(adc24bit))
    # print("Integer value: {}".format(adc24bit))

    #temperatureC = ((adc24bit - 122.400) / 420) + 25
    #print("Temp in celcius: {}".format(temperatureC))

output given negative input

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

沙与沫 2025-02-11 17:34:24

我编写了此功能,以进行从两者补充中写入的24位到小数的转换,您提供的示例提供的数字是 -8323072 ,我在此处检查了此值: https://www.expleloringbinary.com/twos-complent-complent-complent-componment-componverter/

这是我写的代码:

# example data
data = [0x81, 0x00, 0x00]
data_bin = bytearray(data)

def decimal_from_two_comp(bytearr):

    string = ''
    for byte in bytearr:
        string = string + f'{byte:08b}'
    # string with the the 24 bits
    print(string)

    # conversion from two complement to decimal
    decimal = -int(string[0]) * 2**23

    for i,num in enumerate(string[1:]):
        num = int(num)

        decimal += num * 2**(23 - (i + 1))

    return decimal

您可以在 我在该节中呈现的公式中提供的算法提供了结果。

I wrote this function to do the conversion from 24 bit written in two's complement to decimal, the number you provided as an example turned out to be -8323072, I checked this value here: https://www.exploringbinary.com/twos-complement-converter/.

Here is the code I wrote:

# example data
data = [0x81, 0x00, 0x00]
data_bin = bytearray(data)

def decimal_from_two_comp(bytearr):

    string = ''
    for byte in bytearr:
        string = string + f'{byte:08b}'
    # string with the the 24 bits
    print(string)

    # conversion from two complement to decimal
    decimal = -int(string[0]) * 2**23

    for i,num in enumerate(string[1:]):
        num = int(num)

        decimal += num * 2**(23 - (i + 1))

    return decimal

You can check on the wikipedia page for Two's Complement under the section Converting from two's complement representation that the algorithm I provided results in the formula they present in that section.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文