Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Diff: main.cpp
- Revision:
- 19:76632ff3e9fc
- Parent:
- 18:7066655957b3
- Child:
- 20:03a3357de805
diff -r 7066655957b3 -r 76632ff3e9fc main.cpp
--- a/main.cpp Sun Jan 11 16:02:18 2015 +0000
+++ b/main.cpp Sun Jan 11 16:45:40 2015 +0000
@@ -16,10 +16,11 @@
#define ITG3200_42HZ (0x03)
//3-Axis Digital Compass IC
-#define HMC5843_ADDRESS (0x3C >> 1)
-#define HMC5843_REGISTER_XMSB (0x03)
-#define HMC5843_REGISTER_MEASMODE (0x02)
-#define HMC5843_MEASMODE_CONT (0x00)
+#define HMC5883_ADDRESS_W (0x3C)
+#define HMC5883_ADDRESS_R (0x3D)
+#define HMC5883_REGISTER_XMSB (0x03)
+#define HMC5883_REGISTER_MEASMODE (0x02)
+#define HMC5883_MEASMODE_CONT (0x00)
I2C i2c( p9, p10 ); // sda, scl
@@ -27,13 +28,14 @@
void read_adxl345();
void init_itg3200();
void read_itg3200();
-//void init_hmc5843();
+void init_hmc5883();
+void read_hmc5883();
Serial pc(USBTX, USBRX); //tx, rx
int accelerometer_data[3];
int gyro_data[3];
-//int magnetometer_data[3];
+int magnetometer_data[3];
PwmOut led(LED1);
@@ -42,26 +44,13 @@
int main() {
init_adxl345();
- //init_hmc5843();
init_itg3200();
-
- //pc.printf("Press 'u' to turn LED1 brightness up, 'd' to turn it down.\n");
+ init_hmc5883();
while(1) {
- /*
- char c = pc.getc();
- if((c == 'u') && (brightness < 0.5)) {
- brightness += 0.01;
- led = brightness;
- }
- if((c == 'd') && (brightness > 0.0)) {
- brightness -= 0.01;
- led = brightness;
- }
- */
//read_adxl345();
- read_itg3200();
-
+ //read_itg3200();
+ read_hmc5883();
}
}
@@ -123,24 +112,29 @@
pc.printf("GYRO: %i\t%i\t%i", gyro_data[0], gyro_data[1], gyro_data[2]);
}
-/*
-void init_hmc5843() {
- bytes data = 0;
+
+void init_hmc5883() {
+ char data[2];
+ data[0] = HMC5883_REGISTER_MEASMODE;
+ data[1] = HMC5883_MEASMODE_CONT;
- i2c_write(HMC5843_ADDRESS, HMC5843_REGISTER_MEASMODE, HMC5843_MEASMODE_CONT);
-
- i2c_read(HMC5843_ADDRESS, HMC5843_REGISTER_MEASMODE, 1, &data);
- Serial.println((unsigned int)data);
+ i2c.write(HMC5883_ADDRESS_W, data, 2);
+ i2c.read(HMC5883_ADDRESS_R, data, 1);
+
+ pc.printf("%i\n",(unsigned int)data);
}
-void read_hmc5843() {
- bytes bytes[6];
+void read_hmc5883() {
+ char bytes[6];
memset(bytes,0,6);
+ bytes[0] = HMC5883_REGISTER_XMSB;
- i2c_read(HMC5843_ADDRESS, HMC5843_REGISTER_XMSB, 6, bytes);
+ i2c.write(HMC5883_ADDRESS_W, bytes, 1);
+ i2c.read(HMC5883_ADDRESS_R, bytes, 6);
for (int i=0;i<3;++i) {
magnetometer_data[i] = (int)bytes[2*i + 1] + (((int)bytes[2*i]) << 8);
}
-}
-*/
\ No newline at end of file
+
+ pc.printf("Magnet: %i\t%i\t%i", magnetometer_data[0], magnetometer_data[1], magnetometer_data[2]);
+}
\ No newline at end of file