Menu

Adskeeper - header widge

728x90

468x60

Navbar

AdsKeeper - IAB Display Standard Ad Unit

Tuesday, October 6, 2026

STM32F103R6 and Common Anode Seven-Segment Display Interfacing STM32CubeIDE

Overview

Seven-Segment Display is a conventional electronics display device that is very easy to interface and control using any digital circuit, micro-processor parallel port or even manual setting. It's very widely used in low-end electronics control system. It's very low cost, rich in color and size. 

STM32F103R6 and Common Anode Seven-Segment Display Interfacing STM32CubeIDE
A 6-Inch Common Cathode Display - Front

I one 6-Inch Common Cathode 7-Segment Display left from a previous project.

STM32F103R6 and Common Anode Seven-Segment Display Interfacing STM32CubeIDE
A 6-Inch Common Cathode Display - Back

 

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 and Common Anode Seven-Segment Display Interfacing STM32CubeIDE

Running Program in Proteus VSM

 

The STM32 device operate in 3.3VDC only even some of its I/O pins could withstands with +5.0VDC tolerance voltage. So to avoid I/O pins burning we have to use 3.3VDC I/O devices. Interfacing with the 7-Segment display we need to add some 330/220 Ohm resistors to cut down current and voltage since each segment forward voltage is around 2VDC. 

 

STM32CubeIDE HAL Programming 

I use Pinout and Configuration to configure the output pins. After configuring all requirements, click on save button. The HAL source code will automatically generates in C main file.

 

STM32F103R6 and Common Anode Seven-Segment Display Interfacing STM32CubeIDE

STM32CubeIDE IOC

 

The output pins are between PC0 and PC7. We can use other I/O pins on this MCU. However some pins have their alternative functions such as wake up, SPI, I2C, USB, etc. We need to consider this functionalities in a large project.

 

  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.  

 

In the STM32CubeIDE and STM32 HAL we can use direct I/O interface to send data to the output port rather than HAL function. It's fast and effective. 

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 (15) 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 (136) 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 (77) 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 (13) STM32 Black Pill (1) STM32 Blue Pill (12) STM32 HAL (7) STM32CubeIDE (15) STM32F103C8T6 (9) STM32F103R6 (2) STM32F401CCU6 (1) SysTick (4) 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)