/************************************************************************************************** * Function Accel_ReadY() * ------------------------------------------------------------------------------------------------- * Overview: Function read Y axis from accel. * Input: Nothing * Output: Nothing **************************************************************************************************/ int Accel_ReadY(void) { char low_byte; int Out_y; Out_y = ADXL345_Read(_DATAY1); low_byte = ADXL345_Read(_DATAY0); Out_y = (Out_y << 8); Out_y = (Out_y | low_byte); return Out_y; }
/************************************************************************************************** * Function Accel_ReadZ() * ------------------------------------------------------------------------------------------------- * Overview: Function read Z axis from accel. * Input: Nothing * Output: Nothing **************************************************************************************************/ int Accel_ReadZ(void) { char low_byte; int Out_z; Out_z = ADXL345_Read(_DATAZ1); low_byte = ADXL345_Read(_DATAZ0); Out_z = (Out_z << 8); Out_z = (Out_z | low_byte); return Out_z; }
/************************************************************************************************** * Function Accel_ReadX() * ------------------------------------------------------------------------------------------------- * Overview: Function read X axis from accel. * Input: Nothing * Output: Nothing **************************************************************************************************/ int Accel_ReadX(void) { char low_byte; int Out_x; Out_x = ADXL345_Read(_DATAX1); low_byte = ADXL345_Read(_DATAX0); Out_x = (Out_x << 8); Out_x = (Out_x | low_byte); return Out_x; }
// Read Y Axis short Accel_ReadY(void) { unsigned short high_byte, low_byte; short Y; high_byte = ADXL345_Read(_DATAY1); low_byte = ADXL345_Read(_DATAY0); Y = (high_byte & 0x03) << 8; Y = Y | low_byte; if (Y > 511) Y = Y - 1024; return Y; }
// Read Z Axis short Accel_ReadZ(void) { unsigned short high_byte, low_byte; short Z; high_byte = ADXL345_Read(_DATAZ1); low_byte = ADXL345_Read(_DATAZ0); Z = (high_byte & 0x03) << 8; Z = Z | low_byte; if (Z > 511) Z = Z - 1024; return Z; }
// Read X Axis short Accel_ReadX(void) { unsigned short high_byte, low_byte, X_tmp; short X; high_byte = ADXL345_Read(_DATAX1); low_byte = ADXL345_Read(_DATAX0); X_tmp = (high_byte & 0x03) << 8; X_tmp = X_tmp | low_byte; X = X_tmp; if (X_tmp > 511) { X_tmp = 1024 - X_tmp; X = (signed short)(0 - X_tmp); } return X; }
void ADXL345_readValues(struct accValues *accXYZ) { uint8_t buffer[6]; /* Read the register content */ ADXL345_Read(ADXL345_I2C_ADDR, ADXL345_X_ADDR, buffer, 6); accXYZ->accValueX = ((buffer[1] << 8) | (buffer[0])) + X_OFFSET; accXYZ->accValueY = ((buffer[3] << 8) | (buffer[2])) + Y_OFFSET; accXYZ->accValueZ = (buffer[5] << 8) | (buffer[4]); }
bool Accel_Init(void) { unsigned char id; // Go into standby mode to configure the device. ADXL345_Write(0x2D, 0x00); id = ADXL345_Read(0x00); if (id != 0xE5) { return false; } ADXL345_Write(_POWER_CTL, 0x00); // Go into standby mode to configure the device ADXL345_Write(_DATA_FORMAT, 0x08); // Full resolution, +/-2g, 4mg/LSB, right justified ADXL345_Write(_BW_RATE, 0x0A); // Set 100 Hz data rate ADXL345_Write(_FIFO_CTL, 0x80); // stream mode ADXL345_Write(_POWER_CTL, 0x08); // POWER_CTL reg: measurement mode*/ return 0x00; }
/************************************************************************************************** * Function ADXL345_Init() * ------------------------------------------------------------------------------------------------- * Overview: Function that initializes ADXL345 * Input: Nothing * Output: return 0 for OK; return _ACCEL_ERROR for ERROR **************************************************************************************************/ char ADXL345_Init(void) { char id = 0x00; // Go into standby mode to configure the device. ADXL345_Write(0x2D, 0x00); id = ADXL345_Read(0x00); if (id != 0xE5) { return _ACCEL_ERROR; } else { ADXL345_Write(_DATA_FORMAT, 0x08); // Full resolution, +/-2g, 4mg/LSB, right justified ADXL345_Write(_BW_RATE, 0x0A); // Set 100 Hz data rate ADXL345_Write(_FIFO_CTL, 0x80); // stream mode ADXL345_Write(_POWER_CTL, 0x08); // POWER_CTL reg: measurement mode return 0x00; } }