2017-06-05 505 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位「端口」的輸入引腳排列的微控制器平臺上非常常見,並且這些設備的外設也很常見像這樣一次一個字節地提供它們的數據,以減少與外圍設備接口的引腳數量。