2017-06-05 501 views
0

wire.read()< < 8 | wire.read()做什么?需要说明acc_x = Wire.read()<< 8 | Wire.read();

while(Wire.available() < 14);           
until all the bytes are received 
acc_x = Wire.read()<<8|Wire.read();         
acc_y = Wire.read()<<8|Wire.read();         
acc_z = Wire.read()<<8|Wire.read();         
temp = Wire.read()<<8|Wire.read();         
gyro_x = Wire.read()<<8|Wire.read();         
gyro_y = Wire.read()<<8|Wire.read();         
gyro_z = Wire.read()<<8|Wire.read(); 

回答

1

从该编码判断我说的设备报告加速度作为比8位的数量多,也许16位,但在同一时间只有8位...所以假设acc_x值被定义为草图中的整数。所以我们一次填充16位值8位,我猜...

因此,Wire.read()可能会得到一个8位值。接下来的部分<<8移动该值,剩下8位,实际上将其乘以256.第二个Wire.reador,第一个与|一起,将其有效地添加到第一个字节。该读取转换或过程对设备提供的每个测量都会重复进行。

00001010 // let's assume this is the result of the first read 
<<8 gives 00001010_00000000 
00001111 // let's assume this is the result of the second read 
00001010_00000000 | 00001111 // | has the effect of adding here 

gives 00001010_00001111 //final acceleration value 

因此,为了概括,16位号码建立起来通过从第一读取服用次数,左移它使其位的更多显著,然后or荷兰国际集团(添加)的第二结果读。

正如您的标记所暗示的,这称为按位数学运算或按位运算,它在Arduino和所有具有8位“端口”的输入引脚排列的微控制器平台上非常常见,并且这些设备的外设也很常见像这样一次一个字节地提供它们的数据,以减少与外围设备接口的引脚数量。