![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Minor fixes
Dependencies: LSM9DS1_Library SDFileSystem mbed nrf51_rtc
Fork of LSM303DLHTest by
Diff: main.cpp
- Revision:
- 8:8f4d7f1c588f
- Parent:
- 7:cbfdcc57f110
- Child:
- 9:61fcd186ac50
--- a/main.cpp Tue Jul 19 20:53:52 2016 +0000 +++ b/main.cpp Wed Jul 20 21:39:19 2016 +0000 @@ -73,13 +73,201 @@ char data; char temp= subAddress; - //i2c.write(address, temp, 1); - //temp[1] = 0x00; i2c.write(address, &temp, 1); int a = i2c.read(address, &data, 1); return data; } +uint8_t I2CreadBytes(uint8_t address, uint8_t subAddress, uint8_t * dest, uint8_t count) +{ + int i; + char temp_dest[count]; + char temp = subAddress; + i2c.write(address, &temp, 1); + i2c.read(address, temp_dest, count); + + //i2c doesn't take uint8_ts, but rather chars so do this nasty af conversion + for (i=0; i < count; i++) { + dest[i] = temp_dest[i]; + } + return count; +} + + + +void DumpAccelGyroRegs( void ) +{ + char dest[ 0x34 ]; + int i; + + debug.printf("\n\r"); + //2CreadBytes( 0xD6, 0x04, (uint8_t *)dest, 0x34 ); + + + for( i = 0; i < 0x34; i++ ) { + I2CreadByte(0xD6, i + 0x04); + switch( i + 0x04 ) { + case 0x04: + debug.printf("ACT_THS 0x04 %02x\n\r", dest[i]); + break; + case 0x05: + debug.printf("ACT_DUR 0x05 %02x\n\r", dest[i]); + break; + case 0x06: + debug.printf("INT_GEN_CFG_XL 0x06 %02x\n\r", dest[i]); + break; + case 0x07: + debug.printf("INT_GEN_THS_X_XL 0x07 %02x\n\r", dest[i]); + break; + case 0x08: + debug.printf("INT_GEN_THS_Y_XL 0x08 %02x\n\r", dest[i]); + break; + case 0x09: + debug.printf("INT_GEN_THS_Z_XL 0x09 %02x\n\r", dest[i]); + break; + case 0x0A: + debug.printf("INT_GEN_DUR_XL 0x0A %02x\n\r", dest[i]); + break; + case 0x0B: + debug.printf("REFERENCE_G 0x0B %02x\n\r", dest[i]); + break; + case 0x0C: + debug.printf("INT1_CTRL 0x0C %02x\n\r", dest[i]); + break; + case 0x0D: + debug.printf("INT2_CTRL 0x0D %02x\n\r", dest[i]); + break; + case 0x0F: + debug.printf("WHO_AM_I_XG 0x0F %02x\n\r", dest[i]); + break; + case 0x10: + debug.printf("CTRL_REG1_G 0x10 %02x\n\r", dest[i]); + break; + case 0x11: + debug.printf("CTRL_REG2_G 0x11 %02x\n\r", dest[i]); + break; + case 0x12: + debug.printf("CTRL_REG3_G 0x12 %02x\n\r", dest[i]); + break; + case 0x13: + debug.printf("ORIENT_CFG_G 0x13 %02x\n\r", dest[i]); + break; + case 0x14: + debug.printf("INT_GEN_SRC_G 0x14 %02x\n\r", dest[i]); + break; + case 0x15: + debug.printf("OUT_TEMP_L 0x15 %02x\n\r", dest[i]); + break; + case 0x16: + debug.printf("OUT_TEMP_H 0x16 %02x\n\r", dest[i]); + break; + case 0x17: + debug.printf("STATUS_REG_0 0x17 %02x\n\r", dest[i]); + break; + case 0x18: + debug.printf("OUT_X_L_G 0x18 %02x\n\r", dest[i]); + break; + case 0x19: + debug.printf("OUT_X_H_G 0x19 %02x\n\r", dest[i]); + break; + case 0x1A: + debug.printf("OUT_Y_L_G 0x1A %02x\n\r", dest[i]); + break; + case 0x1B: + debug.printf("OUT_Y_H_G 0x1B %02x\n\r", dest[i]); + break; + case 0x1C: + debug.printf("OUT_Z_L_G 0x1C %02x\n\r", dest[i]); + break; + case 0x1D: + debug.printf("OUT_Z_H_G 0x1D %02x\n\r", dest[i]); + break; + case 0x1E: + debug.printf("CTRL_REG4 0x1E %02x\n\r", dest[i]); + break; + case 0x1F: + debug.printf("CTRL_REG5_XL 0x1F %02x\n\r", dest[i]); + break; + case 0x20: + debug.printf("CTRL_REG6_XL 0x20 %02x\n\r", dest[i]); + break; + case 0x21: + debug.printf("CTRL_REG7_XL 0x21 %02x\n\r", dest[i]); + break; + case 0x22: + debug.printf("CTRL_REG8 0x22 %02x\n\r", dest[i]); + break; + case 0x23: + debug.printf("CTRL_REG9 0x23 %02x\n\r", dest[i]); + break; + case 0x24: + debug.printf("CTRL_REG10 0x24 %02x\n\r", dest[i]); + break; + case 0x26: + debug.printf("INT_GEN_SRC_XL 0x26 %02x\n\r", dest[i]); + break; + case 0x27: + debug.printf("STATUS_REG_1 0x27 %02x\n\r", dest[i]); + break; + case 0x28: + debug.printf("OUT_X_L_XL 0x28 %02x\n\r", dest[i]); + break; + case 0x29: + debug.printf("OUT_X_H_XL 0x29 %02x\n\r", dest[i]); + break; + case 0x2A: + debug.printf("OUT_Y_L_XL 0x2A %02x\n\r", dest[i]); + break; + case 0x2B: + debug.printf("OUT_Y_H_XL 0x2B %02x\n\r", dest[i]); + break; + case 0x2C: + debug.printf("OUT_Z_L_XL 0x2C %02x\n\r", dest[i]); + break; + case 0x2D: + debug.printf("OUT_Z_H_XL 0x2D %02x\n\r", dest[i]); + break; + case 0x2E: + debug.printf("FIFO_CTRL 0x2E %02x\n\r", dest[i]); + break; + case 0x2F: + debug.printf("FIFO_SRC 0x2F %02x\n\r", dest[i]); + break; + case 0x30: + debug.printf("INT_GEN_CFG_G 0x30 %02x\n\r", dest[i]); + break; + case 0x31: + debug.printf("INT_GEN_THS_XH_G 0x31 %02x\n\r", dest[i]); + break; + case 0x32: + debug.printf("INT_GEN_THS_XL_G 0x32 %02x\n\r", dest[i]); + break; + case 0x33: + debug.printf("INT_GEN_THS_YH_G 0x33 %02x\n\r", dest[i]); + break; + case 0x34: + debug.printf("INT_GEN_THS_YL_G 0x34 %02x\n\r", dest[i]); + break; + case 0x35: + debug.printf("INT_GEN_THS_ZH_G 0x35 %02x\n\r", dest[i]); + break; + case 0x36: + debug.printf("INT_GEN_THS_ZL_G 0x36 %02x\n\r", dest[i]); + break; + case 0x37: + debug.printf("INT_GEN_DUR_G 0x37 %02x\n\r", dest[i]); + break; + default: + debug.printf("Register Not Valid 0x%02x\n\r"); + break; + } + + } + + +} + + int main() { led1= 1; @@ -89,7 +277,9 @@ struct tm t; time_t seconds; - FILE *fp; + FILE *fpA; + FILE *fpG; + FILE *fpM; // Attach functions to interrupts btn1.rise(&start_smpl); @@ -108,18 +298,20 @@ } lol.calibrate(); - lol.configInt(XG_INT1, INT1_IG_G|INT1_IG_XL, INT_ACTIVE_LOW, INT_PUSH_PULL); //INT1_CTRL (0x0C) + CTRL_REG8 (0x22) - lol.configInt(XG_INT2, INT2_DRDY_G|INT2_DRDY_XL, INT_ACTIVE_LOW, INT_PUSH_PULL); //INT2_CTRL (0x0D) + CTRL_REG8 (0x22) + lol.configInt(XG_INT1, INT_DRDY_XL, INT_ACTIVE_LOW, INT_PUSH_PULL); //INT1_CTRL (0x0C) + CTRL_REG8 (0x22) + lol.configInt(XG_INT2, INT_DRDY_G, INT_ACTIVE_LOW, INT_PUSH_PULL); //INT2_CTRL (0x0D) + CTRL_REG8 (0x22) lol.configAccelInt(YHIE_XL|XHIE_XL, false); // INT_GEN_CFG_XL (06h) lol.configAccelThs((uint8_t)0x25, X_AXIS, (uint8_t)0x00, false); // INT_GEN_THS_X_XL (07h) lol.configAccelThs((uint8_t)0x25, Y_AXIS, (uint8_t)0x00, false); // INT_GEN_THS_Y_XL (08h) lol.configAccelThs((uint8_t)0x25, Z_AXIS, (uint8_t)0x00, false); // INT_GEN_THS_Z_XL (09h) - lol.configGyroInt(ZLIE_G|YLIE_G|XLIE_G, false, true); // INT_GEN_CFG_G (30h) - lol.configGyroThs((int16_t )0x300, X_AXIS, (uint8_t) 0x00, false); // INT_GEN_THS_X_G (31h - 32h) - lol.configGyroThs((int16_t )0x300, Y_AXIS, (uint8_t) 0x00, false); // INT_GEN_THS_Y_G (33h - 34h) - lol.configGyroThs((int16_t )0x300, Z_AXIS, (uint8_t) 0x00, false); // INT_GEN_THS_Z_G (35h - 36h) + lol.configGyroInt(ZHIE_G|YHIE_G|XHIE_G, false, false); // INT_GEN_CFG_G (30h) + lol.configGyroThs((int16_t )0x300, X_AXIS, (uint8_t) 0x02, false); // INT_GEN_THS_X_G (31h - 32h) + lol.configGyroThs((int16_t )0x300, Y_AXIS, (uint8_t) 0x02, false); // INT_GEN_THS_Y_G (33h - 34h) + lol.configGyroThs((int16_t )0x300, Z_AXIS, (uint8_t) 0x02, false); // INT_GEN_THS_Z_G (35h - 36h) + + debug.printf( "\n\r"); debug.printf( "GyroIntSrc (14h) %02x\n\r", lol.getGyroIntSrc()); // INT_GEN_SRC_G (14h) debug.printf( "AccelIntSrc(26h) %02x\n\r", lol.getAccelIntSrc()); // INT_GEN_SRC_XL (26h) @@ -127,8 +319,9 @@ debug.printf( "Inactivity (17h) %02x\n\r", lol.getInactivity()); // STATUS_REG (17h) debug.printf( "INT1_CTRL (0Ch) %02x\n\r", I2CreadByte(0xD6, 0x0C)); - debug.printf( "INT2_CTRL (0CD) %02x\n\r", I2CreadByte(0xD6, 0x0D)); + debug.printf( "INT2_CTRL (0Dh) %02x\n\r", I2CreadByte(0xD6, 0x0D)); debug.printf( "CTRL_REG8 (22h) %02x\n\r", I2CreadByte(0xD6, 0x22)); + debug.printf( "STATUS_REG (27h) %02x\n\r", I2CreadByte(0xD6, 0x27)); debug.printf( "\n\r"); debug.printf( "INT_GEN_CFG_XL (06h) %02x\n\r", I2CreadByte(0xD6, 0x06)); debug.printf( "INT_GEN_SRC_XL (26h) %02x\n\r", I2CreadByte(0xD6, 0x26)); @@ -147,6 +340,8 @@ debug.printf( "INT_GEN_THS_ZL_G (36h) %02x\n\r", I2CreadByte(0xD6, 0x36)); debug.printf( "INT_GEN_DUR_G (37h) %02x\n\r", I2CreadByte(0xD6, 0x37)); + // Dump all registers + DumpAccelGyroRegs(); // // Initialize current time if needed // printf("Enter current date and time:\n"); // printf("YYYY MM DD HH MM SS[enter]\n"); @@ -175,10 +370,36 @@ seconds = rtc.time(); sprintf(secs_str, "%s", ctime(&seconds)); printf("Started at: %s\n\r", secs_str ); - sprintf(filename, "/sd/latch9DOF_%08x",seconds); - fp = fopen(filename, "w"); + sprintf(filename, "/sd/latch9DOFA_%08x.csv",seconds); + fpA = fopen(filename, "w"); // Verify that file can be created - if ( fp == NULL ) { + if ( fpA == NULL ) { + debug.printf("Cannot create file %s\n\r", filename); + wait(0.5); + while(1) { + led1 = !led1; + wait(0.5); + } + } else + debug.printf("File %s created successfully\n\r", filename); + + sprintf(filename, "/sd/latch9DOFG_%08x.csv",seconds); + fpG = fopen(filename, "w"); + // Verify that file can be created + if ( fpG == NULL ) { + debug.printf("Cannot create file %s\n\r", filename); + wait(0.5); + while(1) { + led1 = !led1; + wait(0.5); + } + } else + debug.printf("File %s created successfully\n\r", filename); + + sprintf(filename, "/sd/latch9DOFM_%08x.csv",seconds); + fpM = fopen(filename, "w"); + // Verify that file can be created + if ( fpM == NULL ) { debug.printf("Cannot create file %s\n\r", filename); wait(0.5); while(1) { @@ -191,23 +412,36 @@ // Sample until button 2 is pressed while(!stop) { //led1 = 0; - lol.readAccel(); - lol.readMag(); - lol.readGyro(); - debug.printf("%d, %d, %d, ", lol.ax, lol.ay, lol.az); - debug.printf("%d, %d, %d,", lol.mx, lol.my, lol.mz); - debug.printf("%d, %d, %d\n\r", lol.gx, lol.gy, lol.gz); - fprintf(fp, "%d, %d, %d, ", lol.ax, lol.ay, lol.az); - fprintf(fp, "%d, %d, %d,", lol.mx, lol.my, lol.mz); - fprintf(fp, "%d, %d, %d\n\r", lol.gx, lol.gy, lol.gz); - wait(0.1); + //lol.getAccelIntSrc(); // INT_GEN_SRC_XL (26h) + //I2CreadByte(0xD6, 0x27); + if (lol.accelAvailable()) { + lol.readAccel(); + debug.printf("ACC %d, %d, %d\n\r", lol.ax, lol.ay, lol.az); + fprintf(fpA, "%d, %d, %d\n\r", lol.ax, lol.ay, lol.az); + } + //lol.getMagIntSrc(); // INT_SRC_M (31h) + //I2CreadByte(0xD6, 0x27); + if ( lol.magAvailable(X_AXIS) && lol.magAvailable(X_AXIS) && lol.magAvailable(X_AXIS)) { + lol.readMag(); + debug.printf("MAG %d, %d, %d\n\r", lol.mx, lol.my, lol.mz); + fprintf(fpM, "%d, %d, %d\n\r", lol.mx, lol.my, lol.mz); + } + //lol.getGyroIntSrc(); // INT_GEN_SRC_G (14h) + if ( lol.gyroAvailable()) { + lol.readGyro(); + debug.printf("GYR %d, %d, %d\n\r", lol.gx, lol.gy, lol.gz); + fprintf(fpG, "%d, %d, %d\n\r", lol.gx, lol.gy, lol.gz); + } + //wait(0.1); } // Stop Sampling and close file parp(10); led1 = 1; debug.printf("Stopped sampling\n\r"); debug.printf("Results stored in %s\n\r", filename); - fclose(fp); + fclose(fpA); + fclose(fpM); + fclose(fpG); } }