2023-04-10 08:35 AM
Hi everyone, I am starting to program a board with stm32 (stm32f103c8t6) and I have the following problem.
I have two codes to make a led blink, but one works and the other doesn't work.
#include "stm32f1xx_hal.h"
#define LED_PIN GPIO_PIN_13
#define LED_PORT GPIOC
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
void Error_Handler(void);
int main(void)
{
HAL_Init();
SystemClock_Config();
MX_GPIO_Init();
while (1)
{
HAL_GPIO_TogglePin(LED_PORT, LED_PIN);
HAL_Delay(500);
}
}
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
RCC_OscInitStruct.HSEState = RCC_HSE_ON;
RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1;
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
Error_Handler();
}
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
{
Error_Handler();
}
}
void MX_GPIO_Init(void)
{
__HAL_RCC_GPIOC_CLK_ENABLE();
__HAL_RCC_GPIOD_CLK_ENABLE();
GPIO_InitTypeDef GPIO_InitStruct = {0};
GPIO_InitStruct.Pin = LED_PIN;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_PULLUP;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
HAL_GPIO_Init(LED_PORT, &GPIO_InitStruct);
}
void Error_Handler(void)
{
while (1)
{
}
}
#include "stm32f1xx_hal.h"
#define LED_PIN GPIO_PIN_13
#define LED_PORT GPIOC
static void MX_GPIO_Init(void);
void Error_Handler(void);
int main(void)
{
HAL_Init();
MX_GPIO_Init();
while (1)
{
HAL_GPIO_TogglePin(LED_PORT, LED_PIN);
HAL_Delay(500);
}
}
void SysTick_Handler(void)
{
HAL_IncTick();
}
void MX_GPIO_Init(void)
{
__HAL_RCC_GPIOC_CLK_ENABLE();
__HAL_RCC_GPIOD_CLK_ENABLE();
GPIO_InitTypeDef GPIO_InitStruct = {0};
GPIO_InitStruct.Pin = LED_PIN;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_PULLUP;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
HAL_GPIO_Init(LED_PORT, &GPIO_InitStruct);
}
void Error_Handler(void)
{
while (1)
{
}
}
Is the configuration in the SystemClock_Config function wrong?
What is the difference with the SysTick_Handler function?
Solved! Go to Solution.
2023-04-10 10:03 AM
The HAL_Delay() function depends on the SysTick ticking.
The HAL_Init() code typically initializes the SysTick, but whatever handler you have for it, it needs to increment the tick count.
If the code in stm32f1xx_it.c isn't doing it, suggest copying the body from the second working example, into the first.
2023-04-10 10:03 AM
The HAL_Delay() function depends on the SysTick ticking.
The HAL_Init() code typically initializes the SysTick, but whatever handler you have for it, it needs to increment the tick count.
If the code in stm32f1xx_it.c isn't doing it, suggest copying the body from the second working example, into the first.