Hi,
also ich habe alle int in int16_t geändert.
Serial.println(sizeof(int)); gibt "4" aus -> 4byte pro int richtig? Also war das mit int16_t schonmal nicht verkehrt.
Mein Code sieht jetzt so aus:
Code:
#include <Wire.h>
//Arduino 1.0+ only
#define CTRL_REG1 0x20
#define CTRL_REG2 0x21
#define CTRL_REG3 0x22
#define CTRL_REG4 0x23
#define CTRL_REG5 0x24
int16_t L3G4200D_Address = 0x69; //I2C address of the L3G4200D
int16_t x;
int16_t y;
int16_t z;
void setup() {
Wire1.begin();
Serial.begin(9600);
Serial.println("starting up L3G4200D");
setupL3G4200D(2000); // Configure L3G4200- 250, 500 or 2000 deg/sec
Serial.println(sizeof(int));
delay(1500); //wait for the sensor to be ready
}
void loop() {
getGyroValues();// This will update x, y, and z with new values
Serial.print("X:");
Serial.print(x);
Serial.print(" Y:");
Serial.print(y);
Serial.print(" Z:");
Serial.println(z);
delay(100); //Just here to slow down the serial to make it more readable
}
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);
}
int16_t setupL3G4200D(int16_t scale) {
//FromJim Lindblom of Sparkfun's code
// Enable x, y, z and turn off power down:
writeRegister(L3G4200D_Address, CTRL_REG1, 0b00001111);
// If you'd like to adjust/use the HPF, you can edit the line below to configure CTRL_REG2:
writeRegister(L3G4200D_Address, CTRL_REG2, 0b00000000);
// Configure CTRL_REG3 to generate data ready interrupt on INT2
// No interrupts used on INT1, if you'd like to configure INT1
// or INT2 otherwise, consult the datasheet:
writeRegister(L3G4200D_Address, CTRL_REG3, 0b00001000);
// CTRL_REG4 controls the full-scale range, among other things:
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);
}
// CTRL_REG5 controls high-pass filtering of outputs, use it
// if you'd like:
writeRegister(L3G4200D_Address, CTRL_REG5, 0b00000000);
}
void writeRegister(int16_t deviceAddress, byte address, byte val) {
Wire1.beginTransmission(deviceAddress); // start transmission to device
Wire1.write(address);// send register address
Wire1.write(val);// send value to write
Wire1.endTransmission();// end transmission
}
int16_t readRegister(int16_t deviceAddress, byte address) {
int16_t v;
Wire1.beginTransmission(deviceAddress);
Wire1.write(address); // register to read
Wire1.endTransmission();
Wire1.requestFrom(deviceAddress, 1); // read a byte
while (!Wire1.available()) {
// waiting
}
v = Wire1.read();
return v;
}
Die Ausgabe von diesem Code:
starting up L3G4200D
4
X:3 Y:-12 Z:-1
X:-1 Y:-11 Z:1
X:2 Y:-14 Z:-1
X:1 Y:-13 Z:-3
X:-1 Y:-11 Z:0
X:2 Y:-14 Z:255
X:-2 Y:-12 Z:-4
X:2 Y:-12 Z:-5
X:2 Y:-12 Z:-4
X:-3 Y:-8 Z:0
Die Werte zappeln immernoch liegen aber jetzt schon besser und reagieren tendenziell korrekt auf Beschleunigung um die jeweilige Achse. Ab und zu haben sie aber doch starke ausreisser ("Z:255").
Das ganze hängt im Moment am SCL1/SDA1 ohne externe Pullups. Sobald ich Pullups verwende (2,2kOhm und 10kOhm getestet) funktioniert die I2C Kommunikation gar nicht mehr. Egal ob am SCL0/SDA0 oder SCL1/SDA1. Versorgungsspannung ist 3,3V (an VCC am Sensor angeschlossen).
Schließe ich den Sensor an den Nano an (ohne externe Pullups), ist alles bestens.
Muss ich die Pins noch irgendwie konfigurieren? Wie schalte ich die internen Pullups ab? Geht das überhaupt?
Danke.
EDIT: Oh man... Mir ist gerade aufgefallen, das in der Software vom Nano steht das alle Werte durch 10 geteilt werden. Kein Wunder das die da viel ruhiger sind. Ich habe also nur noch ein einziges Problem, die ausreisser auf 255.
Lesezeichen