Below is a simple example program that demonstrates how to blink an LED connected to an STM32F103 microcontroller using the STM32Cube HAL (Hardware Abstraction Layer) library. This example assumes you have:
STM32CubeMX installed (for configuring the project and generating the initial code).
STM32CubeF1 HAL library (included with STM32CubeMX or downloadable from STMicroelectronics' website).
A suitable IDE (Integrated Development Environment) like Keil µVision, IAR Embedded Workbench, or System Workbench for STM32 (SW4STM32) which is Eclipse-based and free.
An STM32F103 board with an LED connected to a GPIO pin. For this example, let's assume the LED is connected to GPIO Pin 5 of Port A (PA5).
Step 1: Setting Up with STM32CubeMX
Before writing the code, use STM32CubeMX to generate the project structure:
Open STM32CubeMX and create a new project.
Select your board: If your exact board isn't listed, choose the closest match or the "STM32F103xx" series.
Configure the System Clock as desired (e.g., using the HSE with a crystal).
Enable GPIOA and set PA5 as Output for the LED.
Generate Code and select your preferred IDE.
Step 2: Writing the LED Blink Code
Navigate to the generated project in your chosen IDE, open the main.c file, and replace its content with the following code or integrate the necessary parts if you wish to keep the original content:
c
#include "stm32f1xx_hal.h"
// Define the LED GPIO Pin
#define LED_GPIO_Port GPIOA
#define LED_Pin GPIO_PIN_5
// Define Blink Delay in milliseconds
#define BLINK_DELAY 500
int main(void)
{
/* MCU Configuration--------------------------------------------------------*/
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* Configure the system clock */
SystemClock_Config();
/* Initialize all configured peripherals */
MX_GPIO_Init();
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
/* Toggle the LED state (On/Off) */
HAL_GPIO_TogglePin(LED_GPIO_Port, LED_Pin);
/* Delay for BLINK_DELAY milliseconds */
HAL_Delay(BLINK_DELAY);
}
/* USER CODE END 3 */
}
/**
* @brief System Clock Configuration
* @retval None
*/
void SystemClock_Config(void)
{
// This function configures the system clock using an external 8 MHz crystal (HSE).
// It sets up the PLL for a final SYSCLK frequency of 72 MHz, which is common for STM32F103.
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
/** Initializes the CPU, AHB and APB busses clocks
*/
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE; // Use external crystal oscillator
RCC_OscInitStruct.HSEState = RCC_HSE_ON; // Enable HSE oscillator
RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1; // No pre-divider for HSE
RCC_OscInitStruct.HSIState = RCC_HSI_ON; // Enable internal RC oscillator (not used here but often required for startup)
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON; // Enable PLL
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE; // Use HSE as PLL source
RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9; // Multiply HSE frequency by 9 to achieve 72 MHz sysclock
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
Error_Handler(); // Handle configuration error
}
/** Initializes the CPU, AHB and APB busses clocks
*/
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
|RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK; // Use PLL as system clock source
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1; // No divider for AHB clock
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2; // Divide AHB clock by 2 for APB1
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1; // No divider for APB2
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
{
Error_Handler(); // Handle configuration error
}
}
/**
* @brief GPIO Initialization Function
* @param None
* @retval None
*/
void MX_GPIO_Init(void)
{
// Initialize GPIOA and configure PA5 as an output for the LED.
GPIO_InitTypeDef GPIO_InitStruct = {0};
/* Enable the peripheral clock for GPIOA */
__HAL_RCC_GPIOA_CLK_ENABLE()
;
/* Set initial state of the LED to OFF (low level) */
HAL_GPIO_WritePin(LED_GPIO_Port, LED_Pin, GPIO_PIN_RESET);
/* Configure GPIO pin : PA5 as output */
GPIO_InitStruct.Pin = LED_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT; // Set as output mode
GPIO_InitStruct.Pull = GPIO_NOPULL; // No pull-up or pull-down resistor
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH; // Set high speed for the output
HAL_GPIO_Init(LED_GPIO_Port, &GPIO_InitStruct); // Initialize the GPIO
}
Step 3: Compile, Download, and Run
Compile the project in your IDE to ensure there are no errors.
Download/Flash the compiled binary to your STM32F103 board using the IDE's debugging tools or an external programmer like ST-Link.
Run the program. The LED connected to PA5 should now blink every 500 milliseconds.
Troubleshooting Tips
LED Not Blinking?
Check the LED's polarity and connection to PA5.
Verify PA5 is correctly configured as an output in both the code and in STM32CubeMX.
Ensure the system clock configuration is correct and the GPIO clock is enabled.
Check if the delay function HAL_Delay() is working correctly. If not, verify the system clock configuration.
Compilation Errors?
Review the generated code by STM32CubeMX for any conflicts with the manual edits.
Ensure all necessary HAL libraries are included in the project.
Verify that the main.c file is correctly linked to the project and that there are no duplicate definitions.
Program Not Running?
Ensure the microcontroller is properly connected to the programmer (e.g., ST-Link) and the IDE recognizes the device.
Check the target configuration in the IDE to ensure it matches your hardware setup.
LED Always On or Always Off?
Verify the GPIO initialization and the state of the pin in MX_GPIO_Init().
Check if HAL_GPIO_TogglePin() is being called correctly within the loop.
Ensure there are no short circuits or issues with the LED connection.