Author Topic: I2C Real Time Clock - DS1307  (Read 10265 times)

OmarZ

  • Hero Member
  • *****
  • Posts: 243
    • View Profile
I2C Real Time Clock - DS1307
« on: May 08, 2007, 02:38:27 PM »
I2C Real Time Clock DS1307.
The DS1307 serial real-time clock (RTC) is a low-power, full binary-coded decimal (BCD) clock/calendar plus 56 bytes of NV SRAM. Address and data are transferred serially through an I2C, bidirectional bus. The clock/calendar provides seconds, minutes, hours, day, date, month, and year information. The end of the month date is automatically adjusted for months with fewer than 31 days, including corrections for leap year. The clock operates in either the 24-hour or 12-hour format with AM/PM indicator. The DS1307 has a built-in power-sense circuit that detects power failures and automatically switches to the backup supply. Timekeeping operation continues while the part operates from the backup supply.

Key Features
- Real-Time Clock (RTC) Counts Seconds, Minutes, Hours, Date of the Month, Month, Day of the week, and Year with Leap-Year Compensation Valid Up to 2100
- 56-Byte, Battery-Backed, Nonvolatile (NV) RAM for Data Storage
- I2C Serial Interface
- Programmable Square-Wave Output Signal
- Automatic Power-Fail Detect and Switch Circuitry
- Consumes Less than 500nA in Battery-Backup Mode with Oscillator Running

Full data sheet at http://datasheets.maxim-ic.com/en/ds/DS1307.pdf

_________________________________________________________________________________________________________

Schematic, Board and code.

define somewhere global structure
Code: [Select]
            struct
              {
                BYTE_VAL Flags_1;
                BYTE_VAL Flags_2;
               struct
                {
                 BYTE ss;
                 BYTE mm;
                 BYTE hh;
                } Time;
               struct
                {
                 BYTE wd;
                 BYTE dd;
                 BYTE mm;
                 BYTE yy;
                } Date;
               BYTE Alive;
              } DB_SystemClock;

« Last Edit: May 09, 2007, 12:03:36 AM by OmarZ »

OmarZ

  • Hero Member
  • *****
  • Posts: 243
    • View Profile
Re: I2C Real Time Clock - DS1307
« Reply #1 on: May 09, 2007, 12:06:22 AM »
System Clock Management

OmarZ

  • Hero Member
  • *****
  • Posts: 243
    • View Profile
Re: I2C Real Time Clock - DS1307
« Reply #2 on: May 10, 2007, 03:18:20 PM »
Usage:

in main
Code: [Select]

    FC_SystemClock(0);//update ons and wave flags and DB_SystemClock.Alive

in interrupt section
Code: [Select]
    //TMR0 is used for the ticks
    if (INTCON_TMR0IF)
    {
        TickUpdate();
        FC_SystemClock(1); //o.z.
        #if defined(STACK_USE_SLIP)
        MACISR();
        #endif
        INTCON_TMR0IF = 0;
    }

Donatus

  • Jr. Member
  • **
  • Posts: 6
    • View Profile
Re: I2C Real Time Clock - DS1307
« Reply #3 on: October 03, 2007, 03:39:24 PM »
To get uSysClk.h to compile I needed to include "net/compiler.h" because it contains the declaration for BYTE_VAL

Thanks for sharing your code!

-D

OmarZ

  • Hero Member
  • *****
  • Posts: 243
    • View Profile
Re: I2C Real Time Clock - DS1307
« Reply #4 on: October 04, 2007, 12:17:58 PM »
This is modtronix stuff, so you can download SBC65 sources http://www.modtronix.com/products/sbc65ec/websrvr65_v306.exe.

Alain

  • Jr. Member
  • **
  • Posts: 5
    • View Profile
Re: I2C Real Time Clock - DS1307
« Reply #5 on: February 13, 2008, 05:58:14 PM »
Omar

In file "user\uSysClk.c" there is a line "#include "user\uMain.h""
Can you post file Main.h please ?

Did you made any update to those files (uDS1307 and uSysClk) ?

Thanks

Alain

Rudios

  • Newbie
  • *
  • Posts: 2
    • View Profile
Re: I2C Real Time Clock - DS1307
« Reply #6 on: April 25, 2008, 06:12:25 AM »
The thing I make up of your code is that the FC_SystemClock routine in uSysClk.c updates the seconds of the systemclock bytes, and requests a sync every time it changes from 59 (60) to 0.
This is done by setting DB_SystemClock_RequestRTCUpdate = 1.
This line has a comment saying that the main will ask the i2c routine to update from the RTC chip, but where can I find that, because I can't find any more occurences of the DB_SystemClock_RequestRTCUpdate variable.
The thing is that I request all the parameters of the created struct DB_SystemClock, but the only thing that changes are the seconds (because they are changed within the
FC_SystemClock. if I include a DS1307_ReadSysDateTime call inside my main routine, the fields get updated from the RTC into the struct, and will be displayed on the web page.

Thanks
« Last Edit: April 25, 2008, 06:15:07 AM by Rudios »

OmarZ

  • Hero Member
  • *****
  • Posts: 243
    • View Profile
Re: I2C Real Time Clock - DS1307
« Reply #7 on: May 04, 2008, 02:21:53 PM »
In main.c add

//update ons and wave flags and DB_SystemClock.Alive
FC_SystemClock(0);

//Update date/time from RTC
IF (DB_SystemClock_RequestRTCUpdate)
{
  DS1307_ReadSysDateTime();
}

DO NOT CALL FUNCTION IN INTERRUPT: this will cause unexpected results on i2c bus!

Tatsster73

  • Jr. Member
  • **
  • Posts: 9
  • http://russian-brides-best.com/ - all russian women
    • View Profile
    • women ru
I2C Real Time Clock DS1307
« Reply #8 on: December 04, 2009, 03:08:45 PM »
good project take your time do it right and you will have less problems in the long run

mohandino

  • Sr. Member
  • ****
  • Posts: 25
    • View Profile
Re: I2C Real Time Clock - DS1307
« Reply #9 on: December 21, 2010, 05:58:20 AM »
hello

Is these codes OK for ths sbc68ec and the 3.10 firmware ?

thanks