简体   繁体   English

将两个8位寄存器读入C中的ADXL362的12位值

[英]Reading two 8 bit registers into 12 bit value of an ADXL362 in C

I'm querying an ADXL362 Digital Output MEMS Accelerometer for its axis data which it holds as two 8 bit registers which combine to give a 12 bit value and I'm trying to figure out how to combine those values. 我正在查询ADXL362数字输出MEMS加速度计的轴数据,该数据作为两个8位寄存器保存在一起,这些寄存器组合在一起给出12位值,我试图弄清楚如何组合这些值。 I've never been good at bitwise manipulation so any help would be greatly appreciated. 我从来都不擅长按位操作,因此我们将不胜感激。 I would imagine it is something like this: 我可以想象是这样的:

number = Z_data_H << 8 | Z_data_L;
number  = (number & ~(1<<13)) | (0<<13);
number  = (number & ~(1<<14)) | (0<<14);
number  = (number & ~(1<<15)) | (0<<15);
number  = (number & ~(1<<16)) | (0<<16);

ADXL362 data sheet (page 26) ADXL362数据表 (第26页)

Z axis data register Z轴数据寄存器 Z轴数据寄存器

You just have to do: 您只需要做:

signed short number;

number = Z_data_H << 8 | Z_data_L;

The shift left by 8 bit combined with the lower bits you already had figured out are combining the 2 bytes correctly. 左移8位再加上您已经弄清楚的低位,就正确地组合了2个字节。 Just use the appropriate data size to have the C code recoginize the sign of the 12 bit number correctly. 只需使用适当的数据大小,即可使C代码正确识别12位数字的符号。

Note that short not necessarily refers to a 16bit value, depending on your compiler and architecture - so, you might want to attempt to that. 请注意, short不一定指的是16位值,具体取决于您的编译器和体系结构-因此,您可能希望尝试这样做。

Your first line should be what you need: 您的第一行应该是您需要的:

int16_t number;
number = (Z_data_H << 8) | Z_data_L;

The sign-extension bits mean that you can read the value as if it was a 16-bit signed integer. 符号扩展位意味着您可以读取该值,就像它是一个16位带符号整数一样。 The value will simply never be outside the range of a 12-bit integer. 将永远不会超出12位整数的范围。 It's important that you leave those bits intact in order to handle negative values correctly. 重要的是,请保留这些位完整无缺,以便正确处理负值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM