MPU6050のサンプルプログラム2
Dependencies: ConfigFile SDFileSystem mbed
Fork of LAURUS_program by
HMC5883L/HMC5883L.cpp@1:6cd6d2760856, 2015-05-24 (annotated)
- Committer:
- ojan
- Date:
- Sun May 24 17:32:47 2015 +0000
- Revision:
- 1:6cd6d2760856
- Parent:
- 0:bc6f14fc60c7
Send all sensor's data to PC
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 | 1:6cd6d2760856 | 4 | HMC5883L::HMC5883L(I2C* p_i2c): i2c(p_i2c) {} |
ojan | 0:bc6f14fc60c7 | 5 | |
ojan | 0:bc6f14fc60c7 | 6 | HMC5883L::~HMC5883L() { |
ojan | 0:bc6f14fc60c7 | 7 | i2c = NULL; |
ojan | 0:bc6f14fc60c7 | 8 | } |
ojan | 0:bc6f14fc60c7 | 9 | |
ojan | 0:bc6f14fc60c7 | 10 | int HMC5883L::init() { |
ojan | 0:bc6f14fc60c7 | 11 | return 1; |
ojan | 0:bc6f14fc60c7 | 12 | } |
ojan | 0:bc6f14fc60c7 | 13 | |
ojan | 0:bc6f14fc60c7 | 14 | int HMC5883L::read() { |
ojan | 0:bc6f14fc60c7 | 15 | char cmd[2] = {0x02, 0x01}; |
ojan | 0:bc6f14fc60c7 | 16 | int ret = i2c->write(hmc_addr, cmd, 2); |
ojan | 0:bc6f14fc60c7 | 17 | if(ret != 0) return 0; |
ojan | 0:bc6f14fc60c7 | 18 | |
ojan | 0:bc6f14fc60c7 | 19 | cmd[0] = 0x03; |
ojan | 0:bc6f14fc60c7 | 20 | i2c->write(hmc_addr, cmd, 1); |
ojan | 0:bc6f14fc60c7 | 21 | i2c->read(hmc_addr | 0x01, data.reg, 6, true); |
ojan | 0:bc6f14fc60c7 | 22 | |
ojan | 0:bc6f14fc60c7 | 23 | // データのHとLが逆に読み込まれているのでスワップする |
ojan | 0:bc6f14fc60c7 | 24 | for(int i=0; i<3; i++) { |
ojan | 0:bc6f14fc60c7 | 25 | char temp = 0; |
ojan | 0:bc6f14fc60c7 | 26 | temp = data.reg[i*2]; |
ojan | 0:bc6f14fc60c7 | 27 | data.reg[i*2] = data.reg[i*2+1]; |
ojan | 0:bc6f14fc60c7 | 28 | data.reg[i*2+1] = temp; |
ojan | 0:bc6f14fc60c7 | 29 | } |
ojan | 0:bc6f14fc60c7 | 30 | |
ojan | 1:6cd6d2760856 | 31 | // 軸を加速度センサーとあわせる |
ojan | 0:bc6f14fc60c7 | 32 | int16_t temp = data.value[1]; |
ojan | 0:bc6f14fc60c7 | 33 | data.value[1] = data.value[2]; |
ojan | 0:bc6f14fc60c7 | 34 | data.value[2] = temp; |
ojan | 0:bc6f14fc60c7 | 35 | |
ojan | 1:6cd6d2760856 | 36 | temp = data.value[1]; |
ojan | 1:6cd6d2760856 | 37 | data.value[1] = -data.value[0]; |
ojan | 1:6cd6d2760856 | 38 | data.value[0] = temp; |
ojan | 1:6cd6d2760856 | 39 | |
ojan | 1:6cd6d2760856 | 40 | |
ojan | 0:bc6f14fc60c7 | 41 | return 1; |
ojan | 0:bc6f14fc60c7 | 42 | } |