728x90

728x90

Tuesday, November 18, 2025

STM32 Blue Pill and MCP23S17 16x4 LCD Example

In previous post I use the HSPI1 of the STM32F103C8T6 to interfaces with the MCP23S17 16-bit SPI I/O Expander chip. However this chip can interface with any digital I/O devices for instance a conventional HD44780 LCD or a matrix keypad.

STM32 Blue Pill and MCP23S17 16x4 LCD Example

 

In this example the MCP23S17 control an HD44780 compatible LCM at GPIOA and an 8-bit digital input switch at GPIOB. This 8-bit data reading will display on that character LCD in decimal, hexadecimal and binary format.

First we need to set the MCU parameter as follow.

STM32 Blue Pill HSPI1 and MCP23S17 GPIO Example
Clock Configuration


STM32 Blue Pill HSPI1 and MCP23S17 GPIO Example
SPI Setting

I only use the baud rate of 4Mbits per second due to data missing at the MCU SPI master data reception. However the this master MCU can transmit data up to 16Mbits per second to the MCP23S17 chip without error. 

STM32 Blue Pill and MCP23S17 16x4 LCD Example
Schematic

The NSS (or SPI slave select) pin disabled here because it's activated for every one or two data bytes packet. It is not applicable here that use three data bytes packet, slave address, register address and data to be written (SPI sequential write mode).

The HSPI port can be map into GPIO Port A or GPIO Port B. 

MCP23S17 pin latches data at logic low of its CS pin. So at start up or inactive state the master MCU must set its CS pin to logic high. 

  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. #include <stdio.h>

  23. /* Private includes ----------------------------------------------------------*/
  24. /* USER CODE BEGIN Includes */

  25. /* USER CODE END Includes */

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

  28. /* USER CODE END PTD */

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

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

  34. /* USER CODE END PM */

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

  37. /* USER CODE BEGIN PV */

  38. /* USER CODE END PV */

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

  44. /*mcp23x17 registers at bank0*/
  45. #define IODIRA_0 0x00
  46. #define IODIRB_0 0x01
  47. #define IPOLA_0 0x02
  48. #define IPOLB_0 0x03
  49. #define GPINTENA 0x04
  50. #define GPINTENB 0x05
  51. #define GPPUA_0 0x0C
  52. #define GPPUB_0 0x0D
  53. #define GPIOA_0 0x12
  54. #define GPIOB_0 0x13
  55. #define OLATA_0 0x14
  56. #define OLATB_0 0x15

  57. void mcp23s17_send(uint8_t address, uint8_t data){
  58. uint8_t temp[3];
  59. temp[0]=0x40;
  60. temp[1]=address;
  61. temp[2]=data;
  62. HAL_GPIO_WritePin(GPIOA,GPIO_PIN_4,0);
  63. HAL_SPI_Transmit(&hspi1, temp, 3, 1);
  64. HAL_GPIO_WritePin(GPIOA,GPIO_PIN_4,1);
  65. }

  66. uint8_t mcp23s17_receive(uint8_t addr){

  67. uint8_t temp[2], data=0;
  68. temp[0]=0x41;
  69. temp[1]=addr;

  70. HAL_GPIO_WritePin(GPIOA,GPIO_PIN_4,0);
  71. HAL_SPI_Transmit(&hspi1, temp, 2, 1);
  72. HAL_SPI_Receive(&hspi1,&data,1,1);
  73. HAL_GPIO_WritePin(GPIOA,GPIO_PIN_4,1);

  74. return data;
  75. }

  76. uint8_t DPORT;

  77. void delay1(uint16_t dTime){
  78. for(uint16_t i=0;i<dTime;i++);
  79. }

  80. void delay2(uint8_t dTime){
  81. for(uint8_t i=0;i<dTime;i++) delay1(5000);
  82. }

  83. void en(void){
  84. HAL_GPIO_WritePin(GPIOA,GPIO_PIN_4,GPIO_PIN_SET);
  85. //delay1(10);
  86. HAL_GPIO_WritePin(GPIOA,GPIO_PIN_4,GPIO_PIN_RESET);
  87. //delay1(10);
  88. }

  89. void lcdCmd(uint8_t cmd){
  90. uint8_t temp=0x08;
  91. DPORT=temp|(cmd&0xF0);
  92. mcp23s17_send(OLATA_0,DPORT);
  93. en();
  94. //delay1(10);
  95. temp=0;
  96. DPORT=temp|(cmd&0xF0);
  97. mcp23s17_send(OLATA_0,DPORT);
  98. en();

  99. temp=0x08;
  100. DPORT=temp|(cmd<<4);
  101. mcp23s17_send(OLATA_0,DPORT);
  102. en();
  103. //delay1(10);
  104. temp=0;
  105. DPORT=temp|(cmd<<4);
  106. mcp23s17_send(OLATA_0,DPORT);
  107. en();
  108. }

  109. void lcdDat(uint8_t dat){
  110. uint8_t temp=0x0A;
  111. DPORT=temp|(dat&0xF0);
  112. mcp23s17_send(OLATA_0,DPORT);
  113. en();
  114. //delay1(10);
  115. temp=0x02;
  116. DPORT=temp|(dat&0xF0);
  117. mcp23s17_send(OLATA_0,DPORT);
  118. en();

  119. temp=0x0A;
  120. DPORT=temp|(dat<<4);
  121. mcp23s17_send(OLATA_0,DPORT);
  122. en();
  123. //delay1(10);
  124. temp=0x02;
  125. DPORT=temp|(dat<<4);
  126. mcp23s17_send(OLATA_0,DPORT);
  127. en();
  128. }

  129. void lcdInit(void){
  130. DPORT=0x00;
  131. delay1(2000);
  132. mcp23s17_send(IODIRA_0,0x00);
  133. lcdCmd(0x33);
  134. delay1(100);
  135. lcdCmd(0x32);
  136. delay1(100);
  137. lcdCmd(0x28);
  138. delay1(100);
  139. lcdCmd(0x0F);
  140. delay1(100);
  141. lcdCmd(0x01);
  142. delay2(100);
  143. lcdCmd(0x06);
  144. delay1(100);
  145. }

  146. void lcdStr(uint8_t *str){
  147. while(*str) lcdDat(*str++);
  148. }

  149. void lcdXY(uint8_t x,uint8_t y){
  150. // 20x4 LCD
  151. //uint8_t tbe[]={0x80,0xC0,0x94,0xD4};
  152. // 16x4 LCD
  153. uint8_t tbe[]={0x80,0xC0,0x90,0xD0};
  154. lcdCmd(tbe[y-1]+x-1);
  155. delay1(100);
  156. }

  157. void lcdClear(void){
  158. lcdCmd(0x01);
  159. delay2(100);
  160. }

  161. int main(void)
  162. {
  163. /* USER CODE BEGIN 1 */

  164. /* USER CODE END 1 */

  165. /* MCU Configuration--------------------------------------------------------*/

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

  168. /* USER CODE BEGIN Init */

  169. /* USER CODE END Init */

  170. /* Configure the system clock */
  171. SystemClock_Config();

  172. /* USER CODE BEGIN SysInit */

  173. /* USER CODE END SysInit */

  174. /* Initialize all configured peripherals */
  175. MX_GPIO_Init();
  176. MX_SPI1_Init();

  177. HAL_SPI_Init(&hspi1);
  178. /*GPIOB As Inputs With Pull Ups*/
  179. mcp23s17_send(IODIRB_0,0xFF);
  180. mcp23s17_send(GPPUB_0,0xFF);

  181. lcdInit();
  182. lcdClear();
  183. lcdCmd(0x0C);

  184. lcdXY(3,1);
  185. lcdStr("Hello World!");

  186. lcdXY(3,2);
  187. lcdStr("STM32F103C8T6 ");

  188. lcdXY(1,3);
  189. lcdStr("HSPI1 MCP23S17");

  190. lcdXY(1,4);
  191. lcdStr("TC1604A-01R LCD");

  192. HAL_Delay(3000);
  193. lcdClear();

  194. lcdXY(1,1);
  195. lcdStr("STM32CubeIDE");

  196. lcdXY(1,2);
  197. lcdStr("version 1.6.0");

  198. lcdXY(1,3);
  199. lcdStr("HAL Library...");

  200. lcdXY(1,4);
  201. lcdStr("18/11/2025");

  202. HAL_Delay(3000);
  203. lcdClear();

  204. char dec[3],hex[4],bin[8],input=0,input_old=1;
  205. lcdXY(1,1); lcdStr("GPB 8-Bit Input: ");
  206. lcdXY(1,2); lcdStr("Decimal: ");
  207. lcdXY(1,3); lcdStr("Hexadecimal:");
  208. lcdXY(1,4); lcdStr("Binary: ");
  209. while (1)
  210. {
  211. input = mcp23s17_receive(GPIOB_0);
  212. if(input!=input_old){
  213. sprintf(&dec, "%3d",input);
  214. lcdXY(13,2); lcdStr(dec);
  215. sprintf(&hex, "0x%02X",input);
  216. lcdXY(13,3); lcdStr(hex);
  217. for(int8_t i=0;i<8;i++){
  218. if((input&(1<<i))==0) bin[7-i]='0';
  219. else bin[7-i]='1';
  220. }
  221. lcdXY(9,4); lcdStr(bin);
  222. }
  223. input_old=input;
  224. }
  225. /* USER CODE END 3 */
  226. }

  227. /**
  228. * @brief System Clock Configuration
  229. * @retval None
  230. */
  231. void SystemClock_Config(void)
  232. {
  233. RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  234. RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};

  235. /** Initializes the RCC Oscillators according to the specified parameters
  236. * in the RCC_OscInitTypeDef structure.
  237. */
  238. RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
  239. RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  240. RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
  241. RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  242. RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI_DIV2;
  243. RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL16;
  244. if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  245. {
  246. Error_Handler();
  247. }
  248. /** Initializes the CPU, AHB and APB buses clocks
  249. */
  250. RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  251. |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  252. RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  253. RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  254. RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
  255. RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;

  256. if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
  257. {
  258. Error_Handler();
  259. }
  260. }

  261. /**
  262. * @brief SPI1 Initialization Function
  263. * @param None
  264. * @retval None
  265. */
  266. static void MX_SPI1_Init(void)
  267. {

  268. /* USER CODE BEGIN SPI1_Init 0 */

  269. /* USER CODE END SPI1_Init 0 */

  270. /* USER CODE BEGIN SPI1_Init 1 */

  271. /* USER CODE END SPI1_Init 1 */
  272. /* SPI1 parameter configuration*/
  273. hspi1.Instance = SPI1;
  274. hspi1.Init.Mode = SPI_MODE_MASTER;
  275. hspi1.Init.Direction = SPI_DIRECTION_2LINES;
  276. hspi1.Init.DataSize = SPI_DATASIZE_8BIT;
  277. hspi1.Init.CLKPolarity = SPI_POLARITY_LOW;
  278. hspi1.Init.CLKPhase = SPI_PHASE_1EDGE;
  279. hspi1.Init.NSS = SPI_NSS_SOFT;
  280. hspi1.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_16;
  281. hspi1.Init.FirstBit = SPI_FIRSTBIT_MSB;
  282. hspi1.Init.TIMode = SPI_TIMODE_DISABLE;
  283. hspi1.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
  284. hspi1.Init.CRCPolynomial = 10;
  285. if (HAL_SPI_Init(&hspi1) != HAL_OK)
  286. {
  287. Error_Handler();
  288. }
  289. /* USER CODE BEGIN SPI1_Init 2 */

  290. /* USER CODE END SPI1_Init 2 */

  291. }

  292. /**
  293. * @brief GPIO Initialization Function
  294. * @param None
  295. * @retval None
  296. */
  297. static void MX_GPIO_Init(void)
  298. {
  299. GPIO_InitTypeDef GPIO_InitStruct = {0};

  300. /* GPIO Ports Clock Enable */
  301. __HAL_RCC_GPIOA_CLK_ENABLE();

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

  304. /*Configure GPIO pin : PA4 */
  305. GPIO_InitStruct.Pin = GPIO_PIN_4;
  306. GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  307. GPIO_InitStruct.Pull = GPIO_NOPULL;
  308. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
  309. HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

  310. }

  311. /* USER CODE BEGIN 4 */

  312. /* USER CODE END 4 */

  313. /**
  314. * @brief This function is executed in case of error occurrence.
  315. * @retval None
  316. */
  317. void Error_Handler(void)
  318. {
  319. /* USER CODE BEGIN Error_Handler_Debug */
  320. /* User can add his own implementation to report the HAL error return state */
  321. __disable_irq();
  322. while (1)
  323. {
  324. }
  325. /* USER CODE END Error_Handler_Debug */
  326. }

  327. #ifdef USE_FULL_ASSERT
  328. /**
  329. * @brief Reports the name of the source file and the source line number
  330. * where the assert_param error has occurred.
  331. * @param file: pointer to the source file name
  332. * @param line: assert_param error line source number
  333. * @retval None
  334. */
  335. void assert_failed(uint8_t *file, uint32_t line)
  336. {
  337. /* USER CODE BEGIN 6 */
  338. /* User can add his own implementation to report the file name and line number,
  339. ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  340. /* USER CODE END 6 */
  341. }
  342. #endif /* USE_FULL_ASSERT */

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

 

The simulation process in Proteus VSM is slow. However I think I can test this program in physical hardware whenever I have this SPI chip. 

 

STM32 Blue Pill and MCP23S17 16x4 LCD Example

STM32 Blue Pill and MCP23S17 16x4 LCD Example

STM32 Blue Pill and MCP23S17 16x4 LCD Example

STM32 Blue Pill and MCP23S17 16x4 LCD Example

STM32 Blue Pill and MCP23S17 16x4 LCD Example

Click here to download its source file. 


 

No comments:

Post a Comment

320x50

Search This Blog

tyro-728x90