{"id":2241,"date":"2024-07-15T16:51:55","date_gmt":"2024-07-15T16:51:55","guid":{"rendered":"https:\/\/iotthinghub.com\/?p=2241"},"modified":"2024-08-11T17:54:07","modified_gmt":"2024-08-11T17:54:07","slug":"rtc-ds1307-i2c","status":"publish","type":"post","link":"https:\/\/iotthinghub.com\/?p=2241","title":{"rendered":"RTC (DS1307) I2C"},"content":{"rendered":"\n<p class=\"has-text-color has-link-color wp-elements-5704c1ce58bf818620ad862490c58623 wp-block-paragraph\" style=\"color:#5c5c5c\">The DS1307 serial real time clock is a low power, full binary-coded decimal (BCD) clock\/calendar plus 56bytes of NV SRAM. Address and data are transmitted serially via 2-wire, bi-directional bus. The clock\/calendar provides seconds, minutes, hour, day, date, month and year information. The end date at the month date is automatically adjusted for months with fewer than 31days, including correction for leap year. The clock operates in either the 24 hours or 12 hours format with AM\/PM indicator. The DS1307 has a build in power sense circuit that detects power failures and automatically switches to the battery supply.<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-large is-resized\"><img fetchpriority=\"high\" decoding=\"async\" width=\"1024\" height=\"240\" src=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/07\/DS1307-1024x240.jpg\" alt=\"\" class=\"wp-image-2243\" style=\"width:777px;height:auto\" srcset=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/07\/DS1307-1024x240.jpg 1024w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/07\/DS1307-300x70.jpg 300w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/07\/DS1307-768x180.jpg 768w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/07\/DS1307.jpg 1508w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p class=\"has-text-color has-link-color wp-elements-e989a74b95cd02a501c61cd1a1eb4229 wp-block-paragraph\" style=\"color:#5c5c5c\">Controlling clock register<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" width=\"1024\" height=\"520\" src=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/07\/datasheet-4-1024x520.jpg\" alt=\"\" class=\"wp-image-2246\" srcset=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/07\/datasheet-4-1024x520.jpg 1024w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/07\/datasheet-4-300x152.jpg 300w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/07\/datasheet-4-768x390.jpg 768w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/07\/datasheet-4.jpg 1279w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p class=\"has-text-color has-link-color wp-elements-4eaee39cf3db0c3b804e013fe29fd693 wp-block-paragraph\" style=\"color:#5c5c5c\">Here the 07H address is used to control the operation of the SQW\/OUT pin.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large is-resized\"><img decoding=\"async\" width=\"1024\" height=\"195\" src=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/07\/function-2-1024x195.jpg\" alt=\"\" class=\"wp-image-2248\" style=\"width:777px;height:auto\" srcset=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/07\/function-2-1024x195.jpg 1024w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/07\/function-2-300x57.jpg 300w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/07\/function-2-768x146.jpg 768w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/07\/function-2.jpg 1394w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p class=\"has-text-color has-link-color wp-elements-4688736da3510a9faef6e6015c675cef wp-block-paragraph\" style=\"color:#5c5c5c\">If SQWE bit is 0, than<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"157\" src=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/07\/image-1-1024x157.png\" alt=\"\" class=\"wp-image-2249\" srcset=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/07\/image-1-1024x157.png 1024w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/07\/image-1-300x46.png 300w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/07\/image-1-768x118.png 768w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/07\/image-1.png 1375w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p class=\"has-text-color has-link-color wp-elements-de35d8ed2d99b45189011cf3c65f95ab wp-block-paragraph\" style=\"color:#5c5c5c\">Now the first step is to initialize the RTC with seconds, minutes, hours, day, date, month, year and output we want. RTC works on BCD format.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"203\" src=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/07\/table-8-1024x203.jpg\" alt=\"\" class=\"wp-image-2252\" srcset=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/07\/table-8-1024x203.jpg 1024w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/07\/table-8-300x60.jpg 300w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/07\/table-8-768x153.jpg 768w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/07\/table-8.jpg 1399w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p class=\"has-text-color has-link-color wp-elements-ac7420177bcdd36e4e8443b2022a76eb wp-block-paragraph\" style=\"color:#5c5c5c\">Here the time is 10:37:49 or in BCD format 0x10:0x37:0x49. Let\u2019s look at the I<sup>2<\/sup>C protocol for data writing procedure-<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"253\" src=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/07\/datasheet-5-1024x253.jpg\" alt=\"\" class=\"wp-image-2257\" srcset=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/07\/datasheet-5-1024x253.jpg 1024w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/07\/datasheet-5-300x74.jpg 300w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/07\/datasheet-5-768x190.jpg 768w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/07\/datasheet-5.jpg 1415w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p class=\"has-text-color has-link-color wp-elements-3fc6f4c4f74c823ca7097d3c6c0a19b3 wp-block-paragraph\" style=\"color:#5c5c5c\">Here the slave address is 0b0110100 or 0x34. The address byte has auto increment value. The first address is 0x00 which is seconds. The function is-<\/p>\n\n\n\n<p class=\"has-text-color has-link-color has-upper-heading-font-size wp-elements-d51e84ef4d2e8fc7dcff4048931029fa wp-block-paragraph\" style=\"color:#6c8a97\">uint8_t I2C_Write_RTC(uint8_t min,uint8_t hr,uint8_t day,uint8_t date,uint8_t month,uint8_t year)<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; auto-links: false; title: ; quick-code: false; notranslate\" title=\"\">\n#define RTC_Write_Addess    0xD0 \/\/0b11010000\nuint8_t I2C_Write_RTC(uint8_t min,uint8_t hr,uint8_t day,uint8_t date,uint8_t month,uint8_t year)\n{\n\tI2C_Start();     \/\/ Start condition\n\tI2C_Write(RTC_Write_Addess);\n\tI2C_Write(0x00); \/\/ For intial address 0x00\n\tI2C_Write(0);    \/\/ Initial Second for value 0 \n\tI2C_Write(min);  \/\/ Write Minute value\n\tI2C_Write(hr);   \/\/ Write hour value\n\tI2C_Write(day);  \/\/ Write day value 0 for Sunday\n\tI2C_Write(date); \/\/ Write date of month\n\tI2C_Write(month);\/\/ Write month value\n\tI2C_Write(year); \/\/ Write year value\n\tI2C_Write(0x90); \/\/ Square wave with 1Hz output\n\tI2C_Stop();\n}\n<\/pre><\/div>\n\n\n<p class=\"has-text-color has-link-color wp-elements-bf5c19daae1fd5c13526fed4ee20a356 wp-block-paragraph\" style=\"color:#5c5c5c\">Example: write the time 10:37am 16\/11\/23 Thursday is<\/p>\n\n\n\n<figure class=\"wp-block-image size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"71\" src=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/07\/function-3-1024x71.jpg\" alt=\"\" class=\"wp-image-2260\" style=\"width:750px;height:auto\" srcset=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/07\/function-3-1024x71.jpg 1024w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/07\/function-3-300x21.jpg 300w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/07\/function-3-768x53.jpg 768w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/07\/function-3.jpg 1365w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p class=\"has-text-color has-link-color wp-elements-8b0a9fb065ca5a0076fdfc5448d33489 wp-block-paragraph\" style=\"color:#5c5c5c\">In C we can\u2019t return multiple values from a function directly. We can return more than one values from a function by using the method \u201ccalled by address\u201d or \u201ccall by reference\u201d. In the invoke function, we can use two variables to store the results and the function will take pointer data type. So we have to pass the address of the data. We use this return method function to read the date and time from RTC. First look the I<sup>2<\/sup>C protocol for read from RTC-<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"239\" src=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/07\/datasheet-6-1024x239.jpg\" alt=\"\" class=\"wp-image-2264\" srcset=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/07\/datasheet-6-1024x239.jpg 1024w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/07\/datasheet-6-300x70.jpg 300w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/07\/datasheet-6-768x180.jpg 768w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/07\/datasheet-6.jpg 1399w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p class=\"has-text-color has-link-color wp-elements-ca4d27492d7044f59cabdc64cf39bdc5 wp-block-paragraph\" style=\"color:#5c5c5c\">The read from 0x00 register and we use multiple return function using pointer-<\/p>\n\n\n\n<p class=\"has-text-color has-link-color has-upper-heading-font-size wp-elements-49773f81ce7574fd101a50ca0ad7256f wp-block-paragraph\" style=\"color:#6c8a97\">uint8_t I2C_Read_RTC(int *sec, int *mi, int *hr, int *da, int *dat, int *mon, int *yr)<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; auto-links: false; title: ; quick-code: false; notranslate\" title=\"\">\n#define RTC_Read_Addess\t\t0xD1 \/\/0b11010001\nuint8_t I2C_Read_RTC(int *sec, int *mi, int *hr, int *da, int *dat, int *mon, int *yr)\n{\n    I2C_Start();\n\tI2C_Write(RTC_Write_Addess);\n\tI2C_Write(0x00);\n\tI2C_Start();\n\tI2C_Write(RTC_Read_Addess);\n\t*sec=I2C_Read_Ack();\n\t*mi=I2C_Read_Ack();\n\t*hr=I2C_Read_Ack();\n\t*da=I2C_Read_Ack();\n\t*dat=I2C_Read_Ack();\n\t*mon=I2C_Read_Ack();\n\t*yr=I2C_Read_Nack();\n\tI2C_Stop(); \n\t\n}\n<\/pre><\/div>\n\n\n<p class=\"has-text-color has-link-color wp-elements-023e3faea7cf8e9a2d0b8ec92b7943bc wp-block-paragraph\" style=\"color:#5c5c5c\">The reading procedure is from main function, where we call the function we create.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; auto-links: false; title: ; quick-code: false; notranslate\" title=\"\">\nvolatile int second,minute,hour,day,date,month,year;\nI2C_Read_RTC(&amp;second,&amp;minute,&amp;hour,&amp;day,&amp;date,&amp;month,&amp;year);\n<\/pre><\/div>\n\n\n<p class=\"has-text-color has-link-color wp-elements-538da8411f9d820ade0c20dd9e296cb1 wp-block-paragraph\" style=\"color:#5c5c5c\">From the function second, minute, hour, day, date, month and year represent the real time value. Let\u2019s go back to the datasheet, address 0x02 bit 6 represent the 12\/24 hour mode and bit 5 represent the am\/pm mode. If we select 24 hour mode then no need for am\/pm. But if we set 12 hour mode then we must read the 5<sup>th<\/sup> bit for am\/pm count. You have learn how the RTC works and how to read. We make a program to select the desire time and select the start button to start your RTC clock. Here just look at the pin and their function -&gt; MCU: ATmega8<\/p>\n\n\n\n<figure class=\"wp-block-image size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"130\" src=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/07\/image-2-1024x130.png\" alt=\"\" class=\"wp-image-2270\" style=\"width:662px;height:auto\" srcset=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/07\/image-2-1024x130.png 1024w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/07\/image-2-300x38.png 300w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/07\/image-2-768x98.png 768w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/07\/image-2.png 1334w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p class=\"has-text-color has-link-color wp-elements-f1c6e1cbe71f67d0ad7401184a5bbe98 wp-block-paragraph\" style=\"color:#5c5c5c\">Set your parameter and press start for RTC clock, the data will show in LCD display. Follow the connection diagram.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"531\" src=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/07\/RTC-1024x531.jpg\" alt=\"\" class=\"wp-image-2275\" srcset=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/07\/RTC-1024x531.jpg 1024w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/07\/RTC-300x156.jpg 300w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/07\/RTC-768x398.jpg 768w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/07\/RTC.jpg 1431w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/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\" style=\"flex-basis:33.33%\">\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<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\/08\/DS1307-With-Input-SW.rar\" style=\"padding-top:var(--wp--preset--spacing--30);padding-right:var(--wp--preset--spacing--40);padding-bottom:var(--wp--preset--spacing--30);padding-left:var(--wp--preset--spacing--40)\">download<\/a><\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n\n\n\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\" style=\"flex-basis:66.66%\">\n<p class=\"has-text-color has-link-color has-upper-heading-font-size wp-elements-2b0177ad7ee578066cb5e844c1d8a21f wp-block-paragraph\" style=\"color:#252525\"><a href=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/08\/DS1307-With-Input-SW.rar\">DS1307 With Input SW.rar<\/a><\/p>\n<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>The DS1307 serial real time clock is a low power, full binary-coded decimal (BCD) clock\/calendar plus 56bytes of NV SRAM. Address and data are transmitted serially via 2-wire, bi-directional bus. The clock\/calendar provides seconds, minutes, hour, day, date, month and year information. The end date at the month date is automatically adjusted for months with [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[19],"tags":[],"class_list":["post-2241","post","type-post","status-publish","format-standard","hentry","category-i2c"],"_links":{"self":[{"href":"https:\/\/iotthinghub.com\/index.php?rest_route=\/wp\/v2\/posts\/2241","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=2241"}],"version-history":[{"count":15,"href":"https:\/\/iotthinghub.com\/index.php?rest_route=\/wp\/v2\/posts\/2241\/revisions"}],"predecessor-version":[{"id":2648,"href":"https:\/\/iotthinghub.com\/index.php?rest_route=\/wp\/v2\/posts\/2241\/revisions\/2648"}],"wp:attachment":[{"href":"https:\/\/iotthinghub.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2241"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/iotthinghub.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2241"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/iotthinghub.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2241"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}