diff --git a/Core/Inc/main.h b/Core/Inc/main.h index 6e5e298..c085bc0 100644 --- a/Core/Inc/main.h +++ b/Core/Inc/main.h @@ -41,7 +41,7 @@ extern "C" { /* Exported constants --------------------------------------------------------*/ /* USER CODE BEGIN EC */ - +#define MAIN_LOOP_PERIOD 50 /* ms */ /* USER CODE END EC */ /* Exported macro ------------------------------------------------------------*/ diff --git a/Core/Src/main.c b/Core/Src/main.c index 68673b1..b4254b8 100644 --- a/Core/Src/main.c +++ b/Core/Src/main.c @@ -29,6 +29,7 @@ #include "stm32g4xx_hal.h" #include "stm32g4xx_hal_gpio.h" +#include "stm32g4xx_hal_tim.h" #include @@ -74,7 +75,14 @@ static void MX_TIM2_Init(void); /* Private user code ---------------------------------------------------------*/ /* USER CODE BEGIN 0 */ - +void loop_delay() { + static uint32_t last_loop = 0; + int32_t diff = MAIN_LOOP_PERIOD - (HAL_GetTick() - last_loop); + if (diff > 0) { + HAL_Delay(diff); + } + last_loop = HAL_GetTick(); +} /* USER CODE END 0 */ /** @@ -134,6 +142,7 @@ int main(void) { // vehicle_send_mission_select(MISSION_ACCELERATION); last_can = now; } + loop_delay(); } /* USER CODE END 3 */ }