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.
Dependencies: ConfigFile SDFileSystem mbed
Fork of LAURUS_program by
HMC5883L/HMC5883L.cpp
- Committer:
- ojan
- Date:
- 2015-05-15
- Revision:
- 0:bc6f14fc60c7
- Child:
- 1:6cd6d2760856
File content as of revision 0:bc6f14fc60c7:
#include "mbed.h"
#include "HMC5883L.h"
HMC5883L::HMC5883L(I2C* i2c) {
this->i2c = i2c;
}
HMC5883L::~HMC5883L() {
i2c = NULL;
}
int HMC5883L::init() {
return 1;
}
int HMC5883L::read() {
char cmd[2] = {0x02, 0x01};
int ret = i2c->write(hmc_addr, cmd, 2);
if(ret != 0) return 0;
cmd[0] = 0x03;
i2c->write(hmc_addr, cmd, 1);
i2c->read(hmc_addr | 0x01, data.reg, 6, true);
// データのHとLが逆に読み込まれているのでスワップする
for(int i=0; i<3; i++) {
char temp = 0;
temp = data.reg[i*2];
data.reg[i*2] = data.reg[i*2+1];
data.reg[i*2+1] = temp;
}
int16_t temp = data.value[1];
data.value[1] = data.value[2];
data.value[2] = temp;
return 1;
}
