john mayer
/
AK8963
Preliminary changes
Revision 3:232745b180d9, committed 2019-10-08
- Comitter:
- ryudas
- Date:
- Tue Oct 08 16:44:21 2019 +0000
- Parent:
- 2:9dff393e5e86
- Commit message:
- xzx
Changed in this revision
diff -r 9dff393e5e86 -r 232745b180d9 ak8963.cpp --- a/ak8963.cpp Fri Sep 04 23:39:10 2015 +0000 +++ b/ak8963.cpp Tue Oct 08 16:44:21 2019 +0000 @@ -50,6 +50,8 @@ #define LEN_ONE_BYTE 1 AK8963::AK8963(I2C *conn, SlaveAddress addr) { + + printf("Slave Address = %#02x\n", addr); slaveAddress = addr; connection = conn; getSensitivityAdjustment(&sensitivityAdjustment); @@ -74,13 +76,13 @@ AK8963::Status AK8963::read(char registerAddress, char *buf, int length) { // Writes a start address - if (connection->write((slaveAddress << 1), ®isterAddress, LEN_ONE_BYTE) != 0) { + if (connection->write((slaveAddress << 1 | 0x01), ®isterAddress, LEN_ONE_BYTE) != 0) { // I2C write failed. return AK8963::ERROR_I2C_WRITE; } // Reads register data - if (connection->read((slaveAddress << 1), buf, length) != 0) { + if (connection->read((slaveAddress << 1 ), buf, length) != 0) { // I2C read failed. return AK8963::ERROR_I2C_READ; } @@ -99,7 +101,7 @@ } // Writes data - if (connection->write((slaveAddress << 1), data, bufLength) != 0) { + if (connection->write((slaveAddress << 1 ), data, bufLength) != 0) { // I2C write failed. return AK8963::ERROR_I2C_WRITE; } @@ -112,16 +114,21 @@ // Gets the ST1 register value. char st1Value = 0; + + printf("ST! Register = %#02x\n", st1Value); if ((status=AK8963::read(AK8963_REG_ST1, &st1Value, LEN_ONE_BYTE)) != AK8963::SUCCESS) { // I2C read failed. return status; } + printf("ST! Register = %#02x\n", st1Value); + + // Sets a return status corresponds to the obtained value. if ((st1Value & AK8963_BIT_MASK_DRDY) > 0) { status = AK8963::DATA_READY; } else { - status = AK8963::NOT_DATA_READY; + status = AK8963::DATA_READY;// AK8963::NOT_DATA_READY; } return status; @@ -146,20 +153,96 @@ if (mode != AK8963::MODE_POWER_DOWN) { if ((status=AK8963::write(AK8963_REG_CNTL1, &buf, LEN_ONE_BYTE)) != AK8963::SUCCESS) { // I2C write failed. + + printf("FAIL to switch to power down"); + return status; + } + + printf("CNTL1 Register = %#02x\n", buf); + if ((status=AK8963::read(AK8963_REG_WIA, &buf, LEN_ONE_BYTE)) != AK8963::SUCCESS) { return status; } + printf("CNTL1 Register = %#02x\n", buf); + + + printf("starting waiting\n"); + wait(2); wait_us(AK8963_WAIT_POWER_DOWN_US); + printf("Ended Waiting\n"); + } + + buf = 0xFF; + if ((status=AK8963::read(AK8963_REG_CNTL1, &buf, LEN_ONE_BYTE)) != AK8963::SUCCESS) { + return status; + } + printf("CNTL1 Register = %#02x\n", buf); + + buf = 0xFF; + if ((status=AK8963::read(AK8963_REG_CNTL2, &buf, LEN_ONE_BYTE)) != AK8963::SUCCESS) { + return status; + } + printf("CNTL2 Register = %#02x\n", buf); + + + /* buf = 0x01; + if ((status=AK8963::write(AK8963_REG_CNTL2, &buf, LEN_ONE_BYTE)) != AK8963::SUCCESS) { + // I2C write failed. + printf ("Error writing"); + return status; + + }*/ // Switch to the specified mode. - buf = (mode | AK8963_CNTL1_OUTPUT_16BIT); + //buf = (mode | AK8963_CNTL1_OUTPUT_16BIT); + buf = 0x16; if ((status=AK8963::write(AK8963_REG_CNTL1, &buf, LEN_ONE_BYTE)) != AK8963::SUCCESS) { // I2C write failed. + printf ("Error writing to CNTL1"); return status; + } + + wait(2); + wait_us(AK8963_WAIT_POWER_DOWN_US); - wait_us(AK8963_WAIT_POWER_DOWN_US); + buf = 0xFF; + if ((status=AK8963::read(AK8963_REG_WIA, &buf, LEN_ONE_BYTE)) != AK8963::SUCCESS) { + return status; + } + printf("WHOAMI Register = %#02x\n", buf); + + buf = 0xFF; + if ((status=AK8963::read(AK8963_REG_INFO, &buf, LEN_ONE_BYTE)) != AK8963::SUCCESS) { + return status; + } + printf("INFO Register = %#02x\n", buf); + + buf = 0xFF; + if ((status=AK8963::read(AK8963_REG_ST1, &buf, LEN_ONE_BYTE)) != AK8963::SUCCESS) { + return status; + } + printf("ST1 Register = %#02x\n", buf); + + buf = 0xFF; + if ((status=AK8963::read(AK8963_REG_ST2, &buf, LEN_ONE_BYTE)) != AK8963::SUCCESS) { + return status; + } + printf("ST2 Register = %#02x\n", buf); + + buf = 0xFF; + if ((status=AK8963::read(AK8963_REG_CNTL1, &buf, LEN_ONE_BYTE)) != AK8963::SUCCESS) { + return status; + } + printf("CNTL1 Register = %#02x\n", buf); + + buf = 0xFF; + if ((status=AK8963::read(AK8963_REG_CNTL2, &buf, LEN_ONE_BYTE)) != AK8963::SUCCESS) { + return status; + } + printf("CNTL2 Register = %#02x\n", buf); + return AK8963::SUCCESS; }
diff -r 9dff393e5e86 -r 232745b180d9 main.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Tue Oct 08 16:44:21 2019 +0000 @@ -0,0 +1,55 @@ + #include "mbed.h" + #include "ak8963.h" +// #include "I2C.h" + + #define I2C_SPEED_100KHZ 100000 + #define I2C_SPEED_400KHZ 400000 + +// Creates an instance of I2C +I2C connection(I2C_SDA, I2C_SCL); + +AK8963::Status status8963; + + + int main() { + + + + connection.frequency(I2C_SPEED_100KHZ); + // Creates an instance of AK8963 + AK8963 ak8963(&connection, AK8963::SLAVE_ADDR_1); + + if(ak8963.checkConnection() == AK8963::SUCCESS) { + + printf("Everything is fine with I2c connection\n"); + } + // Puts the device into continuous measurement mode. + if(ak8963.setOperationMode(AK8963::MODE_CONTINUOUS_1) == AK8963::SUCCESS) { + + printf("Everything is fine with ak mode operation set\n"); + } + + + + + while(true) { + // checks DRDY + + status8963 = ak8963.isDataReady(); + if (status8963 == AK8963::NOT_DATA_READY) + printf("Data not ready \n"); + else if (status8963 == AK8963::DATA_READY) + printf("Data ready \n"); + + if (status8963 == AK8963::DATA_READY) { + + AK8963::MagneticVector mag = { -1 , -1 , -1 , false}; + ak8963.getMagneticVector(&mag); + // You may use serial output to see data. + printf("%d,%5.1f,%5.1f,%5.1f\n", + mag.isOverflow, + mag.mx, mag.my, mag.mz); + } + + } + } \ No newline at end of file
diff -r 9dff393e5e86 -r 232745b180d9 mbed.bld --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed.bld Tue Oct 08 16:44:21 2019 +0000 @@ -0,0 +1,1 @@ +https://os.mbed.com/users/mbed_official/code/mbed/builds/65be27845400 \ No newline at end of file