{"id":2827,"date":"2024-09-06T15:25:41","date_gmt":"2024-09-06T15:25:41","guid":{"rendered":"https:\/\/iotthinghub.com\/?p=2827"},"modified":"2024-09-06T16:10:01","modified_gmt":"2024-09-06T16:10:01","slug":"rtc-in-stm32","status":"publish","type":"post","link":"https:\/\/iotthinghub.com\/?p=2827","title":{"rendered":"RTC In STM32"},"content":{"rendered":"\n<p class=\"has-text-color has-link-color wp-elements-ff7e8c6d65174802472c2e6baebad867 wp-block-paragraph\" style=\"color:#5c5c5c\">In RTC section we interface an I<sup>2<\/sup>C IC DS1307. The STM32 has built in RTC. In STM32 the real-time clock (RTC) is an independent BCD timer\/counter. The RTC provides a time of-day clock\/calendar with programmable alarm interrupt. The RTC includes also a periodic programmable wakeup flag with interrupt capability. Two 32-bit registers contain the seconds, minutes, hours (12- or 24-hour format), day (day of week), date (day of month), month, and year, expressed in binary coded decimal format (BCD). The sub-seconds value is also available in binary format. Compensations for 28-, 29- (leap year), 30-, and 31-day months are performed automatically. Daylight saving time compensation can also be performed. Let\u2019s start with some settings \u2013<\/p>\n\n\n\n<div class=\"wp-block-columns is-layout-flex wp-container-core-columns-is-layout-7387b849 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<figure class=\"wp-block-image aligncenter size-full is-resized\"><img fetchpriority=\"high\" decoding=\"async\" width=\"596\" height=\"687\" src=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/09\/RTC-Settings.jpg\" alt=\"\" class=\"wp-image-2830\" style=\"width:381px;height:auto\" srcset=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/09\/RTC-Settings.jpg 596w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/09\/RTC-Settings-260x300.jpg 260w\" sizes=\"(max-width: 596px) 100vw, 596px\" \/><\/figure>\n<\/div>\n\n\n\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<figure class=\"wp-block-image aligncenter size-full is-resized\"><img decoding=\"async\" width=\"558\" height=\"700\" src=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/09\/RTC-Settings1.jpg\" alt=\"\" class=\"wp-image-2831\" style=\"width:366px;height:auto\" srcset=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/09\/RTC-Settings1.jpg 558w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/09\/RTC-Settings1-239x300.jpg 239w\" sizes=\"(max-width: 558px) 100vw, 558px\" \/><\/figure>\n<\/div>\n<\/div>\n\n\n\n<p class=\"has-text-color has-link-color wp-elements-f5f1866abf2ce4c169ed228831559a30 wp-block-paragraph\" style=\"color:#5c5c5c\">Here we use external crystal 32.768KHz as low speed clock. Change the calendar according to your current time zone. Some useful HAL library as follow-<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; auto-links: false; title: ; quick-code: false; notranslate\" title=\"\">\nHAL_RTC_SetTime(RTC_HandleTypeDef *hrtc, RTC_TimeTypeDef *sTime, uint32_t Format)\nHAL_RTC_SetDate(RTC_HandleTypeDef *hrtc, RTC_DateTypeDef *sDate, uint32_t Format)\nHAL_RTC_GetTime(RTC_HandleTypeDef *hrtc, RTC_TimeTypeDef *sTime, uint32_t Format)\nHAL_RTC_GetDate(RTC_HandleTypeDef *hrtc, RTC_DateTypeDef *sDate, uint32_t Format)\n<\/pre><\/div>\n\n\n<p class=\"has-text-color has-link-color wp-elements-f39aaf0132fccb389f5d491e5914d54c wp-block-paragraph\" style=\"color:#5c5c5c\">Here the format is either<\/p>\n\n\n\n<ul style=\"color:#5c5c5c\" class=\"wp-block-list has-text-color has-link-color wp-elements-d110b0345adbc5f01ab61325e0dc86e1\">\n<li>RTC_FORMAT_BIN:&nbsp; Binary data format<\/li>\n\n\n\n<li>RTC_FORMAT_BCD:&nbsp; BCD data format<\/li>\n<\/ul>\n\n\n\n<p class=\"has-text-color has-link-color wp-elements-a7f77659c5edddb2c3ddeeb225276e72 wp-block-paragraph\" style=\"color:#5c5c5c\">First we have to declare the time &amp; date then assign the value and get the value. To define time &amp; date as follow-<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; auto-links: false; title: ; quick-code: false; notranslate\" title=\"\">\nRTC_TimeTypeDef sTime;\nRTC_DateTypeDef sDate;\n<\/pre><\/div>\n\n\n<p class=\"has-text-color has-link-color wp-elements-2dd7344e06eaee43edae310ac6345334 wp-block-paragraph\" style=\"color:#5c5c5c\">In STM32Cube we can directly assign the date &amp; time, but is not good practice. Let\u2019s assign some value to RTC-<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; auto-links: false; title: ; quick-code: false; notranslate\" title=\"\">\nsTime.Hours = 0x10;\nsTime.Minutes = 0x0;\nsTime.Seconds = 0x0;\nsDate.WeekDay = RTC_WEEKDAY_SATURDAY;\nsDate.Month = RTC_MONTH_SEPTEMBER;\nsDate.Date = 0x1;\nsDate.Year = 0x24;\nHAL_RTC_SetTime(&amp;hrtc,&amp;sTime,RTC_FORMAT_BCD);\nHAL_RTC_SetDate(&amp;hrtc,&amp;sDate,RTC_FORMAT_BCD);\n<\/pre><\/div>\n\n\n<p class=\"has-text-color has-link-color wp-elements-414f9692e9a4e1305de71dbd763ee0b7 wp-block-paragraph\" style=\"color:#5c5c5c\">Reading procedure is very simple. Just use 2 lines-<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; auto-links: false; title: ; quick-code: false; notranslate\" title=\"\">\nHAL_RTC_GetTime(&amp;hrtc,&amp;sTime,RTC_FORMAT_BCD);\nHAL_RTC_GetDate(&amp;hrtc,&amp;sDate,RTC_FORMAT_BCD);\n<\/pre><\/div>\n\n\n<p class=\"has-text-color has-link-color wp-elements-ac8ea2b58fcce4ec1b16a490f3810216 wp-block-paragraph\" style=\"color:#5c5c5c\">All parameter returns in BCD format, just use %X in C language. The day &amp; time zone as follow-<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-large is-resized\"><img decoding=\"async\" width=\"1024\" height=\"153\" src=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/09\/function-5-1024x153.jpg\" alt=\"\" class=\"wp-image-2853\" style=\"width:723px;height:auto\" srcset=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/09\/function-5-1024x153.jpg 1024w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/09\/function-5-300x45.jpg 300w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/09\/function-5-768x115.jpg 768w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/09\/function-5.jpg 1432w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p class=\"has-text-color has-link-color wp-elements-91eb849d66feb62b90e2e0f4a07e742e wp-block-paragraph\" style=\"color:#5c5c5c\">For example to display all date &amp; time as follow-<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; auto-links: false; title: ; quick-code: false; notranslate\" title=\"\">\nchar display_time&#x5B;16]=&quot;IoTThingHuB.com &quot;;\nvolatile char *day_name&#x5B;8]={&quot;Mon&quot;, &quot;Tue&quot;, &quot;Wed&quot;, &quot;Thu&quot;, &quot;Fri&quot;, &quot;Sat&quot;,&quot;Sun&quot;};\nvolatile char *day_zone&#x5B;2]={&quot;AM&quot;, &quot;PM&quot;};\nsprintf(display_time,&quot; %02X:%02X:%02X %s&quot;,sTime.Hours,sTime.Minutes,sTime.Seconds,day_zone&#x5B;sTime.TimeFormat]);\nsprintf(display_time,&quot; %02X\/%02X\/%02X %s&quot;,sDate.Date,sDate.Month,sDate.Year,day_name&#x5B;sDate.WeekDay-1]);\n<\/pre><\/div>\n\n\n<p class=\"has-text-color has-link-color wp-elements-3f7ce33914b265a87c24cffb42ba378e wp-block-paragraph\" style=\"color:#5c5c5c\">In our previous article we discuss abut OLED interfacing, we will show the clock in OLED. The main program as follow(only the add parameter)-<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; auto-links: false; title: ; quick-code: false; notranslate\" title=\"\">\n#include &quot;ssd1306.h&quot;\nRTC_TimeTypeDef sTime;\nRTC_DateTypeDef sDate;\nchar display_time&#x5B;16]=&quot;IoTThingHuB.com &quot;;\nvolatile char *day_name&#x5B;8]={&quot;Mon&quot;, &quot;Tue&quot;, &quot;Wed&quot;, &quot;Thu&quot;, &quot;Fri&quot;, &quot;Sat&quot;,&quot;Sun&quot;};\nvolatile char *day_zone&#x5B;2]={&quot;AM&quot;, &quot;PM&quot;};\ninit_OLED();\nclear_display();\nOLCD_write_string(0,0,&quot; Real Time Clock&quot;);\nWhile(1){\n\tHAL_RTC_GetTime(&amp;hrtc,&amp;sTime,RTC_FORMAT_BCD);\n    HAL_RTC_GetDate(&amp;hrtc,&amp;sDate,RTC_FORMAT_BCD);\n    sprintf(display_time,&quot; %02X:%02X:%02X %s&quot;,sTime.Hours,sTime.Minutes,sTime.Seconds,day_zone&#x5B;sTime.TimeFormat]);\n\tOLCD_write_string(4,0,display_time);\n\tsprintf(display_time,&quot; %02X\/%02X\/%02X %s&quot;,sDate.Date,sDate.Month,sDate.Year,day_name&#x5B;sDate.WeekDay-1]);\n\tOLCD_write_string(6,0,display_time);\n}\n<\/pre><\/div>\n\n\n<figure class=\"wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio\"><div class=\"wp-block-embed__wrapper\">\n<iframe title=\"STM32 RTC\" width=\"640\" height=\"360\" src=\"https:\/\/www.youtube.com\/embed\/WfftCT7gEiY?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen><\/iframe>\n<\/div><\/figure>\n\n\n\n<div class=\"wp-block-columns is-layout-flex wp-container-core-columns-is-layout-7387b849 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<figure class=\"wp-block-image aligncenter size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"773\" height=\"336\" src=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/09\/RTC-Connection.jpg\" alt=\"\" class=\"wp-image-2849\" style=\"width:380px;height:auto\" srcset=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/09\/RTC-Connection.jpg 773w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/09\/RTC-Connection-300x130.jpg 300w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/09\/RTC-Connection-768x334.jpg 768w\" sizes=\"(max-width: 773px) 100vw, 773px\" \/><\/figure>\n<\/div>\n\n\n\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<div class=\"wp-block-buttons is-content-justification-center is-layout-flex wp-container-core-buttons-is-layout-3e41869c wp-block-buttons-is-layout-flex\">\n<div class=\"wp-block-button\"><a class=\"wp-block-button__link wp-element-button\" href=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/09\/RTC-STM32F0308CC.rar\">Download<\/a><\/div>\n<\/div>\n\n\n\n<p class=\"has-text-color has-link-color has-medium-font-size wp-elements-eb726595613046cca19aaae984916579 wp-block-paragraph\" style=\"color:#252525\"><a href=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/09\/RTC-STM32F0308CC.rar\">RTC STM32F0308CC.rar<\/a><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n<\/div>\n<\/div>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In RTC section we interface an I2C IC DS1307. The STM32 has built in RTC. In STM32 the real-time clock (RTC) is an independent BCD timer\/counter. The RTC provides a time of-day clock\/calendar with programmable alarm interrupt. The RTC includes also a periodic programmable wakeup flag with interrupt capability. Two 32-bit registers contain the seconds, [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[21],"tags":[],"class_list":["post-2827","post","type-post","status-publish","format-standard","hentry","category-arm-standards-peripherals"],"_links":{"self":[{"href":"https:\/\/iotthinghub.com\/index.php?rest_route=\/wp\/v2\/posts\/2827","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/iotthinghub.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/iotthinghub.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/iotthinghub.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/iotthinghub.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2827"}],"version-history":[{"count":13,"href":"https:\/\/iotthinghub.com\/index.php?rest_route=\/wp\/v2\/posts\/2827\/revisions"}],"predecessor-version":[{"id":2854,"href":"https:\/\/iotthinghub.com\/index.php?rest_route=\/wp\/v2\/posts\/2827\/revisions\/2854"}],"wp:attachment":[{"href":"https:\/\/iotthinghub.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2827"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/iotthinghub.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2827"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/iotthinghub.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2827"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}