• 1

    ..

  • 2

    ...

  • 3

    ...

Showing posts with label DS1307 SERIAL RTC AVR HEADER. Show all posts
Showing posts with label DS1307 SERIAL RTC AVR HEADER. Show all posts

Tuesday, 25 February 2014

DS1307 RTC AVR atmega16 - Digital Clock

        Here is a simple digital watch which displays time, date & day in 16x2 lcd. DS1307 RTC is used for the time & date watch and AVR Atmega16 (8/32/64) is used as the display driver. It reads RTC time, date, day from ds1307 throught I2C bus and process it for displaying and displays it in the LCD.


Click here to view our DS1307 tutorial & header file for AVR GCC.
Circuit diagram  for DS1307 RTC Digital Watch with AVR is  as follows: 
 
Figure 1 - Circuit diagram
Connect a BUZZER  at pin PD6 if needed.
  
                To initialize DS1307 with the required time use this program first. It sets time as 12:45:32 & date as 24 Feb 14, Tuesday, by default. If you need to change, do it in BCD format.
 
/*************   WRITE TIME IN DS1307 ***********************/
#ifndef BIT
#define BIT(x)    _BV(x)
#endif

#ifndef SETBIT
#define SETBIT(x,b)     x|=_BV(b);
#endif

#ifndef CLEARBIT
#define CLEARBIT(x,b)   x&=~_BV(b);
#endif

#ifndef TOGGLEBIT
#define TOGGLEBIT(x,b) x^=_BV(b);
#endif

#ifndef CHECKBIT
#define CHECKBIT(x,b)   (x & _BV(b))
#endif

#include <avr/io.h>
#include <avr/interrupt.h>
#include <util/delay.h>
#include <avr/sleep.h>

/* waits (pauses) for ms milliseconds */
void WaitMs(unsigned int ms)
{
      unsigned int m;

      for(m=0;m<=ms/10;m++)
      {
            _delay_ms(10);
      }
}
#define nop() asm __volatile__ ("nop\t\n")


#include "lcd.c"
#include "ds1307.h"

#define M(x) ( (x&0xF0)>>4 | 0x30 )
#define L(x) ( (x&0x0F) | 0x30 )

unsigned char h,m,s; //,pwrd=1;  //,aP;
#define ReadTime() DSReadTime(&h,&m,&s)
#define WriteTime() DSWriteTime(h,m,s)
unsigned char dd,mm,yy,dy;
#define ReadDate() DSReadDate(&dd,&mm,&yy)
#define WriteDate() DSWriteDate(dd,mm,yy)

                    //01 34 678
char T[9];  //="hh:mm:ss*";
char D[11]; //="dd/mm/yy";

#define disptim() LCD_putsXY(4,0,T)
#define dispd() LCD_putsXY(0,1,D)

void upt()
{
T[0]=M(h);
T[1]=L(h);
T[3]=M(m);
T[4]=L(m);
T[6]=M(s);
T[7]=L(s);
//if(aP)          LCD_putc('P');    else        LCD_putc('A');   
}

void upd()
{
D[0]=M(dd);
D[1]=L(dd);
D[3]=M(mm);
D[4]=L(mm);
 D[8]=M(yy);      D[9]=L(yy);
//if(aP)          LCD_putc('P');    else        LCD_putc('A');   
}


int main()
{
 D[2]='/';  D[5]='/'; D[6]='2'; D[7]='0';
 T[2]=':';  T[5]=':';
 DDRB=0x00;
 PORTB=0xFE;  // sw_init();
 LCD_init(COFF);
 LCD_clear();
 LCD_putsPXY(0,0,"WeLcOmE....");
 buzz(); buzz();

 DSinit();
#ifdef MODE_12HR
#else
 T[8]='\0';//24hour mode  default 24hour mode
#endif

 _delay_ms(50);

//for write time
WaitMs(900);
h=0x12; m=0x45; s=0x32; //time = 12:45:32
dd=0x24; mm=0x02; yy=0x14; dy=0x02;  //date is 24 feb 2014 Tuesday
WriteTime();
WriteDate();
DSWriteDay(dy);
WaitMs(900);

ReadTime();
ReadDate();
DSReadDay(&dy);
upt();upd();
disptim();
dispd();

while(1)
      {
            _delay_us(50);                     
            sleep_mode();    
            nop();
      }
 return 0;
}


THE PROGRAM FOR CLOCK IS AS FOLLOWS…….
                The DS1307 is initialized to produce 1Hz at SOUT pin (pin7). This pin is connected to INT0 of ATMEGA to generate an interrupt. The ISR is coded to read time, date, day from DS1307 & display it in LCD. After ISR execution, it enter the main while loop, which makes the CPU to enter Sleep mode. This is implemented to reduce power consumption.

/*********MACRO's DEFINITION**************/
#ifndef BIT
#define BIT(x)  _BV(x)
#endif

#ifndef SETBIT
#define SETBIT(x,b) x|=_BV(b);
#endif

#ifndef CLEARBIT
#define CLEARBIT(x,b)      x&=~_BV(b);
#endif

#ifndef TOGGLEBIT
#define TOGGLEBIT(x,b)     x^=_BV(b);
#endif

#ifndef CHECKBIT
#define CHECKBIT(x,b)      (x & _BV(b))
#endif
/*******************************************************************/

#include <avr/io.h>
#include <avr/interrupt.h>
#include <util/delay.h>
#include <avr/sleep.h>

/* waits (pauses) for ms milliseconds */
void WaitMs(unsigned int ms)
{
     unsigned int m;

     for(m=0;m<=ms/10;m++)
     {
           _delay_ms(10);
     }
}
#define nop() asm __volatile__ ("nop\t\n")


#include "lcd.c"
#include "ds1307.h"

#define M(x) ( (x&0xF0)>>4 | 0x30 )
#define L(x) ( (x&0x0F) | 0x30 )

unsigned char h,m,s; //,pwrd=1;  //,aP;
#define ReadTime() DSReadTime(&h,&m,&s)
#define WriteTime() DSWriteTime(h,m,s)
unsigned char dd,mm,yy,dy;
#define ReadDate() DSReadDate(&dd,&mm,&yy)
#define WriteDate() DSWriteDate(dd,mm,yy)

                  //01 34 678
char T[9]; //="hh:mm:ss*";
char D[11];     //="dd/mm/yy";

#define disptim() LCD_putsXY(4,0,T)
#define dispd() LCD_putsXY(0,1,D)

void upt()
{
T[0]=M(h);
T[1]=L(h);
T[3]=M(m);
T[4]=L(m);
T[6]=M(s);
T[7]=L(s);
//if(aP)        LCD_putc('P');  else       LCD_putc('A'); 
}

void upd()
{
D[0]=M(dd);
D[1]=L(dd);
D[3]=M(mm);
D[4]=L(mm);    
 D[8]=M(yy);    D[9]=L(yy);
//if(aP)        LCD_putc('P');  else       LCD_putc('A'); 
}

void buzz()
{
     SETBIT(PORTD,6)
     _delay_ms(350);
     CLEARBIT(PORTD,6)
}

ISR(INT0_vect)
{//1 int0
     ReadTime();
     ReadDate();
     DSReadDay(&dy);
#ifdef MODE_12HR
//12hour mode
     if( (h&0x20)==0x20)
           { aP=1; T[8]='P';}
     else
           { aP=0; T[8]='A';}
     h=h&0x1F;
#else
  h=h&0x3F; //24hour mode  default 24hour mode
#endif

     upt();upd();
     disptim();
     dispd();
    
     LCD_puts("  ");
     dy=dy & 0x07;
     if(dy==0x01) LCD_puts("SUN");
     else if(dy==0x02) LCD_puts("MON");
     else if(dy==0x03) LCD_puts("TUE");
     else if(dy==0x04) LCD_puts("WED");
     else if(dy==0x05) LCD_puts("THU");
     else if(dy==0x06) LCD_puts("FRI");
     else if(dy==0x07) LCD_puts("SAT");
}

int main()
{
 D[2]='/'; D[5]='/'; D[6]='2'; D[7]='0';
 T[2]=':'; T[5]=':';
 DDRB=0x00;
 PORTB=0xFE;  // sw_init();
 LCD_init(COFF);
 LCD_clear();
 LCD_puts2XY(4,0,"WeLcOmE...");
 LCD_puts2XY(0,1,"www.elecdude.com");
 buzz(); buzz();

 DSinit();
#ifdef MODE_12HR
#else
 T[8]='\0';//24hour mode  default 24hour mode
#endif

 _delay_ms(50);
DDRD = (1<<7) | (1<<6) | (0<<2);
 SETBIT(PORTD,2)
 SETBIT(GICR,INT0)
 MCUCR|= ( (1<<ISC01)|(0<<ISC00) );
   //ext int0 @falling edge(10)           
 set_sleep_mode(SLEEP_MODE_IDLE);
 SETBIT(TIMSK,OCIE0) //T0 compate int
 OCR0=0x15;
 TCNT0=0x00;
 TCCR0=0x00;
LCD_clear();
 WaitMs(900);
 sei();
 while(1)
     {
           _delay_us(50);                 
           sleep_mode();  
           nop();
     }
 return 0;
}

OUTPUT SNAPSHOT:

digital watch: elecdude.com


If you want to add beep every hour, just add in ISR.
Declare a variable hpre=0x00;
CODE:
if(hpre!=h)
     { buzz(); hpre=h;}

Like this you can add a pre-defined alarm at required time.


If you've any queries with this, logon to forum.elecdude.info & post it.
Thankyou.



Here is a simple digital watch which displays time, date & day in 16x2 lcd. DS1307 RTC is used for the time & date watch and AVR Atmega16 (8/32/64) is used as the display driver. It reads RTC time, date, day from ds1307 throught I2C bus and process it for displaying and displays it in the LCD.

Digital Watch


Click here to view our DS1307 tutorial & header file for AVR GCC.

Circuit diagram  for DS1307 RTC Digital Watch with AVR is  as follows:
 
Figure 1 - Circuit diagram
Connect a BUZZER  at pin PD6 if needed.
 
                To initialize DS1307 with the required time use this program first. It sets time as 12:45:32 & date as 24 Feb 14, Tuesday, by default. If you need to change, do it in BCD format.
/*************   WRITE TIME IN DS1307 ***********************/
#ifndef BIT
#define BIT(x)    _BV(x)

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)

Search Here...