Micro-controllers, wireless transmission and database
This project assumes you have already installed STM32CubeIDE. You need to have previously done a basic blink sketch with blue-pill using STM32CubeIDE. I have made a complete video from installing STM32CubeIDE to LED blink program. You can watch it by clicking this link. https://www.youtube.com/watch?v=kXg467nVd_A
Click RCC → High Speed Clock (HSE) to Crystal/Ceramic Resonator
Click Clock Configuration tab → HCLK (MHz) to 72
Click Pinout and Configuration tab
Click Timer → Click TIM1 →
Clock Source set to Internal Clock
Configuration → Parameter Settings →
Prescaler set to 71
Click connectivity --> Click I2C1
For I2C select I2C
Configuration --> Parameter Settings
For I2C speed select Fast Mode
Set PA9 to GPIO_Output
Set PA8 to GPIO_Input
Inside Core/Inc Folder
fonts.h ssd1306.hInside Core/Src Folder
fonts.c ssd1306.c/* USER CODE BEGIN Includes */ #include "fonts.h" #include "ssd1306.h" #include "stdio.h" /* USER CODE END Includes */ /* USER CODE BEGIN PV */ #define TRIG_PIN GPIO_PIN_9 #define TRIG_PORT GPIOA #define ECHO_PIN GPIO_PIN_8 #define ECHO_PORT GPIOA uint32_t pMillis; uint32_t Value1 = 0; uint32_t Value2 = 0; uint16_t Distance = 0; // cm char strCopy[15]; /* USER CODE END PV */ /* USER CODE BEGIN 2 */ HAL_TIM_Base_Start(&htim1); HAL_GPIO_WritePin(TRIG_PORT, TRIG_PIN, GPIO_PIN_RESET); // pull the TRIG pin low SSD1306_Init(); /* USER CODE END 2 */ /* USER CODE BEGIN WHILE */ while (1) { HAL_GPIO_WritePin(TRIG_PORT, TRIG_PIN, GPIO_PIN_SET); // pull the TRIG pin HIGH __HAL_TIM_SET_COUNTER(&htim1, 0); while (__HAL_TIM_GET_COUNTER (&htim1) < 10); // wait for 10 us HAL_GPIO_WritePin(TRIG_PORT, TRIG_PIN, GPIO_PIN_RESET); // pull the TRIG pin low pMillis = HAL_GetTick(); // used this to avoid infinite while loop (for timeout) // wait for the echo pin to go high while (!(HAL_GPIO_ReadPin (ECHO_PORT, ECHO_PIN)) && pMillis + 10 > HAL_GetTick()); Value1 = __HAL_TIM_GET_COUNTER (&htim1); pMillis = HAL_GetTick(); // used this to avoid infinite while loop (for timeout) // wait for the echo pin to go low while ((HAL_GPIO_ReadPin (ECHO_PORT, ECHO_PIN)) && pMillis + 50 > HAL_GetTick()); Value2 = __HAL_TIM_GET_COUNTER (&htim1); Distance = (Value2-Value1)* 0.034/2; SSD1306_GotoXY (0, 0); SSD1306_Puts ("Distance:", &Font_11x18, 1); sprintf(strCopy,"%d ", Distance); SSD1306_GotoXY (0, 30); SSD1306_Puts (strCopy, &Font_16x26, 1); SSD1306_UpdateScreen(); HAL_Delay(50); /* USER CODE END WHILE */ /* USER CODE BEGIN 3 */ } /* USER CODE END 3 */