Robofun 機器人論壇
標題:
arduino 加速度感測器 gy50 L3G4200 詢問
[打印本頁]
作者:
yasd468520
時間:
2019-8-21 13:37
標題:
arduino 加速度感測器 gy50 L3G4200 詢問
程式碼打好了 可是序列埠監控視窗顯示的 X值 Y值 Z值 感測器不管怎麼動 都顯示-1
對於程式我不是很厲害 我是參考別人的數值下去改
但始終找不出原因..
只好上來找大神來幫助
#include <Wire.h>
#include <L3G.h>
#define CTRL_REG1 0x20
#define CTRL_REG2 0x21
#define CTRL_REG3 0x22
#define CTRL_REG4 0x23
#define CTRL_REG5 0x24
int L3G4200D_Address = 105;
int x;
int y;
int z;
void setup(){
Wire.begin();
Serial.begin(9600);
Serial.println("starting up L3G4200D");
setupL3G4200D(2000);
delay(1500);
}
void loop(){
getGyroValues();
Serial.print("X:");
Serial.print(x);
Serial.print(" , ");
Serial.print("Y:");
Serial.print(y);
Serial.print(" , ");
Serial.print("Z:");
Serial.println(z);
delay(100);
}
void getGyroValues(){
byte xMSB = readRegister(L3G4200D_Address, 0x29);
byte xLSB = readRegister(L3G4200D_Address, 0x28);
x = ((xMSB << 8) | xLSB);
byte yMSB = readRegister(L3G4200D_Address, 0x2B);
byte yLSB = readRegister(L3G4200D_Address, 0x2A);
y = ((yMSB << 8) | yLSB);
byte zMSB = readRegister(L3G4200D_Address, 0x2D);
byte zLSB = readRegister(L3G4200D_Address, 0x2C);
z = ((zMSB << 8) | zLSB);
}
int setupL3G4200D(int scale){
writeRegister(L3G4200D_Address, CTRL_REG1, 0b00001111);
writeRegister(L3G4200D_Address, CTRL_REG2, 0b00000000);
writeRegister(L3G4200D_Address, CTRL_REG3, 0b00001000);
if(scale == 250){
writeRegister(L3G4200D_Address, CTRL_REG4, 0b00000000);
}else if(scale == 500){
writeRegister(L3G4200D_Address, CTRL_REG4, 0b00010000);
}else{
writeRegister(L3G4200D_Address, CTRL_REG4, 0b00110000);
}
writeRegister(L3G4200D_Address, CTRL_REG5, 0b00000000);
}
void writeRegister(int deviceAddress, byte address, byte val){
Wire.beginTransmission(deviceAddress);
Wire.write(address);
Wire.write(val);
Wire.endTransmission();
}
int readRegister(int deviceAddress, byte address){
int V;
Wire.beginTransmission(deviceAddress);
Wire.write(address);
Wire.endTransmission();
Wire.requestFrom(deviceAddress,1);
while(Wire.available()){
}
V = Wire.read();
return V;
}
擷取.PNG
(13.44 KB, 下載次數: 422)
下載附件
2019-8-21 13:37 上傳
歡迎光臨 Robofun 機器人論壇 (https://robofun.net/forum/)
Powered by Discuz! X3.2