在嵌入式C中,如何将返回的十六进制值转换为“Unsigned Char”形式可用的“整数”类型
我们正在编写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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你只需要这样的东西:
You just need something like this:
是的,这很棘手且难以想象。请记住,您获得的 MSB 和 LSB(可能)是您需要的实际二进制表示形式;你不需要转换这些东西。这就是 i2c 的工作原理。所以:
——皮特
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:
-- pete