Saturday, August 29, 2020

STM32F103R6 Common Anode Seven Segments Display Example

In this programming example, I will use a single common anode 7-Segment display with an STM32F103R6 controller to show a free running numbers range between 0 and F. I use the HAL_GPIO_WritePin function to process the output data.


STM32F103R6 Common Anode Seven Segments Display Example

Running Program in Proteus VSM

I use Pinout and Configuration to configure the output pins. 

STM32F103R6 Common Anode Seven Segments Display Example

STM32CubeIDE IOC

The output pins are between PC0 and PC7.

  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. const unsigned char dAnode[16] = {0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,
  29. 0x90,0x88,0x83,0xC6,0xA1,0x86,0x8E};
  30. char temp=0,gpioNum=0x0001,cnt=0;
  31.  
  32. int main(void)
  33. {
  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. /* USER CODE BEGIN WHILE */
  45. while (1)
  46. {
  47.  
  48. gpioNum=0x01;
  49. temp=0x01;
  50. for(int i=0;i<8;i++){
  51. HAL_GPIO_WritePin(GPIOC,gpioNum,dAnode[cnt]&temp);
  52. gpioNum<<=1;
  53. temp<<=1;
  54. }
  55.  
  56. cnt+=1;
  57. if(cnt>15) cnt=0;
  58. HAL_Delay(200);
  59. }
  60. /* USER CODE END 3 */
  61. }
  62.  
  63. /**
  64.   * @brief System Clock Configuration
  65.   * @retval None
  66.   */
  67. void SystemClock_Config(void)
  68. {
  69. RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  70. RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
  71.  
  72. /** Initializes the RCC Oscillators according to the specified parameters
  73.   * in the RCC_OscInitTypeDef structure.
  74.   */
  75. RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
  76. RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  77. RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
  78. RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
  79. if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  80. {
  81. Error_Handler();
  82. }
  83. /** Initializes the CPU, AHB and APB buses clocks
  84.   */
  85. RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  86. |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  87. RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSI;
  88. RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  89. RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
  90. RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
  91.  
  92. if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK)
  93. {
  94. Error_Handler();
  95. }
  96. }
  97.  
  98. /**
  99.   * @brief GPIO Initialization Function
  100.   * @param None
  101.   * @retval None
  102.   */
  103. static void MX_GPIO_Init(void)
  104. {
  105. GPIO_InitTypeDef GPIO_InitStruct = {0};
  106.  
  107. /* GPIO Ports Clock Enable */
  108. __HAL_RCC_GPIOC_CLK_ENABLE();
  109. __HAL_RCC_GPIOA_CLK_ENABLE();
  110.  
  111. /*Configure GPIO pin Output Level */
  112. HAL_GPIO_WritePin(GPIOC, GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3
  113. |GPIO_PIN_4|GPIO_PIN_5|GPIO_PIN_6|GPIO_PIN_7, GPIO_PIN_RESET);
  114.  
  115. /*Configure GPIO pins : PC0 PC1 PC2 PC3
  116.   PC4 PC5 PC6 PC7 */
  117. GPIO_InitStruct.Pin = GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3
  118. |GPIO_PIN_4|GPIO_PIN_5|GPIO_PIN_6|GPIO_PIN_7;
  119. GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  120. GPIO_InitStruct.Pull = GPIO_NOPULL;
  121. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  122. HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
  123.  
  124. }
  125.  
  126. /* USER CODE BEGIN 4 */
  127.  
  128. /* USER CODE END 4 */
  129.  
  130. /**
  131.   * @brief This function is executed in case of error occurrence.
  132.   * @retval None
  133.   */
  134. void Error_Handler(void)
  135. {
  136. /* USER CODE BEGIN Error_Handler_Debug */
  137. /* User can add his own implementation to report the HAL error return state */
  138. __disable_irq();
  139. while (1)
  140. {
  141. }
  142. /* USER CODE END Error_Handler_Debug */
  143. }
  144.  
  145. #ifdef USE_FULL_ASSERT
  146. /**
  147.   * @brief Reports the name of the source file and the source line number
  148.   * where the assert_param error has occurred.
  149.   * @param file: pointer to the source file name
  150.   * @param line: assert_param error line source number
  151.   * @retval None
  152.   */
  153. void assert_failed(uint8_t *file, uint32_t line)
  154. {
  155. /* USER CODE BEGIN 6 */
  156. /* User can add his own implementation to report the file name and line number,
  157.   ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  158. /* USER CODE END 6 */
  159. }
  160. #endif /* USE_FULL_ASSERT */
  161.  
  162. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
  163.  

 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
  5. STM32F103R6 Shifts LEDs Using STM32CubeIDE 

Monday, August 24, 2020

STM32F103R6 Shifts LEDs Using STM32CubeIDE

STM32F103R6 is a 32-bit ARM Cortex M3 microcontroller. It could operater up to 72MHz. To program this type of microcontroller, the programmer may choose the Assembly language. Currently there are a lot of C/C++ compiler that support ARM chip. Some compilers are open-source while others are paid. Keil uVision is well known compiler that support many type of chips. However the manufacturer of this chip offers a free C/C++ compiler for their controller, which is the STM32CubeIDE. It's free to use. 

STM32F103R6 Shifts LEDs Using STM32CubeIDE
Simulating Program in Proteus
In this introductory example, I will use this chip to shift a bar-graph LED connects to PORTA. The clock rate is 8MHz. I use STM32 HAL driver because it's easy to program.

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

 

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
  5. STM32F103R6 Common Anode Seven Segments Display Example 
  6. STM32F103R6 Common Anode Seven Segments Display And Switch Interfacing