728x90

728x90

Saturday, November 15, 2025

STM32 Blue Pill HSPI1 and SN74HC595N 7-Segment Display Example

In previous post, I use the SN74HC595N and the HSPI1 of the STM32F103C8T6 to control the output LEDs. This SPI chip could interface with any parallel input devices, 7-Segment, parallel port LCD, elector-mechanical relays etc.

For a simple example here I use a single common anode 7-segment display to show received SPI data.

STM32 Blue Pill HSPI1 and SN74HC595N 7-Segment Display Example

 

I made my own SN74HC595N 7-Segment board for testing.

STM32 Blue Pill HSPI1 and SN74HC595N 7-Segment Display Example
The SN74HC595N 7-Segment Board

 

The display I took from a scrapped HP Print-Scan-Copy device. The display type is common anode.

In this example the STM32F103C8T6 run at 64MHz that is driven from its 8MHz HSI oscillator and its Phase Lock Loop.

STM32 Blue Pill HSPI1 and SN74HC595N LED Example
Clock Configuration

I use the HSPI1 module Master Half Duplex Mode operates at 16MHz. 

STM32 Blue Pill HSPI1 and SN74HC595N LED Example
SPI and GPIO Setting

 

I use Proteus VSM to draw and simulate this programming example. The program will send the Hexadecimal from 0 to F before it reset.

STM32 Blue Pill HSPI1 and SN74HC595N 7-Segment Display Example
Proteus Simulation Program

 

The source code I wrote is just a few lines.

  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) 2025 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. /* Private includes ----------------------------------------------------------*/
  23. /* USER CODE BEGIN Includes */

  24. /* USER CODE END Includes */

  25. /* Private typedef -----------------------------------------------------------*/
  26. /* USER CODE BEGIN PTD */

  27. /* USER CODE END PTD */

  28. /* Private define ------------------------------------------------------------*/
  29. /* USER CODE BEGIN PD */
  30. /* USER CODE END PD */

  31. /* Private macro -------------------------------------------------------------*/
  32. /* USER CODE BEGIN PM */

  33. /* USER CODE END PM */

  34. /* Private variables ---------------------------------------------------------*/
  35. SPI_HandleTypeDef hspi1;

  36. /* USER CODE BEGIN PV */

  37. /* USER CODE END PV */

  38. /* Private function prototypes -----------------------------------------------*/
  39. void SystemClock_Config(void);
  40. static void MX_GPIO_Init(void);
  41. static void MX_SPI1_Init(void);
  42. /* USER CODE BEGIN PFP */

  43. /* USER CODE END PFP */

  44. /* Private user code ---------------------------------------------------------*/
  45. /* USER CODE BEGIN 0 */

  46. /* USER CODE END 0 */

  47. /**
  48. * @brief The application entry point.
  49. * @retval int
  50. */
  51. int main(void)
  52. {
  53. /* USER CODE BEGIN 1 */

  54. /* USER CODE END 1 */

  55. /* MCU Configuration--------------------------------------------------------*/

  56. /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  57. HAL_Init();

  58. /* USER CODE BEGIN Init */

  59. /* USER CODE END Init */

  60. /* Configure the system clock */
  61. SystemClock_Config();

  62. /* USER CODE BEGIN SysInit */

  63. /* USER CODE END SysInit */

  64. /* Initialize all configured peripherals */
  65. MX_GPIO_Init();
  66. MX_SPI1_Init();
  67. /* USER CODE BEGIN 2 */

  68. /* USER CODE END 2 */
  69. unsigned char patterns[16]={0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,
  70. 0x07,0x7F,0x6F,0x77,0x7C,0x39,0x5E,0x79,0x71};
  71. unsigned char i=0,temp;
  72. /* Infinite loop */
  73. /* USER CODE BEGIN WHILE */
  74. while (1)
  75. {
  76. temp=~patterns[i];
  77. HAL_SPI_Transmit(&hspi1,&temp,1,100);
  78. HAL_GPIO_WritePin(GPIOA, GPIO_PIN_4,GPIO_PIN_SET);
  79. HAL_GPIO_WritePin(GPIOA, GPIO_PIN_4,GPIO_PIN_RESET);
  80. i++;
  81. if(i>15) i=0;
  82. HAL_Delay(500);
  83. }
  84. /* USER CODE END 3 */
  85. }

  86. /**
  87. * @brief System Clock Configuration
  88. * @retval None
  89. */
  90. void SystemClock_Config(void)
  91. {
  92. RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  93. RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};

  94. /** Initializes the RCC Oscillators according to the specified parameters
  95. * in the RCC_OscInitTypeDef structure.
  96. */
  97. RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
  98. RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  99. RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
  100. RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  101. RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI_DIV2;
  102. RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL16;
  103. if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  104. {
  105. Error_Handler();
  106. }
  107. /** Initializes the CPU, AHB and APB buses clocks
  108. */
  109. RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  110. |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  111. RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  112. RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  113. RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
  114. RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;

  115. if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
  116. {
  117. Error_Handler();
  118. }
  119. }

  120. /**
  121. * @brief SPI1 Initialization Function
  122. * @param None
  123. * @retval None
  124. */
  125. static void MX_SPI1_Init(void)
  126. {

  127. /* USER CODE BEGIN SPI1_Init 0 */

  128. /* USER CODE END SPI1_Init 0 */

  129. /* USER CODE BEGIN SPI1_Init 1 */

  130. /* USER CODE END SPI1_Init 1 */
  131. /* SPI1 parameter configuration*/
  132. hspi1.Instance = SPI1;
  133. hspi1.Init.Mode = SPI_MODE_MASTER;
  134. hspi1.Init.Direction = SPI_DIRECTION_1LINE;
  135. hspi1.Init.DataSize = SPI_DATASIZE_8BIT;
  136. hspi1.Init.CLKPolarity = SPI_POLARITY_LOW;
  137. hspi1.Init.CLKPhase = SPI_PHASE_1EDGE;
  138. hspi1.Init.NSS = SPI_NSS_SOFT;
  139. hspi1.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_4;
  140. hspi1.Init.FirstBit = SPI_FIRSTBIT_MSB;
  141. hspi1.Init.TIMode = SPI_TIMODE_DISABLE;
  142. hspi1.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
  143. hspi1.Init.CRCPolynomial = 10;
  144. if (HAL_SPI_Init(&hspi1) != HAL_OK)
  145. {
  146. Error_Handler();
  147. }
  148. /* USER CODE BEGIN SPI1_Init 2 */

  149. /* USER CODE END SPI1_Init 2 */

  150. }

  151. /**
  152. * @brief GPIO Initialization Function
  153. * @param None
  154. * @retval None
  155. */
  156. static void MX_GPIO_Init(void)
  157. {
  158. GPIO_InitTypeDef GPIO_InitStruct = {0};

  159. /* GPIO Ports Clock Enable */
  160. __HAL_RCC_GPIOA_CLK_ENABLE();

  161. /*Configure GPIO pin Output Level */
  162. HAL_GPIO_WritePin(GPIOA, GPIO_PIN_4, GPIO_PIN_RESET);

  163. /*Configure GPIO pin : PA4 */
  164. GPIO_InitStruct.Pin = GPIO_PIN_4;
  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(GPIOA, &GPIO_InitStruct);

  169. }

  170. /* USER CODE BEGIN 4 */

  171. /* USER CODE END 4 */

  172. /**
  173. * @brief This function is executed in case of error occurrence.
  174. * @retval None
  175. */
  176. void Error_Handler(void)
  177. {
  178. /* USER CODE BEGIN Error_Handler_Debug */
  179. /* User can add his own implementation to report the HAL error return state */
  180. __disable_irq();
  181. while (1)
  182. {
  183. }
  184. /* USER CODE END Error_Handler_Debug */
  185. }

  186. #ifdef USE_FULL_ASSERT
  187. /**
  188. * @brief Reports the name of the source file and the source line number
  189. * where the assert_param error has occurred.
  190. * @param file: pointer to the source file name
  191. * @param line: assert_param error line source number
  192. * @retval None
  193. */
  194. void assert_failed(uint8_t *file, uint32_t line)
  195. {
  196. /* USER CODE BEGIN 6 */
  197. /* User can add his own implementation to report the file name and line number,
  198. ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  199. /* USER CODE END 6 */
  200. }
  201. #endif /* USE_FULL_ASSERT */

  202. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/

STM32 Blue Pill HSPI1 and SN74HC595N 7-Segment Display Example

I connect the supply voltage of the SN74HC595N to +5VDC to get an appropriate brightness while the STM32 Blue Pill works at +3.3VDC only. Click here to download this example. 

 

No comments:

Post a Comment

320x50

Search This Blog

tyro-728x90