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)
Reactions:

0 comments:

Post a Comment

Search Here...