bzr branch
http://bzr.ed.am/elec/quadcopter
3
by Tim Marston
added test programs for all 4 IMU sensors |
1 |
#include <Wire.h> // I2C Arduino Library |
2 |
||
3 |
#define address 0x1E // 0011110b, I2C 7bit address of HMC5883 |
|
4 |
||
5 |
void setup() |
|
6 |
{ |
|
7 |
// initialize Serial and I2C communications |
|
8 |
Serial.begin(9600); |
|
9 |
Wire.begin(); |
|
10 |
||
11 |
// put the HMC5883 IC into the correct operating mode |
|
12 |
Wire.beginTransmission(address); // open communication with HMC5883 |
|
13 |
Wire.write(0x02); // select mode register |
|
14 |
Wire.write(0x00); // continuous measurement mode |
|
15 |
Wire.endTransmission(); |
|
16 |
} |
|
17 |
||
18 |
void loop() |
|
19 |
{ |
|
20 |
int x, y, z; // triple axis data |
|
21 |
||
22 |
// tell the HMC5883 where to begin reading data |
|
23 |
Wire.beginTransmission(address); |
|
24 |
Wire.write(0x03); // select register 3, X MSB register |
|
25 |
Wire.endTransmission(); |
|
26 |
||
27 |
||
28 |
// read data from each axis, 2 registers per axis |
|
29 |
Wire.requestFrom(address, 6); |
|
30 |
if(6<=Wire.available()){ |
|
31 |
x = Wire.read()<<8; // X msb |
|
32 |
x |= Wire.read(); // X lsb |
|
33 |
z = Wire.read()<<8; // Z msb |
|
34 |
z |= Wire.read(); // Z lsb |
|
35 |
y = Wire.read()<<8; // Y msb |
|
36 |
y |= Wire.read(); // Y lsb |
|
37 |
} |
|
38 |
||
39 |
// print out values of each axis |
|
40 |
Serial.print("x: "); |
|
41 |
Serial.print(x); |
|
42 |
Serial.print(" y: "); |
|
43 |
Serial.print(y); |
|
44 |
Serial.print(" z: "); |
|
45 |
Serial.println(z); |
|
46 |
||
47 |
delay(250); |
|
48 |
} |