{"id":1622,"date":"2023-03-13T03:59:22","date_gmt":"2023-03-13T03:59:22","guid":{"rendered":"https:\/\/blog.embeddedexpert.io\/?p=1622"},"modified":"2023-03-13T03:59:26","modified_gmt":"2023-03-13T03:59:26","slug":"working-with-stm32-and-internal-rtc-part-2-driver-development","status":"publish","type":"post","link":"https:\/\/blog.embeddedexpert.io\/?p=1622","title":{"rendered":"Working with STM32 and internal RTC Part 2: Driver development"},"content":{"rendered":"\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/03\/AdobeStock_139809605-1024x1024.jpeg\" alt=\"\" class=\"wp-image-1616\" width=\"NaN\" height=\"NaN\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/03\/AdobeStock_139809605-1024x1024.jpeg 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/03\/AdobeStock_139809605-300x300.jpeg 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/03\/AdobeStock_139809605-150x150.jpeg 150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/03\/AdobeStock_139809605-768x768.jpeg 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/03\/AdobeStock_139809605-1536x1536.jpeg 1536w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/03\/AdobeStock_139809605-2048x2048.jpeg 2048w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/03\/AdobeStock_139809605-1150x1150.jpeg 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/03\/AdobeStock_139809605-750x750.jpeg 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/03\/AdobeStock_139809605-400x400.jpeg 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/03\/AdobeStock_139809605-250x250.jpeg 250w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p>In the previous guide (<a rel=\"noreferrer noopener\" href=\"https:\/\/blog.embeddedexpert.io\/?p=1615\" data-type=\"URL\" data-id=\"https:\/\/blog.embeddedexpert.io\/?p=1615\" target=\"_blank\">here<\/a>), we took a look at the feature of internal RTC of STM32F4. In this guide, we shall develop the driver to read\/write from\/to RTC.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>In this guide, we shall cover the following:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Initializing the RTC.<\/li><li>Update RTC.<\/li><li>Read from RTC.<\/li><li>Code.<\/li><li>Results.<\/li><\/ul>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">5. Initializing the RTC:<\/h2>\n\n\n\n<p>Create new source and header file with name of rtc.c and rtc.h respectively.<\/p>\n\n\n\n<p>Within the header file, declare the following enum which holds the days of week:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">typedef enum\n{\n\n\tMonday\t\t=0x01,\n\tTuesday\t\t=0x02,\n\tWednesday\t=0x03,\n\tThursday\t=0x04,\n\tFriday\t\t=0x05,\n\tSaturday\t=0x06,\n\tSunday\t\t=0x07\n}DoW;<\/pre><\/div>\n\n\n\n<p><\/p>\n\n\n\n<p>Also, the following structure which holds the variables:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">typedef struct ts {\n    uint8_t sec;         \/* seconds *\/\n    uint8_t min;         \/* minutes *\/\n    uint8_t hour;        \/* hours *\/\n    uint8_t mday;        \/* day of the month *\/\n    uint8_t mon;         \/* month *\/\n    int16_t year;        \/* year *\/\n    uint8_t wday;        \/* day of the week *\/\n    uint8_t yday;        \/* day in the year *\/\n    uint8_t isdst;       \/* daylight saving time *\/\n    uint8_t year_s;      \/* year in short notation*\/\n\n}ts;<\/pre><\/div>\n\n\n\n<p>Also, the following the functions:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">void RTC_Init();\nvoid rtc_update(ts *ts);\nvoid RTC_Get(ts *ts);<\/pre><\/div>\n\n\n\n<p><\/p>\n\n\n\n<p>Hence, the entire header file as following:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">#ifndef RTC_H_\n#define RTC_H_\n\n\n#include &quot;stdint.h&quot;\n\n\ntypedef enum\n{\n\n\tMonday\t\t=0x01,\n\tTuesday\t\t=0x02,\n\tWednesday\t=0x03,\n\tThursday\t=0x04,\n\tFriday\t\t=0x05,\n\tSaturday\t=0x06,\n\tSunday\t\t=0x07\n}DoW;\n\n\n\ntypedef struct ts {\n    uint8_t sec;         \/* seconds *\/\n    uint8_t min;         \/* minutes *\/\n    uint8_t hour;        \/* hours *\/\n    uint8_t mday;        \/* day of the month *\/\n    uint8_t mon;         \/* month *\/\n    int16_t year;        \/* year *\/\n    uint8_t wday;        \/* day of the week *\/\n    uint8_t yday;        \/* day in the year *\/\n    uint8_t isdst;       \/* daylight saving time *\/\n    uint8_t year_s;      \/* year in short notation*\/\n\n}ts;\n\n\nvoid RTC_Init();\nvoid rtc_update(ts *ts);\nvoid RTC_Get(ts *ts);\n\n\n#endif \/* RTC_H_ *\/<\/pre><\/div>\n\n\n\n<p><\/p>\n\n\n\n<p>Now, we move the source rtc.c file:<\/p>\n\n\n\n<p>First step of the initializing is to enable clock access to the power interface. <\/p>\n\n\n\n<p>In order to find which bus is power interface is connected to, we need to refer to the block diagram of STM32F411 which can be found in the datasheet:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"643\" height=\"1024\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/03\/Screenshot-2023-03-12-at-7.27.35-AM-643x1024.png\" alt=\"\" class=\"wp-image-1623\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/03\/Screenshot-2023-03-12-at-7.27.35-AM-643x1024.png 643w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/03\/Screenshot-2023-03-12-at-7.27.35-AM-188x300.png 188w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/03\/Screenshot-2023-03-12-at-7.27.35-AM-768x1224.png 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/03\/Screenshot-2023-03-12-at-7.27.35-AM-964x1536.png 964w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/03\/Screenshot-2023-03-12-at-7.27.35-AM-1150x1833.png 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/03\/Screenshot-2023-03-12-at-7.27.35-AM-750x1195.png 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/03\/Screenshot-2023-03-12-at-7.27.35-AM-400x637.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/03\/Screenshot-2023-03-12-at-7.27.35-AM-250x398.png 250w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/03\/Screenshot-2023-03-12-at-7.27.35-AM.png 1250w\" sizes=\"(max-width: 643px) 100vw, 643px\" \/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p>Hence, we can find that power interface and RTC module are both connected to APB1 bus. Hence, we can enable power interface clock access as following:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"411\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/03\/Screenshot-2023-03-12-at-7.29.57-AM-1024x411.png\" alt=\"\" class=\"wp-image-1624\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/03\/Screenshot-2023-03-12-at-7.29.57-AM-1024x411.png 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/03\/Screenshot-2023-03-12-at-7.29.57-AM-300x121.png 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/03\/Screenshot-2023-03-12-at-7.29.57-AM-768x308.png 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/03\/Screenshot-2023-03-12-at-7.29.57-AM-1536x617.png 1536w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/03\/Screenshot-2023-03-12-at-7.29.57-AM-2048x823.png 2048w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/03\/Screenshot-2023-03-12-at-7.29.57-AM-1150x462.png 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/03\/Screenshot-2023-03-12-at-7.29.57-AM-750x301.png 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/03\/Screenshot-2023-03-12-at-7.29.57-AM-400x161.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/03\/Screenshot-2023-03-12-at-7.29.57-AM-250x100.png 250w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>Declare a function with name of void RTC_Init() which return nothing and takes no argument.<\/p>\n\n\n\n<p>Within the function:<\/p>\n\n\n\n<p><\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">\t\/*Enable clock access to PWR *\/\n\tRCC-&gt;APB1ENR |= RCC_APB1ENR_PWREN;<\/pre><\/div>\n\n\n\n<p>Now, within power control register, disable backup domain write protection as following:<\/p>\n\n\n\n<p><\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"440\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/03\/Screenshot-2023-03-12-at-7.42.05-AM-1024x440.png\" alt=\"\" class=\"wp-image-1625\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/03\/Screenshot-2023-03-12-at-7.42.05-AM-1024x440.png 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/03\/Screenshot-2023-03-12-at-7.42.05-AM-300x129.png 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/03\/Screenshot-2023-03-12-at-7.42.05-AM-768x330.png 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/03\/Screenshot-2023-03-12-at-7.42.05-AM-1536x660.png 1536w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/03\/Screenshot-2023-03-12-at-7.42.05-AM-2048x880.png 2048w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/03\/Screenshot-2023-03-12-at-7.42.05-AM-1150x494.png 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/03\/Screenshot-2023-03-12-at-7.42.05-AM-750x322.png 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/03\/Screenshot-2023-03-12-at-7.42.05-AM-400x172.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/03\/Screenshot-2023-03-12-at-7.42.05-AM-250x107.png 250w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"180\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/03\/Screenshot-2023-03-12-at-7.39.22-AM-1024x180.png\" alt=\"\" class=\"wp-image-1626\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/03\/Screenshot-2023-03-12-at-7.39.22-AM-1024x180.png 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/03\/Screenshot-2023-03-12-at-7.39.22-AM-300x53.png 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/03\/Screenshot-2023-03-12-at-7.39.22-AM-768x135.png 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/03\/Screenshot-2023-03-12-at-7.39.22-AM-1536x270.png 1536w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/03\/Screenshot-2023-03-12-at-7.39.22-AM-2048x359.png 2048w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/03\/Screenshot-2023-03-12-at-7.39.22-AM-1150x202.png 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/03\/Screenshot-2023-03-12-at-7.39.22-AM-750x132.png 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/03\/Screenshot-2023-03-12-at-7.39.22-AM-400x70.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/03\/Screenshot-2023-03-12-at-7.39.22-AM-250x44.png 250w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>Note: there is typo, 0 means disabled and 1 means enabled<\/p>\n\n\n\n<p><\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">\t\/*Enable Backup access to config RTC*\/\n\tPWR-&gt;CR |=PWR_CR_DBP;<\/pre><\/div>\n\n\n\n<p>Since the STM32F411 Nucleo-64 has onboard 32.768KHz oscillator which is far  better when comes to stability and accuracy compared to the internal one, we shall enable the it and wait for it be set:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"548\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/03\/Screenshot-2023-03-13-at-6.11.14-AM-1024x548.png\" alt=\"\" class=\"wp-image-1630\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/03\/Screenshot-2023-03-13-at-6.11.14-AM-1024x548.png 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/03\/Screenshot-2023-03-13-at-6.11.14-AM-300x161.png 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/03\/Screenshot-2023-03-13-at-6.11.14-AM-768x411.png 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/03\/Screenshot-2023-03-13-at-6.11.14-AM-1536x822.png 1536w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/03\/Screenshot-2023-03-13-at-6.11.14-AM-2048x1096.png 2048w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/03\/Screenshot-2023-03-13-at-6.11.14-AM-1150x615.png 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/03\/Screenshot-2023-03-13-at-6.11.14-AM-750x401.png 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/03\/Screenshot-2023-03-13-at-6.11.14-AM-400x214.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/03\/Screenshot-2023-03-13-at-6.11.14-AM-250x134.png 250w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">\t\/*Enable Low Speed External (LSE)*\/\n\tRCC-&gt;BDCR |=RCC_BDCR_LSEON;\n\n\t\/*Wait for LSE to be ready*\/\n\twhile((RCC-&gt;BDCR &amp; RCC_BDCR_LSERDY) != RCC_BDCR_LSERDY){}<\/pre><\/div>\n\n\n\n<p>Set the RTC clock source to be the external low speed oscillator:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"998\" height=\"1024\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/03\/Screenshot-2023-03-13-at-6.16.45-AM-998x1024.png\" alt=\"\" class=\"wp-image-1631\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/03\/Screenshot-2023-03-13-at-6.16.45-AM-998x1024.png 998w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/03\/Screenshot-2023-03-13-at-6.16.45-AM-293x300.png 293w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/03\/Screenshot-2023-03-13-at-6.16.45-AM-768x788.png 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/03\/Screenshot-2023-03-13-at-6.16.45-AM-1498x1536.png 1498w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/03\/Screenshot-2023-03-13-at-6.16.45-AM-1997x2048.png 1997w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/03\/Screenshot-2023-03-13-at-6.16.45-AM-1150x1179.png 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/03\/Screenshot-2023-03-13-at-6.16.45-AM-750x769.png 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/03\/Screenshot-2023-03-13-at-6.16.45-AM-400x410.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/03\/Screenshot-2023-03-13-at-6.16.45-AM-250x256.png 250w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/03\/Screenshot-2023-03-13-at-6.16.45-AM.png 2030w\" sizes=\"(max-width: 998px) 100vw, 998px\" \/><\/figure>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">\t\/*Set RTC clock source to LSE*\/\n\tRCC-&gt;BDCR |=RCC_BDCR_RTCSEL_0;\n\tRCC-&gt;BDCR &amp;=~ RCC_BDCR_RTCSEL_1;<\/pre><\/div>\n\n\n\n<p>Now, enable the RTC:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">\t\/*Enable the RTC*\/\n\tRCC-&gt;BDCR |=RCC_BDCR_RTCEN;<\/pre><\/div>\n\n\n\n<p>In order to initialize the RTC properly, we need to unlock the write protection as following:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"164\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/03\/Screenshot-2023-03-12-at-8.18.56-AM-1024x164.png\" alt=\"\" class=\"wp-image-1628\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/03\/Screenshot-2023-03-12-at-8.18.56-AM-1024x164.png 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/03\/Screenshot-2023-03-12-at-8.18.56-AM-300x48.png 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/03\/Screenshot-2023-03-12-at-8.18.56-AM-768x123.png 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/03\/Screenshot-2023-03-12-at-8.18.56-AM-1536x247.png 1536w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/03\/Screenshot-2023-03-12-at-8.18.56-AM-2048x329.png 2048w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/03\/Screenshot-2023-03-12-at-8.18.56-AM-1150x185.png 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/03\/Screenshot-2023-03-12-at-8.18.56-AM-750x120.png 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/03\/Screenshot-2023-03-12-at-8.18.56-AM-400x64.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/03\/Screenshot-2023-03-12-at-8.18.56-AM-250x40.png 250w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">\t#define RTC_WRITE_PROTECTION_ENABLE_1 ((uint8_t)0xCAU)\n\t#define RTC_WRITE_PROTECTION_ENABLE_2 ((uint8_t)0x53U)\n\t\/*Disable RTC registers write protection*\/\n\tRTC-&gt;WPR = RTC_WRITE_PROTECTION_ENABLE_1;\n\tRTC-&gt;WPR = RTC_WRITE_PROTECTION_ENABLE_2;<\/pre><\/div>\n\n\n\n<p>Enter the initializing mode by setting INIT bit in RTC_ISR register:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"397\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/03\/Screenshot-2023-03-13-at-6.20.27-AM-1024x397.png\" alt=\"\" class=\"wp-image-1632\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/03\/Screenshot-2023-03-13-at-6.20.27-AM-1024x397.png 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/03\/Screenshot-2023-03-13-at-6.20.27-AM-300x116.png 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/03\/Screenshot-2023-03-13-at-6.20.27-AM-768x298.png 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/03\/Screenshot-2023-03-13-at-6.20.27-AM-1536x596.png 1536w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/03\/Screenshot-2023-03-13-at-6.20.27-AM-1150x446.png 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/03\/Screenshot-2023-03-13-at-6.20.27-AM-750x291.png 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/03\/Screenshot-2023-03-13-at-6.20.27-AM-400x155.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/03\/Screenshot-2023-03-13-at-6.20.27-AM-250x97.png 250w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/03\/Screenshot-2023-03-13-at-6.20.27-AM.png 2030w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">\t\/*Start init mode*\/\n\tRTC-&gt;ISR |= RTC_ISR_INIT;<\/pre><\/div>\n\n\n\n<p>Wait until the initializing is entered:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">\t\/*Wait until Initializing mode is active*\/\n\twhile((RTC-&gt;ISR &amp;RTC_ISR_INITF)!=RTC_ISR_INITF);<\/pre><\/div>\n\n\n\n<p>Set the Asynch prescaler:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">\t#define RTC_ASYNCH_PREDIV          ((uint32_t)0x7F)\n\t\/*Set Asynch prescaler*\/\n\tRTC-&gt;PRER&amp;=~(0x7F&lt;&lt;RTC_PRER_PREDIV_A_Pos);\n\tRTC-&gt;PRER|=(RTC_ASYNCH_PREDIV&lt;&lt;RTC_PRER_PREDIV_A_Pos);<\/pre><\/div>\n\n\n\n<p>Set the Synch prescaler:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">\t#define RTC_SYNCH_PREDIV           ((uint32_t)0x00F9)\n\t\/*Set Sync prescaler*\/\n\tRTC-&gt;PRER&amp;=~(0x7FFF&lt;&lt;RTC_PRER_PREDIV_S_Pos);\n\tRTC-&gt;PRER|=(RTC_SYNCH_PREDIV&lt;&lt;RTC_PRER_PREDIV_S_Pos);<\/pre><\/div>\n\n\n\n<p>Note: Those values are taken from STM32CubeMX.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>Exit the initializing mode by reseting INIT bit in ISR register:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">\/*Exit the initialization mode*\/\n\tRTC-&gt;ISR&amp;=~RTC_ISR_INIT;<\/pre><\/div>\n\n\n\n<p>Wait for synchronization by polling the INITF bit in ISR:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">\t\/*Wait for synchronization*\/\n\twhile((RTC-&gt;ISR &amp;RTC_ISR_INITF)==RTC_ISR_INITF);<\/pre><\/div>\n\n\n\n<p>Enable write protection by writing any value to WPR register:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">\t\/*Enable RTC registers write protection*\/\n\tRTC-&gt;WPR = 0xFF;<\/pre><\/div>\n\n\n\n<p>Finally disable backup access:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">\t\/*Disable Backup access*\/\n\tPWR-&gt;CR &amp;=~PWR_CR_DBP;<\/pre><\/div>\n\n\n\n<p>Now, the RTC is running and start counting.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">6. Update RTC:<\/h2>\n\n\n\n<p>Declare the following function:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">void rtc_update(ts *ts)<\/pre><\/div>\n\n\n\n<p>Which return nothing and pointer to ts structure as argument.<\/p>\n\n\n\n<p>Within the function:<\/p>\n\n\n\n<p>Enable backup access to configure the RTC once again:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">\t\/*Enable Backup access to config RTC*\/\n\tPWR-&gt;CR |=PWR_CR_DBP;<\/pre><\/div>\n\n\n\n<p>Disable RTC write protection:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">\t\/*Disable RTC registers write protection*\/\n\tRTC-&gt;WPR = RTC_WRITE_PROTECTION_ENABLE_1;\n\tRTC-&gt;WPR = RTC_WRITE_PROTECTION_ENABLE_2;<\/pre><\/div>\n\n\n\n<p>Start the initializing and wait for it:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">\/*Start init mode*\/\n\tRTC-&gt;ISR |= RTC_ISR_INIT;\n\n\t\/*Wait until Initializing mode is active*\/\n\twhile((RTC-&gt;ISR &amp; RTC_ISR_INITF)!=RTC_ISR_INITF);<\/pre><\/div>\n\n\n\n<p>Before continuing, we need to declare the following two function:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Set RTC date<\/li><li>Set RTC time<\/li><\/ul>\n\n\n\n<p>The set RTC date:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">static void rtc_date_config(uint32_t WeekDay, uint32_t Day, uint32_t Month, uint32_t Year)<\/pre><\/div>\n\n\n\n<p>The function takes four arguments, day of the week, day of the month, the month and the year and return nothing.<\/p>\n\n\n\n<p>Within the data, we can use the following to set the date:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">uint32_t temp = 0U;\n\n  temp = (WeekDay &lt;&lt; RTC_DR_WDU_Pos)                                                        | \\\n         (((Year &amp; 0xF0U) &lt;&lt; (RTC_DR_YT_Pos - 4U)) | ((Year &amp; 0x0FU) &lt;&lt; RTC_DR_YU_Pos))   | \\\n         (((Month &amp; 0xF0U) &lt;&lt; (RTC_DR_MT_Pos - 4U)) | ((Month &amp; 0x0FU) &lt;&lt; RTC_DR_MU_Pos)) | \\\n         (((Day &amp; 0xF0U) &lt;&lt; (RTC_DR_DT_Pos - 4U)) | ((Day &amp; 0x0FU) &lt;&lt; RTC_DR_DU_Pos));<\/pre><\/div>\n\n\n\n<p>Since the format is based on BCD format, and each field is based on two values, for example, Year tens in BCD formant and Year uint in BCD format:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"860\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/03\/Screenshot-2023-03-13-at-6.34.03-AM-1024x860.png\" alt=\"\" class=\"wp-image-1633\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/03\/Screenshot-2023-03-13-at-6.34.03-AM-1024x860.png 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/03\/Screenshot-2023-03-13-at-6.34.03-AM-300x252.png 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/03\/Screenshot-2023-03-13-at-6.34.03-AM-768x645.png 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/03\/Screenshot-2023-03-13-at-6.34.03-AM-1536x1289.png 1536w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/03\/Screenshot-2023-03-13-at-6.34.03-AM-1150x965.png 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/03\/Screenshot-2023-03-13-at-6.34.03-AM-750x630.png 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/03\/Screenshot-2023-03-13-at-6.34.03-AM-400x336.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/03\/Screenshot-2023-03-13-at-6.34.03-AM-250x210.png 250w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/03\/Screenshot-2023-03-13-at-6.34.03-AM.png 2030w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>Finally write the date to RTC-&gt;DR register:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">RTC-&gt;DR=(temp);<\/pre><\/div>\n\n\n\n<p>For setting the time, it is similar to setting the date as formula:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">static void rtc_time_config( uint32_t Hours, uint32_t Minutes, uint32_t Seconds)\n{\n   uint32_t temp = 0U;\n\n  temp = (((Hours &amp; 0xF0U) &lt;&lt; (RTC_TR_HT_Pos - 4U)) | ((Hours &amp; 0x0FU) &lt;&lt; RTC_TR_HU_Pos))     | \\\n         (((Minutes &amp; 0xF0U) &lt;&lt; (RTC_TR_MNT_Pos - 4U)) | ((Minutes &amp; 0x0FU) &lt;&lt; RTC_TR_MNU_Pos)) | \\\n         (((Seconds &amp; 0xF0U) &lt;&lt; (RTC_TR_ST_Pos - 4U)) | ((Seconds &amp; 0x0FU) &lt;&lt; RTC_TR_SU_Pos));\n  RTC-&gt;TR=(temp);\n}\n<\/pre><\/div>\n\n\n\n<p>Also, we need a method to change the integer to BCD format, the function as following:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">static uint8_t rtc_convert_bin2bcd(uint8_t value)\n{\n\treturn  (uint8_t)((((value) \/ 10U) &lt;&lt; 4U) | ((value) % 10U));\n}<\/pre><\/div>\n\n\n\n<p>We shall continue with rtc_update,<\/p>\n\n\n\n<p>Now, we can update the date and time using the function declared before:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">\t\/*Set desired date *\/\n\trtc_date_config(rtc_convert_bin2bcd(ts-&gt;wday),rtc_convert_bin2bcd(ts-&gt;mday),rtc_convert_bin2bcd(ts-&gt;mon),rtc_convert_bin2bcd(ts-&gt;year_s));\n\n\t\/*Set desired time *\/\n\trtc_time_config(rtc_convert_bin2bcd(ts-&gt;hour),rtc_convert_bin2bcd(ts-&gt;min),rtc_convert_bin2bcd(ts-&gt;sec));<\/pre><\/div>\n\n\n\n<p>Exit the initializing mode:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">\t\/*Exit the initialization mode*\/\n\tRTC-&gt;ISR&amp;=~RTC_ISR_INIT;\n\n\t\/*Wait for synchro*\/\n\twhile((RTC-&gt;ISR &amp;RTC_ISR_INITF)==RTC_ISR_INITF);\n\n\t\/*Enable RTC registers write protection*\/\n\tRTC-&gt;WPR = 0xFF;<\/pre><\/div>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">7. Get RTC data:<\/h2>\n\n\n\n<p>Declare the following function:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">void RTC_Get(ts *ts)<\/pre><\/div>\n\n\n\n<p>Which takes pointer to ts structure as argument and return nothing:<\/p>\n\n\n\n<p>Before continuing, we shall declare functions to get the data as following:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">static uint32_t rtc_date_get_day(void)\n{\n return (uint32_t)((READ_BIT(RTC-&gt;DR, (RTC_DR_DT | RTC_DR_DU))) &gt;&gt; RTC_DR_DU_Pos);\n}\n\nstatic uint32_t rtc_date_get_year(void)\n{\n return (uint32_t)((READ_BIT(RTC-&gt;DR, (RTC_DR_YT | RTC_DR_YU))) &gt;&gt; RTC_DR_YU_Pos);\n}\n\nstatic uint32_t rtc_date_get_month(void)\n{\n return (uint32_t)((READ_BIT(RTC-&gt;DR, (RTC_DR_MT | RTC_DR_MU)))&gt;&gt; RTC_DR_MU_Pos);\n}\n\nstatic uint32_t rtc_time_get_second(void)\n{\n return (uint32_t)(READ_BIT(RTC-&gt;TR, (RTC_TR_ST | RTC_TR_SU)) &gt;&gt; RTC_TR_SU_Pos);\n}\n\nstatic uint32_t rtc_time_get_minute(void)\n{\n return (uint32_t)(READ_BIT(RTC-&gt;TR, (RTC_TR_MNT | RTC_TR_MNU))&gt;&gt; RTC_TR_MNU_Pos);\n}\n\n\nstatic uint32_t rtc_time_get_hour(void)\n{\n  return (uint32_t)((READ_BIT(RTC-&gt;TR, (RTC_TR_HT | RTC_TR_HU))) &gt;&gt; RTC_TR_HU_Pos);\n}\n\n\nstatic uint32_t rtc_date_get_DoW(void)\n{\n\treturn (uint32_t)((READ_BIT(RTC-&gt;DR, (RTC_DR_WDU ))) &gt;&gt; RTC_DR_WDU_Pos);\n\n}<\/pre><\/div>\n\n\n\n<p>Each function will read the required register and return the BCD format of the variable:<\/p>\n\n\n\n<p>Take second for example:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">(READ_BIT(RTC-&gt;TR, (RTC_TR_ST | RTC_TR_SU)) &gt;&gt; RTC_TR_SU_Pos);<\/pre><\/div>\n\n\n\n<p>The second part has two variable , second ten and second unit. We need to read both values and we need to read both and convert the value from BCD to integer as following:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">static uint8_t rtc_convert_bcd2bin(uint8_t value )\n{\n\treturn (uint8_t)(((uint8_t)((value) &amp; (uint8_t)0xF0U) &gt;&gt; (uint8_t)0x4U) * 10U + ((value) &amp; (uint8_t)0x0FU));\n}<\/pre><\/div>\n\n\n\n<p>Hence, we can fill the ts structure as following:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">\t\/*Time *\/\n\tts-&gt;sec\t\t= rtc_convert_bcd2bin(rtc_time_get_second());\n\tts-&gt;min\t\t= rtc_convert_bcd2bin(rtc_time_get_minute());\n\tts-&gt;hour\t= rtc_convert_bcd2bin(rtc_time_get_hour());\n\t\/*Date*\/\n\tts-&gt;mday\t= rtc_convert_bcd2bin(rtc_date_get_day());\n\tts-&gt;mon\t\t= rtc_convert_bcd2bin(rtc_date_get_month());\n\tts-&gt;year\t= rtc_convert_bcd2bin(rtc_date_get_year());\n\tts-&gt;wday\t= rtc_convert_bcd2bin(rtc_date_get_DoW());<\/pre><\/div>\n\n\n\n<p><\/p>\n\n\n\n<p>Hence, the source file as following:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">\n#include &quot;rtc.h&quot;\n#include &quot;stm32f4xx.h&quot;\n\n\n#define RTC_WRITE_PROTECTION_ENABLE_1 \t((uint8_t)0xCAU)\n#define RTC_WRITE_PROTECTION_ENABLE_2 \t((uint8_t)0x53U)\n\n\n#define RTC_ASYNCH_PREDIV          \t\t((uint32_t)0x7F)\n#define RTC_SYNCH_PREDIV           \t\t((uint32_t)0x00F9)\n\n\n\n\nstatic uint8_t rtc_convert_bin2bcd(uint8_t value)\n{\n\treturn  (uint8_t)((((value) \/ 10U) &lt;&lt; 4U) | ((value) % 10U));\n}\n\nstatic uint8_t rtc_convert_bcd2bin(uint8_t value )\n{\n\treturn (uint8_t)(((uint8_t)((value) &amp; (uint8_t)0xF0U) &gt;&gt; (uint8_t)0x4U) * 10U + ((value) &amp; (uint8_t)0x0FU));\n}\n\n\n\nstatic void rtc_date_config(uint32_t WeekDay, uint32_t Day, uint32_t Month, uint32_t Year)\n{\n   uint32_t temp = 0U;\n\n  temp = (WeekDay &lt;&lt; RTC_DR_WDU_Pos)                                                        | \\\n         (((Year &amp; 0xF0U) &lt;&lt; (RTC_DR_YT_Pos - 4U)) | ((Year &amp; 0x0FU) &lt;&lt; RTC_DR_YU_Pos))   | \\\n         (((Month &amp; 0xF0U) &lt;&lt; (RTC_DR_MT_Pos - 4U)) | ((Month &amp; 0x0FU) &lt;&lt; RTC_DR_MU_Pos)) | \\\n         (((Day &amp; 0xF0U) &lt;&lt; (RTC_DR_DT_Pos - 4U)) | ((Day &amp; 0x0FU) &lt;&lt; RTC_DR_DU_Pos));\n\n  RTC-&gt;DR=(temp);\n}\n\n\n\nstatic void rtc_time_config( uint32_t Hours, uint32_t Minutes, uint32_t Seconds)\n{\n   uint32_t temp = 0U;\n\n  temp = (((Hours &amp; 0xF0U) &lt;&lt; (RTC_TR_HT_Pos - 4U)) | ((Hours &amp; 0x0FU) &lt;&lt; RTC_TR_HU_Pos))     | \\\n         (((Minutes &amp; 0xF0U) &lt;&lt; (RTC_TR_MNT_Pos - 4U)) | ((Minutes &amp; 0x0FU) &lt;&lt; RTC_TR_MNU_Pos)) | \\\n         (((Seconds &amp; 0xF0U) &lt;&lt; (RTC_TR_ST_Pos - 4U)) | ((Seconds &amp; 0x0FU) &lt;&lt; RTC_TR_SU_Pos));\n  RTC-&gt;TR=(temp);\n}\n\n\n\n\n\n\nvoid RTC_Init()\n{\n\n\t\/*Enable Backup access to config RTC*\/\n\tPWR-&gt;CR |=PWR_CR_DBP;\n\n\t\/*Enable Low Speed External (LSE)*\/\n\tRCC-&gt;BDCR |=RCC_BDCR_LSEON;\n\n\t\/*Wait for LSE to be ready*\/\n\twhile((RCC-&gt;BDCR &amp; RCC_BDCR_LSERDY) != RCC_BDCR_LSERDY){}\n\n\n\t\/*Set RTC clock source to LSE*\/\n\tRCC-&gt;BDCR |=RCC_BDCR_RTCSEL_0;\n\tRCC-&gt;BDCR &amp;=~ RCC_BDCR_RTCSEL_1;\n\n\t\/*Enable the RTC*\/\n\tRCC-&gt;BDCR |=RCC_BDCR_RTCEN;\n\n\t\/*Disable RTC registers write protection*\/\n\tRTC-&gt;WPR = RTC_WRITE_PROTECTION_ENABLE_1;\n\tRTC-&gt;WPR = RTC_WRITE_PROTECTION_ENABLE_2;\n\n\t\/*Start init mode*\/\n\tRTC-&gt;ISR |= RTC_ISR_INIT;\n\n\t\/*Wait until Initializing mode is active*\/\n\twhile((RTC-&gt;ISR &amp;RTC_ISR_INITF)!=RTC_ISR_INITF);\n\n\t\/*Set Asynch prescaler*\/\n\tRTC-&gt;PRER&amp;=~(0x7F&lt;&lt;RTC_PRER_PREDIV_A_Pos);\n\tRTC-&gt;PRER|=(RTC_ASYNCH_PREDIV&lt;&lt;RTC_PRER_PREDIV_A_Pos);\n\n\t\/*Set Sync prescaler*\/\n\tRTC-&gt;PRER &amp;=~(0x7FFF&lt;&lt;RTC_PRER_PREDIV_S_Pos);\n\tRTC-&gt;PRER|=(RTC_SYNCH_PREDIV&lt;&lt;RTC_PRER_PREDIV_S_Pos);\n\n\t\/*Exit the initialization mode*\/\n\tRTC-&gt;ISR&amp;=~RTC_ISR_INIT;\n\n\t\/*Wait for synchronization*\/\n\twhile((RTC-&gt;ISR &amp;RTC_ISR_INITF)==RTC_ISR_INITF);\n\n\t\/*Enable RTC registers write protection*\/\n\tRTC-&gt;WPR = 0xFF;\n\n\t\/*Disable Backup access*\/\n\tPWR-&gt;CR &amp;=~PWR_CR_DBP;\n\n}\n\nvoid rtc_update(ts *ts)\n{\n\t\/*Enable Backup access to config RTC*\/\n\tPWR-&gt;CR |=PWR_CR_DBP;\n\n\t\/*Disable RTC registers write protection*\/\n\tRTC-&gt;WPR = RTC_WRITE_PROTECTION_ENABLE_1;\n\tRTC-&gt;WPR = RTC_WRITE_PROTECTION_ENABLE_2;\n\n\n\t\/*Start init mode*\/\n\tRTC-&gt;ISR |= RTC_ISR_INIT;\n\n\t\/*Wait until Initializing mode is active*\/\n\twhile((RTC-&gt;ISR &amp; RTC_ISR_INITF)!=RTC_ISR_INITF);\n\n\t\/*Set desired date *\/\n\trtc_date_config(rtc_convert_bin2bcd(ts-&gt;wday),rtc_convert_bin2bcd(ts-&gt;mday),rtc_convert_bin2bcd(ts-&gt;mon),rtc_convert_bin2bcd(ts-&gt;year_s));\n\n\t\/*Set desired time *\/\n\trtc_time_config(rtc_convert_bin2bcd(ts-&gt;hour),rtc_convert_bin2bcd(ts-&gt;min),rtc_convert_bin2bcd(ts-&gt;sec));\n\n\n\t\/*Exit the initialization mode*\/\n\tRTC-&gt;ISR&amp;=~RTC_ISR_INIT;\n\n\t\/*Wait for synchro*\/\n\twhile((RTC-&gt;ISR &amp;RTC_ISR_INITF)==RTC_ISR_INITF);\n\n\t\/*Enable RTC registers write protection*\/\n\tRTC-&gt;WPR = 0xFF;\n}\n\n\n\nstatic uint32_t rtc_date_get_day(void)\n{\n return (uint32_t)((READ_BIT(RTC-&gt;DR, (RTC_DR_DT | RTC_DR_DU))) &gt;&gt; RTC_DR_DU_Pos);\n}\n\nstatic uint32_t rtc_date_get_year(void)\n{\n return (uint32_t)((READ_BIT(RTC-&gt;DR, (RTC_DR_YT | RTC_DR_YU))) &gt;&gt; RTC_DR_YU_Pos);\n}\n\nstatic uint32_t rtc_date_get_month(void)\n{\n return (uint32_t)((READ_BIT(RTC-&gt;DR, (RTC_DR_MT | RTC_DR_MU)))&gt;&gt; RTC_DR_MU_Pos);\n}\n\nstatic uint32_t rtc_time_get_second(void)\n{\n return (uint32_t)(READ_BIT(RTC-&gt;TR, (RTC_TR_ST | RTC_TR_SU)) &gt;&gt; RTC_TR_SU_Pos);\n}\n\nstatic uint32_t rtc_time_get_minute(void)\n{\n return (uint32_t)(READ_BIT(RTC-&gt;TR, (RTC_TR_MNT | RTC_TR_MNU))&gt;&gt; RTC_TR_MNU_Pos);\n}\n\n\nstatic uint32_t rtc_time_get_hour(void)\n{\n  return (uint32_t)((READ_BIT(RTC-&gt;TR, (RTC_TR_HT | RTC_TR_HU))) &gt;&gt; RTC_TR_HU_Pos);\n}\n\n\nstatic uint32_t rtc_date_get_DoW(void)\n{\n\treturn (uint32_t)((READ_BIT(RTC-&gt;DR, (RTC_DR_WDU ))) &gt;&gt; RTC_DR_WDU_Pos);\n\n}\n\nvoid RTC_Get(ts *ts)\n{\n\t\/*Time *\/\n\tts-&gt;sec\t\t= rtc_convert_bcd2bin(rtc_time_get_second());\n\tts-&gt;min\t\t= rtc_convert_bcd2bin(rtc_time_get_minute());\n\tts-&gt;hour\t= rtc_convert_bcd2bin(rtc_time_get_hour());\n\t\/*Date*\/\n\tts-&gt;mday\t= rtc_convert_bcd2bin(rtc_date_get_day());\n\tts-&gt;mon\t\t= rtc_convert_bcd2bin(rtc_date_get_month());\n\tts-&gt;year\t= rtc_convert_bcd2bin(rtc_date_get_year());\n\tts-&gt;wday\t= rtc_convert_bcd2bin(rtc_date_get_DoW());\n}\n<\/pre><\/div>\n\n\n\n<p><\/p>\n\n\n\n<p>Within main:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">#include &quot;LiquidCrystal_PCF8574.h&quot;\n#include &quot;delay.h&quot;\n#include &quot;stdio.h&quot;\n#include &quot;rtc.h&quot;\n\nchar lcd_data[20];\n\n\n\nts time;\n\nint main(void)\n{\n\tlcd_init();\n\tRTC_Init();\n\n\ttime.hour=17;\n\ttime.min=15;\n\ttime.sec=30;\n\n\ttime.wday=Saturday;\n\ttime.mday=11;\n\ttime.mon=3;\n\ttime.year_s=23;\n\n\t\/\/rtc_update(&amp;time);\n\n\n\twhile(1)\n\t{\n\t\tRTC_Get(&amp;time);\n\t\tsetCursor(0,0);\n\t\tsprintf(lcd_data,&quot;%d:%d:%d    &quot;,time.hour,time.min,time.sec);\n\t\tlcd_send_string(lcd_data);\n\t\tsprintf(lcd_data,&quot;%d\/%d\/%d     &quot;,time.mday,time.mon,time.year_s);\n\t\tsetCursor(0,1);\n\t\tlcd_send_string(lcd_data);\n\t\tsetCursor(0,2);\n\t\tswitch (time.wday)\n\t\t{\n\t\t\tcase Monday\t\t: sprintf(lcd_data,&quot;Monday  &quot;); break;\n\n\t\t\tcase Tuesday\t: sprintf(lcd_data,&quot;Tuesday  &quot;); break;\n\n\t\t\tcase Wednesday\t: sprintf(lcd_data,&quot;Wednesday  &quot;); break;\n\n\t\t\tcase Thursday\t: sprintf(lcd_data,&quot;Thursday  &quot;); break;\n\n\t\t\tcase Friday\t\t: sprintf(lcd_data,&quot;Friday    &quot;); break;\n\n\t\t\tcase Saturday\t: sprintf(lcd_data,&quot;Saturday   &quot;); break;\n\n\t\t\tcase Sunday\t\t: sprintf(lcd_data,&quot;Sunday  &quot;); break;\n\n\t\t\tdefault\t\t\t: break;\n\n\t\t}\n\n\t\tlcd_send_string(lcd_data);\n\n\n\t}\n\n}\n<\/pre><\/div>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">8. Code:<\/h2>\n\n\n\n<p>You can download the source code from here:<\/p>\n\n\n\n<div class=\"wp-block-file\"><a href=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/03\/Internal_RTC_external.zip\">Internal_RTC_external<\/a><a href=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/03\/Internal_RTC_external.zip\" class=\"wp-block-file__button\" download>Download<\/a><\/div>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">9. Results:<\/h2>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"472\" height=\"1024\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/03\/IMG_0390-472x1024.png\" alt=\"\" class=\"wp-image-1635\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/03\/IMG_0390-472x1024.png 472w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/03\/IMG_0390-138x300.png 138w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/03\/IMG_0390-768x1665.png 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/03\/IMG_0390-709x1536.png 709w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/03\/IMG_0390-945x2048.png 945w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/03\/IMG_0390-1150x2493.png 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/03\/IMG_0390-750x1626.png 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/03\/IMG_0390-400x867.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/03\/IMG_0390-250x542.png 250w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/03\/IMG_0390.png 1290w\" sizes=\"(max-width: 472px) 100vw, 472px\" \/><\/figure><\/div>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In the previous guide (here), we took a look at the feature of internal RTC of STM32F4. In this guide, we shall develop the driver to read\/write from\/to RTC. In this guide, we shall cover the following: Initializing the RTC. Update RTC. Read from RTC. Code. Results. 5. Initializing the RTC: Create new source and [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3,2,19,11,7,12],"tags":[],"class_list":["post-1622","post","type-post","status-publish","format-standard","hentry","category-data-structures","category-embedded-systems","category-lcd","category-peripheral-drivers","category-state-machine","category-stm32"],"_links":{"self":[{"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/posts\/1622"}],"collection":[{"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1622"}],"version-history":[{"count":2,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/posts\/1622\/revisions"}],"predecessor-version":[{"id":1636,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/posts\/1622\/revisions\/1636"}],"wp:attachment":[{"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1622"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1622"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1622"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}