static int adxl357_simple(char* args, char* response){ UNUSED(args); UNUSED(response); char str[64]; UART_PutString("Configuring ADXL357...\r\n"); // === Step 1: Basic Configuration === uint8_t i2c_speed = 0x01; // Setting I2C Speed to 400KHz I2CWriteBytes(0x1D, 0x2C, 1, &i2c_speed); uint8_t odr_value = 0x0A; // Output Data Rate set to 500Hz I2CWriteBytes(0x1D, 0x28, 1, &odr_value); uint8_t act_en = 0x01; // Activity enable register I2CWriteBytes(0x1D, 0x24, 1, &act_en); uint8_t act_count = 0x01; // Activity count I2CWriteBytes(0x1D, 0x27, 1, &act_count); uint8_t intr_ftn = 0x18; // Interrupt function setting I2CWriteBytes(0x1D, 0x2A, 1, &intr_ftn); uint8_t act_th_h = 0x13; // Threshold Register High I2CWriteBytes(0x1D, 0x25, 1, &act_th_h); uint8_t act_th_l = 0x88; // Threshold Register Low I2CWriteBytes(0x1D, 0x26, 1, &act_th_l); uint8_t x_offset_h = 0xFF; // X-axis offset High (-85 value in 2's compliment) I2CWriteBytes(0x1D, 0x1E, 1, &x_offset_h); uint8_t x_offset_l = 0xAB; // X-axis offset Low (-85 value in 2's compliment) I2CWriteBytes(0x1D, 0x1F, 1, &x_offset_l); uint8_t y_offset_h = 0xFF; // Y-axis offset High (-125 value in 2's compliment) I2CWriteBytes(0x1D, 0x20, 1, &y_offset_h); uint8_t y_offset_l = 0x83; // Y-axis offset Low (-125 value in 2's compliment) I2CWriteBytes(0x1D, 0x21, 1, &y_offset_l); uint8_t z_offset_h = 0x00; // Z-axis offset High (185 value in 2's compliment) I2CWriteBytes(0x1D, 0x22, 1, &z_offset_h); uint8_t z_offset_l = 0xB9; // Z-axis offset Low (185 value in 2's compliment) I2CWriteBytes(0x1D, 0x23, 1, &z_offset_l); uint8_t measurement_value = 0x00; // Measurement I2CWriteBytes(0x1D, 0x2D, 1, &measurement_value); UART_PutString("Registers configured.\r\n"); // === Step 2: Loop and read X/Y/Z every 2s === while (1) { uint8_t buf[9]; uint8_t intr_stat; int32_t x, y, z; float x_g, y_g, z_g; // Burst read 9 bytes: XDATA3..ZDATA1 I2CReadBytes(0x1D, 0x08, 9, buf); // X-axis x = ((int32_t)buf[0] << 16) | ((int32_t)buf[1] << 8) | buf[2]; x >>= 4; if (x & 0x80000) x |= 0xFFF00000; // Y-axis y = ((int32_t)buf[3] << 16) | ((int32_t)buf[4] << 8) | buf[5]; y >>= 4; if (y & 0x80000) y |= 0xFFF00000; // Z-axis z = ((int32_t)buf[6] << 16) | ((int32_t)buf[7] << 8) | buf[8]; z >>= 4; if (z & 0x80000) z |= 0xFFF00000; x_g = convert_to_g(x, sensitivity); y_g = convert_to_g(y, sensitivity); z_g = convert_to_g(z, sensitivity); char str[64]; sprintf(str, "X=%.3f g Y=%.3f g Z=%.3f g\r\n", x_g, y_g, z_g); UART_PutString(str); //sprintf(str, "X=%ld Y=%ld Z=%ld\r\n", x, y, z); //UART_PutString(str); //char str[64]; //sprintf(str, "Sensitivity =%.3f \r\n", sensitivity); //UART_PutString(str); UART_PutString("*****************************\r\n"); I2CReadBytes(0x1D, 0x04, 1, &intr_stat); sprintf(str, "ADXL357 Interrupt Status = 0x%02X\r\n", intr_stat); UART_PutString(str); UART_PutString("-----------------------------\r\n"); // separator line //CyDelay(1000); // wait 1 seconds } return 1; }