Monday, August 28, 2023

STM32F103C8T6 Blue Pill SysTick LED Blinking

Systick is timer that come with all of ARM Cortext-M3 micro-controllers. It's useful for creating a precise timing delay, time measurement, multi-tasking, etc.

In this example, I will use STM32 ARM SysTick to blink an LED. It's the built-in LED on STM32 Blue Pill module.

STM32F103C8T6 Blue Pill SysTick LED Blinking
Wiring Diagram







STM32CubeIDE Source Code:

  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. /* Private function prototypes -----------------------------------------------*/
  24. void SystemClock_Config(void);
  25. static void MX_GPIO_Init(void);
  26. /* USER CODE BEGIN PFP */
  27.  
  28. /* USER CODE END PFP */
  29.  
  30. /* Private user code ---------------------------------------------------------*/
  31. /* USER CODE BEGIN 0 */
  32.  
  33. /* USER CODE END 0 */
  34.  
  35. /**
  36.   * @brief The application entry point.
  37.   * @retval int
  38.   */
  39. int main(void)
  40. {
  41. /* USER CODE BEGIN 1 */
  42.  
  43. /* USER CODE END 1 */
  44.  
  45. /* MCU Configuration--------------------------------------------------------*/
  46.  
  47. /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  48. HAL_Init();
  49.  
  50. /* USER CODE BEGIN Init */
  51.  
  52. /* USER CODE END Init */
  53.  
  54. /* Configure the system clock */
  55. SystemClock_Config();
  56.  
  57. /* USER CODE BEGIN SysInit */
  58.  
  59. /* USER CODE END SysInit */
  60.  
  61. /* Initialize all configured peripherals */
  62. MX_GPIO_Init();
  63.  
  64. /*SysTick Configuration*/
  65. SystemCoreClockUpdate();
  66. /*Generate interrupt for each 10 ms*/
  67. SysTick_Config(SystemCoreClock/100);
  68. SysTick ->CTRL = 0;
  69. SysTick ->VAL = 0;
  70. SysTick ->CTRL = (SysTick_CTRL_TICKINT_Msk
  71. |SysTick_CTRL_ENABLE_Msk
  72. |SysTick_CTRL_CLKSOURCE_Msk);
  73.  
  74. while (1)
  75. {
  76. /* USER CODE END WHILE */
  77.  
  78. /* USER CODE BEGIN 3 */
  79. }
  80. /* USER CODE END 3 */
  81. }
  82.  
  83. void SysTick_Handler(void)
  84. {
  85.  
  86. HAL_IncTick();
  87. /*Toggle PC13 for every 1 second*/
  88. if(uwTick>=100){
  89. HAL_GPIO_TogglePin(GPIOC,GPIO_PIN_13);
  90. uwTick=0;
  91. }
  92. }
  93.  
  94. /**
  95.   * @brief System Clock Configuration
  96.   * @retval None
  97.   */
  98. void SystemClock_Config(void)
  99. {
  100. RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  101. RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
  102.  
  103. /** Initializes the RCC Oscillators according to the specified parameters
  104.   * in the RCC_OscInitTypeDef structure.
  105.   */
  106. RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
  107. RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  108. RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
  109. RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
  110. if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  111. {
  112. Error_Handler();
  113. }
  114. /** Initializes the CPU, AHB and APB buses clocks
  115.   */
  116. RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  117. |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  118. RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSI;
  119. RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  120. RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
  121. RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
  122.  
  123. if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK)
  124. {
  125. Error_Handler();
  126. }
  127. }
  128.  
  129. /**
  130.   * @brief GPIO Initialization Function
  131.   * @param None
  132.   * @retval None
  133.   */
  134. static void MX_GPIO_Init(void)
  135. {
  136. GPIO_InitTypeDef GPIO_InitStruct = {0};
  137.  
  138. /* GPIO Ports Clock Enable */
  139. __HAL_RCC_GPIOC_CLK_ENABLE();
  140. __HAL_RCC_GPIOA_CLK_ENABLE();
  141.  
  142. /*Configure GPIO pin Output Level */
  143. HAL_GPIO_WritePin(GPIOC, GPIO_PIN_13, GPIO_PIN_RESET);
  144.  
  145. /*Configure GPIO pin : PC13 */
  146. GPIO_InitStruct.Pin = GPIO_PIN_13;
  147. GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  148. GPIO_InitStruct.Pull = GPIO_NOPULL;
  149. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  150. HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
  151.  
  152. }
  153.  
  154. /* USER CODE BEGIN 4 */
  155.  
  156. /* USER CODE END 4 */
  157.  
  158. /**
  159.   * @brief This function is executed in case of error occurrence.
  160.   * @retval None
  161.   */
  162. void Error_Handler(void)
  163. {
  164. /* USER CODE BEGIN Error_Handler_Debug */
  165. /* User can add his own implementation to report the HAL error return state */
  166. __disable_irq();
  167. while (1)
  168. {
  169. }
  170. /* USER CODE END Error_Handler_Debug */
  171. }
  172.  
  173. #ifdef USE_FULL_ASSERT
  174. /**
  175.   * @brief Reports the name of the source file and the source line number
  176.   * where the assert_param error has occurred.
  177.   * @param file: pointer to the source file name
  178.   * @param line: assert_param error line source number
  179.   * @retval None
  180.   */
  181. void assert_failed(uint8_t *file, uint32_t line)
  182. {
  183. /* USER CODE BEGIN 6 */
  184. /* User can add his own implementation to report the file name and line number,
  185.   ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  186. /* USER CODE END 6 */
  187. }
  188. #endif /* USE_FULL_ASSERT */
  189.  
  190. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
  191.  

 

Click here to download its source file. 

STM32F103C8T6 Blue Pill SysTick LED Blinking
Running Program on BreadBoard

 For other similar posts please check,

  1. Getting Started With STM32F103C8T6 Module with STM32CubeIDE
  2.  STM32F103C8T6 Blue Pill SysTick and Multiplexing Display Example
  3.  STM32F103C8T6 Blue Pill Switch And Multiplexing Display Interface Using SysTick
  4.  STM32F103C8T6 Blue Pill SysTick LED Blinking
  5. STM32F103R6 Shifts LEDs Using STM32CubeIDE 
  6. STM32F103R6 Common Anode Seven Segments Display Example 

STM32F103C8T6 Blue Pill Switch And Multiplexing Display Interface Using SysTick

In previous post, I have wrote about using STM32 ARM SysTick to drive a multiplexing display. In this post, it's similar to the previous one's. Additionally I will add a push button to count the number of switch pressing. This button also use SysTick too. It's periodically check the switch pressing event.

STM32F103C8T6 Blue Pill Switch And Multiplexing Display Interface Using SysTick

Running Program On Breadboard







It will count up to 999 before it rolls down to 0. Input button is active low. It's pulled up by software without external resistor.

STM32F103C8T6 Blue Pill Switch And Multiplexing Display Interface Using SysTick

Simulating Program In Proteus 8.15


 I have to to configure the OSC frequency and other settings to make to run properly.

STM32F103C8T6 Blue Pill Switch And Multiplexing Display Interface Using SysTick
MCU Properties

STM32CubeIDE is easy to use because it has code generation wizard.

STM32F103C8T6 Blue Pill Switch And Multiplexing Display Interface Using SysTick
MCU software configuration
Source Code:

  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. /* USER CODE BEGIN PFP */
  28.  
  29.  
  30. const char dCathode[10]={0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F};
  31. volatile unsigned int msCnt=0,msSsd=0,pressCnt=0,swTime=0;
  32.  
  33. int main(void)
  34. {
  35.  
  36. /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  37. HAL_Init();
  38.  
  39. /* Configure the system clock */
  40. SystemClock_Config();
  41.  
  42. /* Initialize all configured peripherals */
  43. MX_GPIO_Init();
  44.  
  45. /*SysTick Configuration*/
  46. SystemCoreClockUpdate();
  47. /*Generate interrupt for 100 us*/
  48. SysTick_Config(SystemCoreClock/10000);
  49. SysTick ->CTRL = 0;
  50. SysTick ->VAL =0;
  51. SysTick ->CTRL = (SysTick_CTRL_TICKINT_Msk
  52. |SysTick_CTRL_ENABLE_Msk
  53. |SysTick_CTRL_CLKSOURCE_Msk);
  54.  
  55. /* USER CODE BEGIN WHILE */
  56. while (1)
  57. {
  58. /*Multiplexing Display Process*/
  59. switch(msSsd){
  60. case 0:
  61. GPIOC->ODR=0;
  62. GPIOA ->ODR=dCathode[pressCnt/100]<<1;
  63. GPIOC ->ODR = GPIO_ODR_ODR13;
  64. break;
  65.  
  66. case 5:
  67. GPIOC->ODR=0;
  68. GPIOA ->ODR=dCathode[(pressCnt%100)/10]<<1;
  69. GPIOC ->ODR = GPIO_ODR_ODR14;
  70. break;
  71.  
  72. case 10:
  73. GPIOC->ODR=0;
  74. GPIOA ->ODR=dCathode[pressCnt%10]<<1;
  75. GPIOC ->ODR = GPIO_ODR_ODR15;
  76. break;
  77. case 15:
  78. msSsd=0;
  79. GPIOA->ODR=0;
  80. GPIOC->ODR=0;
  81. }
  82. /*Switch Pressing*/
  83. if(msCnt>=300){
  84. if(HAL_GPIO_ReadPin(SW_GPIO_Port,SW_Pin)==0){
  85. pressCnt+=1;
  86. msCnt=0;
  87. }
  88. }
  89. if(pressCnt>=1000) pressCnt=0;
  90. }
  91. /* USER CODE END 3 */
  92. }
  93.  
  94. void SysTick_Handler(void)
  95. {
  96.  
  97. HAL_IncTick();
  98.  
  99. if(uwTick>=10){
  100. msCnt+=1;
  101. msSsd+=1;
  102. swTime+=1;
  103. uwTick=0;
  104. }
  105.  
  106. //if(msSsd>15) msSsd=0;
  107. if(pressCnt>999) pressCnt=0;
  108.  
  109. }
  110.  
  111. void SystemClock_Config(void)
  112. {
  113. RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  114. RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
  115.  
  116. /** Initializes the RCC Oscillators according to the specified parameters
  117.   * in the RCC_OscInitTypeDef structure.
  118.   */
  119. RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
  120. RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  121. RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
  122. RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
  123. if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  124. {
  125. Error_Handler();
  126. }
  127. /** Initializes the CPU, AHB and APB buses clocks
  128.   */
  129. RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  130. |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  131. RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSI;
  132. RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  133. RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
  134. RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
  135.  
  136. if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK)
  137. {
  138. Error_Handler();
  139. }
  140. }
  141.  
  142. /**
  143.   * @brief GPIO Initialization Function
  144.   * @param None
  145.   * @retval None
  146.   */
  147. static void MX_GPIO_Init(void)
  148. {
  149. GPIO_InitTypeDef GPIO_InitStruct = {0};
  150.  
  151. /* GPIO Ports Clock Enable */
  152. __HAL_RCC_GPIOC_CLK_ENABLE();
  153. __HAL_RCC_GPIOA_CLK_ENABLE();
  154. __HAL_RCC_GPIOB_CLK_ENABLE();
  155.  
  156. /*Configure GPIO pin Output Level */
  157. HAL_GPIO_WritePin(GPIOC, GPIO_PIN_13|GPIO_PIN_14|GPIO_PIN_15, GPIO_PIN_RESET);
  158.  
  159. /*Configure GPIO pin Output Level */
  160. HAL_GPIO_WritePin(GPIOA, GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3|GPIO_PIN_4
  161. |GPIO_PIN_5|GPIO_PIN_6|GPIO_PIN_7, GPIO_PIN_RESET);
  162.  
  163. /*Configure GPIO pins : PC13 PC14 PC15 */
  164. GPIO_InitStruct.Pin = GPIO_PIN_13|GPIO_PIN_14|GPIO_PIN_15;
  165. GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  166. GPIO_InitStruct.Pull = GPIO_NOPULL;
  167. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  168. HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
  169.  
  170. /*Configure GPIO pins : PA1 PA2 PA3 PA4
  171.   PA5 PA6 PA7 */
  172. GPIO_InitStruct.Pin = GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3|GPIO_PIN_4
  173. |GPIO_PIN_5|GPIO_PIN_6|GPIO_PIN_7;
  174. GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  175. GPIO_InitStruct.Pull = GPIO_NOPULL;
  176. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  177. HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
  178.  
  179. /*Configure GPIO pin : SW_Pin */
  180. GPIO_InitStruct.Pin = SW_Pin;
  181. GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
  182. GPIO_InitStruct.Pull = GPIO_PULLUP;
  183. HAL_GPIO_Init(SW_GPIO_Port, &GPIO_InitStruct);
  184.  
  185. }
  186.  
  187. /* USER CODE BEGIN 4 */
  188.  
  189. /* USER CODE END 4 */
  190.  
  191. /**
  192.   * @brief This function is executed in case of error occurrence.
  193.   * @retval None
  194.   */
  195. void Error_Handler(void)
  196. {
  197. /* USER CODE BEGIN Error_Handler_Debug */
  198. /* User can add his own implementation to report the HAL error return state */
  199. __disable_irq();
  200. while (1)
  201. {
  202. }
  203. /* USER CODE END Error_Handler_Debug */
  204. }
  205.  
  206. #ifdef USE_FULL_ASSERT
  207. /**
  208.   * @brief Reports the name of the source file and the source line number
  209.   * where the assert_param error has occurred.
  210.   * @param file: pointer to the source file name
  211.   * @param line: assert_param error line source number
  212.   * @retval None
  213.   */
  214. void assert_failed(uint8_t *file, uint32_t line)
  215. {
  216. /* USER CODE BEGIN 6 */
  217. /* User can add his own implementation to report the file name and line number,
  218.   ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  219. /* USER CODE END 6 */
  220. }
  221. #endif /* USE_FULL_ASSERT */
  222.  
  223. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
  224.  

 

Click here to download its source file. For other similar posts please check,

  1. Getting Started With STM32F103C8T6 Module with STM32CubeIDE
  2.  STM32F103C8T6 Blue Pill SysTick and Multiplexing Display Example
  3.  STM32F103C8T6 Blue Pill Switch And Multiplexing Display Interface Using SysTick
  4.  STM32F103C8T6 Blue Pill SysTick LED Blinking


 

Search This Blog

Labels

25AA010A (1) 8051 (7) 93AA46B (1) ADC (30) Analog Comparator (1) Arduino (15) ARM (6) AT89C52 (7) ATMega32 (54) AVR (57) CCS PICC (28) DAC (1) DHT11 (2) Display (105) Distance Sensor (3) DS18B20 (3) dsPIC (2) dsPIC30F1010 (2) EEPROM (5) Environment Sensor (4) esp8266 (1) I2C (29) Input/Output (67) Interrupt (19) Keil (5) Keypad (10) LCD (46) Master/Slave (1) MAX7221 (1) MCP23017 (5) MCP23S17 (4) Meter (3) MikroC (2) Motor (15) MPLABX (66) Nokia 5110 LCD (3) OLED (2) One-Wire (6) Oscillator (8) PCB (6) PCD8544 (3) PCF8574 (5) PIC (107) PIC12F (2) PIC16F628A (2) PIC16F630 (1) PIC16F716 (3) PIC16F818 (10) PIC16F818/819 (2) PIC16F84A (15) PIC16F876A (1) PIC16F877A (9) PIC16F88 (1) PIC16F887 (60) PIC18 (19) PIC18F1220 (4) PIC18F2550 (3) PIC18F4550 (12) PWM (11) RTC (8) Sensor (10) SH1106 (1) Shift Register (11) Shift Registers (2) SPI (24) STM32 (6) STM32 Blue Pill (6) STM32CubeIDE (6) STM32F103C8T6 (6) SysTick (3) temperature sensor (11) Thermometer (21) Timer/Counter (30) TM1637 (2) UART (7) Ultrasonic (4) Voltmeter (7) WDT (1) XC16 (2) XC8 (94)