Subversion Repositories FlightCtrl

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
1993 - 1
/*HMC5883LRegisterScanner.pde
2
**A sketch that attempts to read every register from a slave device
3
**Written by Wayne Truchsess  http://dsscircuits.com
4
*/
5
 
6
#include "Wire.h"
7
#define I2C 0x1E
8
 
9
byte x;
10
 
11
void setup() {
12
  Wire.begin();
13
  Serial.begin(9600);
14
  delay(1000);
15
 
16
  Wire.beginTransmission(I2C);   // start transmission to device
17
  Wire.send(0); // send register address
18
  Wire.send(0x70); // send value to write
19
  Wire.endTransmission(); // end transmission
20
  delay(10);
21
  Wire.beginTransmission(I2C);   // start transmission to device
22
  Wire.send(1); // send register address
23
  Wire.send(B11000000); // send value to write
24
  Wire.endTransmission(); // end transmission
25
  delay(10);
26
  Wire.beginTransmission(I2C);   // start transmission to device
27
  Wire.send(2); // send register address
28
  Wire.send(0x00); // send value to write
29
  Wire.endTransmission(); // end transmission
30
 
31
  delay(100);
32
  baselineRegisters();
33
}
34
 
35
void loop() {
36
  int x,y,z;
37
  Wire.beginTransmission(I2C);
38
  Wire.send(3); // will start from DATA X MSB and fetch all the others
39
  Wire.endTransmission();
40
 
41
  Wire.beginTransmission(I2C);
42
  Wire.requestFrom(I2C, 6);
43
  if(6 == Wire.available()) {
44
    // read out the 3 values, 2 bytes each.
45
    x = (Wire.receive() << 8) + Wire.receive();
46
    y = (Wire.receive() << 8) + Wire.receive();
47
    z = (Wire.receive() << 8) + Wire.receive();
48
    // the HMC5843 will automatically wrap around on the next request
49
  }
50
  Wire.endTransmission();
51
 
52
  Serial.print(x);
53
  Serial.print(" ");
54
  Serial.print(y);
55
  Serial.print(" ");
56
  Serial.println(z);
57
 
58
  delay(100);
59
  return;
60
}
61
 
62
void baselineRegisters() {
63
 for(int l = 0x00; l < 0x12; l++){
64
    Wire.beginTransmission(I2C);
65
    Wire.send(l);
66
    Wire.endTransmission();
67
    //delay(100);
68
    Wire.beginTransmission(I2C);
69
    Wire.requestFrom(I2C,1);
70
    x = Wire.receive();
71
    Serial.print("Register Address ");
72
    Serial.print(l,DEC);
73
    Serial.print(" = ");
74
    Serial.print(x,BIN);
75
    Serial.print(" = ");
76
    Serial.print(x,DEC);
77
    Serial.println("     ");
78
    Wire.endTransmission();
79
  }
80
}