Skip to main content
Visitor II
May 15, 2024
Question

I cant get continuous Data from Sensors with I2C

  • May 15, 2024
  • 1 reply
  • 1717 views

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);

		 }
 }

 

 

 

 

 

    This topic has been closed for replies.

    1 reply

    Graduate II
    May 15, 2024

    So continuous or when you press the button, seem like counter-directives.. Or whilst you press the button?

    Is PC13 the button? Why are you setting it rather than just reading?

    Use the </> code pasting icon when in-lining code to the posts.

    The I2C should be able to continuously/repetitively interact with I2C device(s), at rates exceeding your ability to display and visually process the data.

     

    Graduate II
    May 15, 2024

    If you want it to keep reading the I2C device the code needs to flow, and not be blocked waiting on the state of the pin.

    Watch also debouncing, the MCU can loop multiple times in the time you press and release the button, and the period where the contacts bounce/oscillate