Sunday 16 February 2014

DS1307 SERIAL RTC (Real Time Clock) - AVR INTERFACING HEADER FILE - FUNCTION CODES


        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. The DS1307 operates as a slave device on the I2C bus. Access is obtained by implementing a START condition and providing a device identification code followed by a register address. Subsequent registers can be accessed sequentially until a STOP condition is executed.

        When VCC falls below 1.25 x VBAT, the device terminates an access in progress and resets the device address counter. Inputs to the device will not be recognized at this time to prevent erroneous data from being written to the device from an out-of-tolerance system. When VCC falls below VBAT, the device switches into a low-current battery-backup mode. Upon power-up, the device switches from battery to VCC when VCC is greater than VBAT +0.2V and recognizes inputs when VCC is greater than 1.25 x VBAT.


FEATURES
·         Real-Time Clock (RTC) Counts Seconds, Minutes, Hours, Date of the Month, Month, Day of the week, and Year with Leap-Year Compensation
·         56-Byte, Battery-Backed, General-Purpose RAM with Unlimited Writes
·         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
·         Optional Industrial Temperature Range: -40°C to +85°C
·         Available in 8-Pin Plastic DIP or SO

Interfacing Circuit Diagram:



FUNCTIONS OF I2C.H
    void I2CInit();
    void I2CStart();
    void I2CStop();
    unsigned char I2CWriteSLA(unsigned char sla);
    unsigned char I2CWriteByte(unsigned char dat);
    unsigned char I2CReadByte(unsigned char *data);

FUNCTIONS IN DS1307.H
    DSinit();
        To initialise DS1307 RTC chip

    DSReadTime(&h, &m, &s)
        To read time hr, min, sec from RTC

    DSReadDate(&d, &m, &y)
        To read date from RTC
   
    DSReadDay(&dy)
        To read day from RTC

    DSWriteDay(dy)
        To write day to RTC


REGISTER MAP OF DS1307 RTC

  NOTE:
              CH is Clock Halt bit,  OSC disabled if CH=1
               12/24~ =1 -> 12 hour mode, bit5 0=AM  1=PM
                            =0 -> 24 hour mode, bit5 10hour

Control Register:
Bit 7: Output Control (OUT). This bit controls the output level of the SQW/OUT pin when the square-wave output
is disabled. If SQWE = 0, the logic level on the SQW/OUT pin is 1 if OUT = 1 and is 0 if OUT = 0. On initial
application of power to the device, this bit is typically set to a 0.

Bit 4: Square-Wave Enable (SQWE). This bit, when set to logic 1, enables the oscillator output. The frequency of
the square-wave output depends upon the value of the RS0 and RS1 bits. With the square-wave output set to 1Hz,
the clock registers update on the falling edge of the square wave. On initial application of power to the device, this
bit is typically set to a 0.

Bits 1 and 0: Rate Select (RS[1:0]). These bits control the frequency of the square-wave output when the squarewave
output has been enabled. It can be set to 1Hz, 4.096KHz, 8.192KHz or 32.768KHz On initial application of power to
the device, these bits are typically set to a 1.

/*
THIS WORK IS INTENDED TO BE USED FOR HOBBY & LEARNING PURPOSE ONLY. NO PART OF THIS CAN BE PUBLISHED OR USED IN COMMERCIAL PRODUCTS WITHOUT A WRITTEN PERMISSION FROM ELECDUDE.

#ifndef _ds1307_H

#define _ds1307_H
#include <util/twi.h>

#ifndef BIT
#define BIT(x)    _BV(x)

0 comments:

Post a Comment

Search Here...