728x90

Showing posts with label RTC. Show all posts
Showing posts with label RTC. Show all posts

Monday, February 16, 2026

ATMega644P SPI DS3234 RTC LCD

Overview

The DS3234 is a low-cost, extremely accurate SPI bus real-time clock (RTC) with an integrated temperature-compensated crystal oscillator (TCXO) and crystal. The DS3234 incorporates a precision, temperature-compensated voltage reference and comparator circuit to monitor VCC. When VCC drops below the power-fail voltage (VPF), the device asserts the RST output and also disables read and write access to the part when VCC drops below both VPF and VBAT. The RST pin is monitored as a push button input for generating a µP reset. The device switches to the backup supply input and maintains accurate timekeeping when main power to the device is interrupted. The integration of the crystal resonator enhances the long-term accuracy of the device as well as reduces the piece-part count in a manufacturing line. The DS3234 is available in commercial and industrial temperature ranges, and is offered in an industry-standard 300-mil, 20-pin SO package. 

ATMega644P SPI DS3234 RTC LCD

It does not have a DIP version due to larger footprint. But a ready to use module is widely available at very low cost. 

SparkFun DeadOn RTC Breakout - DS3234 - SparkFun Electronics

Its internal time keeping and general purpose registers are similar to the DS1307 and almost identical to DS3232 (TWI version). However its SPI interface yield a higher speed data communication than TWI.

ATMega644P SPI DS3234 RTC LCD
DS3234 Pin Configuration and Description

 Its timing data is very precise due its internal 32.678KHz crystal oscillator with programmable capacitors array.

ATMega644P SPI DS3234 RTC LCD
Block Diagram

The DS3234 provides 256 bytes of general-purpose battery-backed read/write memory. The SRAM can be written or read whenever VCC is above either VPF or VBAT.  

ATMega644P SPI DS3234 RTC LCD
 Address Map for DS3234 Timekeeping Registers and SRAM

This SPI chip doesn't have a device address. It just uses a Chip Select (CS) pin that is active low. Its R/W MSB bit of the internal SRAM address identifies between device write and read( '0' for read and '1' for write).

ATMega644P SPI DS3234 RTC LCD
DS3234 Single-Byte Write and Read Sequence

 However for a high speed multiple write or read bytes we can use its SPI mutliple-Byte Burst Transfer.

ATMega644P SPI DS3234 RTC LCD
SPI Multiple-Byte Burst Transfer

 It is very common for data reading from this chip.

ATMega644P and DS3234 Interfacing

Programming and interfacing with this chip is very easy using a simple 8-bit MCU dedicated SPI communication module. Even a low-end 8-bit micro-controller that doesn't have an SPI communication interface the programmer can emulate an SPI communication protocol to interact with this RTC chip. 

ATMega644P SPI DS3234 RTC LCD
Typical Operating Circuit

Commonly a master MCU uses only four wire SPI to make a bi-directional communication interface with this RTC chip.

ATMega644P SPI DS3234 RTC LCD
Proteus Simulation

 

ATMega644P SPI DS3234 RTC LCD
Virtual DSO Waveform

 

In this example the ATMega644P read the time and date data from the SRAM of DS3234. A character LCD show time and date on the display. Serial data inputs to MISO pin are sample at the trailing edge of each clock cycles of SCK pin to correct the data reception that is CPOL=0 and CPHA=1.

C Source Code "main.c":

  1. /*
  2. * 12-spi_ds3234_1602.c
  3. *
  4. * Created: 2/16/2026 10:52:32 PM
  5. * Author : Admin
  6. */

  7. #include <stdio.h>
  8. #include <avr/io.h>
  9. #include <util/delay.h>
  10. #define F_CPU 16000000UL

  11. #define DDR_SPI DDRB
  12. #define PRT_SPI PORTB
  13. #define DD_MOSI 5
  14. #define DD_MISO 6
  15. #define DD_SCK 7
  16. #define DD_SS 4

  17. void SPI_MasterInit(void)
  18. {
  19. /* Set MOSI and SCK output, all others input */
  20. DDR_SPI = (1<<DD_MOSI)|(1<<DD_SCK)|(1<<DD_SS);
  21. /* Enable SPI, Master, set clock rate fck/16
  22. data is sample at the falling edge of SCK*/
  23. SPCR = (1<<SPE)|(1<<MSTR)|(1<<SPR0)|(1<<CPHA);
  24. }

  25. void SPI_MasterTransmit(char cData)
  26. {
  27. /* Start transmission */
  28. SPDR = cData;
  29. /* Wait for transmission complete */
  30. while(!(SPSR & (1<<SPIF)))
  31. ;
  32. }

  33. void SPI_SlaveInit(void)
  34. {
  35. /* Set MISO output, all others input */
  36. DDR_SPI = (1<<DD_MISO);
  37. /* Enable SPI */
  38. SPCR = (1<<SPE);
  39. }

  40. char SPI_SlaveReceive(void)
  41. {
  42. /* Wait for reception complete */
  43. while(!(SPSR & (1<<SPIF)))
  44. ;
  45. /* Return Data Register */
  46. return SPDR;
  47. }

  48. int main(void)
  49. {
  50. /* Replace with your application code */
  51. SPI_MasterInit();
  52. PRT_SPI|=(1<<DD_SS); //Set CS Pin High
  53. lcd_init();
  54. lcd_text("ATMega644P AVR");
  55. lcd_xy(1,2);
  56. lcd_text("DS3234 SPI RTC");
  57. _delay_ms(5000);
  58. lcd_command(0x0C);
  59. unsigned char data[7],msg[16];
  60. while (1)
  61. {
  62. PRT_SPI&=~(1<<DD_SS);
  63. SPI_MasterTransmit(0x00);
  64. for (char i=0;i<7;i++)
  65. {
  66. SPI_MasterTransmit(0x00);
  67. data[i]=SPDR;
  68. }
  69. PRT_SPI|=(1<<DD_SS);
  70. sprintf(msg,"Date: 20%02X/%02X/%02X",data[6],data[5],data[4]);
  71. lcd_xy(1,1);
  72. lcd_text(msg);
  73. sprintf(msg,"Time: %02X:%02X:%02X",data[2],data[1],data[0]);
  74. lcd_xy(1,2);
  75. lcd_text(msg);
  76. _delay_ms(100);
  77. }
  78. }



I can not buy this chip at local store. So I only tested this SPI RTC chip using a software simulator. 

 

 

 

 

Saturday, February 7, 2026

ATMega644P TWI DS1307 MCP23017 LCD

In previous posts the ATMega644P TWI interface with the DS13017 RTC and MCP23017 GPIO expansion chip. Here I will put them altogethers. This TWI bus will drives the DS1307 RTC and the MCP23017 that controls a character LCD.

ATMega644P TWI DS1307 MCP23017 LCD 

The master microcontroller read the RTC data from ds1307 and it will send to the MCP23017 based LCD. 

Source Code:

  1. /*
  2. * 10-i2c_ds1307_mcp23017_lcd.c
  3. *
  4. * Created: 2/7/2026 7:42:39 PM
  5. * Author : Admin
  6. */

  7. #include <avr/io.h>
  8. #include <util/delay.h>
  9. #define F_CPU 16000000UL

  10. void rtc_init(void){
  11. char rtc[8]={0x30,0x00,0x17,0x07,0x31,0x01,0x26,1<<4};
  12. for (char i=0;i<8;i++)
  13. {
  14. twi_start();
  15. //D0 is DS1307 Write Address
  16. twi_write(0xD0);
  17. //Select Control Register
  18. twi_write(i);
  19. //Enable SQWE bit blinks at 1 Hz
  20. twi_write(rtc[i]);
  21. twi_stop();
  22. _delay_ms(10);
  23. }
  24. }

  25. char rtc[6], msg[16];
  26. int main(void)
  27. {
  28. /* Replace with your application code */
  29. _delay_ms(1000);
  30. lcd_init();
  31. lcd_text("ATMega644P TWI");
  32. lcd_xy(1,2);
  33. lcd_text("MCP23017 DS1307");
  34. _delay_ms(10000);
  35. lcd_clear();
  36. lcd_command(0x0C);
  37. while (1)
  38. {
  39. for(char i=0;i<7;i++){
  40. /*Second Register*/
  41. twi_start();
  42. twi_write(0xD0);
  43. /*Select Second register*/
  44. twi_write(i);
  45. twi_stop();
  46. _delay_us(10);
  47. twi_start();
  48. twi_write(0xD1);
  49. rtc[i]=twi_read(1);
  50. twi_stop();
  51. _delay_us(10);
  52. }
  53. lcd_xy(1,1);
  54. sprintf(msg,"Time: %02X:%02X:%02X",rtc[2],rtc[1],rtc[0]);
  55. lcd_text(msg);
  56. lcd_xy(1,2);
  57. sprintf(msg,"Date: %02X/%02X/20%02X",rtc[4],rtc[5],rtc[6]);
  58. lcd_text(msg);
  59. _delay_ms(500);
  60. }
  61. }


Circuit Simulation:

ATMega644P TWI DS1307 MCP23017 LCD
Circuit and Simulation

 

AVR Prototype Board

ATMega644P TWI DS1307 MCP23017 LCD

 

ATMega644P TWI DS1307 MCP23017 LCD 

This PCB was offered by PCBWay (pcbway.com).

I have been using PCBWay for many years now. PCBWay fabricate PCBs at low cost, fast processing time for only 24 hours, and fast delivery time using any carrier options. This double side 10cmx10cm can be fabricate at only 5USD for 5 to 10pcs by PCBWay. It's a standard PCB with silk screen and solder mask.

A DIY dsPIC30F2010 and dsPIC30F1010 Prototype Board with Programmer
10 PCBs for only 5USD
 

For different size of PCB we can instantly quote on PCBWay website using a zip PCB Gerber file without account.

A DIY dsPIC30F2010 and dsPIC30F1010 Prototype Board with Programmer
PCBWay Instant Quote

 

Click here to download this example

For a full ATMega644P tutorials please visit this page.

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. 


 

 

 

 

 

Search This Blog

Labels