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.
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.
![]() |
| 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.
![]() |
| 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.
![]() |
| 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.
![]() |
| 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.
- /* USER CODE BEGIN Header */
- /**
- ******************************************************************************
- * @file : main.c
- * @brief : Main program body
- ******************************************************************************
- * @attention
- *
- * <h2><center>© Copyright (c) 2025 STMicroelectronics.
- * All rights reserved.</center></h2>
- *
- * This software component is licensed by ST under BSD 3-Clause license,
- * the "License"; You may not use this file except in compliance with the
- * License. You may obtain a copy of the License at:
- * opensource.org/licenses/BSD-3-Clause
- *
- ******************************************************************************
- */
- /* USER CODE END Header */
- /* Includes ------------------------------------------------------------------*/
- #include "main.h"
- #include "lcd4bits.h"
- RTC_HandleTypeDef hrtc;
- /* Private function prototypes -----------------------------------------------*/
- void SystemClock_Config(void);
- static void MX_GPIO_Init(void);
- static void MX_RTC_Init(void);
- uint8_t select=0, old_seconds=0, counter=0;
- char sDate[17],sTime[16];
- RTC_DateTypeDef rtc_date;
- RTC_TimeTypeDef rtc_time;
- /**
- * @brief The application entry point.
- * @retval int
- */
- int main(void)
- {
- /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
- HAL_Init();
- /* Configure the system clock */
- SystemClock_Config();
- /* Initialize all configured peripherals */
- MX_GPIO_Init();
- MX_RTC_Init();
- lcdInit();
- lcdStr(" STM32F401CCU6");
- lcdXY(1,2);
- lcdStr("LSE RTC Example");
- HAL_Delay(3000);
- lcdClear();
- //Cursor OFF
- lcdCmd(0x0C);
- HAL_Delay(5);
- /* Infinite loop */
- /* USER CODE BEGIN WHILE */
- while (1)
- {
- HAL_RTC_GetTime(&hrtc,&rtc_time,RTC_FORMAT_BIN);
- HAL_RTC_GetDate(&hrtc,&rtc_date,RTC_FORMAT_BIN);
- sprintf(sDate,"Date: %04d/%02d/%02d",2000+rtc_date.Year,
- rtc_date.Month,rtc_date.Date);
- sprintf(sTime,"Time: %02d:%02d:%02d",rtc_time.Hours,
- rtc_time.Minutes,rtc_time.Seconds);
- lcdXY(1,1); lcdStr(sDate);
- lcdXY(1,2); lcdStr(sTime);
- if(select!=0){
- lcdCmd(0x0F);
- RTC_Select();
- RTC_Adjust();
- counter++;
- if(counter>=100) { counter=0; select=0; lcdCmd(0x0C); }
- }
- if(HAL_GPIO_ReadPin(SELECT_GPIO_Port, SELECT_Pin)==0) {
- select++;
- counter=0;
- }
- HAL_Delay(250);
- }
- /* USER CODE END 3 */
- }
- void RTC_Set(void){
- RTC_TimeTypeDef sTime;
- RTC_DateTypeDef sDate ;
- hrtc.Instance = RTC;
- hrtc.Init.HourFormat = RTC_HOURFORMAT_24;
- hrtc.Init.AsynchPrediv = 127;
- hrtc.Init.SynchPrediv = 255;
- hrtc.Init.OutPut = RTC_OUTPUT_DISABLE;
- hrtc.Init.OutPutPolarity = RTC_OUTPUT_POLARITY_HIGH;
- hrtc.Init.OutPutType = RTC_OUTPUT_TYPE_OPENDRAIN;
- if (HAL_RTC_Init(&hrtc) != HAL_OK)
- {
- Error_Handler();
- }
- sDate.WeekDay = RTC_WEEKDAY_SATURDAY;
- sDate.Month = rtc_date.Month;
- sDate.Date = rtc_date.Date;
- sDate.Year = rtc_date.Year;
- if (HAL_RTC_SetDate(&hrtc, &sDate, RTC_FORMAT_BIN) != HAL_OK)
- {
- Error_Handler();
- }
- sTime.Hours = rtc_time.Hours;
- sTime.Minutes = rtc_time.Minutes;
- sTime.Seconds = rtc_time.Seconds;
- sTime.DayLightSaving = RTC_DAYLIGHTSAVING_NONE;
- sTime.StoreOperation = RTC_STOREOPERATION_SET;
- if (HAL_RTC_SetTime(&hrtc, &sTime, RTC_FORMAT_BIN) != HAL_OK)
- {
- Error_Handler();
- }
- }
- void RTC_Select(void){
- switch(select){
- case 1: lcdXY(10,1); break;
- case 2: lcdXY(13,1); break;
- case 3: lcdXY(16,1); break;
- case 4: lcdXY(8,2); break;
- case 5: lcdXY(11,2); break;
- case 6: lcdXY(14,2); break;
- case 7: select=0; lcdCmd(0x0C); break;
- }
- }
- void RTC_Adjust(void){
- switch(select){
- case 1:
- if(HAL_GPIO_ReadPin(GPIOB,UP_Pin)==0){
- if(rtc_date.Year<99) rtc_date.Year++;
- RTC_Set(); counter=0; HAL_Delay(200);
- }
- if(HAL_GPIO_ReadPin(GPIOB,DOWN_Pin)==0){
- if(rtc_date.Year>1) rtc_date.Year--;
- RTC_Set(); counter=0; HAL_Delay(200);
- }
- break;
- case 2:
- if(HAL_GPIO_ReadPin(GPIOB,UP_Pin)==0){
- if(rtc_date.Month<12) rtc_date.Month++;
- RTC_Set(); counter=0; HAL_Delay(200);
- }
- if(HAL_GPIO_ReadPin(GPIOB,DOWN_Pin)==0){
- if(rtc_date.Month>1) rtc_date.Month--;
- RTC_Set(); counter=0; HAL_Delay(200);
- }
- break;
- case 3:
- if(HAL_GPIO_ReadPin(GPIOB,UP_Pin)==0){
- if(rtc_date.Date<30) rtc_date.Date++;
- RTC_Set(); counter=0; HAL_Delay(200);
- }
- if(HAL_GPIO_ReadPin(GPIOB,DOWN_Pin)==0){
- if(rtc_date.Date>1) rtc_date.Date--;
- RTC_Set(); counter=0; HAL_Delay(200);
- }
- break;
- case 4:
- if(HAL_GPIO_ReadPin(GPIOB,UP_Pin)==0){
- if(rtc_time.Hours<23) rtc_time.Hours++;
- RTC_Set(); counter=0; HAL_Delay(200);
- }
- if(HAL_GPIO_ReadPin(GPIOB,DOWN_Pin)==0){
- if(rtc_time.Hours>1) rtc_time.Hours--;
- RTC_Set(); counter=0; HAL_Delay(200);
- }
- break;
- case 5:
- if(HAL_GPIO_ReadPin(GPIOB,UP_Pin)==0){
- if(rtc_time.Minutes<59) rtc_time.Minutes++;
- RTC_Set(); counter=0;
- HAL_Delay(200);
- }
- if(HAL_GPIO_ReadPin(GPIOB,DOWN_Pin)==0){
- if(rtc_time.Minutes>1) rtc_time.Minutes--;
- RTC_Set(); counter=0; HAL_Delay(200);
- }
- break;
- case 6:
- if(HAL_GPIO_ReadPin(GPIOB,UP_Pin)==0){
- if(rtc_time.Seconds<59) rtc_time.Seconds++;
- RTC_Set(); counter=0; HAL_Delay(100);
- }
- if(HAL_GPIO_ReadPin(GPIOB,DOWN_Pin)==0){
- if(rtc_time.Seconds>1) rtc_time.Seconds--;
- RTC_Set(); counter=0; HAL_Delay(100);
- }
- break;
- }
- }
- /**
- * @brief System Clock Configuration
- * @retval None
- */
- void SystemClock_Config(void)
- {
- RCC_OscInitTypeDef RCC_OscInitStruct = {0};
- RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
- RCC_PeriphCLKInitTypeDef PeriphClkInitStruct = {0};
- /** Configure the main internal regulator output voltage
- */
- __HAL_RCC_PWR_CLK_ENABLE();
- __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE2);
- /** Initializes the RCC Oscillators according to the specified parameters
- * in the RCC_OscInitTypeDef structure.
- */
- RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI|RCC_OSCILLATORTYPE_LSE;
- RCC_OscInitStruct.LSEState = RCC_LSE_ON;
- RCC_OscInitStruct.HSIState = RCC_HSI_ON;
- RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
- RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
- if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
- {
- Error_Handler();
- }
- /** Initializes the CPU, AHB and APB buses clocks
- */
- RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
- |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
- RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSI;
- RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
- RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
- RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
- if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK)
- {
- Error_Handler();
- }
- PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_RTC;
- PeriphClkInitStruct.RTCClockSelection = RCC_RTCCLKSOURCE_LSE;
- if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct) != HAL_OK)
- {
- Error_Handler();
- }
- }
- /**
- * @brief RTC Initialization Function
- * @param None
- * @retval None
- */
- static void MX_RTC_Init(void)
- {
- /* USER CODE BEGIN RTC_Init 0 */
- /* USER CODE END RTC_Init 0 */
- RTC_TimeTypeDef sTime = {0};
- RTC_DateTypeDef sDate = {0};
- /* USER CODE BEGIN RTC_Init 1 */
- /* USER CODE END RTC_Init 1 */
- /** Initialize RTC Only
- */
- hrtc.Instance = RTC;
- hrtc.Init.HourFormat = RTC_HOURFORMAT_24;
- hrtc.Init.AsynchPrediv = 127;
- hrtc.Init.SynchPrediv = 255;
- hrtc.Init.OutPut = RTC_OUTPUT_DISABLE;
- hrtc.Init.OutPutPolarity = RTC_OUTPUT_POLARITY_HIGH;
- hrtc.Init.OutPutType = RTC_OUTPUT_TYPE_OPENDRAIN;
- if (HAL_RTC_Init(&hrtc) != HAL_OK)
- {
- Error_Handler();
- }
- /* USER CODE BEGIN Check_RTC_BKUP */
- /* USER CODE END Check_RTC_BKUP */
- /** Initialize RTC and set the Time and Date
- */
- sTime.Hours = 18;
- sTime.Minutes = 20;
- sTime.Seconds = 30;
- sTime.DayLightSaving = RTC_DAYLIGHTSAVING_NONE;
- sTime.StoreOperation = RTC_STOREOPERATION_SET;
- if (HAL_RTC_SetTime(&hrtc, &sTime, RTC_FORMAT_BIN) != HAL_OK)
- {
- Error_Handler();
- }
- sDate.WeekDay = RTC_WEEKDAY_SATURDAY;
- sDate.Month = RTC_MONTH_NOVEMBER;
- sDate.Date = 8;
- sDate.Year = 25;
- if (HAL_RTC_SetDate(&hrtc, &sDate, RTC_FORMAT_BIN) != HAL_OK)
- {
- Error_Handler();
- }
- /* USER CODE BEGIN RTC_Init 2 */
- /* USER CODE END RTC_Init 2 */
- }
- /**
- * @brief GPIO Initialization Function
- * @param None
- * @retval None
- */
- static void MX_GPIO_Init(void)
- {
- GPIO_InitTypeDef GPIO_InitStruct = {0};
- /* GPIO Ports Clock Enable */
- __HAL_RCC_GPIOC_CLK_ENABLE();
- __HAL_RCC_GPIOA_CLK_ENABLE();
- __HAL_RCC_GPIOB_CLK_ENABLE();
- /*Configure GPIO pin Output Level */
- HAL_GPIO_WritePin(GPIOA, GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_4|GPIO_PIN_5
- |GPIO_PIN_6|GPIO_PIN_7, GPIO_PIN_RESET);
- /*Configure GPIO pins : PA0 PA1 PA4 PA5
- PA6 PA7 */
- GPIO_InitStruct.Pin = GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_4|GPIO_PIN_5
- |GPIO_PIN_6|GPIO_PIN_7;
- GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
- GPIO_InitStruct.Pull = GPIO_NOPULL;
- GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
- HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
- /*Configure GPIO pins : SELECT_Pin UP_Pin DOWN_Pin */
- GPIO_InitStruct.Pin = SELECT_Pin|UP_Pin|DOWN_Pin;
- GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
- GPIO_InitStruct.Pull = GPIO_PULLUP;
- HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
- }
- /* USER CODE BEGIN 4 */
- /* USER CODE END 4 */
- /**
- * @brief This function is executed in case of error occurrence.
- * @retval None
- */
- void Error_Handler(void)
- {
- /* USER CODE BEGIN Error_Handler_Debug */
- /* User can add his own implementation to report the HAL error return state */
- __disable_irq();
- while (1)
- {
- }
- /* USER CODE END Error_Handler_Debug */
- }
- #ifdef USE_FULL_ASSERT
- /**
- * @brief Reports the name of the source file and the source line number
- * where the assert_param error has occurred.
- * @param file: pointer to the source file name
- * @param line: assert_param error line source number
- * @retval None
- */
- void assert_failed(uint8_t *file, uint32_t line)
- {
- /* USER CODE BEGIN 6 */
- /* User can add his own implementation to report the file name and line number,
- ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
- /* USER CODE END 6 */
- }
- #endif /* USE_FULL_ASSERT */
- /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
Tesing this program in Proteus is very easy without abruption.
![]() |
| Proteus Simulation #1 |
![]() |
| Simulation #2 |
I prototype this STM32 Black Pill board on my bread board withing a short time. It works fine.
![]() |
| Breadboard Prototype #1 |
![]() |
| Breadboard Prototype #2 |
Click here to download its source file.








No comments:
Post a Comment