Site icon Ercan Koçlar

Real Time Clock Circuit and MikroC Library with DS1307 Integration

DS1307-gercek-zamanli-saat

DS1307-gercek-zamanli-saat

This post is also available in: Türkçe (Turkish)

  1. DS1307  – Real Time Clock
  2. DS1307   Operating Principles
  3. DS1307 Real Time Clock Circuit
  4. DS1307 Real Time MikroC Library
  5. RESULT
  6. Library Files
  7. References

1-DS1307  – Real Time Clock


2-DS1307  Operating Principles

With a 3 volt lithium battery, DS1307 can operate for up to 10 years

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 secondsSince 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.

 

 

 

 

 

 

 

 

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

 

DS1307 data writing block diagram

 

DS1307 data reading block diagram
DS1307 Data Reading System Complete Protocol

 

3- DS1307 Real-Time Clock Circuit

3A – DS1307 Circuit Diagram

DS1307 circuit diagram

3B – Required Materials

  1. 1AD 32,768 khz Crystal
  2. 2 AD 20 k resistance
  3. 1 AD 3v battery socket and Lithyum battery
  4. 1 AD 100 nf polyester condenser
  5. 1 AD DS1307 integration
  6. 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

DS1307-clock-devresi
32.768khz kristal
The outer sheath of the crystal must be grounded.
smd crystal

 

4- DS1307 Real-Time Clock MicroC Library

4A- Functions

 

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:

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:

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:

 

– 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 :

– 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 :

 

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 :

 

– 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 :

 

– 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 :

– 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:

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 :

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 :

 

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:

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 :

 

– 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 :

Its Use :

ALARM_TARIH(01,15,01,2018);// accordingly, the alarm will be active on January 15th, Monday, 2018

Feedback :

 

4B- Application of Functions

//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
}
//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 
  }

}
Getting printed date and time in GLCD which are read via DS1307

DS1307 Work – gif file

RESULT


Library Files

 


References


 

This post is also available in: Türkçe (Turkish)

Exit mobile version