728x90

Showing posts with label STM32F103C8T6. Show all posts
Showing posts with label STM32F103C8T6. Show all posts

Saturday, November 22, 2025

STM32 Blue Pill HSPI1 and ST7735 128x160 TFT LCD

I have a 1.8 inches 128x160 TFT display module from Ali-Express years ago. It uses the ST7735 controller. It's very popular with Arduino board. This LCD module is available from many manufacturers with the ST7735 LCD controller or its compatible. However we can use other MCU to interface with this LCD via its 4-wire SPI interface. Most of manufacturers of this LCD module have datasheet and example code for various micro-controllers for instance, lcdwiki.com or buydisplay.com.

STM32 Blue Pill HSPI1 and ST7735 128x160 TFT LCD

 

I found some sample code from those manufacturers that was written in C that can be ported to many MCU with some code modification. However I want to use an STM32 Blue Pill I posses due to their popularity, high speed and their rich of peripheral. 

We can use the MCU hardware SPI or even a software bit-banging to control this LCD module. Using a software bit-banging is easy and it could fit to any other MCU since it use only micro-controller digital I/O pins. However it's very slow speed.

Using the hardware SPI is very straight forward in STM32CubeIDE with its code generator wizard. The STM32F103C8T6 supports up to 16Mbits per second baud rate that's fast enough to send a bulk raw graphic data to the LCD module.

The MCU clock frequency is set to 64MHz sources from its 8MHz HSI and PLL to get its maximum frequency.

STM32 Blue Pill HSPI1 and MCP23S17 GPIO Example
Clock Configuration

The SPI baud rate is 16MHz transmit only mode or even master transmit.

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

 I use the lcdwiki C LCD and graphic library. It contain some LCD configurations, English and Chinese fonts, and raw image.

  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 variables ---------------------------------------------------------*/
  23. SPI_HandleTypeDef hspi1;


  24. /* Private function prototypes -----------------------------------------------*/
  25. void SystemClock_Config(void);
  26. static void MX_GPIO_Init(void);
  27. static void MX_SPI1_Init(void);
  28. /* USER CODE BEGIN PFP */

  29. /**
  30. * @brief The application entry point.
  31. * @retval int
  32. */
  33. #include "lcd.h"
  34. #include "test.h"



  35. int main(void)
  36. {

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

  39. /* Configure the system clock */
  40. SystemClock_Config();


  41. /* Initialize all configured peripherals */
  42. MX_GPIO_Init();
  43. MX_SPI1_Init();
  44. /* USER CODE BEGIN 2 */

  45. /* USER CODE END 2 */

  46. /* Infinite loop */
  47. /* USER CODE BEGIN WHILE */
  48. HAL_SPI_Init(&hspi1);

  49. HAL_Delay(1000);
  50. LCD_Init();

  51. LCD_Clear(WHITE);

  52. Gui_StrCenter(0,0,BLUE,BLUE,"HELLO WORLD!",16,1);
  53. Gui_StrCenter(0,16,RED,BLUE,"STM32F103C8T6",16,1);
  54. Gui_StrCenter(0,32,BLACK,YELLOW,"STM32CubeIDE",16,0);
  55. Gui_StrCenter(0,48,WHITE,BLUE,"ST7735R TFT LCD",16,0);
  56. Gui_StrCenter(0,64,BLUE,GREEN,"SPI1 Half-Duplex",16,0);
  57. Gui_StrCenter(0,80,BLUE,BLUE,"Sat/22/11/2025",16,1);
  58. Gui_StrCenter(0,96,BLUE,BLUE,"MCU: +3.3VDC",16,1);
  59. Gui_StrCenter(0,112,BLUE,BLUE,"LCD: +5.0VDC",16,1);
  60. Gui_StrCenter(0,128,BLACK,BLUE,"HAL Example",16,1);
  61. uint8_t counter=0,msg[16];
  62. while (1)
  63. {
  64. /* USER CODE END WHILE */
  65. //main_test();
  66. sprintf(msg," Counter: %3d ",counter);
  67. Gui_StrCenter(0,144,YELLOW,RED,msg,16,0);
  68. counter++;
  69. HAL_Delay(100);
  70. /* USER CODE BEGIN 3 */
  71. }
  72. /* USER CODE END 3 */
  73. }

  74. /**
  75. * @brief System Clock Configuration
  76. * @retval None
  77. */
  78. void SystemClock_Config(void)
  79. {
  80. RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  81. RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};

  82. /** Initializes the RCC Oscillators according to the specified parameters
  83. * in the RCC_OscInitTypeDef structure.
  84. */
  85. RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
  86. RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  87. RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
  88. RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  89. RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI_DIV2;
  90. RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL16;
  91. if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  92. {
  93. Error_Handler();
  94. }
  95. /** Initializes the CPU, AHB and APB buses clocks
  96. */
  97. RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  98. |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  99. RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  100. RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  101. RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
  102. RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;

  103. if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
  104. {
  105. Error_Handler();
  106. }
  107. }

  108. /**
  109. * @brief SPI1 Initialization Function
  110. * @param None
  111. * @retval None
  112. */
  113. static void MX_SPI1_Init(void)
  114. {

  115. /* USER CODE BEGIN SPI1_Init 0 */

  116. /* USER CODE END SPI1_Init 0 */

  117. /* USER CODE BEGIN SPI1_Init 1 */

  118. /* USER CODE END SPI1_Init 1 */
  119. /* SPI1 parameter configuration*/
  120. hspi1.Instance = SPI1;
  121. hspi1.Init.Mode = SPI_MODE_MASTER;
  122. hspi1.Init.Direction = SPI_DIRECTION_2LINES;
  123. hspi1.Init.DataSize = SPI_DATASIZE_8BIT;
  124. hspi1.Init.CLKPolarity = SPI_POLARITY_LOW;
  125. hspi1.Init.CLKPhase = SPI_PHASE_1EDGE;
  126. hspi1.Init.NSS = SPI_NSS_SOFT;
  127. hspi1.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_4;
  128. hspi1.Init.FirstBit = SPI_FIRSTBIT_MSB;
  129. hspi1.Init.TIMode = SPI_TIMODE_DISABLE;
  130. hspi1.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
  131. hspi1.Init.CRCPolynomial = 10;
  132. if (HAL_SPI_Init(&hspi1) != HAL_OK)
  133. {
  134. Error_Handler();
  135. }
  136. /* USER CODE BEGIN SPI1_Init 2 */

  137. /* USER CODE END SPI1_Init 2 */

  138. }

  139. /**
  140. * @brief GPIO Initialization Function
  141. * @param None
  142. * @retval None
  143. */
  144. static void MX_GPIO_Init(void)
  145. {
  146. GPIO_InitTypeDef GPIO_InitStruct = {0};

  147. /* GPIO Ports Clock Enable */
  148. __HAL_RCC_GPIOA_CLK_ENABLE();

  149. /*Configure GPIO pin Output Level */
  150. HAL_GPIO_WritePin(GPIOA, GPIO_PIN_2|GPIO_PIN_3|GPIO_PIN_4, GPIO_PIN_RESET);

  151. /*Configure GPIO pins : PA2 PA3 PA4 */
  152. GPIO_InitStruct.Pin = GPIO_PIN_2|GPIO_PIN_3|GPIO_PIN_4;
  153. GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  154. GPIO_InitStruct.Pull = GPIO_NOPULL;
  155. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  156. HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

  157. }

  158. /* USER CODE BEGIN 4 */

  159. /* USER CODE END 4 */

  160. /**
  161. * @brief This function is executed in case of error occurrence.
  162. * @retval None
  163. */
  164. void Error_Handler(void)
  165. {
  166. /* USER CODE BEGIN Error_Handler_Debug */
  167. /* User can add his own implementation to report the HAL error return state */
  168. __disable_irq();
  169. while (1)
  170. {
  171. }
  172. /* USER CODE END Error_Handler_Debug */
  173. }

  174. #ifdef USE_FULL_ASSERT
  175. /**
  176. * @brief Reports the name of the source file and the source line number
  177. * where the assert_param error has occurred.
  178. * @param file: pointer to the source file name
  179. * @param line: assert_param error line source number
  180. * @retval None
  181. */
  182. void assert_failed(uint8_t *file, uint32_t line)
  183. {
  184. /* USER CODE BEGIN 6 */
  185. /* User can add his own implementation to report the file name and line number,
  186. ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  187. /* USER CODE END 6 */
  188. }
  189. #endif /* USE_FULL_ASSERT */

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

I commented out some functions such as software bit-banging and touch sensor. 

STM32 Blue Pill HSPI1 and ST7735 128x160 TFT LCD
Proteus Simulation

Simulation this program in Proteus is slower than in physical hardware. 

STM32 Blue Pill HSPI1 and ST7735 128x160 TFT LCD

 

Click here to download its source file. 


 

 

Monday, November 17, 2025

STM32 Blue Pill HSPI1 and MCP23S17 GPIO Example

The MCP23S17 is a 16-bit I/O expander chip that use the Serial Peripheral Interface (SPI). It has two 8-bit general purpose I/O port GPIOA and GPIOB. Its internal registers is similar to the MCP23017 that use the I2C interface. This chip a simple 28-Pin DIP or SMD package suitable for student or electronic hobbyists. For more detail about using this chip please visit this post.

STM32 Blue Pill HSPI1 and MCP23S17 GPIO Example
Simulation Program

Since it's a general I/O port we can use this this chip to control LEDs, relays, input switches, matrix key pad, LCD etc.

STM32 Blue Pill HSPI1 and MCP23S17 GPIO Example

 

In this example I use the HSPI1 communication interface of the STM32F103C8T6 to read the input from from GPIOB and then send to the output port, GPIOA. This chip has internal programmable weak pull up resistors for both ports.

The micro-controller is 64MHz, driven from its internal 8MHz HSI and PLL.

STM32 Blue Pill HSPI1 and MCP23S17 GPIO Example
Clock Configuration

The SPI mode is set to Full Duplex Master Mode with NSS disabled. Since the NSS pin is activated every 8-bit clock pulse. In this situation the Chip Select (Active Low) pin is only active from Low to High every three 24 clock pulses or three data packets. Its baud rate is set to 4MBits per seconds since it has problem when receiving SPI data. It misses some data bits at higher baud rate. 

STM32 Blue Pill HSPI1 and MCP23S17 GPIO Example
SPI Setting

 

Once transmission session contain three data bytes,

  1. Address of this chip (0x40 for writing and 0x41  for reading) since the address pins A2:A1 are wired to GND.
  2. Address of its internal register, for example the IODIRA locate at the address 0x00
  3. And data to be written to that registers, for instance writing 0x00 to IODIRA making GPIOA as digital output.

In this section I use the,

  1. HAL_SPI_Transmit
  2. HAL_SPI_Receive 

There are a lot of SPI function in the HAL libraries but I found these two functions very easy to handle. 

  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 variables ---------------------------------------------------------*/
  23. SPI_HandleTypeDef hspi1;

  24. /* Private function prototypes -----------------------------------------------*/
  25. void SystemClock_Config(void);
  26. static void MX_GPIO_Init(void);
  27. static void MX_SPI1_Init(void);
  28. /* USER CODE BEGIN PFP */

  29. /*mcp23x17 registers at bank0*/
  30. #define IODIRA_0 0x00
  31. #define IODIRB_0 0x01
  32. #define IPOLA_0 0x02
  33. #define IPOLB_0 0x03
  34. #define GPINTENA 0x04
  35. #define GPINTENB 0x05
  36. #define GPPUA_0 0x0C
  37. #define GPPUB_0 0x0D
  38. #define GPIOA_0 0x12
  39. #define GPIOB_0 0x13
  40. #define OLATA_0 0x14
  41. #define OLATB_0 0x15

  42. void mcp23s17_send(uint8_t address, uint8_t data){
  43. uint8_t temp[3];
  44. temp[0]=0x40;
  45. temp[1]=address;
  46. temp[2]=data;
  47. HAL_GPIO_WritePin(GPIOA,GPIO_PIN_4,0);
  48. HAL_SPI_Transmit(&hspi1, temp, 3, 1);
  49. HAL_GPIO_WritePin(GPIOA,GPIO_PIN_4,1);
  50. }

  51. uint8_t mcp23s17_receive(uint8_t addr){

  52. uint8_t temp[2], data=0;
  53. temp[0]=0x41;
  54. temp[1]=addr;

  55. HAL_GPIO_WritePin(GPIOA,GPIO_PIN_4,0);
  56. HAL_SPI_Transmit(&hspi1, temp, 2, 1);
  57. HAL_SPI_Receive(&hspi1,&data,1,1);
  58. HAL_GPIO_WritePin(GPIOA,GPIO_PIN_4,1);

  59. return data;
  60. }

  61. int main(void)
  62. {

  63. /* MCU Configuration--------------------------------------------------------*/

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

  66. /* Configure the system clock */
  67. SystemClock_Config();

  68. /* Initialize all configured peripherals */
  69. MX_GPIO_Init();
  70. MX_SPI1_Init();

  71. HAL_SPI_Init(&hspi1);

  72. uint8_t data;

  73. //HAL_GPIO_WritePin(GPIOA,GPIO_PIN_4,1);
  74. mcp23s17_send(IODIRA_0,0x00);
  75. mcp23s17_send(IODIRB_0,0xFF);
  76. mcp23s17_send(GPPUB_0,0xFF);

  77. for(uint8_t i=0;i<8;i++){
  78. static uint8_t data=0;
  79. data|=1<<i;
  80. mcp23s17_send(OLATA_0,data);
  81. HAL_Delay(100);
  82. }
  83. HAL_Delay(3000);
  84. while (1)
  85. {
  86. data = mcp23s17_receive(GPIOB_0);
  87. mcp23s17_send(OLATA_0,data);

  88. }
  89. /* USER CODE END 3 */
  90. }

  91. /**
  92. * @brief System Clock Configuration
  93. * @retval None
  94. */
  95. void SystemClock_Config(void)
  96. {
  97. RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  98. RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};

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

  120. if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
  121. {
  122. Error_Handler();
  123. }
  124. }

  125. /**
  126. * @brief SPI1 Initialization Function
  127. * @param None
  128. * @retval None
  129. */
  130. static void MX_SPI1_Init(void)
  131. {

  132. /* USER CODE BEGIN SPI1_Init 0 */

  133. /* USER CODE END SPI1_Init 0 */

  134. /* USER CODE BEGIN SPI1_Init 1 */

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

  154. /* USER CODE END SPI1_Init 2 */

  155. }

  156. /**
  157. * @brief GPIO Initialization Function
  158. * @param None
  159. * @retval None
  160. */
  161. static void MX_GPIO_Init(void)
  162. {
  163. GPIO_InitTypeDef GPIO_InitStruct = {0};

  164. /* GPIO Ports Clock Enable */
  165. __HAL_RCC_GPIOA_CLK_ENABLE();

  166. /*Configure GPIO pin Output Level */
  167. HAL_GPIO_WritePin(GPIOA, GPIO_PIN_4, GPIO_PIN_SET);

  168. /*Configure GPIO pin : PA4 */
  169. GPIO_InitStruct.Pin = GPIO_PIN_4;
  170. GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  171. GPIO_InitStruct.Pull = GPIO_NOPULL;
  172. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
  173. HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

  174. }

  175. /* USER CODE BEGIN 4 */

  176. /* USER CODE END 4 */

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

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

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



I just use the simulator since I don't have this chip in my own lab. It was release many years now that I could not find it at local store. It is available from some popular online store such as Ali-Express, E bay or Amazon. 

STM32 Blue Pill HSPI1 and MCP23S17 GPIO Example
Input Reading #1

 
STM32 Blue Pill HSPI1 and MCP23S17 GPIO Example
Input Reading #2

Click here to download its source file. 


 



Sunday, November 16, 2025

STM32 Blue Pill HSPI1 and SN74HC595N 16x4 LCD Example

In previous post I use the HSPI1 communication interface of the STM32F103C8T6 to interface with the SN74HC595N serial-in-parallel out shift registers to control LEDs and 7-Segment display. In this programming example I use this chip to control the HD44780 compatible LCD controller. I use the TC1604A-01(R) LCM module from Tinsharp that I keep it many years now.

STM32 Blue Pill HSPI1 and SN74HC595N 16x4 LCD Example

The SPI communication is one direction that the master micro-controller just send serial data including command and display data to the LCD module. Hence the LCD R/W pin must wired to GND. 

SN74HC595 chips in DIP package

 

The controlling mode is 4-bit data mode that need to send the 8-bit command/data twice via its high nibble and low nibble.

The MCU clock is 64MHz that is driven from its 8MHz HSI and PLL.

STM32 Blue Pill HSPI1 and SN74HC595N LED Example
Clock Configuration

The operate in half duplex master mode with a baud rate of 16Mbits per second.

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

The NSS pin is ignored. So the enable pin must be set via software. I select PB4 as the SPI slave select pin. The Display Data RAM (DD RAM) of the TC1604A-01(R) is shown in the figure below.

STM32 Blue Pill HSPI1 and SN74HC595N 16x4 LCD Example
I put the LCD driver with the main.c 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) 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. uint8_t DPORT;

  48. void delay1(uint16_t dTime){
  49. for(uint16_t i=0;i<dTime;i++);
  50. }

  51. void delay2(uint8_t dTime){
  52. for(uint8_t i=0;i<dTime;i++) delay1(5000);
  53. }

  54. void en(void){
  55. HAL_GPIO_WritePin(GPIOA,GPIO_PIN_4,GPIO_PIN_SET);
  56. delay1(1000);
  57. HAL_GPIO_WritePin(GPIOA,GPIO_PIN_4,GPIO_PIN_RESET);
  58. }

  59. void lcdCmd(uint8_t cmd){
  60. uint8_t temp=0x08;
  61. DPORT=temp|(cmd&0xF0);
  62. HAL_SPI_Transmit(&hspi1,&DPORT,1,10);
  63. en();
  64. delay1(10);
  65. temp=0;
  66. DPORT=temp|(cmd&0xF0);
  67. HAL_SPI_Transmit(&hspi1,&DPORT,1,10);
  68. en();
  69. delay1(100);

  70. temp=0x08;
  71. DPORT=temp|(cmd<<4);
  72. HAL_SPI_Transmit(&hspi1,&DPORT,1,10);
  73. en();
  74. delay1(10);
  75. temp=0;
  76. DPORT=temp|(cmd<<4);
  77. HAL_SPI_Transmit(&hspi1,&DPORT,1,10);
  78. en();
  79. }

  80. void lcdDat(uint8_t dat){
  81. uint8_t temp=0x0A;
  82. DPORT=temp|(dat&0xF0);
  83. HAL_SPI_Transmit(&hspi1,&DPORT,1,10);
  84. en();
  85. delay1(10);
  86. temp=0x02;
  87. DPORT=temp|(dat&0xF0);
  88. HAL_SPI_Transmit(&hspi1,&DPORT,1,10);
  89. en();
  90. delay1(100);

  91. temp=0x0A;
  92. DPORT=temp|(dat<<4);
  93. HAL_SPI_Transmit(&hspi1,&DPORT,1,10);
  94. en();
  95. delay1(10);
  96. temp=0x02;
  97. DPORT=temp|(dat<<4);
  98. HAL_SPI_Transmit(&hspi1,&DPORT,1,10);
  99. en();
  100. }

  101. void lcdInit(void){
  102. DPORT=0x00;
  103. delay1(2000);
  104. lcdCmd(0x33);
  105. delay1(100);
  106. lcdCmd(0x32);
  107. delay1(100);
  108. lcdCmd(0x28);
  109. delay1(100);
  110. lcdCmd(0x0F);
  111. delay1(100);
  112. lcdCmd(0x01);
  113. delay1(2000);
  114. lcdCmd(0x06);
  115. delay1(100);
  116. }

  117. void lcdStr(uint8_t *str){
  118. while(*str) lcdDat(*str++);
  119. }

  120. void lcdXY(uint8_t x,uint8_t y){
  121. // 20x4 LCD
  122. //uint8_t tbe[]={0x80,0xC0,0x94,0xD4};
  123. // 16x4 LCD
  124. uint8_t tbe[]={0x80,0xC0,0x90,0xD0};
  125. lcdCmd(tbe[y-1]+x-1);
  126. delay1(100);
  127. }

  128. void lcdClear(void){
  129. lcdCmd(0x01);
  130. delay1(12000);
  131. }

  132. /**
  133. * @brief The application entry point.
  134. * @retval int
  135. */
  136. int main(void)
  137. {
  138. /* USER CODE BEGIN 1 */

  139. /* USER CODE END 1 */

  140. /* MCU Configuration--------------------------------------------------------*/

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

  143. /* USER CODE BEGIN Init */

  144. /* USER CODE END Init */

  145. /* Configure the system clock */
  146. SystemClock_Config();

  147. /* USER CODE BEGIN SysInit */

  148. /* USER CODE END SysInit */

  149. /* Initialize all configured peripherals */
  150. MX_GPIO_Init();
  151. MX_SPI1_Init();
  152. /* USER CODE BEGIN 2 */
  153. HAL_Delay(100);
  154. lcdInit();
  155. lcdClear();
  156. lcdCmd(0x0C);
  157. lcdXY(3,1);
  158. lcdStr("Hello World!");

  159. lcdXY(3,2);
  160. lcdStr("STM32F103C8T6 ");

  161. lcdXY(1,3);
  162. lcdStr("HSPI1 SN74HC595N");

  163. lcdXY(1,4);
  164. lcdStr("TC1604A-01R LCD");

  165. HAL_Delay(5000);
  166. lcdClear();

  167. lcdXY(1,1);
  168. lcdStr("STM32CubeIDE");

  169. lcdXY(1,2);
  170. lcdStr("version 1.6.0");

  171. lcdXY(1,3);
  172. lcdStr("HAL Library...");

  173. lcdXY(1,4);
  174. lcdStr("16/11/2025");

  175. /* USER CODE END 2 */

  176. /* Infinite loop */
  177. /* USER CODE BEGIN WHILE */
  178. while (1)
  179. {
  180. /* USER CODE END WHILE */

  181. /* USER CODE BEGIN 3 */
  182. }
  183. /* USER CODE END 3 */
  184. }

  185. /**
  186. * @brief System Clock Configuration
  187. * @retval None
  188. */
  189. void SystemClock_Config(void)
  190. {
  191. RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  192. RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};

  193. /** Initializes the RCC Oscillators according to the specified parameters
  194. * in the RCC_OscInitTypeDef structure.
  195. */
  196. RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
  197. RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  198. RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
  199. RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  200. RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI_DIV2;
  201. RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL16;
  202. if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  203. {
  204. Error_Handler();
  205. }
  206. /** Initializes the CPU, AHB and APB buses clocks
  207. */
  208. RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  209. |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  210. RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  211. RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  212. RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
  213. RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;

  214. if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
  215. {
  216. Error_Handler();
  217. }
  218. }

  219. /**
  220. * @brief SPI1 Initialization Function
  221. * @param None
  222. * @retval None
  223. */
  224. static void MX_SPI1_Init(void)
  225. {

  226. /* USER CODE BEGIN SPI1_Init 0 */

  227. /* USER CODE END SPI1_Init 0 */

  228. /* USER CODE BEGIN SPI1_Init 1 */

  229. /* USER CODE END SPI1_Init 1 */
  230. /* SPI1 parameter configuration*/
  231. hspi1.Instance = SPI1;
  232. hspi1.Init.Mode = SPI_MODE_MASTER;
  233. hspi1.Init.Direction = SPI_DIRECTION_1LINE;
  234. hspi1.Init.DataSize = SPI_DATASIZE_8BIT;
  235. hspi1.Init.CLKPolarity = SPI_POLARITY_LOW;
  236. hspi1.Init.CLKPhase = SPI_PHASE_1EDGE;
  237. hspi1.Init.NSS = SPI_NSS_SOFT;
  238. hspi1.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_4;
  239. hspi1.Init.FirstBit = SPI_FIRSTBIT_MSB;
  240. hspi1.Init.TIMode = SPI_TIMODE_DISABLE;
  241. hspi1.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
  242. hspi1.Init.CRCPolynomial = 10;
  243. if (HAL_SPI_Init(&hspi1) != HAL_OK)
  244. {
  245. Error_Handler();
  246. }
  247. /* USER CODE BEGIN SPI1_Init 2 */

  248. /* USER CODE END SPI1_Init 2 */

  249. }

  250. /**
  251. * @brief GPIO Initialization Function
  252. * @param None
  253. * @retval None
  254. */
  255. static void MX_GPIO_Init(void)
  256. {
  257. GPIO_InitTypeDef GPIO_InitStruct = {0};

  258. /* GPIO Ports Clock Enable */
  259. __HAL_RCC_GPIOA_CLK_ENABLE();

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

  262. /*Configure GPIO pin : PA4 */
  263. GPIO_InitStruct.Pin = GPIO_PIN_4;
  264. GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  265. GPIO_InitStruct.Pull = GPIO_NOPULL;
  266. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  267. HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

  268. }

  269. /* USER CODE BEGIN 4 */

  270. /* USER CODE END 4 */

  271. /**
  272. * @brief This function is executed in case of error occurrence.
  273. * @retval None
  274. */
  275. void Error_Handler(void)
  276. {
  277. /* USER CODE BEGIN Error_Handler_Debug */
  278. /* User can add his own implementation to report the HAL error return state */
  279. __disable_irq();
  280. while (1)
  281. {
  282. }
  283. /* USER CODE END Error_Handler_Debug */
  284. }

  285. #ifdef USE_FULL_ASSERT
  286. /**
  287. * @brief Reports the name of the source file and the source line number
  288. * where the assert_param error has occurred.
  289. * @param file: pointer to the source file name
  290. * @param line: assert_param error line source number
  291. * @retval None
  292. */
  293. void assert_failed(uint8_t *file, uint32_t line)
  294. {
  295. /* USER CODE BEGIN 6 */
  296. /* User can add his own implementation to report the file name and line number,
  297. ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  298. /* USER CODE END 6 */
  299. }
  300. #endif /* USE_FULL_ASSERT */

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


I made my own DIY SN74HC595N LCD driver controlling a TC1604A-01(R) LCM module.

STM32 Blue Pill HSPI1 and SN74HC595N 16x4 LCD Example

STM32 Blue Pill HSPI1 and SN74HC595N 16x4 LCD Example

It can be used with some Arduino libraries. It is very easy to use as it needs only five pins.

STM32 Blue Pill HSPI1 and SN74HC595N 16x4 LCD Example
Simulation #1

STM32 Blue Pill HSPI1 and SN74HC595N 16x4 LCD Example
Simulation #2

I use Proteus VSM to simulate this example.

In real hardware this LCD module work at +5VDC only while the STM32 Blue pill input output logic voltage is +3.3VDC. However some newer LCD module with the HD44780 compatible LCD controller able to operate at +3.3VDC.

 

STM32 Blue Pill HSPI1 and SN74HC595N 16x4 LCD Example
Running Program #2

STM32 Blue Pill HSPI1 and SN74HC595N 16x4 LCD Example
Running Program #1

Click here to download this example. 


 


Search This Blog

Labels

23K256 (1) 24C16B (2) 25AA010A (2) 8051 (7) 93AA46B (1) ADC (34) Analog Comparator (1) Arduino (16) ARM (13) AT89C52 (7) ATMega32 (58) ATMega644P (27) AVR (87) Bootloader (1) CCS PICC (31) Change Notify Interrupt (CNI) (2) DAC (2) DHT11 (4) Display (133) Distance Sensor (3) 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 (2) I2C (38) ILI9341 (1) Input/Output (75) Interrupt (22) Keil (5) Keypad (16) KS0108 (2) LCD (75) LM35 (3) Master/Slave (2) MAX7221 (1) MCP23017 (8) MCP23S17 (7) MCP4921 (1) MCP4922 (2) Meter (3) MikroC (2) Motor (15) MPLABX (73) Nokia 5110 LCD (4) OLED (2) One-Wire (7) Oscillator (8) PCB (10) PCD8544 (3) PCF8574 (10) PIC (108) PIC12F (3) 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) Pin Change Interrupt (1) PWM (12) RTC (12) SBN0064G (1) Sensor (13) SH1106 (3) Shift Register (13) Shift Registers (10) Software TWI (2) SPI (39) ST7735 (1) STM32 (11) STM32 Black Pill (1) STM32 Blue Pill (12) STM32 HAL (5) STM32CubeIDE (13) STM32F103C8T6 (9) 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)