Tuesday, October 13, 2020

STM32F103R6 SPI Interfaces To SN74HC595N Character LCD Driver

In previous post, I show an example of driving a HD44780-based character LCD using 4-bit data mode. Driving this LCD module is quite easy. We can use other driving chip to make the communication interface between the micro-controller and this LCD module simpler. For example the programmer can add a I2C I/O expanding chip (PCF8574), or an SPI I/O expanding chip (SN74HC595N or 74HC164).

STM32F103R6 SPI Interfaces To SN74HC595N Character LCD Driver

Simulating Program Using Proteus VSM 8.15

In this example, I use an SN74HC595N serial-in-parallel-out shift registers to drive a character LCD. This chip uses the Serial Peripheral Interface (SPI). The LCD module is a 20x4 HD44780-based LCD controller.

STM32F103R6 SPI Interfaces To SN74HC595N Character LCD Driver

Device Configuration Tool For SPI1 Communication Module


We need to configure the SPI1 communication module of this micro-controller as above. I choose the slave select or the Hardware NSS output signal using hardware. So we don't need to write the slave select signal in program. This signal is automatic activate after the SPI data has transmitted. 

  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) 2023 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 variables ---------------------------------------------------------*/
  24. SPI_HandleTypeDef hspi1;
  25.  
  26. /* Private function prototypes -----------------------------------------------*/
  27. void SystemClock_Config(void);
  28. static void MX_GPIO_Init(void);
  29. static void MX_SPI1_Init(void);
  30.  
  31. uint8_t DPORT;
  32.  
  33. void delay1(uint16_t dTime){
  34. for(uint16_t i=0;i<dTime;i++);
  35. }
  36.  
  37. void delay2(uint8_t dTime){
  38. for(uint8_t i=0;i<dTime;i++) delay1(5000);
  39. }
  40.  
  41. void lcdCmd(uint8_t cmd){
  42. uint8_t temp=0x02;
  43. DPORT=temp|(cmd&0xF0);
  44. HAL_SPI_Transmit(&hspi1,&DPORT,1,10);
  45. delay1(10);
  46. temp=0;
  47. DPORT=temp|(cmd&0xF0);
  48. HAL_SPI_Transmit(&hspi1,&DPORT,1,10);
  49. delay1(100);
  50.  
  51. temp=0x02;
  52. DPORT=temp|(cmd<<4);
  53. HAL_SPI_Transmit(&hspi1,&DPORT,1,10);
  54. delay1(10);
  55. temp=0;
  56. DPORT=temp|(cmd<<4);
  57. HAL_SPI_Transmit(&hspi1,&DPORT,1,10);
  58. }
  59.  
  60. void lcdDat(uint8_t dat){
  61. uint8_t temp=0x03;
  62. DPORT=temp|(dat&0xF0);
  63. HAL_SPI_Transmit(&hspi1,&DPORT,1,10);
  64. delay1(10);
  65. temp=0x01;
  66. DPORT=temp|(dat&0xF0);
  67. HAL_SPI_Transmit(&hspi1,&DPORT,1,10);
  68. delay1(100);
  69.  
  70. temp=0x03;
  71. DPORT=temp|(dat<<4);
  72. HAL_SPI_Transmit(&hspi1,&DPORT,1,10);
  73. delay1(10);
  74. temp=0x01;
  75. DPORT=temp|(dat<<4);
  76. HAL_SPI_Transmit(&hspi1,&DPORT,1,10);
  77. }
  78.  
  79. void lcdInit(void){
  80. DPORT=0x00;
  81. delay1(2000);
  82. lcdCmd(0x33);
  83. delay1(100);
  84. lcdCmd(0x32);
  85. delay1(100);
  86. lcdCmd(0x28);
  87. delay1(100);
  88. lcdCmd(0x0F);
  89. delay1(100);
  90. lcdCmd(0x01);
  91. delay1(2000);
  92. lcdCmd(0x06);
  93. delay1(100);
  94. }
  95.  
  96. void lcdStr(uint8_t *str){
  97. while(*str) lcdDat(*str++);
  98. }
  99.  
  100. void lcdXY(uint8_t x,uint8_t y){
  101. // 20x4 LCD
  102. uint8_t tbe[]={0x80,0xC0,0x94,0xD4};
  103. lcdCmd(tbe[y-1]+x-1);
  104. delay1(100);
  105. }
  106.  
  107. void lcdClear(void){
  108. lcdCmd(0x01);
  109. delay1(100);
  110. }
  111.  
  112. /**
  113.   * @brief The application entry point.
  114.   * @retval int
  115.   */
  116. int main(void)
  117. {
  118.  
  119. /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  120. HAL_Init();
  121.  
  122. /* Configure the system clock */
  123. SystemClock_Config();
  124.  
  125. /* Initialize all configured peripherals */
  126. MX_GPIO_Init();
  127. MX_SPI1_Init();
  128.  
  129. lcdInit();
  130. lcdClear();
  131. delay2(50);
  132.  
  133. lcdXY(5,1);
  134. lcdStr("Hello World!");
  135. delay2(50);
  136.  
  137. lcdXY(1,2);
  138. lcdStr("STM32F103R6Tx ARM");
  139. delay2(50);
  140.  
  141. lcdXY(1,3);
  142. lcdStr("SPI Interfaces To");
  143. delay2(50);
  144.  
  145. lcdXY(1,4);
  146. lcdStr("SN74HC595N Serial In");
  147. delay2(50);
  148.  
  149. lcdClear();
  150. lcdXY(1,1);
  151. lcdStr("Parallel Out Shift");
  152. delay2(50);
  153. lcdXY(1,2);
  154. lcdStr("Registers And 20x4");
  155. delay2(50);
  156. lcdXY(1,3);
  157. lcdStr("HD44780 Character");
  158. delay2(50);
  159. lcdXY(1,4);
  160. lcdStr("LCD Display Driving ");
  161. /* Infinite loop */
  162. /* USER CODE BEGIN WHILE */
  163. while (1)
  164. {
  165.  
  166. }
  167. }
  168.  
  169. /**
  170.   * @brief System Clock Configuration
  171.   * @retval None
  172.   */
  173. void SystemClock_Config(void)
  174. {
  175. RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  176. RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
  177.  
  178. /** Initializes the RCC Oscillators according to the specified parameters
  179.   * in the RCC_OscInitTypeDef structure.
  180.   */
  181. RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
  182. RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  183. RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
  184. RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
  185. if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  186. {
  187. Error_Handler();
  188. }
  189. /** Initializes the CPU, AHB and APB buses clocks
  190.   */
  191. RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  192. |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  193. RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSI;
  194. RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  195. RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
  196. RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
  197.  
  198. if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK)
  199. {
  200. Error_Handler();
  201. }
  202. }
  203.  
  204. /**
  205.   * @brief SPI1 Initialization Function
  206.   * @param None
  207.   * @retval None
  208.   */
  209. static void MX_SPI1_Init(void)
  210. {
  211.  
  212. /* USER CODE BEGIN SPI1_Init 0 */
  213.  
  214. /* USER CODE END SPI1_Init 0 */
  215.  
  216. /* USER CODE BEGIN SPI1_Init 1 */
  217.  
  218. /* USER CODE END SPI1_Init 1 */
  219. /* SPI1 parameter configuration*/
  220. hspi1.Instance = SPI1;
  221. hspi1.Init.Mode = SPI_MODE_MASTER;
  222. hspi1.Init.Direction = SPI_DIRECTION_2LINES;
  223. hspi1.Init.DataSize = SPI_DATASIZE_8BIT;
  224. hspi1.Init.CLKPolarity = SPI_POLARITY_LOW;
  225. hspi1.Init.CLKPhase = SPI_PHASE_1EDGE;
  226. hspi1.Init.NSS = SPI_NSS_HARD_OUTPUT;
  227. hspi1.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_128;
  228. hspi1.Init.FirstBit = SPI_FIRSTBIT_MSB;
  229. hspi1.Init.TIMode = SPI_TIMODE_DISABLE;
  230. hspi1.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
  231. hspi1.Init.CRCPolynomial = 10;
  232. if (HAL_SPI_Init(&hspi1) != HAL_OK)
  233. {
  234. Error_Handler();
  235. }
  236. /* USER CODE BEGIN SPI1_Init 2 */
  237.  
  238. /* USER CODE END SPI1_Init 2 */
  239.  
  240. }
  241.  
  242. /**
  243.   * @brief GPIO Initialization Function
  244.   * @param None
  245.   * @retval None
  246.   */
  247. static void MX_GPIO_Init(void)
  248. {
  249.  
  250. /* GPIO Ports Clock Enable */
  251. __HAL_RCC_GPIOA_CLK_ENABLE();
  252.  
  253. }
  254.  
  255. /* USER CODE BEGIN 4 */
  256.  
  257. /* USER CODE END 4 */
  258.  
  259. /**
  260.   * @brief This function is executed in case of error occurrence.
  261.   * @retval None
  262.   */
  263. void Error_Handler(void)
  264. {
  265. /* USER CODE BEGIN Error_Handler_Debug */
  266. /* User can add his own implementation to report the HAL error return state */
  267. __disable_irq();
  268. while (1)
  269. {
  270. }
  271. /* USER CODE END Error_Handler_Debug */
  272. }
  273.  
  274. #ifdef USE_FULL_ASSERT
  275. /**
  276.   * @brief Reports the name of the source file and the source line number
  277.   * where the assert_param error has occurred.
  278.   * @param file: pointer to the source file name
  279.   * @param line: assert_param error line source number
  280.   * @retval None
  281.   */
  282. void assert_failed(uint8_t *file, uint32_t line)
  283. {
  284. /* USER CODE BEGIN 6 */
  285. /* User can add his own implementation to report the file name and line number,
  286.   ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  287. /* USER CODE END 6 */
  288. }
  289. #endif /* USE_FULL_ASSERT */
  290.  
  291. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
  292.  

Its source code is very simple. It's similar to the previous example. We just process the 4-bit LCD controlling. Then we will need to put those LCD controlling data into the SPI1 data buffer before it's sent over the SPI communication interface. 



If we want to use the 74HC164 serial-in-parallel-out shift registers we will need to drive the LCD Enable pin directly from the micro-controller. Click here to download its source file.

No comments:

Post a Comment