Menu

Adskeeper - header widge

728x90

468x60

Navbar

AdsKeeper - IAB Display Standard Ad Unit

Wednesday, October 7, 2026

STM32F103R6 LED Blinking using SysTick in STM32CubeIDE

Overview

The STM32 micro-controller series have a lot of time modules. Some of them are general purpose timing/counting while other's have special purposes such a capture/compare, pulse triggering, PWM, etc. SysTick is a general purpose timer module that exists in every ARM architecture MCU including the STM32 micro-controller series. 

 

STM32F103R6 LED Blinking using SysTick in STM32CubeIDE
Some LED for MCU Output

 

Using a timer interrupt allow any task to become active at any specific time. It makes the main program loop to do other tasks without waiting. SysTick is a timer bases on interrupt inside the STM32 ARM micro-controller.

Some LED for MCU Output
Simulating Program In Proteus

 

STM32F103R6 SysTick Programming 

In this introductory example, I use this time to toggle an LED. LED blinking is placed inside the SysTick function. It left the main program loop blank. We can place any task inside the main program loop, for example a button pressing event.

 

Some LED for MCU Output

Code Configuration Wizard


Code configuration wizard allows the programmer to generate code by selecting and setting any features of the STM32 micro-controller. In this example, I select PC0 as an digital output connects to the LED. Its core source codes are generated by IOC.

 

  1. /* USER CODE BEGIN Header */
  2. /**
  3.   ******************************************************************************
  4.   * @file : main.c
  5.   * @brief : Main program body
  6.   ******************************************************************************
  7.   * @attention
  8.   *
  9.   * <h2><center>&copy; Copyright (c) 2022 STMicroelectronics.
  10.   * All rights reserved.</center></h2>
  11.   *
  12.   * This software component is licensed by ST under BSD 3-Clause license,
  13.   * the "License"; You may not use this file except in compliance with the
  14.   * License. You may obtain a copy of the License at:
  15.   * opensource.org/licenses/BSD-3-Clause
  16.   *
  17.   ******************************************************************************
  18.   */
  19. /* USER CODE END Header */
  20. /* Includes ------------------------------------------------------------------*/
  21. #include "main.h"
  22.  
  23.  
  24. /* Private function prototypes -----------------------------------------------*/
  25. void SystemClock_Config(void);
  26. static void MX_GPIO_Init(void);
  27.  
  28. int main(void)
  29. {
  30.  
  31. /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  32. HAL_Init();
  33. /* Configure the system clock */
  34. SystemClock_Config();
  35. /* Initialize all configured peripherals */
  36. MX_GPIO_Init();
  37.  
  38. /*SysTick Configuration*/
  39. SystemCoreClockUpdate();
  40. /*Generate interrupt for 10 ms*/
  41. SysTick_Config(SystemCoreClock/100);
  42. SysTick ->CTRL=0;
  43. SysTick ->VAL=0;
  44. SysTick ->CTRL=(SysTick_CTRL_TICKINT_Msk //Enable SysTick Exception
  45. |SysTick_CTRL_ENABLE_Msk //Enable SysTick system timer
  46. |SysTick_CTRL_CLKSOURCE_Msk); //Use process clock source
  47.  
  48. while (1)
  49. {
  50.  
  51. }
  52. /* USER CODE END 3 */
  53. }
  54.  
  55. void SysTick_Handler(void)
  56. {
  57. HAL_IncTick();
  58. if(uwTick>=10){
  59. HAL_GPIO_TogglePin(GPIOC,GPIO_PIN_0);
  60. uwTick=0;
  61. }
  62. }
  63.  
  64. /**
  65.   * @brief System Clock Configuration
  66.   * @retval None
  67.   */
  68. void SystemClock_Config(void)
  69. {
  70. RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  71. RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
  72.  
  73. /** Initializes the RCC Oscillators according to the specified parameters
  74.   * in the RCC_OscInitTypeDef structure.
  75.   */
  76. RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
  77. RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  78. RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
  79. RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
  80. if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  81. {
  82. Error_Handler();
  83. }
  84. /** Initializes the CPU, AHB and APB buses clocks
  85.   */
  86. RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  87. |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  88. RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSI;
  89. RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  90. RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
  91. RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
  92.  
  93. if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK)
  94. {
  95. Error_Handler();
  96. }
  97. }
  98.  
  99. /**
  100.   * @brief GPIO Initialization Function
  101.   * @param None
  102.   * @retval None
  103.   */
  104. static void MX_GPIO_Init(void)
  105. {
  106. GPIO_InitTypeDef GPIO_InitStruct = {0};
  107.  
  108. /* GPIO Ports Clock Enable */
  109. __HAL_RCC_GPIOC_CLK_ENABLE();
  110.  
  111. /*Configure GPIO pin Output Level */
  112. HAL_GPIO_WritePin(GPIOC, GPIO_PIN_0, GPIO_PIN_RESET);
  113.  
  114. /*Configure GPIO pin : PC0 */
  115. GPIO_InitStruct.Pin = GPIO_PIN_0;
  116. GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  117. GPIO_InitStruct.Pull = GPIO_NOPULL;
  118. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  119. HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
  120.  
  121. }
  122.  
  123. /* USER CODE BEGIN 4 */
  124.  
  125. /* USER CODE END 4 */
  126.  
  127. /**
  128.   * @brief This function is executed in case of error occurrence.
  129.   * @retval None
  130.   */
  131. void Error_Handler(void)
  132. {
  133. /* USER CODE BEGIN Error_Handler_Debug */
  134. /* User can add his own implementation to report the HAL error return state */
  135. __disable_irq();
  136. while (1)
  137. {
  138. }
  139. /* USER CODE END Error_Handler_Debug */
  140. }
  141.  
  142. #ifdef USE_FULL_ASSERT
  143. /**
  144.   * @brief Reports the name of the source file and the source line number
  145.   * where the assert_param error has occurred.
  146.   * @param file: pointer to the source file name
  147.   * @param line: assert_param error line source number
  148.   * @retval None
  149.   */
  150. void assert_failed(uint8_t *file, uint32_t line)
  151. {
  152. /* USER CODE BEGIN 6 */
  153. /* User can add his own implementation to report the file name and line number,
  154.   ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  155. /* USER CODE END 6 */
  156. }
  157. #endif /* USE_FULL_ASSERT */
  158.  
  159. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
  160.  

 

The SysTick interrupt occurs for every 100us. Click here to download its source file.


No comments:

Post a Comment

Adskeep- Standard Ads

336x280

Search This Blog

AdsKeeper - 2091654

Labels

23K256 (1) 24C16B (2) 25AA010A (2) 8051 (7) 93AA46B (1) ADC (34) Analog Comparator (1) Arduino (16) ARM (20) AT89C52 (7) ATMega32 (59) ATMega644P (27) AVR (88) AVR PCB (2) Bootloader (1) CCS PICC (31) Change Notify Interrupt (CNI) (2) DAC (2) DHT11 (4) Display (140) Distance Sensor (3) dot matrix (1) ds1307 (10) DS18B20 (5) ds3231 (1) ds3232 (2) dsPIC (5) dsPIC30F1010 (3) dsPIC30F2010 (5) EEPROM (5) Environment Sensor (5) ESP32 (1) esp8266 (1) Graphical LCD (3) I2C (38) ILI9341 (1) Input/Output (81) Interrupt (22) IoT PCB (1) Keil (5) Keypad (16) KS0108 (3) LCD (76) LM35 (3) Master/Slave (2) MAX7221 (1) MCP23017 (8) MCP23S17 (7) MCP4921 (1) MCP4922 (2) Meter (3) MikroC (2) Module PCB (1) Motor (15) MPLABX (73) Nokia 5110 LCD (4) OLED (2) One-Wire (7) Oscillator (8) PCB (12) PCD8544 (3) PCF8574 (10) PIC (108) PIC12F (3) PIC16 (105) PIC16F628A (3) PIC16F630 (2) PIC16F716 (4) PIC16F818 (11) PIC16F818/819 (3) PIC16F84A (16) PIC16F876A (2) PIC16F877A (9) PIC16F88 (2) PIC16F887 (60) PIC18 (19) PIC18F1220 (5) PIC18F2550 (5) PIC18F4550 (12) PICKit2 (1) PICMicro PCB (4) Pin Change Interrupt (1) PWM (12) RTC (12) SBN0064G (1) Sensor (13) SH1106 (3) Shift Register (13) Shift Registers (12) SN74HC595N (2) Software TWI (2) SPI (39) ST7735 (1) STM32 (18) STM32 Black Pill (1) STM32 Blue Pill (12) STM32 HAL (12) STM32CubeIDE (20) STM32F103C8T6 (9) STM32F103R6 (7) STM32F401CCU6 (1) SysTick (7) temperature sensor (13) TFT (1) TG12864A (1) Thermometer (22) Timer/Counter (32) TM1637 (2) twi (10) UART (8) Ultrasonic (4) Voltmeter (7) WDT (1) XC16 (2) XC8 (96)