code with new accelerometers
Dependencies: BLE_API i2c-serial-conflict nRF51822
Fork of accel_to_blenano_i2c by
Diff: main.cpp
- Revision:
- 9:c175975679dc
- Parent:
- 8:6c538756395a
--- a/main.cpp Thu Apr 13 23:22:20 2017 +0000 +++ b/main.cpp Wed Apr 26 00:54:35 2017 +0000 @@ -23,6 +23,8 @@ #define BLE_Nano //#define nRF_51822 +#define LIS331HH +//#define LIS3DH #ifdef nRF_51822 #define SCL 28 @@ -34,7 +36,8 @@ #define SDA P0_10 #endif -#define DEV_ADDR 0xA0 + +#ifdef LIS3DH #define ADDR_ONE 0x30 #define ADDR_TWO 0x32 #define AXIS_X 0x00 @@ -58,6 +61,38 @@ #define DATARATE_POWERDOWN 0 // Power down #define DATARATE_LOWPOWER_1K6HZ 0b1000 // Low power mode (1.6KHz) #define DATARATE_LOWPOWER_5KHZ 0b1001 // Low power mode (5KHz) / Normal power mode (1.25KHz) +#endif + + +#ifdef LIS331HH +#define ADDR_ONE 0x30 +#define ADDR_TWO 0x32 +#define AXIS_X 0x00 +#define AXIS_Y 0x01 +#define AXIS_Z 0x02 +#define REG_OUT_X_L 0x28 +#define REG_CTRL1 0x20 +#define REG_CTRL4 0x23 +#define REG_WHOAMI 0x0F +#define RANGE_2G 0x00 +#define DEVICE_ID 0x33 + + +#define DATARATE_1KHZ 0b11 // 1000Hz +#define DATARATE_400HZ 0b10 // 400Hz +#define DATARATE_100HZ 0b01 // 100Hz +#define DATARATE_50HZ 0b00 // 50Hz +#define DATARATE_POWERDOWN 0 // Power down +#define DATARATE_NORMALMODE 0b001 +#define DATARATE_LOWPOWER_0.5HZ 0b010 +#define DATARATE_LOWPOWER_1HZ 0b011 +#define DATARATE_LOWPOWER_2HZ 0b100 +#define DATARATE_LOWPOWER_5HZ 0b101 +#define DATARATE_LOWPOWER_10HZ 0b110 +#endif + + + Serial pc(USBTX, USBRX); TwoWire Wire = TwoWire(NRF_TWI0); @@ -162,8 +197,16 @@ uint8_t* val = new uint8_t[1]; AT24C512_ReadBytes(REG_CTRL1, val, 1, i2cAddr); pc.printf("REG_CTRL1: 0x%x\r\n", *val); - val[0] &= 0b1111; //mask off lower bits - val[0] |= (dataRate << 4); + val[0] &= 0b11100111; //d + val[0] |= (dataRate << 3); + pc.printf("REG_CTRL1: 0x%x\r\n", *val); + AT24C512_WriteBytes(REG_CTRL1, val, 1, i2cAddr); +} + +void setPowerMode(uint8_t powerMode, uint16_t i2cAddr) { + uint8_t* val = new uint8_t[1]; + val[0] &= 0b11111; + val[0] |= powerMode << 5; pc.printf("REG_CTRL1: 0x%x\r\n", *val); AT24C512_WriteBytes(REG_CTRL1, val, 1, i2cAddr); } @@ -214,6 +257,7 @@ setAxisStatus(AXIS_Y, true, ADDR_ONE); setAxisStatus(AXIS_Z, true, ADDR_ONE); setDataRate(DATARATE_400HZ, ADDR_ONE); + setPowerMode(DATARATE_NORMAL_MODE, ADDR_ONE); setHighResolution(true, ADDR_ONE); setBDU(true, ADDR_ONE); setRange(RANGE_2G, ADDR_ONE); @@ -222,16 +266,17 @@ setAxisStatus(AXIS_Y, true, ADDR_TWO); setAxisStatus(AXIS_Z, true, ADDR_TWO); setDataRate(DATARATE_400HZ, ADDR_TWO); + setPowerMode(DATARATE_NORMAL_MODE, ADDR_TWO); setHighResolution(true, ADDR_TWO); setBDU(true, ADDR_TWO); setRange(RANGE_2G, ADDR_TWO); uint8_t* val = new uint8_t[1]; - *val = 0x88; + *val = 0x80; AT24C512_WriteBytes(REG_CTRL4, val, 1, ADDR_ONE); AT24C512_WriteBytes(REG_CTRL4, val, 1, ADDR_TWO); AT24C512_ReadBytes(REG_CTRL4, val, 1, ADDR_ONE); - pc.printf("REG_CTRL4, should be 0x88: 0x%x\r\n", *val); + pc.printf("REG_CTRL4, should be 0x80: 0x%x\r\n", *val); uint8_t* whoami = new uint8_t[1]; AT24C512_ReadBytes(REG_WHOAMI, whoami, 1, ADDR_ONE);