This post is also available in:
- DS1307 – Real Time Clock
- DS1307 Operating Principles
- DS1307 Real Time Clock Circuit
- DS1307 Real Time MikroC Library
- RESULT
- Library Files
- References
1-DS1307 – Real Time Clock
- We will discuss clock and calendar construction using the DS1307 integration. Here we will first examine in detail the internal structure of the DS1307 and how to use it so that you can use the mikroC library you wrote or you can create your own library in this light.
2-DS1307 Operating Principles
- The DS1307 integration is a real-time clock integration that works with the I2C protocol.
- It contains 64x8k SRAM. SRAM means briefly as long as there is electricity, the information is stored, and once the power is off, the information is deleted.
- In some part of RAM it stores seconds, minutes, hours, day, date, month, and year information.
- DS1307 can date up to a maximum of 2100 years, which is quite sufficient
- It is supported by one 3.3v flat battery (Lithium) for smooth operation of the clock when the power is cut off. Accordingly, the battery enters the circuit without loss of energy when the power is cut off and can operate for about 10 years with 1 battery
- One of the other nice features of the DS1307 is that it calculates the variable month of February and 30 or 31 days of the month according to years and does not bother the user.
- DS1307 requires a 32.768khz crystal, which we can call heart rhythm.
- In short, there is a system in the DS1307, which keeps counting and storing data in the SRAM, which means that data must be understood and read here.
2A- DS1307 Pins and Their Functions
PIN No | PIN Name | Role |
1 | X1 | These pins are pins which 32,768khz crystal is to be connected |
2 | X2 | |
3 | Vbat | This pin is the pin is the pin to be connected to the (+) end of the battery, which allows the clock to run when the power is turned off. (-) end is connected to GND |
4 | GND | GND |
5 | SDA | Serial data pin. Used in the I2C protocol, which pin is used as SDA on PIC is connected here |
6 | SCL | Clock pin. Used in I2C protocol which pin is used as SDA on PIC is connected here |
7 | SQW/OUT | A number of DS1307 signal outputs can be received, this feature is disabled at my own circuit and in my library, there is no effect on normal clock operation. |
Vcc | Main supply pin. The DS1307 should be fed with 5 volts from the mains supply. |
2B – Reading the RAM Map from the DS1307
The table below is the RAM map of the DS1307. According to this, we will understand which address and which data should be read and evaluated. Addresses are written in hexadecimal format.
ADDRESS | 7Bit | 6Bit | 5Bit | 4Bit | 3Bit | 2Bit | 1Bit | 0Bit | FUNCTION | BIT |
00h | CH | 10 Second | Second | Second Data | 00-59 | |||||
01h | 0 | 10 Minute | Minute | Minute Data | 00-59 | |||||
02h | 0 | 12/24 | 10h./Pm | 10h. | Hour | Hour Data | 1-12/00-23 | |||
03h | 0 | 0 | 0 | 0 | 0 | Day | Day Data | 01-07 | ||
04h | 0 | 0 | 10 Date | Date | Date Data | 01-31 | ||||
05h | 0 | 0 | 0 | 10 Month | Month | Month Data | 01-12 | |||
06h | 10 Year | Year | Year Data | 00-99 | ||||||
07h | OUT | 0 | 0 | SQWE | 0 | 0 | RS1 | RS0 | Control | – |
Important Note: The data stored in the DS1307 is stored in BCD format. ie if we think about the second data, for example, we read the address that stores the second data, so we have data like our confusion 00100101 . When we read this, we must read it separately as 4 bits, we understand that the first 4 bits (0010=2) and the last 4 bits (0101=5) then it is at 25th seconds. Since all data are stored as BCD, they should be broken down and evaluated.
Important Note2 : There is no need to reselect the address because it automatically increases the pointer when an address is selected in the DS1307. For example, if we start reading at 00h, we will select 00h once, and then every time the readout automatically switches to the next address, there is no need to select an address each time.
Let’s examine the addresses now.
-
00h Address Review – Seconds
- CH = This address has an important function beside the second information. This is the CH section as you can see in 7thbit. When the clock is set, this bit is set to 1 to stop the clock, then the 7th bit is reset to zero and the clock starts running.
- 10 Seconds= “10 seconds” section keeps a decade of a second. In other words, if we say 25 seconds, “2” and “5” are gets seperated. “2” data is stored in 3 bits in this section (6thbit-5thbit-4thbit =010).
- Second = In this section, the unity part is stored. In other words, if we say 25 seconds, it is divided into “2” – “5“, and “5” data is stored in this 4 bits of this section (3rdbit-2ndbit-1stbit-0thbit = 0101)
- Dimension= The second, naturally, will take the value 0-59, so it specifies the lowest and highest point of the value to be read from here.
-
01h Address Review – Minute
- 10 Minutes = “10 minutes” section keeps a ten-minute portion of the minute. In other words, when we say 59 minutes, it gets seperated as “5” – “9” and”5” data is stored in 3 bit section (6thbit-5thbit-4thbit = 101).
- Minute= In this section first digit of the minute is stored. When we say 59 minutes it gets seperated as “5” – “9“and “9” data is stored in 4 bit section (3rdbit-2ndbit-1stbit-0thbit=1001).
- Dimension= The second, naturally, will take the value 0-59, so it specifies the lowest and highest point of the value to be read from here.
-
02h Address Review – Hour
- 12/24 = Time display format is selected from 24 or 12 type
- If 12 hours type to be selected, then 6thbit = 1
- If 24 hours type to be selected, then 6thbit = 0
- 10 Hours- PM/AM = If;
- At 6thbit, if 24 hours type is selected = as the clock will have value between 00-24 at this bit, in order to provide 2 value it must be read along with 5th bit because the binary meaning of 2 is “10“. The 10 part of clock is kept here. When we say it is 24:00 o’clock then it gets seperated as “2“-“4“. “2” value is kept at 5thbit and 6th bit.
- At 6. bit, if 12 hours type is selected seçilmiş ise = According to the 12 hours basis, time gets defined as PM or AM and accordingly hour can get value between 01-12. Example: 12 at noon -> PM 12:00 12 at the evening -> AM 12:00 .When DS1307 counts, it does not go to 13 after 12, but goes directly to 01.
- Hour = The first digit of the hour is kept here and when we say it is 24:00 o’clock, it gets seperated as “2“-“4“. The value “4” is kept in 4 bit (3rdbit-2ndbit-1stbit-0thbit=0100).
- Dimension = According to selection of 6. bit, 01-12(PM/AM) or 00-24 are the lowest and highest point of the value.
- 12/24 = Time display format is selected from 24 or 12 type
-
03h Address Review – Day
- Day = By this address, it is understood which day of the week it is. It should not be confused with which day of the month it is. So the data to be read here is only about the day of the week. According to this, it is read over 2ndbit-1stbit-0thbit
- Dimension = As a week consists of 7 days, it can be read between 1-7.
-
04h Address Review – Date
- 10 Date = This section states which day of the present month it is. According to this, September 29, for example, at this section “2” data will be stored.(5thbit-4thbit=10)
- Date = At this section the first digit of the date is stored. When September 29 is considered, it will be seperated as “2” – “9“. According to this 3rdbit-2ndbit-1stbit-0thbit = 1001.
- Dimension= One month consists of maximum 31 days therefore it takes value between 01-31.
-
05h Address Review – Month
- 10 Months = It states which month of the present year. At this section, second digit of the month is read. If it is 12th month, then, it happens to be “1” – “2” and “1” data is read from this section (5thbit-4thbit = 01)
- Month = First digit of the data of which month of the year is also read from here. According to this, 12th month is “1” – “2” therefore from this part “2” data (3rdbit-2ndbit-1stbit-0thbit=0010) is read from here.
- Dimension = Within 1 year there is maximum 12 months therefore it takes value between 01-12.
-
06h Address Review – Year
- 10 Years = In order to read year value, the data which are read must be added into 2000 value. If we read 18 value in this address, it means 2018. Similarly “1” – “8” are seperated and the second digit “1” must be read.
- Year = At this section “8” value is read which is first digit of “18“.
- Dimension = DS1307 is able to count until year 2100, therefore at this section the value is read between 00-99.
-
07h Address Review – Signal Generator
- At this section DS1307 we can receive signals at some frequencies. I cancelled this feature in my own library and in my own circuit. you do not have to use it to run the clock.
- You can see from the table below which bits produce which frekans signal in which case
- At this section DS1307 we can receive signals at some frequencies. I cancelled this feature in my own library and in my own circuit. you do not have to use it to run the clock.
RS1 | RS0 | SQW/OUT | SQWE | OUT |
0 | 0 | 1HZ | 1 | x |
0 | 1 | 4.096kHz | 1 | x |
1 | 0 | 8.192kHz | 1 | x |
1 | 1 | 32.768kHz | 1 | x |
x | x | 0 | 0 | 0 |
x | x | 1 | 0 | 1 |
2C – DS1307 Running Protocols
- In this section we will examine the order in which the DS1307 communicates, the block diagrams for reading data and writing data.
- These will help us to understand in which order the functions that are written to us should be called.
- Each part that will work with I2C in the I2C protocol has an address.
- The communication address for DS1307 is (Not RAM address!) 1101000 dır.
-
DS1307 Data Writing System
- As can be seen from the above picture, when the communication with the S-start bit is started, the parts connected to the entire I2C line are notified so that the parts come to the stand-by position.
- Then if the specific address(7 bit) of the part is sent, that piece will now communicate with us and others will disconnect from the communication
- If we are going to do read in this case 1 (one); if writing 0 (zero) bits are sent
- A – Acknowledge bit comes to MCU from DS1307 that is, after acknowledge is received which RAM address of DS1307 is going to be proceeded, then this time that address is sent.
- Again, when the acknowledge comes, the data to be written this time is sent. Dates are sent 8 bits after each 8 bits, acknowledgement is received from DS1307 and the process loops in this way.
- When the communication is to be finished, Pause (P) bit is sent by MCU and therefore the communication finishes.
- As mentioned in the picture, the red bits are sent from the MCU to the DS1307, and the black ones are the bits coming from the DS1307 to MCU.
-
DS1307 Data Reading System
- Communication again starts with start bit and then DS1307 address is sent and communication gets provided.
- It is stated that 8. bit to be 1 and the reading will be made in this way and after receiving acknowlegment code it is read as groups of 8 bits each.
- After reading is done, and data is received this time acknowledgement code is sent from MCU to DS1307 therefore DS1307 automatically goes to next address if there is no acknowledgement, it keeps reading the same address.
- At last when the communication is to be cut, after last read data group instead of acknowledgement code, error code is sent from NACK MCU to DS1307 inauthentically therefore DS1307 is informed that reading is done. If the communication is interrupted without sending this NACK signal, the DS1307 can increment the address and proceed to the next address, and if one more reading is made, 1 address line is skipped and an erroneous reading is made.
- An important distinction here is that the diagram above only describes the data communication part of the reading process, but it is necessary to apply another pre-protocol before reading. We will see this below.
-
DS1307 Complete Data Reading Routine
- Communication starts with start bit. The moment DS1307 address is sent and if it is well noticed, it must be noted that 8. bit should be changed to 0 for writing step.
- This is to inform the DS1307 of the RAM address to be read because we can not transfer any information to the DS1307 if we do reading it; this is why it can be selected in writing mode to read from which RAM address.
- Then the restart bit is sent, this time it is ready to read, the 8th bit is 1 and the reading is done and terminated as seen in the previous topic.
3- DS1307 Real-Time Clock Circuit
- In this section we will see the basic circuit required to use the DS1307.
- I set up the circuit as usual because it is a test circuit, you can set it up in a copper plaque
3A – DS1307 Circuit Diagram
- saat_SCL and saat_SDA directly connected to PIC relavant branches.
- pull-up must be done to these.
3B – Required Materials
- 1AD 32,768 khz Crystal
- 2 AD 20 k resistance
- 1 AD 3v battery socket and Lithyum battery
- 1 AD 100 nf polyester condenser
- 1 AD DS1307 integration
- 1 AD 8li integration socket
Note: If all lithium batteries are not charged, be sure to check the code above to see if it is charged. otherwise attempting to recharge the uncharged batteries may cause them to explode.
3C – Circuit Preparation
- The circuit is simple and sophisticated, but a matter is very important in circuit setup.
- The biggest problem in clock circuits is the time shift. One of the reasons is that the crystal is not at a sufficient level; the other is that the necessary precautions are not taken when installing the circuit.
- Crystal, must be taken from those made for clock circuits
- The crystal should be attached to the nearest point to the DS1307.
- The outer sheath of the crystal must be grounded.
- These measures provide a more stable working clock. My clock has been around for about 3 months now and it has been working without stopping and now there is not any major deviation, especially I am changing the temperature of the room, there is only 2-3 seconds time shift, sometimes accelerating sometimes slowing down, I have not experienced any deviation other than that.
- Another issue is that I have received smd crystal before from China, maybe these crystals can be more stable. there are no metal covers and there is FC written on it. As far as I read, it means FINE CRYSTAL. but I am not sure about this information and I have not tried smd crystal. If there is someone who try them out, I’d appreciate if they inform me about this.
4- DS1307 Real-Time Clock MicroC Library
- In order to use the DS1307 library I wrote in MicroC, you need to first download I2C library which I published earlier and add it into your project.
- Since all the functions in the library are user functions, no color separation is made.
- In the library, there are functions to convert from BCD to Decimal and Decimal to BCD.
4A- Functions
- We will now briefly describe the functions with their parameters and, at the end, explain with a comprehensive example.
4A – (A) Conversion Functions
– BCD_DESIMAL_CEVIRICI Function
Function : unsigned char BCD_DESIMAL_CEVIRICI(unsigned char BCD_KOD)
Purpose : Converts 8 bit BCD format binary code to decimal.
Parameters:
- BCD_KOD : The BCD code to be translated is written here
Its use :
BCD_DESIMAL_CEVIRICI(dakika); // converts minute (variable) to decimal
Feedback : The result is sent as feedback.
– DESIMAL_BCD_CEVIRICI Function
Function: unsigned char DESIMAL_BCD_CEVIRICI(unsigned char DESIMAL)
Purpose: Converts 8 bit BCD format binary code to decimal.
Parameters:
- DESIMAL : The BCD code to be translated is written here
Its Use :
DESIMAL_BCD_CEVIRICI(DAKIKA);// converts minute (variable) to BCD format
Feedback : The result is sent as feedback.
4A – (B) Clock Reading Functions
– DS1307_SANIYE_OKU Function
Function : unsigned char DS1307_SANIYE_OKU()
Purposr : Reads second.
Parameters : N/A
Its Use :
DS1307_SANIYE_OKU();//reads second value
Feedback:
- Takes the second data back.
- The received data is in the decimal range and there is no need to do any conversion.
– DS1307_DAKIKA_OKU Function
Function : unsigned char DS1307_DAKIKA_OKU()
Purpose : Reads minute data
Parameters : N/A
Its Use :
DS1307_DAKIKA_OKU();//reads minute value
Feedback :
- Takes the minute data back
- The received data is in the decimal range and there is no need to do any conversion.
– DS1307_SAAT_OKU Function
Function : unsigned char DS1307_SAAT_OKU()
Purpose : Reads hour data
Parameters : N/A
Its Use:
DS1307_SAAT_OKU();//reads hour value
Feedback :
- Takes the hour data back
- The received data is in the decimal range and there is no need to do any conversion
4A – (C) Date Reading Function
– DS1307_GUN_OKU Function
Function : unsigned char DS1307_GUN_OKU()
Purpose : Reads the which day of the week data
Parameters : N/A
Its Use :
DS1307_GUN_OKU() ; //reads the which day of the week data
Its Use :
- It returns the result
- It states which day of the week it is in numbers. Example : Monday = 1 etc.
– DS1307_AYIN_KACI_OKU Function
Function : unsigned char DS1307_AYIN_KACI_OKU()
Purpose : Reads which day of the month it is
Parameters : N/A
Its Use :
DS1307_AYIN_KACI_OKU() ; //reads which day of the month it is
Feedback :
- It returns the result
- Reads which day of the month it is.
– DS1307_AY_OKU Function
Function : unsigned char DS1307_AY_OKU()
Purpose: Reads information for which month of the year it is. Example January = 1 etc
Parameters : N/A
Its Use:
DS1307_AY_OKU();// reads which month it is
Feedback :
- It returns the result
- Reads which month of the year it is.
– DS1307_YIL_OKU Function
Function : unsigned int DS1307_YIL_OKU()
Purpose : Reads year information. Gives the year information completely . Example : 2017 etc
Parameters : N/A
Its Use :
DS1307_YIL_OKU();// reads year value
Feedback:
- It returns the result
- Provides year information
4A – (D) Hour and Date Setting Functions
– DS1307_SAAT_TARIH_AYARLA Function
Function : unsigned char DS1307_SAAT_TARIH_AYARLA(unsigned char SANIYE,unsigned char DAKIKA,unsigned char SAAT,unsigned char GUN,unsigned char AYIN_KACI,unsigned char AY,unsigned int YIL)
Purpose : Sets time and date.
Parameters :
- SANIYE: It is to set the second, takes value between 0-59.
- DAKIKA : It is to set the minute, takes value between 0-59.
- SAAT : It is to set the hour, takes value between 0-24. ( The library has a 24-hour system only)
- GUN : It is to set which day of the week it is. Takes value between 1-7
- AYIN_KACI : It is to set which day of the week it is. According to the month to be selected, it can take 1 – 30/31/28
- AY : It is to set which month it is. Takes value between 1-12.
- YIL : It is to set which year it is. Takes value between 2000-2100.
Its Use :
DS1307_SAAT_TARIH_AYARLA(0,5,3,6,16,12,2017);// Time: 03:05:00 (5 past 3 at night) Date: 16.12.2017 Saturday(6th day)
Feedback :
- If “1” value appears, then the process is successful.
4A – (E) Alarm Function
– ALARM_ZAMAN Function
Function : unsigned char ALARM_ZAMAN(unsigned char SANIYE,unsigned char DAKIKA,unsigned char SAAT)
Purpose: Sets alarm according to the time.
Parameters:
- SANIYE : It is to write the second we want the alarm to run
- DAKIKA : It is to write the minute we want the alarm to run
- SAAT : It is to write the hour we want the alarm to run
Its use :
ALARM_ZAMAN(0,10,23);// (00:10:23 - 10 past 11 in the evening) alarm is activated when all parameters are correct
Feedback :
- If “1” value appears, then it means the alarm successfully worked .
– ALARM_TARIH Function
Function : unsigned char ALARM_TARIH(unsigned char GUN,unsigned char AYIN_KACI,unsigned char AY,unsigned int YIL)
Purpose : Sets alarm according to the time
Parameters :
- GUN : It is determine which day of the week it is
- AYIN_KACI: It is to determine which day of the month it is
- AY: It is to determine which month it is
- YIL: It is to determine which year it is
Its Use :
ALARM_TARIH(01,15,01,2018);// accordingly, the alarm will be active on January 15th, Monday, 2018
Feedback :
- If “1” value appears, then it means the alarm successfully worked .
4B- Application of Functions
- We will see the following 2 examples.
- 1st – The code we will use to set the clock
- 2nd – The code we will use to read the clock
- I wrote the values I read in the GLCD using my own library, so you can print these values as you like.
- Let’s see below for setting the clock
//I2C protocol pin settings sbit I2C_SDA at RA5_bit; sbit I2C_SCL at RE0_bit; sbit I2C_SDA_VERI at LATA5_bit;//27.01.2019 güncellemesi ile eklendi sbit I2C_SDA_Direction at TRISA5_bit; sbit I2C_SCL_Direction at TRISE0_bit; void main() { ADCON1=13; CMCON=7; DS1307_SAAT_TARIH_AYARLA(0,5,3,6,16,12,2017);//date and time are set }
- Firstly, we load the above code on the PIC. We should load 2-3 minutes ahead of the PC clock, and when the clock arrives, we will have to run PIC so the DS1307 will load the correct date and time.
- One thing to keep in mind though: this code should be used 1 time only and it should not be used again. Otherwise it reloads the old date and time. We have to disable this code once we have used it.
- Now let’s read the time and date
//I2C sbit I2C_SDA at RA5_bit; sbit I2C_SCL at RE0_bit; sbit I2C_SDA_VERI at LATA5_bit;//27.01.2019 güncellemesi ile eklendi sbit I2C_SDA_Direction at TRISA5_bit; sbit I2C_SCL_Direction at TRISE0_bit; void main() { unsigned char txt[7];// stringe this is created for conversion ADCON1=13; CMCON=7; SAP1024_INIT (240,128,6);//GLCD I run this while(1)// an infinite loop was set up to read the time constantly { ShortToStr(DS1307_SANIYE_OKU(),txt);// second is read and converted to string SAP1024_YAZI_YAZMA(7,1,txt);// second data is printed in GLCD SAP1024_YAZI_YAZMA(8,1,":");// ":" punctuation is added in between ShortToStr(DS1307_DAKIKA_OKU(),txt);//minute is read and converted to string SAP1024_YAZI_YAZMA(4,1,txt);// minute data is printed in GLCD SAP1024_YAZI_YAZMA(5,1,":");// ":" punctuation is added in between ShortToStr(DS1307_SAAT_OKU(),txt);// hour is read and converted to string SAP1024_YAZI_YAZMA(1,1,txt);// hour data is printed in GLCD intToStr(DS1307_YIL_OKU(),txt);//year is read and converted to string SAP1024_YAZI_YAZMA(7,3,txt);// year data is printed in GLCD SAP1024_YAZI_YAZMA(8,3,"."); ":" punctuation is added in between ShortToStr(DS1307_AY_OKU(),txt);//month is read and converted to string SAP1024_YAZI_YAZMA(4,3,txt);// month data is printed in GLCD SAP1024_YAZI_YAZMA(5,3,"."); "." punctuation is added in between ShortToStr(DS1307_AYIN_KACI_OKU(),txt);//which day of the month data is read and converted to string SAP1024_YAZI_YAZMA(1,3,txt);// which day of the month data is printed in GLCD delay_ms(1000);// we made sure once every second the clock information ia being read } }
- In the above code, the read values were converted to string and the GLCD got written. I use the library “SAP1024B” which I wrote as GLCD. For the library you use, you should use its printing functions in GLCD.
- As you can see, there is no function to set the clock.
- After that moment, the DS1307 is independent of the PIC as long as the battery is plugged in. If the code loaded into the PIC is erased, the power is cut, they do not affect the DS1307, and once the date is written into it, the battery keeps counting, so we can continue to count DS1307 independently.
RESULT
- You have learned the internal structure and use of the DS1307. With this information, you can write your own library and use it in your projects, or you can use the library I wrote.
- Above you can see the data read by LEDs or DISPLAY, even though it is printed on the GCLD, it is totally up to you.
- You can ask your questions to the Forum.
Library Files
- DS1307 IC with Clock Circuit (RTC) MikroC Library – .mcl – (FREE)
Bağlantıyı Görmek İçin Giriş Yapın ya da Ücretsiz Üye Olun
- I2C MikroC Library – .mcl – (FREE)
Bağlantıyı Görmek İçin Giriş Yapın ya da Ücretsiz Üye Olun
References
- Application Note 58 Crystal Considerations for Dallas Real-Time Clocks
- Application Note 58 Crystal Considerations with Dallas Real Time Clocks
- Design Considerations for Maxim Real-Time Clocks
- DS1307 64 x 8, Serial, I2C Real-Time Clock
This post is also available in: