728x90

728x90

Saturday, November 8, 2025

STM32F401CCU6 LSE Real Time Clock LCD Example

Overview

The STM32F4 has an internal Real Time Clock (RTC) module that can generate date, time, alarm etc. This module can driven from its internal 32KHz oscillator or even an external precise 32.768KHz crystal oscillator.

STM32F401CCU6 LSE Real Time Clock LCD Example

 

STM32CubeIDE Programming

The STM32F401CCU6 Black Pill has pins-compatible the STM32 Blu Pill (STM32F103CxTx). But I use the specific device in schematic design. Proteus VSM 8.19 also able to simulate the firmware.

STM32F401CCU6 LSE Real Time Clock LCD Example
Schematic Design in Proteus VSM

 

I use the STM32CubeIDE to write its C source codes. Its code generation wizard is very user-friendly and time-saving. First I need to configure the following blocks,

  • Input/Output pins selection
  • Select its 16MHz HSI Clock
  • Enable the LSE clock source 
  • Select its external precise 32.768KHz crystal oscillator
  • Enable the RTC in Timer Tab

Then click on generate source codes.

1. GPIO Setting 

I select GPIOA for 4-bit LCD connections and GPIOB for input switches.

STM32F401CCU6 LSE Real Time Clock LCD Example
STM32CubeIDE GPIO Setting for the STM32F401CCU6
 

It has internal pull-ups resistors for GPIO input mode. However I need to add external pull-up resistors around 3.9kOhm to the input buttons.

2. RCC 

I enable the Low Speed Clock (LSE) that's already on the STM32 Black Pill board with a frequency of 32.768KHz. However I select the 16MHz HSI clock source for main micro-controller clock.

STM32F401CCU6 LSE Real Time Clock LCD Example
STM32CubeIDE RCC Setting for the STM32F401CCU6

 3. RTC

I enable the internal Real Time Clock (RTC) and Calendar. The date and time data format are in binary that is easy to adjust.

STM32F401CCU6 LSE Real Time Clock LCD Example
STM32CubeIDE RTC setting for the STM32F401CCU6

Then click on the generate source code to get its skeleton C program with our proffered setting.

I need to add the HD44780 4-bit LCD driver in project Inc and Src directories before it can be called in C main program.

I add my own source code to the main.c file as follow.

  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 "lcd4bits.h"

  23. RTC_HandleTypeDef hrtc;

  24. /* Private function prototypes -----------------------------------------------*/
  25. void SystemClock_Config(void);
  26. static void MX_GPIO_Init(void);
  27. static void MX_RTC_Init(void);

  28. uint8_t select=0, old_seconds=0, counter=0;
  29. char sDate[17],sTime[16];
  30. RTC_DateTypeDef rtc_date;
  31. RTC_TimeTypeDef rtc_time;


  32. /**
  33. * @brief The application entry point.
  34. * @retval int
  35. */
  36. int main(void)
  37. {

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

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

  42. /* Initialize all configured peripherals */
  43. MX_GPIO_Init();

  44. MX_RTC_Init();


  45. lcdInit();
  46. lcdStr(" STM32F401CCU6");
  47. lcdXY(1,2);
  48. lcdStr("LSE RTC Example");
  49. HAL_Delay(3000);
  50. lcdClear();
  51. //Cursor OFF
  52. lcdCmd(0x0C);
  53. HAL_Delay(5);

  54. /* Infinite loop */
  55. /* USER CODE BEGIN WHILE */
  56. while (1)
  57. {
  58. HAL_RTC_GetTime(&hrtc,&rtc_time,RTC_FORMAT_BIN);
  59. HAL_RTC_GetDate(&hrtc,&rtc_date,RTC_FORMAT_BIN);

  60. sprintf(sDate,"Date: %04d/%02d/%02d",2000+rtc_date.Year,
  61. rtc_date.Month,rtc_date.Date);
  62. sprintf(sTime,"Time: %02d:%02d:%02d",rtc_time.Hours,
  63. rtc_time.Minutes,rtc_time.Seconds);

  64. lcdXY(1,1); lcdStr(sDate);
  65. lcdXY(1,2); lcdStr(sTime);

  66. if(select!=0){
  67. lcdCmd(0x0F);
  68. RTC_Select();
  69. RTC_Adjust();
  70. counter++;
  71. if(counter>=100) { counter=0; select=0; lcdCmd(0x0C); }
  72. }

  73. if(HAL_GPIO_ReadPin(SELECT_GPIO_Port, SELECT_Pin)==0) {
  74. select++;
  75. counter=0;
  76. }

  77. HAL_Delay(250);

  78. }
  79. /* USER CODE END 3 */
  80. }


  81. void RTC_Set(void){
  82. RTC_TimeTypeDef sTime;
  83. RTC_DateTypeDef sDate ;

  84. hrtc.Instance = RTC;
  85. hrtc.Init.HourFormat = RTC_HOURFORMAT_24;
  86. hrtc.Init.AsynchPrediv = 127;
  87. hrtc.Init.SynchPrediv = 255;
  88. hrtc.Init.OutPut = RTC_OUTPUT_DISABLE;
  89. hrtc.Init.OutPutPolarity = RTC_OUTPUT_POLARITY_HIGH;
  90. hrtc.Init.OutPutType = RTC_OUTPUT_TYPE_OPENDRAIN;
  91. if (HAL_RTC_Init(&hrtc) != HAL_OK)
  92. {
  93. Error_Handler();
  94. }

  95. sDate.WeekDay = RTC_WEEKDAY_SATURDAY;
  96. sDate.Month = rtc_date.Month;
  97. sDate.Date = rtc_date.Date;
  98. sDate.Year = rtc_date.Year;

  99. if (HAL_RTC_SetDate(&hrtc, &sDate, RTC_FORMAT_BIN) != HAL_OK)
  100. {
  101. Error_Handler();
  102. }

  103. sTime.Hours = rtc_time.Hours;
  104. sTime.Minutes = rtc_time.Minutes;
  105. sTime.Seconds = rtc_time.Seconds;
  106. sTime.DayLightSaving = RTC_DAYLIGHTSAVING_NONE;
  107. sTime.StoreOperation = RTC_STOREOPERATION_SET;
  108. if (HAL_RTC_SetTime(&hrtc, &sTime, RTC_FORMAT_BIN) != HAL_OK)
  109. {
  110. Error_Handler();
  111. }
  112. }

  113. void RTC_Select(void){

  114. switch(select){
  115. case 1: lcdXY(10,1); break;
  116. case 2: lcdXY(13,1); break;
  117. case 3: lcdXY(16,1); break;
  118. case 4: lcdXY(8,2); break;
  119. case 5: lcdXY(11,2); break;
  120. case 6: lcdXY(14,2); break;
  121. case 7: select=0; lcdCmd(0x0C); break;
  122. }
  123. }

  124. void RTC_Adjust(void){

  125. switch(select){
  126. case 1:
  127. if(HAL_GPIO_ReadPin(GPIOB,UP_Pin)==0){
  128. if(rtc_date.Year<99) rtc_date.Year++;
  129. RTC_Set(); counter=0; HAL_Delay(200);
  130. }
  131. if(HAL_GPIO_ReadPin(GPIOB,DOWN_Pin)==0){
  132. if(rtc_date.Year>1) rtc_date.Year--;
  133. RTC_Set(); counter=0; HAL_Delay(200);
  134. }
  135. break;
  136. case 2:
  137. if(HAL_GPIO_ReadPin(GPIOB,UP_Pin)==0){
  138. if(rtc_date.Month<12) rtc_date.Month++;
  139. RTC_Set(); counter=0; HAL_Delay(200);
  140. }
  141. if(HAL_GPIO_ReadPin(GPIOB,DOWN_Pin)==0){
  142. if(rtc_date.Month>1) rtc_date.Month--;
  143. RTC_Set(); counter=0; HAL_Delay(200);
  144. }
  145. break;
  146. case 3:
  147. if(HAL_GPIO_ReadPin(GPIOB,UP_Pin)==0){
  148. if(rtc_date.Date<30) rtc_date.Date++;
  149. RTC_Set(); counter=0; HAL_Delay(200);
  150. }
  151. if(HAL_GPIO_ReadPin(GPIOB,DOWN_Pin)==0){
  152. if(rtc_date.Date>1) rtc_date.Date--;
  153. RTC_Set(); counter=0; HAL_Delay(200);
  154. }
  155. break;
  156. case 4:
  157. if(HAL_GPIO_ReadPin(GPIOB,UP_Pin)==0){
  158. if(rtc_time.Hours<23) rtc_time.Hours++;
  159. RTC_Set(); counter=0; HAL_Delay(200);
  160. }
  161. if(HAL_GPIO_ReadPin(GPIOB,DOWN_Pin)==0){
  162. if(rtc_time.Hours>1) rtc_time.Hours--;
  163. RTC_Set(); counter=0; HAL_Delay(200);
  164. }
  165. break;
  166. case 5:
  167. if(HAL_GPIO_ReadPin(GPIOB,UP_Pin)==0){
  168. if(rtc_time.Minutes<59) rtc_time.Minutes++;
  169. RTC_Set(); counter=0;
  170. HAL_Delay(200);
  171. }
  172. if(HAL_GPIO_ReadPin(GPIOB,DOWN_Pin)==0){
  173. if(rtc_time.Minutes>1) rtc_time.Minutes--;
  174. RTC_Set(); counter=0; HAL_Delay(200);
  175. }
  176. break;
  177. case 6:
  178. if(HAL_GPIO_ReadPin(GPIOB,UP_Pin)==0){
  179. if(rtc_time.Seconds<59) rtc_time.Seconds++;
  180. RTC_Set(); counter=0; HAL_Delay(100);
  181. }
  182. if(HAL_GPIO_ReadPin(GPIOB,DOWN_Pin)==0){
  183. if(rtc_time.Seconds>1) rtc_time.Seconds--;
  184. RTC_Set(); counter=0; HAL_Delay(100);
  185. }
  186. break;

  187. }
  188. }

  189. /**
  190. * @brief System Clock Configuration
  191. * @retval None
  192. */
  193. void SystemClock_Config(void)
  194. {
  195. RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  196. RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
  197. RCC_PeriphCLKInitTypeDef PeriphClkInitStruct = {0};

  198. /** Configure the main internal regulator output voltage
  199. */
  200. __HAL_RCC_PWR_CLK_ENABLE();
  201. __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE2);
  202. /** Initializes the RCC Oscillators according to the specified parameters
  203. * in the RCC_OscInitTypeDef structure.
  204. */
  205. RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI|RCC_OSCILLATORTYPE_LSE;
  206. RCC_OscInitStruct.LSEState = RCC_LSE_ON;
  207. RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  208. RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
  209. RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
  210. if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  211. {
  212. Error_Handler();
  213. }
  214. /** Initializes the CPU, AHB and APB buses clocks
  215. */
  216. RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  217. |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  218. RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSI;
  219. RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  220. RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
  221. RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;

  222. if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK)
  223. {
  224. Error_Handler();
  225. }
  226. PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_RTC;
  227. PeriphClkInitStruct.RTCClockSelection = RCC_RTCCLKSOURCE_LSE;
  228. if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct) != HAL_OK)
  229. {
  230. Error_Handler();
  231. }

  232. }

  233. /**
  234. * @brief RTC Initialization Function
  235. * @param None
  236. * @retval None
  237. */
  238. static void MX_RTC_Init(void)
  239. {

  240. /* USER CODE BEGIN RTC_Init 0 */

  241. /* USER CODE END RTC_Init 0 */

  242. RTC_TimeTypeDef sTime = {0};
  243. RTC_DateTypeDef sDate = {0};

  244. /* USER CODE BEGIN RTC_Init 1 */

  245. /* USER CODE END RTC_Init 1 */
  246. /** Initialize RTC Only
  247. */
  248. hrtc.Instance = RTC;
  249. hrtc.Init.HourFormat = RTC_HOURFORMAT_24;
  250. hrtc.Init.AsynchPrediv = 127;
  251. hrtc.Init.SynchPrediv = 255;
  252. hrtc.Init.OutPut = RTC_OUTPUT_DISABLE;
  253. hrtc.Init.OutPutPolarity = RTC_OUTPUT_POLARITY_HIGH;
  254. hrtc.Init.OutPutType = RTC_OUTPUT_TYPE_OPENDRAIN;
  255. if (HAL_RTC_Init(&hrtc) != HAL_OK)
  256. {
  257. Error_Handler();
  258. }

  259. /* USER CODE BEGIN Check_RTC_BKUP */

  260. /* USER CODE END Check_RTC_BKUP */

  261. /** Initialize RTC and set the Time and Date
  262. */
  263. sTime.Hours = 18;
  264. sTime.Minutes = 20;
  265. sTime.Seconds = 30;
  266. sTime.DayLightSaving = RTC_DAYLIGHTSAVING_NONE;
  267. sTime.StoreOperation = RTC_STOREOPERATION_SET;
  268. if (HAL_RTC_SetTime(&hrtc, &sTime, RTC_FORMAT_BIN) != HAL_OK)
  269. {
  270. Error_Handler();
  271. }
  272. sDate.WeekDay = RTC_WEEKDAY_SATURDAY;
  273. sDate.Month = RTC_MONTH_NOVEMBER;
  274. sDate.Date = 8;
  275. sDate.Year = 25;

  276. if (HAL_RTC_SetDate(&hrtc, &sDate, RTC_FORMAT_BIN) != HAL_OK)
  277. {
  278. Error_Handler();
  279. }
  280. /* USER CODE BEGIN RTC_Init 2 */

  281. /* USER CODE END RTC_Init 2 */

  282. }

  283. /**
  284. * @brief GPIO Initialization Function
  285. * @param None
  286. * @retval None
  287. */
  288. static void MX_GPIO_Init(void)
  289. {
  290. GPIO_InitTypeDef GPIO_InitStruct = {0};

  291. /* GPIO Ports Clock Enable */
  292. __HAL_RCC_GPIOC_CLK_ENABLE();
  293. __HAL_RCC_GPIOA_CLK_ENABLE();
  294. __HAL_RCC_GPIOB_CLK_ENABLE();

  295. /*Configure GPIO pin Output Level */
  296. HAL_GPIO_WritePin(GPIOA, GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_4|GPIO_PIN_5
  297. |GPIO_PIN_6|GPIO_PIN_7, GPIO_PIN_RESET);

  298. /*Configure GPIO pins : PA0 PA1 PA4 PA5
  299. PA6 PA7 */
  300. GPIO_InitStruct.Pin = GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_4|GPIO_PIN_5
  301. |GPIO_PIN_6|GPIO_PIN_7;
  302. GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  303. GPIO_InitStruct.Pull = GPIO_NOPULL;
  304. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
  305. HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

  306. /*Configure GPIO pins : SELECT_Pin UP_Pin DOWN_Pin */
  307. GPIO_InitStruct.Pin = SELECT_Pin|UP_Pin|DOWN_Pin;
  308. GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
  309. GPIO_InitStruct.Pull = GPIO_PULLUP;
  310. HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);

  311. }

  312. /* USER CODE BEGIN 4 */

  313. /* USER CODE END 4 */

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

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

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


Tesing this program in Proteus is very easy without abruption. 

STM32F401CCU6 LSE Real Time Clock LCD Example
Proteus Simulation #1

 
STM32F401CCU6 LSE Real Time Clock LCD Example
Simulation #2
 

I prototype this STM32 Black Pill board on my bread board withing a short time. It works fine.

STM32F401CCU6 LSE Real Time Clock LCD Example
Breadboard Prototype #1

STM32F401CCU6 LSE Real Time Clock LCD Example
Breadboard Prototype #2

Click here to download its source file. 


 

 

 

 

 

No comments:

Post a Comment

320x50

Search This Blog

tyro-728x90