Friday, September 11, 2020

STM32F103R6 Interfaces To Character LCD In 4-Bit Mode

In previous post, I showed a simple programming example of using a character LCD with the STM32F103R6 ARM micro-controller using an 8-bit data bus. As it's already known, this LCD module is able to accept data from the micro-controller using 4-bit data bus. However the whole data ore command is still 8-bit wide. The MCU need to send data twice. The higher nibble needed to send and latch into the LCD module first. Then the lower nibble will be sent and latched into the LCD module.

STM32F103R6 Interfaces To Character LCD In 4-Bit Mode
Program Simulation

In this programming example, I will use PORTC of the STM32F103R6 to interface with the HD44780 LCD module. Where,

  • PC0 connects to LCD Register Select (RS)
  • LCD Read/Write (R/W) connects to GND as we only need to write data or command to the LCD module
  • PC1 connects to LCD Enable (E) pin. It latches data into the LCD module whenever a High to Low transition occurs.
  • PC4...PC7 connect to LCD data bus D4...D7.

Anyway the programmer should select other port or pin number of this micro-controller.

STM32F103R6 Interfaces To Character LCD In 4-Bit Mode
Device Configuration Tool
 

Device Configuration Tool could generates the source code for target controller without writing 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) 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. #define DPORT GPIOC->ODR
  24.  
  25. void delay1(uint16_t dTime){
  26. for(uint16_t i=0;i<dTime;i++);
  27. }
  28.  
  29. void delay2(uint8_t dTime){
  30. for(uint8_t i=0;i<dTime;i++) delay1(5000);
  31. }
  32.  
  33. void lcdCmd(uint8_t cmd){
  34. uint8_t temp=0x02;
  35. DPORT=temp|(cmd&0xF0);
  36. delay1(10);
  37. temp=0;
  38. DPORT=temp|(cmd&0xF0);
  39. delay1(100);
  40.  
  41. temp=0x02;
  42. DPORT=temp|(cmd<<4);
  43. delay1(10);
  44. temp=0;
  45. DPORT=temp|(cmd<<4);
  46. }
  47.  
  48. void lcdDat(uint8_t dat){
  49. uint8_t temp=0x03;
  50. DPORT=temp|(dat&0xF0);
  51. delay1(10);
  52. temp=0x01;
  53. DPORT=temp|(dat&0xF0);
  54. delay1(100);
  55.  
  56. temp=0x03;
  57. DPORT=temp|(dat<<4);
  58. delay1(10);
  59. temp=0x01;
  60. DPORT=temp|(dat<<4);
  61. }
  62.  
  63. void lcdInit(void){
  64. DPORT=0x00;
  65. delay1(2000);
  66. lcdCmd(0x33);
  67. delay1(100);
  68. lcdCmd(0x32);
  69. delay1(100);
  70. lcdCmd(0x28);
  71. delay1(100);
  72. lcdCmd(0x0F);
  73. delay1(100);
  74. lcdCmd(0x01);
  75. delay1(2000);
  76. lcdCmd(0x06);
  77. delay1(100);
  78. }
  79.  
  80. void lcdStr(uint8_t *str){
  81. while(*str) lcdDat(*str++);
  82. }
  83.  
  84. void lcdXY(uint8_t x,uint8_t y){
  85. // 20x4 LCD
  86. uint8_t tbe[]={0x80,0xC0,0x94,0xD4};
  87. lcdCmd(tbe[y-1]+x-1);
  88. delay1(100);
  89. }
  90.  
  91. void lcdClear(void){
  92. lcdCmd(0x01);
  93. delay1(100);
  94. }
  95.  
  96. /* Private function prototypes -----------------------------------------------*/
  97. void SystemClock_Config(void);
  98. static void MX_GPIO_Init(void);
  99.  
  100. /**
  101.   * @brief The application entry point.
  102.   * @retval int
  103.   */
  104. int main(void)
  105. {
  106. /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  107. HAL_Init();
  108.  
  109. /* Configure the system clock */
  110. SystemClock_Config();
  111.  
  112. /* Initialize all configured peripherals */
  113. MX_GPIO_Init();
  114.  
  115. lcdInit();
  116. lcdClear();
  117. delay2(200);
  118.  
  119. lcdXY(5,1);
  120. lcdStr("Hello World!");
  121. delay2(100);
  122.  
  123. lcdXY(1,2);
  124. lcdStr("STM32F103R6Tx ARM");
  125. delay2(100);
  126.  
  127. lcdXY(1,3);
  128. lcdStr("Cortex-M3 Processor");
  129. delay2(200);
  130.  
  131. lcdXY(5,4);
  132. lcdStr("STM32CubeIDE");
  133. delay2(200);
  134.  
  135. /* Infinite loop */
  136. /* USER CODE BEGIN WHILE */
  137. while (1)
  138. {
  139.  
  140. }
  141. }
  142.  
  143. /**
  144.   * @brief System Clock Configuration
  145.   * @retval None
  146.   */
  147. void SystemClock_Config(void)
  148. {
  149. RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  150. RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
  151.  
  152. /** Initializes the RCC Oscillators according to the specified parameters
  153.   * in the RCC_OscInitTypeDef structure.
  154.   */
  155. RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
  156. RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  157. RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
  158. RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
  159. if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  160. {
  161. Error_Handler();
  162. }
  163. /** Initializes the CPU, AHB and APB buses clocks
  164.   */
  165. RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  166. |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  167. RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSI;
  168. RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  169. RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
  170. RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
  171.  
  172. if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK)
  173. {
  174. Error_Handler();
  175. }
  176. }
  177.  
  178. /**
  179.   * @brief GPIO Initialization Function
  180.   * @param None
  181.   * @retval None
  182.   */
  183. static void MX_GPIO_Init(void)
  184. {
  185. GPIO_InitTypeDef GPIO_InitStruct = {0};
  186.  
  187. /* GPIO Ports Clock Enable */
  188. __HAL_RCC_GPIOC_CLK_ENABLE();
  189.  
  190. /*Configure GPIO pin Output Level */
  191. HAL_GPIO_WritePin(GPIOC, GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_4|GPIO_PIN_5
  192. |GPIO_PIN_6|GPIO_PIN_7, GPIO_PIN_RESET);
  193.  
  194. /*Configure GPIO pins : PC0 PC1 PC4 PC5
  195.   PC6 PC7 */
  196. GPIO_InitStruct.Pin = GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_4|GPIO_PIN_5
  197. |GPIO_PIN_6|GPIO_PIN_7;
  198. GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  199. GPIO_InitStruct.Pull = GPIO_NOPULL;
  200. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  201. HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
  202.  
  203. }
  204.  
  205. /* USER CODE BEGIN 4 */
  206.  
  207. /* USER CODE END 4 */
  208.  
  209. /**
  210.   * @brief This function is executed in case of error occurrence.
  211.   * @retval None
  212.   */
  213. void Error_Handler(void)
  214. {
  215. /* USER CODE BEGIN Error_Handler_Debug */
  216. /* User can add his own implementation to report the HAL error return state */
  217. __disable_irq();
  218. while (1)
  219. {
  220. }
  221. /* USER CODE END Error_Handler_Debug */
  222. }
  223.  
  224. #ifdef USE_FULL_ASSERT
  225. /**
  226.   * @brief Reports the name of the source file and the source line number
  227.   * where the assert_param error has occurred.
  228.   * @param file: pointer to the source file name
  229.   * @param line: assert_param error line source number
  230.   * @retval None
  231.   */
  232. void assert_failed(uint8_t *file, uint32_t line)
  233. {
  234. /* USER CODE BEGIN 6 */
  235. /* User can add his own implementation to report the file name and line number,
  236.   ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  237. /* USER CODE END 6 */
  238. }
  239. #endif /* USE_FULL_ASSERT */
  240.  
  241. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
  242.   

I use a 20x4 character LCD. If you prefer a 16x2 LCD you need to modify its display RAM address in the program. Click here to download its source file.

No comments:

Post a Comment