Menu

Adskeeper - header widge

728x90

468x60

Navbar

AdsKeeper - IAB Display Standard Ad Unit

Wednesday, October 7, 2026

STM32F103R6 STM32CubeIDE 7-Segment and Switch Interfacing

Overview

In previous post I showed a simple 7-Segment interfacing with the STM32F103R6. Now I add a push button to make a pulse counting. It counts up to 0x0F and then it rolls down to 0 (Hexadecimal counting).

 

STM32F103R6 STM32CubeIDE 7-Segment and Switch Interfacing
Some 4x4 tactile switches


The input button should be a tactile switches, push button or even wiring shorting to ground.  

STM32F103R6 STM32CubeIDE 7-Segment and Switch Interfacing

Simulating Program Using Proteus VSM 8.15

 

The metal contact of switch always have bouncing that creates input noise. We can de-bounce the input by hardware or software. We can add an RC low pass filter circuit to the input pin to eliminate the high frequency noise (Hardware de-bounce). Otherwise using software de-bouncing is very easy without adding external components. We just add a short delay time around 200 ms to by pass the input bouncing time. Using a loop that detect the logic low and wait until it triggers to logic also by-pass the input bouncing but the system needs to wait until the input switch pressing released.

 

STM32CubeIDE and STM32 HAL 

Using HAL driver allow us to configure its peripherals without writing the configuration codes. We set the input pins, pull-up or pull-down, output pins, system clock, etc. We can use any frequency, input/output port as we need.

 

STM32F103R6 STM32CubeIDE 7-Segment and Switch Interfacing
STM32F103R6 Pins Configuration

 

Only PortC is required. Between PC0 and PC7 are digital output that drive a single common anode 7-Segment display. PC2 is an digital input connects to a momentary switch. It's active low as its default high state is already configure in software.   

 

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

 

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 (137) 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 (78) 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 (14) STM32 Black Pill (1) STM32 Blue Pill (12) STM32 HAL (8) STM32CubeIDE (16) STM32F103C8T6 (9) STM32F103R6 (3) 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)