Friday 30 January 2015

interfacing DS1307 (RTC) with AVR By Elecdude



//***********************************************************************************//
 Author: ElecDude
         admin@elecdude.com        

 Please report bugs, errors, modifications, etc. Thank you

 Copyright - 2015 - ElecDude

 USAGE AND REDISTRIBUTION OF THIS SOURCE CODE IS PERMITTED PROVIDED THAT
 THE FOLLOWING CONDITIONS ARE MET:

    1. REDISTRIBUTIONS OF SOURCE CODE MUST RETAIN THE ABOVE ORIGINAL COPYRIGHT
  NOTICE AND THE ASSOCIATED DISCLAIMER, THIS LIST OF CONDITIONS AND
  THE FOLLOWING DISCLAIMER.
    2. REDISTRIBUTIONS IN BINARY FORM MUST REPRODUCE THE ABOVE COPYRIGHT
  NOTICE, THIS LIST OF CONDITIONS AND THE FOLLOWING DISCLAIMER IN
  THE DOCUMENTATION AND/OR OTHER MATERIALS PROVIDED WITH THE
  DISTRIBUTION.

 THIS IS PROVIDED WITHOUT ANY  EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
 BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 A PARTICULAR PURPOSE ARE DISCLAIMED. TO BE USED FOR LEARNING PURPOSE ONLY.  
 IN NO EVENT SHALL THE CONTRIBUTORS OR COPYRIGHT OWNER, BE LIABLE FOR ANY DIRECT,
 INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES ARISING IN ANY WAY OUT OF THE
 USE OF THIS SOURCE CODE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//***********************************************************************************//


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

DSWriteTime(h, m, s); To write time hr, min, sec to RTC

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

    DSWriteDate(d, m, y); To write date to RTC

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

    DSWriteDay(dy); To write day to RTC
***********************************************************/

#ifndef _ds1307_H
#define _ds1307_H
#include "I2C.h"


//declare variables for hour, minutes, seconds, day, date, month & year.
unsigned char h=0,m=0,s=0; //comment these lines if defined in Main.c
unsigned char dy=0,dd=0,mm=0,yy=0; //comment these lines if defined in Main.c

/*_________________________________________________________________________
                   DECLARATION FOR DS1307                   */
#define DS_R 0xD1   //7 bit SLA + 1(read bit)
#define DS_W 0xD0 //7 bit SLA + 0(write bit)

//DS1307 RTC Registers addresses....
#define secr   0x00
#define minr   0x01
#define hrr 0x02
#define dayr   0x03
#define dater 0x04
#define monr   0x05
#define yrr 0x06
#define conr 0x07
/*_________________________________________________________________________*/
// DSWrite(addr1,data1);
// To write 'data1' in the 'addr1' location
unsigned char DSWrite(unsigned char addr,uint8_t data)
{
//_delay_us(500);
  I2CStart();

  if(! (I2CWriteSLA(DS_W)) )//Select device
return FALSE;

  if(! (I2CWriteByte(addr)) )//select destn. register
return FALSE;

  if(! (I2CWriteByte(data)) )//write data
return FALSE;
  I2CStop();
  return TRUE;
}

// DSRead(addr1,&data1);
// To data 'data1' from the 'addr1' location
unsigned char DSRead(unsigned char addr,unsigned char *data)
{
  I2CStart();
  if(! (I2CWriteSLA(DS_W)) )//select device
return FALSE;
  if(! (I2CWriteByte(addr)) )//select register
return FALSE;

//_______________Send Repeat start for read_____________
  I2CStart();

  if(! (I2CWriteSLA(DS_R)) )// select device in MASTER READ mode
return FALSE;
  if(! (I2CReadByte(data)) )//read data form device
return FALSE;

  I2CStop();
  return TRUE;
}
/*_________________________________________________________________________*/
// DSinit();
        // To initialise DS1307 RTC chip
void DSinit()
{
unsigned char x;
DSRead(secr,&x);
x&=(~(1<<CH)); //Clear CH Bit
DSWrite(secr,x);

x=0x10;
DSWrite(conr,x);//enable out @ 1Hz
}

// DSWriteTime(h, m, s)
// To write time hr, min, sec to RTC
DSWriteTime(unsigned char h, uns
igned char m, unsigned char s)
{
DSWrite(secr,s);
DSWrite(minr,m);
DSWrite(hrr,h);
}

// DSReadTime(&h, &m, &s)
// To read time hr, min, sec from RTC
DSReadTime(unsigned char *h, unsigned char *m, unsigned char *s)
{
DSRead(secr,&s);
DSRead(minr,&m);
DSRead(hrr,&h);
}

// DSWriteDate(d, m, y)
// To write date to RTC
DSWriteDate(unsigned char dd, unsigned char mm, unsigned char yy)
{
DSWrite(dater,dd);
DSWrite(monr,mm);
DSWrite(yrr,yy);
}
// DSReadDate(&d, &m, &y)
// To read date from RTC
DSReadDate(unsigned char *h, unsigned char *m, unsigned char *s)
{
DSRead(dater,&dd);
DSRead(monr,&mm);
DSRead(yrr,&yy);
}

// DSReadDay(&dy)
// To read day from RTC
DSReadDay(unsigned char *dy)
{
DSRead(dayr,&dy);
}
// DSWriteDay(dy)
// To write day to RTC
DSWriteDay(unsigned char dy)
{
DSWrite(dayr,dy);
}

#endif
DS1307.H 

Please follow this link for I2C header File 

If you enjoyed this post plz let us know your views via comments.
This helps us to do much more better.
Thankyou.


0 comments:

Post a Comment

Search Here...