2013-09-22 23:53:48 -03:00
|
|
|
/*******************************************
|
|
|
|
* Sample sketch that configures an HMC5883L 3 axis
|
|
|
|
* magnetometer to continuous mode and reads back
|
|
|
|
* the three axis of data.
|
|
|
|
*******************************************/
|
|
|
|
|
2015-08-11 03:28:43 -03:00
|
|
|
#include <AP_Common/AP_Common.h>
|
|
|
|
#include <AP_Math/AP_Math.h>
|
|
|
|
#include <AP_Param/AP_Param.h>
|
2013-09-22 23:53:48 -03:00
|
|
|
|
2015-08-11 03:28:43 -03:00
|
|
|
#include <AP_HAL/AP_HAL.h>
|
|
|
|
#include <AP_HAL_FLYMAPLE/AP_HAL_FLYMAPLE.h>
|
2013-09-22 23:53:48 -03:00
|
|
|
|
2015-10-16 17:22:11 -03:00
|
|
|
const AP_HAL::HAL& hal = AP_HAL::get_HAL();
|
2013-09-22 23:53:48 -03:00
|
|
|
|
|
|
|
#define HMC5883L 0x1E
|
|
|
|
|
|
|
|
void setup() {
|
2015-10-25 17:10:41 -03:00
|
|
|
hal.console->printf("Initializing HMC5883L at address %x\r\n",
|
2013-09-22 23:53:48 -03:00
|
|
|
HMC5883L);
|
|
|
|
|
|
|
|
uint8_t stat = hal.i2c->writeRegister(HMC5883L,0x02,0x00);
|
|
|
|
if (stat == 0) {
|
2015-10-25 17:10:41 -03:00
|
|
|
hal.console->printf("successful init\r\n");
|
2013-09-22 23:53:48 -03:00
|
|
|
} else {
|
2015-10-25 17:10:41 -03:00
|
|
|
hal.console->printf("failed init: return status %d\r\n",
|
2013-09-22 23:53:48 -03:00
|
|
|
(int)stat);
|
|
|
|
for(;;);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void loop() {
|
|
|
|
uint8_t data[6];
|
|
|
|
//read 6 bytes (x,y,z) from the device
|
|
|
|
uint8_t stat = hal.i2c->readRegisters(HMC5883L,0x03,6, data);
|
|
|
|
|
|
|
|
if (stat == 0){
|
|
|
|
int x, y, z;
|
|
|
|
x = data[0] << 8;
|
|
|
|
x |= data[1];
|
|
|
|
y = data[2] << 8;
|
|
|
|
y |= data[3];
|
|
|
|
z = data[4] << 8;
|
|
|
|
z |= data[5];
|
2015-10-25 17:10:41 -03:00
|
|
|
hal.console->printf("x: %d y: %d z: %d \r\n", x, y, z);
|
2013-09-22 23:53:48 -03:00
|
|
|
} else {
|
2015-10-25 17:10:41 -03:00
|
|
|
hal.console->printf("i2c error: status %d\r\n", (int)stat);
|
2013-09-22 23:53:48 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
AP_HAL_MAIN();
|