Thursday, September 14, 2023

STM32F103C8T6 Blue Pill And Character LCD Interfacing In 8-Bit Mode Example

In this ARM programming example, I will use an earlier day character LCD to show text. This character LCD conventionally uses an HD44780 LCD controller manufactured by Hitachi. Currently there are a lot of low cost equivalent LCD controllers.

STM32F103C8T6 Blue Pill And Character LCD Interfacing In 8-Bit Mode Example
Blue Pill Experiment On Bread Board

I will not write the details about this controller here because it's already explained in previous tutorial. But it uses the ATMega32 AVR micro-controller

STM32F103C8T6 Blue Pill And Character LCD Interfacing In 8-Bit Mode Example
Blue Pill Simulating Program Using Proteus VSM 8.15

I use the lower nibble of Port A to send command or data to LCD module. PortA is a 16-bit bi-directional I/O. PC14 connects to LCD Register Select (RS) while PC15 connects to LCD En (Enable). As we just needs to send data or command to LCD module, the LCD Read/Write (R/W) just connect to GND.

STM32F103C8T6 Blue Pill And Character LCD Interfacing In 8-Bit Mode Example

GPIO Setting In Device Configuration Tool

Without writing a legacy style C/C++ for ARM micro-controller, I use the Code Configuration Tool inside the STM32CubeIDE. It will generate C/C++ source code whenever we have finish all preferred setting on target MCU. 

STM32F103C8T6 Blue Pill And Character LCD Interfacing In 8-Bit Mode Example
SYS Setting

In SYS tab, I select Serial Wire as the debug interface with ST-LINK V2. The System Wake-Up check box must left unchecked to enable the PA0 as general purpose I/O.

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

I use its internal HSI RC clock source. However this module has an external 8MHz crystal oscillator. Click here to download its source file.

STM32F103C8T6 Blue Pill And Character LCD Interfacing In 8-Bit Mode Example

Clock Configuration

I re-arrange the Blue Pill model in Proteus VSM 8.15. I want to make its pin map similar to the physical hardware.

STM32F103C8T6 Blue Pill And Character LCD Interfacing In 8-Bit Mode Example
Schematic Diagram

 

STM32F103C8T6 Blue Pill And Character LCD Interfacing In 8-Bit Mode Example
Top View #1


STM32F103C8T6 Blue Pill And Character LCD Interfacing In 8-Bit Mode Example
Top View #2

However it can simulate like the original model. The STM32 Blue Pill supply voltage is +3.3V while the LCD module supply voltage is +5V. The USB power bus gives the module a stable +5V DC voltage. I use another micro USB cable connects to the USB header on the STM32 Blue Pill module.

STM32F103C8T6 Blue Pill And Character LCD Interfacing In 8-Bit Mode Example
Using A +3.3V Supply Voltage For The LCD Module

However we can connect these modules to +5.0V supply voltage from the ST-LINK V2 debugger. As shown in the picture above, using a +3.3V supply for LCD module causing an insufficient power that make the display data unclear.


No comments:

Post a Comment

Search This Blog

Labels

25AA010A (1) 8051 (7) 93AA46B (1) ADC (30) Analog Comparator (1) Arduino (15) ARM (6) AT89C52 (7) ATMega32 (56) AVR (57) CCS PICC (28) DAC (1) DHT11 (2) Display (105) Distance Sensor (3) DS18B20 (3) dsPIC (2) dsPIC30F1010 (2) EEPROM (5) Environment Sensor (4) esp8266 (1) I2C (29) Input/Output (67) Interrupt (19) Keil (5) Keypad (10) LCD (47) Master/Slave (1) MAX7221 (1) MCP23017 (5) MCP23S17 (4) Meter (3) MikroC (2) Motor (15) MPLABX (71) Nokia 5110 LCD (3) OLED (2) One-Wire (6) Oscillator (8) PCB (6) PCD8544 (3) PCF8574 (5) PIC (107) PIC12F (2) PIC16F628A (2) PIC16F630 (1) PIC16F716 (3) PIC16F818 (10) PIC16F818/819 (2) PIC16F84A (15) PIC16F876A (1) PIC16F877A (9) PIC16F88 (1) PIC16F887 (60) PIC18 (19) PIC18F1220 (4) PIC18F2550 (3) PIC18F4550 (12) PWM (11) RTC (8) Sensor (10) SH1106 (1) Shift Register (11) Shift Registers (3) SPI (24) STM32 (6) STM32 Blue Pill (6) STM32CubeIDE (6) STM32F103C8T6 (6) SysTick (3) temperature sensor (11) Thermometer (21) Timer/Counter (31) TM1637 (2) UART (7) Ultrasonic (4) Voltmeter (7) WDT (1) XC16 (2) XC8 (94)