2024-02-23 01:50 AM
i am using i2c1 and uart2 to show any values i have created program but i am not getting any values from accelerometer. can you tell me any one, solution of this problem
I2C_HandleTypeDef hi2c1;
UART_HandleTypeDef huart2;
/* USER CODE BEGIN PV */
#define LIS3DH_ADDR 0x19// I2C address of LIS3DH accelerometer
/* USER CODE END PV */
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_I2C1_Init(void);
static void MX_USART2_UART_Init(void);
/* USER CODE BEGIN PFP */
/* USER CODE END PFP */
/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
void accelerometer_read_data(uint8_t reg, uint8_t* data, uint16_t size) {
HAL_I2C_Master_Transmit(&hi2c1, LIS3DH_ADDR, ®, 1, HAL_MAX_DELAY);
HAL_I2C_Master_Receive(&hi2c1, LIS3DH_ADDR, data, size, HAL_MAX_DELAY);
}
/* USER CODE END 0 */
/**
* @brief The application entry point.
* @retval int
*/
int main(void)
{
/* USER CODE BEGIN 1 */
/* USER CODE END 1 */
/* MCU Configuration--------------------------------------------------------*/
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* USER CODE BEGIN Init */
/* USER CODE END Init */
/* Configure the system clock */
SystemClock_Config();
/* USER CODE BEGIN SysInit */
/* USER CODE END SysInit */
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_I2C1_Init();
MX_USART2_UART_Init();
/* USER CODE BEGIN 2 */
uint8_t data[6];
int16_t x, y, z;
float x_g, y_g, z_g;
char buffer[50];
char buffer1[50];
// /* U
while (1) {
// Read accelerometer data registers
accelerometer_read_data(0x28 | 0x80, data, 6); // Read from OUT_X_L, 0x28 with MSB set
// Extract accelerometer data
x = (int16_t)((data[1] << 8) | data[0]);
y = (int16_t)((data[3] << 8) | data[2]);
z = (int16_t)((data[5] << 8) | data[4]);
// Convert raw data to gravitational force (g)
x_g = (float)x / 16384.0; // 16384 LSB/g for +/- 2g range
y_g = (float)y / 16384.0;
z_g = (float)z / 16384.0;
// Print accelerometer data to serial console
sprintf(buffer, "X: %.2f g, Y: %.2f g, Z: %.2f g\r\n", x_g, y_g, z_g);
HAL_UART_Transmit(&huart2, (uint8_t*)buffer, strlen(buffer), HAL_MAX_DELAY);
HAL_Delay(1000); // Delay for 1 second
}
/* USER CODE END 3 */
}
2024-02-26 07:46 AM
Hi @Kai_Satone ,
Could you try to use our official PID examples and let me know if you get some results?
Thanks
2024-02-26 07:52 AM
Please use this button to properly post source code:
2024-02-26 07:55 AM
@Kai_Satone wrote:
// Print accelerometer data to serial console
sprintf(buffer, "X: %.2f g, Y: %.2f g, Z: %.2f g\r\n", x_g, y_g, z_g);
Have you enabled support for floats in sprintf() et al ?
What happens if you just print the raw data bytes ?
2024-02-26 08:08 AM
I think yes, if floats are unable, this might trigger a warning at compilation.
Why do you use this syntax for the first params of accelerometer_read_data : 0x28 | 0x80?