簡體   English   中英

需要說明acc_x = Wire.read()<< 8 | Wire.read();

[英]need explanation acc_x = Wire.read()<<8|Wire.read();

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();

從這段代碼來看,我認為設備報告的加速度大於8位數字,也許是16位,但一次僅8位...因此,假設acc_x值在草圖中定義為整數。 所以我猜一次填充8位的16位值...

因此, Wire.read()可能會獲得8位值。 下一部分<<8將該值左移8位,有效地將其乘以256。第二個Wire.read與ed or ed與| ,有效地將其添加到第一個字節。 對於設備提供的每種測量,都會重復執行此“讀取或移位”過程。

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

綜上所述,通過從第一次讀取中獲取數字,將其左移以使其位更重要,然后對( or添加)第二次讀取的結果來構建16位數字。

就像您的標簽所暗示的那樣,這稱為按位數學或按位操作,這在Arduino和所有微控制器平台上都很常見,這些平台的輸入引腳排列為8位“端口”,並且這些設備的外設也很常見以提供其數據。每次一次為一個字節,以減少連接外設的引腳數。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM