在嵌入式C中,如何将返回的十六进制值转换为“Unsigned Char”形式可用的“整数”类型

发布于 2024-10-27 21:43:26 字数 799 浏览 1 评论 0原文

我们正在编写I2C接口的代码, 我们将 16 位十六进制数读取为两个 8 位十六进制 MSB 和 LSB,并将这些值作为“Unsigned Char”返回。

我们想要连接这些 MSB 和 LSB "char" 值,最后我们需要一个 "Integer" 值进行进一步处理。

例如:以下 2 个方法返回一个 “Unsigned Char” 值,每个

1)

unsigned char i2c_readAck(void)
{
    TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWEA);
    while(!(TWCR & (1<<TWINT)));

    return TWDR;

}/* i2c_readAck */

2)

unsigned char i2c_readNak(void)
{
    TWCR = (1<<TWINT) | (1<<TWEN);
    while(!(TWCR & (1<<TWINT)));

    return TWDR;

}/* i2c_readNak */

我们必须从这 2 个方法中获取 MSB 和 LSB 值,这些值是实际需要的十六进制值, 但在 unsigned char 类型中,将其连接起来,最后连接的值必须转换为可用的整数格式,

我们发现对话部分非常棘手, 有人可以帮助我们吗?

We are writing a code for I2C interface,
where we are reading a 16 bit Hex number as two 8 bit Hex MSB and LSB, and we are returning these values as "Unsigned Char".

we want to concatenate these MSB and LSB "char" values, and finally we need one "Integer" value for our further processing.

for example: the following 2 methods are returning one "Unsigned Char" value, each

1)

unsigned char i2c_readAck(void)
{
    TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWEA);
    while(!(TWCR & (1<<TWINT)));

    return TWDR;

}/* i2c_readAck */

2)

unsigned char i2c_readNak(void)
{
    TWCR = (1<<TWINT) | (1<<TWEN);
    while(!(TWCR & (1<<TWINT)));

    return TWDR;

}/* i2c_readNak */

we have to fetch MSB and LSB values from these 2 methods who are actual HEX values needed,
but in unsigned char type, concatenate it, and the finally the concatenated value must be converted to usable Integer format,

we are finding the the conversation part very tricky,
can anyone help us??

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

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

发布评论

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

评论(2

花之痕靓丽 2024-11-03 21:43:26

你只需要这样的东西:

unsigned char msb = ...; // read MSB
unsigned char lsb = ...; // read LSB
int val = (msb << 8) | lsb; // combine MSB and LSB to make an int

You just need something like this:

unsigned char msb = ...; // read MSB
unsigned char lsb = ...; // read LSB
int val = (msb << 8) | lsb; // combine MSB and LSB to make an int
深空失忆 2024-11-03 21:43:26

是的,这很棘手且难以想象。请记住,您获得的 MSB 和 LSB(可能)是您需要的实际二进制表示形式;你不需要转换这些东西。这就是 i2c 的工作原理。所以:

  int number = 0, msb, lsb;

  msb = 0xff & read_one_unsigned_i2c_byte();
  lsb = 0xff & read_one_unsigned_i2c_byte();

  number = msb << 8;      // gotta' shift msb to upper 8 bits of final result
  number = number | lsb;  // and then IOR in the lower 8 bits

——皮特

Yes, it is tricky and hard to visualize. Remember that the MSB and LSB you get (probably) are the actual binary representations you need; you don't need to convert those things. That's how i2c works. So:

  int number = 0, msb, lsb;

  msb = 0xff & read_one_unsigned_i2c_byte();
  lsb = 0xff & read_one_unsigned_i2c_byte();

  number = msb << 8;      // gotta' shift msb to upper 8 bits of final result
  number = number | lsb;  // and then IOR in the lower 8 bits

-- pete

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