MPU6050のサンプルプログラム2
Dependencies: ConfigFile SDFileSystem mbed
Fork of LAURUS_program by
HMC5883L/HMC5883L.cpp@0:bc6f14fc60c7, 2015-05-15 (annotated)
- Committer:
- ojan
- Date:
- Fri May 15 17:24:32 2015 +0000
- Revision:
- 0:bc6f14fc60c7
- Child:
- 1:6cd6d2760856
Laurus integrated program (ver.0.0)
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ojan | 0:bc6f14fc60c7 | 1 | #include "mbed.h" |
ojan | 0:bc6f14fc60c7 | 2 | #include "HMC5883L.h" |
ojan | 0:bc6f14fc60c7 | 3 | |
ojan | 0:bc6f14fc60c7 | 4 | HMC5883L::HMC5883L(I2C* i2c) { |
ojan | 0:bc6f14fc60c7 | 5 | this->i2c = i2c; |
ojan | 0:bc6f14fc60c7 | 6 | } |
ojan | 0:bc6f14fc60c7 | 7 | |
ojan | 0:bc6f14fc60c7 | 8 | HMC5883L::~HMC5883L() { |
ojan | 0:bc6f14fc60c7 | 9 | i2c = NULL; |
ojan | 0:bc6f14fc60c7 | 10 | } |
ojan | 0:bc6f14fc60c7 | 11 | |
ojan | 0:bc6f14fc60c7 | 12 | int HMC5883L::init() { |
ojan | 0:bc6f14fc60c7 | 13 | return 1; |
ojan | 0:bc6f14fc60c7 | 14 | } |
ojan | 0:bc6f14fc60c7 | 15 | |
ojan | 0:bc6f14fc60c7 | 16 | int HMC5883L::read() { |
ojan | 0:bc6f14fc60c7 | 17 | char cmd[2] = {0x02, 0x01}; |
ojan | 0:bc6f14fc60c7 | 18 | int ret = i2c->write(hmc_addr, cmd, 2); |
ojan | 0:bc6f14fc60c7 | 19 | if(ret != 0) return 0; |
ojan | 0:bc6f14fc60c7 | 20 | |
ojan | 0:bc6f14fc60c7 | 21 | cmd[0] = 0x03; |
ojan | 0:bc6f14fc60c7 | 22 | i2c->write(hmc_addr, cmd, 1); |
ojan | 0:bc6f14fc60c7 | 23 | i2c->read(hmc_addr | 0x01, data.reg, 6, true); |
ojan | 0:bc6f14fc60c7 | 24 | |
ojan | 0:bc6f14fc60c7 | 25 | // データのHとLが逆に読み込まれているのでスワップする |
ojan | 0:bc6f14fc60c7 | 26 | for(int i=0; i<3; i++) { |
ojan | 0:bc6f14fc60c7 | 27 | char temp = 0; |
ojan | 0:bc6f14fc60c7 | 28 | temp = data.reg[i*2]; |
ojan | 0:bc6f14fc60c7 | 29 | data.reg[i*2] = data.reg[i*2+1]; |
ojan | 0:bc6f14fc60c7 | 30 | data.reg[i*2+1] = temp; |
ojan | 0:bc6f14fc60c7 | 31 | } |
ojan | 0:bc6f14fc60c7 | 32 | |
ojan | 0:bc6f14fc60c7 | 33 | int16_t temp = data.value[1]; |
ojan | 0:bc6f14fc60c7 | 34 | data.value[1] = data.value[2]; |
ojan | 0:bc6f14fc60c7 | 35 | data.value[2] = temp; |
ojan | 0:bc6f14fc60c7 | 36 | |
ojan | 0:bc6f14fc60c7 | 37 | return 1; |
ojan | 0:bc6f14fc60c7 | 38 | } |