I cant get continuous Data from Sensors with I2C
The MCU I use is stm32f103c8t6 and the sensor is mpu-9255 on the board, there are 5 sensors and I show these sensor values on the LCD screen. I connected a button and when I press the button it moves to the next sensor. The problem is that I want to continuously receive the data of the sensors and display them on the LCD continuously. Instead, I was able to display one-time data with a delay, but after connecting the button toggle, I could not receive any data. It saves a fixed data and shows only that. As I said, what changes do I need to make to continuously receive and print data from a sensor every time I press the button?
By the way, if I take the value of only one sensor in "while" and print it on the LCD at once, I can get continuous data, but this does not work for more than one sensor.(I assume there is no hardware problem with this method)
This is the main.c file. If you need the library file to understand it, I can send it too.
while (1)
{
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
if(HAL_GPIO_ReadPin(GPIOC, GPIO_PIN_13)==GPIO_PIN_SET)
{
lcd_print(2, 15, "1");
sensorindex++;
if(sensorindex>4)
{
sensorindex=0;
}
lcd_clear();
HAL_Delay(500);
HAL_GPIO_WritePin(GPIOC, GPIO_PIN_13, RESET);
}
else if(HAL_GPIO_ReadPin(GPIOC, GPIO_PIN_13)==GPIO_PIN_RESET)
{
lcd_print(2, 15, "2");
}
else
{
lcd_print(2, 15, "3");
}
MPU9255_t sensorData;
MPU9255_Read_All(&hi2c1, &sensorData);
char lcdBuffer[64];
if(sensorindex==0)
{
int32_t pressure = BMP180_GetPressure();
sprintf(lcdBuffer, "B: %d", (int)pressure);
lcd_print(1, 1, lcdBuffer);
}
else if(sensorindex==1)
{
sprintf(lcdBuffer, "T:%.2f", sensorData.Temperature);
lcd_print(1, 1, lcdBuffer);
}
else if(sensorindex==2)
{
sprintf(lcdBuffer, "Gx:%.2f", sensorData.Gx);
lcd_print(1,1,lcdBuffer);
sprintf(lcdBuffer, "Gy:%.2f", sensorData.Gy);
lcd_print(1,7,lcdBuffer);
sprintf(lcdBuffer, "Gz:%.2f", sensorData.Gz);
lcd_print(2,1,lcdBuffer);
}
else if(sensorindex==3)
{
sprintf(lcdBuffer, "Ax:%.2f", sensorData.Ax);
lcd_print(1,1,lcdBuffer);
sprintf(lcdBuffer, "Ay:%.2f", sensorData.Ay);
lcd_print(1,7,lcdBuffer);
sprintf(lcdBuffer, "Az:%.2f", sensorData.Az);
lcd_print(2,1,lcdBuffer);
}
else if(sensorindex==4)
{
sprintf(lcdBuffer, "Mx:%d", (int)sensorData.Mx);
lcd_print(1,1,lcdBuffer);
sprintf(lcdBuffer, "My:%d", (int)sensorData.My);
lcd_print(1,7,lcdBuffer);
sprintf(lcdBuffer, "Mz:%d", (int)sensorData.Mz);
lcd_print(2,1,lcdBuffer);
}
}
