+ Post New Thread
Results 1 to 13 of 13
  1. #1
    Full Member level 4
    Points: 1,916, Level: 10

    Join Date
    Sep 2014
    Location
    india
    Posts
    225
    Helped
    1 / 1
    Points
    1,916
    Level
    10

    ds1307 Rtc clock running slow by 6 second per day.

    Dear All,
    I am using ds1307 in my clock design.but it seems back by 6 second per day.Then it become 3 minute back after 1 month.Overall design & code given below.
    design_details.pdf

    Code:
    
    #include 
    
    
    
    
    sbit set=P1^0;
    sbit mov=P1^1;
    sbit inc=P1^2;
    sbit dec=P1^3;
    sbit rly=P1^4;
    sbit e5=P1^5;//3rd e5
    sbit e6=P2^3;//4th	e6
    sbit blink=P2^1;
    sbit bus=P3^7;
    sbit ind=P3^6;
    sbit e4=P1^7;//1st e4
    sbit e3=P1^6;//2nd e3
    sbit SDA=P2^6;
    sbit SCL=P2^7;
    
    bit d;
    bit e;
    bit f;
    bit nxt;
    bit nxta;
    
    unsigned char dec_hex(unsigned char tt)
    {
    if(tt>59)
    tt+=36;
    
    else if(tt>49)
    tt+=30;
    else if(tt>39)
    tt+=24;
    else if(tt>29)
    tt+=18;
    else if(tt>19)
    tt+=12;
    else if(tt>9)
    tt+=6;
    return(tt);
    } 
    
    
    void all_disp();
    void time_set();
    
    void alarm_set();
    
    void set_date();
    void blink_on();
    	void buzzer();
    void no_op();
    void setdisp_ampm();
    	 void alarm_setsat();
    
    unsigned int zt;
    
    
    
    
    void alarm_bell_HI();
    void alarm_bell_LOW();
    void display_day();
    void puls();
    void no_puls();
    
    
    
    
    #define First_Line 0x80
    #define Second_Line 0xc0
    #define Curser_On 0x0f
    #define Curser_Off 0x0c
    #define Clear_Display 0x01
    #define idle f==1
    #define no_idle f==0
    
    #define Data_Port P0
    
    sbit Lcd_rs = P2^0;
    //sbit Lcd_rw = P2^1;
    sbit Lcd_en = P2^2;
    
    sbit sda_rtc =P2^5;
    sbit scl_rtc =P2^4;
    
    /*sbit Lcd_rs = P3^5;
    sbit Lcd_rw = P3^6;
    sbit Lcd_en = P3^7; */
    
    void Lcd8_Init();
    void Lcd8_Command(unsigned char);
    void Lcd8_Write(unsigned char,unsigned char);
    void Lcd8_Display(unsigned char,const unsigned char*,unsigned int);
    void Lcd8_decimal2(unsigned char,unsigned char val);
    void Delay(unsigned int);
    void del();
    
    void hour_mode();
    void set_mod1();
    void set_mod2();
    
    unsigned char LB,SB;
    void Seg8_decimal2(min);
    
    void Led8decimal2(hour);
    unsigned char num;
    void EEPROM_WriteByte(unsigned char eeprom_Address, unsigned char eeprom_Data);
    unsigned char EEPROM_ReadByte(unsigned char eeprom_Address);
    void I2C_Start();
    void I2C_Stop(void);
    void I2C_Ack();
    void I2C_Write(unsigned char dat);
    unsigned char I2C_Read();
     #define EEPROM_ID 0xa0
    void delay_ms(unsigned int ms_count);
    void I2C_NoAck();
    void I2C_Clock();
    void delay_us(unsigned int us_count);
    void msdelay(unsigned int value);
    void emerg_bell();
    void display_ampm1();
    void display_ampm2();
    
    //unsigned char number[]="1,2,3,4";
    
    
    
    #define DS1307_ID 0xD0
    #define SEC 0x00
    #define MIN 0x01
    #define HOUR 0x02
    #define DAY 0x03
    #define DATE 0x04
    #define MONTH 0x05
    #define YEAR 0x06
    #define ctrl 0x07
    #define FRIDAY 0x06
    #define SATURDAY 0x00
    #define SUNDAY 0x01
    
    
    
    DS1307_get(unsigned char);
    void DS1307_settime(unsigned char, unsigned char, unsigned char);
    void DS1307_setdate(unsigned char, unsigned char, unsigned char,unsigned char);
    Send2lcd(unsigned char);
    void Rtc_Write(unsigned char,unsigned char);
    Rtc_Read(unsigned char);
    void Rtc_rd_wr_sub();
    void Rtc_Init();
    void Rtc_Start();
    void Rtc_Tx();
    void Rtc_Rx();
    void Rtc_Stop();
    void Rtc_Ack();
    void disp();
    void disp_time();
    void display_ampm();
    void dispampm();
    void set_ampm();
    
    void set_mod();
    
    void alarm_chk1();
    void alarm_chk2();
    void alarm_chk3();
    void alarm_chk4();
    
    void all_alrmset();
     void all_alrmsetsat();
    
    void friday_chk();
    void regular_chk();
    	void special_mode();
    
    void idle_chk();
    void no_idle_chk();
    
    void key_lock2();
    
    
    unsigned int Rtc_add_wr,Rtc_add_rd;
    unsigned char d_rtc,datain_rtc,in_rtc,temp_rtc,dat_rtc,flag_rtc;
    
    unsigned char i,j,a[10],cur=0,cur1=0,cur2=0,cur3=0,cur4=0;
    unsigned char daym,dati,sec,hour,min,date,day,month,year,mode=0,clk_ampm,sec1,hour1,min1,sec2,hour2,min2,hh,mm,ss,dd,mn,yy,ahour,amin,ampm,asec,amin1,ahour1,ampm1;
     
    
    unsigned char count[8]={0x82,0x85,0x8a,0x8d,0xc2,0xc5,0xcb,0xcd};   ///0x8a changed
    code unsigned char memo[78]={0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x10,0x11,0x12,0x13,0x14,0x15,0x16,0x17,0x18,0x19,0x20,0x21,0x22,0x23,0x24,0x25,0x26,0x27,0x28,0x29,0x30,0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x2a,0x2b,0x2c,0x2d,0x2e,0x2f,0x3a,0x3b,0x3c,0x3d,0x3e,0x3f,0x1a,0x1b,0x1c,0x1d,0x1e,0x1f,0xa,0xb,0xc,0xd,0xe,0xf,0x39,0x40,0x4a,0x4b,0xa1,0xa2,0xa3,0xa4,0xa5,0xa6,0xa7,0xa8,0xa9,0xaa,0xab,0xac};
    code unsigned char memo1[66]={0x42,0x43,0x44,0x45,0x46,0x47,0x48,0x49,0x50,0x51,0x52,0x53,0x54,0x55,0x56,0x57,0x58,0x59,0x60,0x61,0x62,0x63,0x64,0x65,0x66,0x67,0x68,0x69,0x70,0x71,0x72,0x73,0x74,0x75,0x76,0x77,0x78,0x79,0x5a,0x5b,0x5c,0x5d,0x5e,0x5f,0x6a,0x6b,0x6c,0x6d,0x6e,0x6f,0x7a,0x7b,0x7c,0x7d,0x7e,0x7f,0x8a,0x8b,0x8c,0x8d,0x8e,0x8f,0x9a,0x9b,0x9c,0x9d};
    
    	unsigned char memo2[2]={0x9e,0x9f};
    unsigned char count1[6]={0x86,0x89,0x8b,0xc6,0xc9,0xcb};  
    unsigned char count2[2]={0x8b,0xcb};   
    
    unsigned char rly_hex(unsigned char tt)
    {
    if(tt>59)
    tt+=36;
    else if(tt>49)
    tt+=30;
    else if(tt>39)
    tt+=24;
    else if(tt>29)
    tt+=18;
    else if(tt>19)
    tt+=12;
    else if(tt>9)
    tt+=6;
    return(tt);
    } 
    
    void all_disp()
    	
    {
    	
    sec = DS1307_get(SEC);
    min = DS1307_get(MIN);
    hour = DS1307_get(HOUR);
    	
    dispampm();
    setdisp_ampm();
    hour=hour& 0x1f;
    display_ampm();
    
    date = DS1307_get(DATE);
    day = DS1307_get(DAY);
    month = DS1307_get(MONTH);
    year = DS1307_get(YEAR);
    	
    sec=Send2lcd(sec);
    min=Send2lcd(min);
    hour=Send2lcd(hour);
    day=Send2lcd(day);
    	
    date=Send2lcd(date);
    year=Send2lcd(year);
    month=Send2lcd(month);
    
    
    //P3=day;
    
    
    Lcd8_decimal2(0xc6,hour);
    Lcd8_Write(0xc8,'.');
    Lcd8_decimal2(0xc9,min);
    Lcd8_Write(0xcb,'.');
    Lcd8_decimal2(0xcc,sec);
    	Lcd8_decimal2(0x82,date);
    Lcd8_Write(0x84,'-');
    Lcd8_decimal2(0x85,month);
    Lcd8_Write(0x87,'-');
    Lcd8_Display(0x88,"20 ",2);
    
    Lcd8_decimal2(0x8a,year);
    
    //Lcd8_decimal2(0x8d,day);
     daym=EEPROM_ReadByte(memo2[1]);
    
    	if(daym==0x07)
    	{
    		Lcd8_Display(0x8d,"EXM",3);
    	}
    
    		if(daym==0x05)
    	{
    		Lcd8_Display(0x8d,"RGL",3);
    	}
    
    
    Seg8_decimal2(min);//...................................................................................................................
    Led8decimal2(hour);//...................................................................................................
    
    
    
    }
    
    void main()
    	
    {
    nxt=0;
    rly=0;	
    dati=0x00;
    Lcd8_Init();  //lcd 16x2 initialisation
    Lcd8_Display(0x82,"ZAKARIYA ",8);     ///showing name for 1 seconds
    	Lcd8_Display(0xc3,"Technologies ",12);
    
    Delay(65000);
    Delay(65000);
    
    
    Lcd8_Command(0x01);  //blanking lcd
    Rtc_Init();  ///Ds1307 initialisation
    while(1)
    	
    {
    
    all_disp();  //getting  time data from ds1307 and displaying on lcd & Led display
    display_day();  //displaying day
    
    if(day==0x07) goto jump;  //No alrm for saturday
    if(day==0x01) goto jump;  //no alarm for sunday
    
    	if(daym==0x07)  //if manually set by pressing key
    	special_mode();   //clock in special mode
    	else
    straight:
    regular_chk();  //regular mode
    jump:
    	if(!set)
    		key_lock2();  ///show as keypad locked(when press set/inc/dec)
    
     if(!inc)
    	 key_lock2();  ///show as keypad locked(when press set/inc/dec)
    
     if(!dec)
    	 
       key_lock2();  ///show as keypad locked(when press set/inc/dec)
    
     
    
      if(!set)   //emergency bell button
     alarm_bell_LOW();
    
    if(inc==0)   //key for special mode
    {
    Delay(65000);
    Delay(65000);
    Delay(65000);
    }
    if(inc==0)
    {
    
    daym=0x07;	
    	
     EEPROM_WriteByte(memo2[1], daym);
    		//Lcd8_Display(0xc1,"processing..",12);
    
    
    }	
    	
    
    
    
    
    if(dec==0)
    if(daym==0x07)	
    {
    Delay(65000);
    Delay(65000);
    Delay(65000);
    }
    if(dec==0)
    {
    
    daym=0x05;	
    	
     EEPROM_WriteByte(memo2[1], daym);
    	//Lcd8_Display(0xc1,"processing..",12);
    
    
    }	
    
    
    
    
    
    
    
    	
    	
    	
    if(mov==0)
    {
    Delay(65000);
    Delay(65000);
    Delay(65000);
    }
    if(mov==0)
    {
    		Lcd8_Command(0x01);
    
    	Lcd8_Display(0xc4,"Unlocked ",8);
    	Delay(65000);
    time_set();
    	cur2=0;
    	cur4=0;
    			Lcd8_Command(0x01);
    		Lcd8_Command(0x0c);
    
    	Lcd8_Display(0x82,"REGLR ",5);
    	Lcd8_Display(0xC4,"SCHEDULE ",8);
    
    		Delay(65000);
    		Delay(65000);
    
    all_alrmset();
    	Lcd8_Command(0x01);
    Lcd8_Display(0x82,"EXAM",4);
    	Lcd8_Display(0xC4,"SCHEDULE ",8);
    
    	Delay(65000);
    	Delay(65000);
    
    all_alrmsetsat();
    
    set_date();
    
    }
    if(dati==0x03)
    {
    blink=~blink;
    dati=0x00;
    }
    
    
    
    }
    }
    
    
    void dispampm()
    	
    {
    	if(hour==0x00)
    	{
    		 d=0;
    clk_ampm=0;
    Rtc_Start();
    
    Rtc_Write(0x02,0x40);
     
    Rtc_Stop(); /* Stop i2c bus */
    	}
    
    		if(hour==0x01)
    	{
    				 d=0;
    clk_ampm=0;
    
    Rtc_Start();
    
    Rtc_Write(0x02,0x41);
     
    Rtc_Stop(); /* Stop i2c bus */
    	}
    
    			if(hour==0x02)
    	{
    				 d=0;
    clk_ampm=0;
    
    Rtc_Start();
    
    Rtc_Write(0x02,0x42);
     
    Rtc_Stop(); /* Stop i2c bus */
    	}
    
    		if(hour==0x03)
    	{
    				 d=0;
    clk_ampm=0;
    
    Rtc_Start();
    
    Rtc_Write(0x02,0x43);
     
    Rtc_Stop(); /* Stop i2c bus */
    	}
    
    		if(hour==0x04)
    	{
    				 d=0;
    clk_ampm=0;
    
    Rtc_Start();
    
    Rtc_Write(0x02,0x44);
     
    Rtc_Stop(); /* Stop i2c bus */
    	}
    
    		if(hour==0x05)
    	{
    				 d=0;
    clk_ampm=0;
    
    Rtc_Start();
    
    Rtc_Write(0x02,0x45);
     
    Rtc_Stop(); /* Stop i2c bus */
    	}
    
    		if(hour==0x06)
    	{
    				 d=0;
    clk_ampm=0;
    
    Rtc_Start();
    
    Rtc_Write(0x02,0x46);
     
    Rtc_Stop(); /* Stop i2c bus */
    	}
    
    		if(hour==0x07)
    	{
    				 d=0;
    clk_ampm=0;
    
    Rtc_Start();
    
    Rtc_Write(0x02,0x47);
     
    Rtc_Stop(); /* Stop i2c bus */
    	}
    
    		if(hour==0x08)
    	{
    				 d=0;
    clk_ampm=0;
    
    Rtc_Start();
    
    Rtc_Write(0x02,0x48);
     
    Rtc_Stop(); /* Stop i2c bus */
    	}
    
    		if(hour==0x09)
    	{
    				 d=0;
    clk_ampm=0;
    
    Rtc_Start();
    
    Rtc_Write(0x02,0x49);
     
    Rtc_Stop(); /* Stop i2c bus */
    	}
    
    		if(hour==0x10)
    	{
    				 d=0;
    clk_ampm=0;
    
    Rtc_Start();
    
    Rtc_Write(0x02,0x4a);
     
    Rtc_Stop(); /* Stop i2c bus */
    	}
    
    		if(hour==0x11)
    	{
    				 d=0;
    clk_ampm=0;
    
    Rtc_Start();
    
    Rtc_Write(0x02,0x4b);
     
    Rtc_Stop(); /* Stop i2c bus */
    	}
    
    		if(hour==0x12)
    	{
    				 d=1;
    clk_ampm=1;
    
    Rtc_Start();
    
    Rtc_Write(0x02,0x72);
     
    Rtc_Stop(); /* Stop i2c bus */
    	}
    
    		if(hour==0x13)
    	{
    						 d=1;
    clk_ampm=1;
    
    Rtc_Start();
    
    Rtc_Write(0x02,0x61);
     
    Rtc_Stop(); /* Stop i2c bus */
    	}
    
    		if(hour==0x14)
    	{
    						 d=1;
    clk_ampm=1;
    
    Rtc_Start();
    
    Rtc_Write(0x02,0x62);
     
    Rtc_Stop(); /* Stop i2c bus */
    	}
    
    		if(hour==0x15)
    	{
    						 d=1;
    clk_ampm=1;
    
    Rtc_Start();
    
    Rtc_Write(0x02,0x63);
     
    Rtc_Stop(); /* Stop i2c bus */
    	}
    
    		if(hour==0x16)
    	{
    						 d=1;
    clk_ampm=1;
    
    Rtc_Start();
    
    Rtc_Write(0x02,0x64);
     
    Rtc_Stop(); /* Stop i2c bus */
    	}
    
    		if(hour==0x17)
    	{
    						 d=1;
    clk_ampm=1;
    
    Rtc_Start();
    
    Rtc_Write(0x02,0x65);
     
    Rtc_Stop(); /* Stop i2c bus */
    	}
    
    		if(hour==0x18)
    	{
    						 d=1;
    clk_ampm=1;
    
    Rtc_Start();
    
    Rtc_Write(0x02,0x66);
     
    Rtc_Stop(); /* Stop i2c bus */
    	}
    
    		if(hour==0x19)
    	{
    						 d=1;
    clk_ampm=1;
    
    Rtc_Start();
    
    Rtc_Write(0x02,0x67);
     
    Rtc_Stop(); /* Stop i2c bus */
    	}
    
    		if(hour==0x20)
    	{
    						 d=1;
    clk_ampm=1;
    
    Rtc_Start();
    
    Rtc_Write(0x02,0x68);
     
    Rtc_Stop(); /* Stop i2c bus */
    	}
    
    		if(hour==0x21)
    	{
    						 d=1;
    clk_ampm=1;
    
    Rtc_Start();
    
    Rtc_Write(0x02,0x69);
     
    Rtc_Stop(); /* Stop i2c bus */
    	}
    
    		if(hour==0x22)
    	{
    						 d=1;
    clk_ampm=1;
    
    Rtc_Start();
    
    Rtc_Write(0x02,0x6a);
     
    Rtc_Stop(); /* Stop i2c bus */
    	}
    
    		if(hour==0x23)
    	{
    						 d=1;
    clk_ampm=1;
    
    Rtc_Start();
    
    Rtc_Write(0x02,0x6b);
     
    Rtc_Stop(); /* Stop i2c bus */
    	}
    	
    }
    
    
    
    
    
    void key_lock2() 
    {
    
     
    {
    	Lcd8_Command(0x01);
    
    	Lcd8_Display(0xc1,"Keypad locked ",13);
    
    Delay(65000);
    Lcd8_Command(0x01);
    }
    }
    
    
    
    
    
    
    void idle_chk()
    	
    {
    if(hour==7)
    if(d==1)
    	f=1;
    if(hour==8)
    	if(d==1)
    		f=1;
    if(hour==9)
    	if(d==1)
    		f=1;
    if(hour==10)
    	if(d==1)
    		f=1;
    if(hour==11)
    	if(d==1)
    	f=1;
    if(hour==12)
    	if(d==0)
    	f=1;
    if(hour==1)
    	if(d==0)
    	f=1;
    if(hour==2)
    	if(d==0)
    	f=1;
    if(hour==3)
    	if(d==0)
    	f=1;
    if(hour==4)
    	if(d==0)
    	f=1;
    if(hour==5)
    	if(d==0)
    	f=1;
    if(hour==6)
    	if(d==0)
    	f=1;
    	
    }	
    
    
    
    void no_idle_chk()
    	
    {
    if(hour==7)
    if(d==0)
    	f=0;
    if(hour==8)
    	if(d==0)
    		f=0;
    if(hour==9)
    	if(d==0)
    		f=0;
    if(hour==10)
    	if(d==0)
    		f=0;
    if(hour==11)
    	if(d==0)
    	f=0;
    if(hour==12)
    	if(d==1)
    	f=0;
    if(hour==1)
    	if(d==1)
    	f=0;
    if(hour==2)
    	if(d==1)
    	f=0;
    if(hour==3)
    	if(d==1)
    	f=0;
    if(hour==4)
    	if(d==1)
    	f=0;
    if(hour==5)
    	if(d==1)
    	f=0;
    if(hour==6)
    	if(d==1)
    	f=0;
    	
    }	
    
    
    
    
    
    
     void all_alrmset(void)
     {
    	 nxt=0;
    	 up:alarm_set();
    	 //alarm_setsat();
    	 if(nxt==0) goto up;
    
     }
     
      void all_alrmsetsat(void)
     {
    	 nxta=0;
    	 up:alarm_setsat();
    	 if(nxta==0) goto up;
    
     }
     
    
     
     
     
     
     
     void friday_chk()
     {
     }
    
     void regular_chk()
     {
     alarm_chk1();
     alarm_chk2();
     }
    
     
      void special_mode()
     {
     alarm_chk3();
     alarm_chk4();
     }
    
     
     
     void setdisp_ampm()
     {
    	 	if(hour==0x52)
    		{
    	  d=0;
    		clk_ampm=00;
    		}
    		if(hour==0x41)
    		{
    	  d=0;
    		clk_ampm=00;
    		}
        if(hour==0x42)
    		{
    	  d=0;
    		clk_ampm=00;
    		}
        if(hour==0x43)
    		{
    	  d=0;
    		clk_ampm=00;
    		}
    		if(hour==0x44)
    		{
    	  d=0;
    		clk_ampm=00;
    		}
        if(hour==0x45)
    		{
    	  d=0;
    		clk_ampm=00;
    		}
        if(hour==0x46)
    		{
    	  d=0;
    		clk_ampm=00;
    		}
        if(hour==0x47)
    		{
    	  d=0;
    		clk_ampm=00;
    		}
        if(hour==0x48)
    		{
    	  d=0;
    		clk_ampm=00;
    		}
        if(hour==0x49)
    		{
    	  d=0;
    		clk_ampm=00;
    		}
        if(hour==0x4a)
    		{
    	  d=0;
    		clk_ampm=00;
    		}
        if(hour==0x4b)
    		{
    	  d=0;
    		clk_ampm=00;
    		}
    
    	if(hour==0x72)
    	{
        d=1;
    		clk_ampm=01;
    	}
      if(hour==0x61)
    	{
        d=1;
    	  clk_ampm=01;
    	}
      if(hour==0x62)
    	{
        d=1;
    		clk_ampm=01;
    	}
      if(hour==0x63)
    	{
        d=1;
    		clk_ampm=01;
    	}
      if(hour==0x64)
    	{
        d=1;
    		clk_ampm=01;
    	}
      if(hour==0x65)
    	{
        d=1;
    		clk_ampm=01;
    	}
      if(hour==0x66)
    	{
        d=1;
    		clk_ampm=01;
    	}
      if(hour==0x67)
    	{
        d=1;
    		clk_ampm=01;
    	}
      if(hour==0x68)
    	{
        d=1;
    		clk_ampm=01;
    	}
      if(hour==0x69)
    	{
        d=1;
    		clk_ampm=01;
    	}
      if(hour==0x6a)
    	{
        d=1;
    		clk_ampm=01;
    	}
      if(hour==0x6b)
    	{
        d=1;
    		clk_ampm=01;
    	}
    
     }
    
    
    
    
    
    
    void disp_time()
    	
    {
    sec = DS1307_get(SEC);
    min = DS1307_get(MIN);
    hour = DS1307_get(HOUR);
    	hour=hour& 0x1f;
    sec=Send2lcd(sec);
    min=Send2lcd(min);
    hour=Send2lcd(hour);
    
    	Lcd8_decimal2(0xc2,hour);
    Lcd8_Write(0xc4,'.');
    Lcd8_decimal2(0xc5,min);
    Lcd8_Write(0xc7,'.');
    Lcd8_decimal2(0xc8,sec);
    set_ampm();
    
    
    Seg8_decimal2(min);
    Led8decimal2(hour);
    	
    	
    }
    void display_ampm()
    	
    {
     if(d==0)
      Lcd8_Display(0xce,"AM ",2);/////////////////////////////////////////////////////////changed
    
     if(d==1)
    	Lcd8_Display(0xce,"PM ",2);/////////////////////////////////////////////////////////changed
    }
    
    
    void display_ampm1()
    	
    {
     if(d==0)
      Lcd8_Display(0x8b,"AM ",2);/////////////////////////////////////////////////////////changed
    
     if(d==1)
    	Lcd8_Display(0x8b,"PM ",2);/////////////////////////////////////////////////////////changed
    }
    
    
    void display_ampm2()
    	
    {
     if(d==0)
      Lcd8_Display(0xcb,"AM ",2);/////////////////////////////////////////////////////////changed
    
     if(d==1)
    	Lcd8_Display(0xcb,"PM ",2);/////////////////////////////////////////////////////////changed
    }
    
    
    
    
    
    
    
    
    
    void set_ampm()
    	
    {
    	 if(d==0)
      Lcd8_Display(0xcb,"AM ",2);/////////////////////////////////////////////////////////changed
    	 clk_ampm=00;
    
     if(d==1)
    	Lcd8_Display(0xcb,"PM ",2);/////////////////////////////////////////////////////////changed
    clk_ampm=01;
    }
    
    void emerg_bell()
    {
    }
    
    void no_op()
    
    {
    	
    }
    	
    void hour_mode()
    	
    {
    	
    	if(hour>12)
    		hour=hour-12;
    	
    }
    
    void puls()
    	
    {
    	Rtc_Start();
    
    Rtc_Write(0x07,0x10); /* Write date on RAM address 04H */
    	
    
    Rtc_Stop(); /* Stop i2c bus */
    }
    
    void no_puls()
    {
    Rtc_Start();
    
    Rtc_Write(0x07,0x00); /* Write date on RAM address 04H */
    	
    
    Rtc_Stop(); /* Stop i2c bus */
    }
    
    
    void display_day()
    {
    
     if(day==0x01)
     
    Lcd8_Display(0xc1,"SUN    ",3);
    
     if(day==0x02)
    
    Lcd8_Display(0xc1,"MON    ",3);
     if(day==0x03)
    
    Lcd8_Display(0xc1,"TUE   ",3);
     if(day==0x04)
    
    Lcd8_Display(0xc1,"WED ",3);
     if(day==0x05)
    
    Lcd8_Display(0xc1,"THU  ",3);
     if(day==0x06)
    
    Lcd8_Display(0xc1,"FRI    ",3);
     if(day==0x07)
    
    Lcd8_Display(0xc1,"SAT  ",3);
    
    }
    
    
    void set_date()
    
    {
    	blink_on();
    
    Lcd8_Command(0x01);
    
    	
    all_disp();
    	Lcd8_decimal2(0x8d,day);
    cur=0;
    while(set)
    {
    	
    	Lcd8_Command(0x0f);
    Lcd8_Command(count[cur]);
    
    if(mov==0)
    {
    while(!mov);
    cur++;
    			blink_on();
    
    if(cur==4)
    cur=0;
    }
    
    
    else if(count[cur]==0x82)
    {
    if(inc==0)
    {
    while(inc==0);
    date++;
    blink_on();
    Lcd8_decimal2(count[cur],date);
    
    if(date>31)
    date=0;
    }
    
    else if(dec==0)
    {
    while(!dec);
    date--;
    blink_on();
    
    if(date==0xff)
    date=99;
    }
    
    }
    
    else if(count[cur]==0x85)
    {
    if(inc==0)
    {
    while(inc==0);
    month++;
    	blink_on();
    
    Lcd8_decimal2(count[cur],month);
    
    
    if(month>12)
    month=0;
    }
    
    else if(dec==0)
    {
    while(!dec);
    month--;
    	blink_on();
    
    if(month==0xff)
    month=99;
    }
    
    }
    
    
    else if(count[cur]==0x8a)
    {
    if(inc==0)
    {
    while(!inc);
    year++;
    	blink_on();
    
    	Lcd8_decimal2(count[cur],year);
    
    if(year>99)
    year=0;
    }
    else if(dec==0)
    {
    while(!dec);
    year--;
    	blink_on();
    
    if(year==0xff)
    year=99;
    }
    }
    
    
    
    else if(count[cur]==0x8d)
    {
    if(inc==0)
    {
    while(!inc);
    day++;
    	blink_on();
    
     //Lcd8_Command(0x01);
    
    
    	display_day();
    
    	Lcd8_decimal2(count[cur],day);
    
    if(day>6)
    day=0;
    }
    else if(dec==0)
    {
    while(!dec);
    day--;
    	blink_on();
    		display_day();
    
    
    if(day==0xff)
    day=7;
    }
    }
    
    
    }
    dd=dec_hex(date);
    mn=dec_hex(month);
    yy=dec_hex(year);
    									
    day=dec_hex(day);
    
    
    
    DS1307_setdate(dd,mn,yy,day); 
    
    Lcd8_Command(0x0c);
    puls();
    
    
    }
    
    
    
    void blink_on()
    {
    blink=0;
      Delay(6500);
    blink=1;
    	
    }
    
    void set_mod()
    	
    {
    	
    if(mode==1)
    {
    d=0;
    clk_ampm=00;
    Lcd8_Display(0xcb,"AM ",2);/////////////////////////////////////////////////////////changed
    }
    
    if(mode==2)
    
    {
    d=1;
    clk_ampm=01;
    
    Lcd8_Display(0xcb,"PM ",2);/////////////////////////////////////////////////////////changed
    	
    }
    
    }	
    
    
    void set_mod1()
    	
    {
    	
    if(mode==1)
    {
    d=0;
    		//hour=hour-12;
    
    Lcd8_Display(0x8b,"AM ",2);/////////////////////////////////////////////////////////changed
    }
    
    if(mode==2)
    
    {
    	d=1;
    	//hour=hour+12;
    Lcd8_Display(0x8b,"PM ",2);/////////////////////////////////////////////////////////changed
    	
    }
    
    }	
    
    
    
    
    void set_mod2()
    	
    {
    	
    if(mode==1)
    {
    d=0;
    		//hour=hour-12;
    
    Lcd8_Display(0xcb,"AM ",2);/////////////////////////////////////////////////////////changed
    }
    
    if(mode==2)
    
    {
    	d=1;
    	//hour=hour+12;
    Lcd8_Display(0xcb,"PM ",2);/////////////////////////////////////////////////////////changed
    	
    }
    
    }	
    
    
    
    
    
    
    
    	void time_set()
    
    {	
    
    	blink_on();
    
     Lcd8_Command(Curser_On);
    
     Lcd8_Command(0x01);
     Lcd8_Display(0x84,"Set Time",8);
    disp_time();
    
    
    cur=4;
    while(set)
    
    
    {
    		Lcd8_Command(0x0f);
    Lcd8_Command(count[cur]);
    
    
    if(mov==0)
    {
    while(!mov);
    cur++;
    blink_on();
    
    if(cur==7)
    cur=4;
    }
    
    
    else if(count[cur]==0xc2)
    {
    if(inc==0)
    {
    while(inc==0);
    hour++;
    		blink_on();
    	Led8decimal2(hour);
    	Lcd8_decimal2(0xc2,hour);
    
    
    
    if(hour>12)
    hour=0;
    }
    
    
    
    
    else if(dec==0)
    {
    while(!dec);
    hour--;
    
    		blink_on();
    
    	Led8decimal2(hour);
    	Lcd8_decimal2(0xc2,hour);
    
    
    if(hour==0)
    hour=12;
    }
    
    }
    
    else if(count[cur]==0xc5)
    {
    if(inc==0)
    {
    while(inc==0);
    min++;
    		blink_on();
    
    Seg8_decimal2(min);
    		Lcd8_decimal2(0xc5,min);
    
    
    if(min>=60)
    min=0;
    }
    
    else if(dec==0)
    {
    while(!dec);
    min--;
    		blink_on();
    
    	Seg8_decimal2(min);
    			Lcd8_decimal2(0xc5,min);
    
    
    if(min==0xff)
    min=31;
    }
    
    }
    
    
    else if(count[cur]==0xcb)
    {
    if(inc==0)
    {
    while(!inc);
    mode++;
    set_mod();
    Delay(6500);
    
    
    blink_on();
    //display_ampm();
    if(mode>1)
    mode=0;
    
    }
    }
    
    
    }
    									
    
    hh=rly_hex(hour);
    mm=rly_hex(min);
    
    
    if(d==1)
    	hh= hh | 0x60;
    if(d==0)
    	hh= hh | 0x40;
    
    
    DS1307_settime(hh,mm,ss);
    
    }
    	
    
    	void buzzer()
    		
    	{
    
    	e4=0;
    	e3=0;
    P3=0xff;
    e4=1;	
    e3=1;
    		  Delay(6500);
    Led8decimal2(hour);
    
    	}
    	
    	
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    void alarm_setsat()
    	{
    
    
    	no_puls();
    	cur4++;
     ahour=EEPROM_ReadByte(memo1[cur4]);
      cur4++;
     amin=EEPROM_ReadByte(memo1[cur4]);
    	cur4++;
    	 ampm=EEPROM_ReadByte(memo1[cur4]);
    	cur4++;
    
    ahour1=EEPROM_ReadByte(memo1[cur4]);
    	  cur4++;
    amin1=EEPROM_ReadByte(memo1[cur4]);
    		cur4++;
    ampm1=EEPROM_ReadByte(memo1[cur4]);
    
    Delay(10000);
    	blink_on();
    
    
    	Lcd8_Command(Curser_Off);
    
    	Lcd8_Command(0x0f);
     
      Lcd8_Command(0x01);
    	Lcd8_Display(0x88,":",1);
      	Lcd8_Display(0xc8,":",1);
    		if(cur4<25)
    		{	
    						
      
      Lcd8_Display(0x82,"LB)",3);
    
    	Lcd8_decimal2(0x86,ahour);
      Lcd8_decimal2(0x89,amin);
    	Lcd8_decimal2(0x8b,ampm);
    if(ampm==0)
    {
    	d=0;
    	display_ampm1();
    
    }
    if(ampm==1)
    {
    	d=1;
    	display_ampm1();
    
    }
    	
      Lcd8_Display(0xc2,"LB)",3);
      Lcd8_decimal2(0xc6,ahour1);
      Lcd8_decimal2(0xc9,amin1);
    	Lcd8_decimal2(0xcb,ampm1);
    
    if(ampm1==0)
    {
    	d=0;
    	display_ampm2();
    
    }
    if(ampm1==1)
    {
    	d=1;
    	display_ampm2();
    
    }
    
    
    
    
    
    		}
    else
    		{	
      Lcd8_Display(0x82,"SB)",3);
      Lcd8_decimal2(0x86,ahour);
      Lcd8_decimal2(0x89,amin);
    	Lcd8_decimal2(0x8b,ampm);
    if(ampm==0)
    {
    	d=0;
    	display_ampm1();
    
    }
    if(ampm==1)
    {
    	d=1;
    	display_ampm1();
    
    }
    
    			
    			
      Lcd8_Display(0xc2,"SB)",3);
      Lcd8_decimal2(0xc6,ahour1);
      Lcd8_decimal2(0xc9,amin1);
    	Lcd8_decimal2(0xcb,ampm1);
    
    if(ampm1==0)
    {
    	d=0;
    	display_ampm2();
    
    }
    if(ampm1==1)
    {
    	d=1;
    	display_ampm2();
    
    }
    
    		}
    
    
    cur1=0;
    
    while(set)
    {
    Lcd8_Command(0x0f);
    Lcd8_Command(count1[cur1]);
    if(mov==0)
    {
    while(!mov);
    cur1++;
    if(cur1==6)
    cur1=0;
    }
    else if(count1[cur1]==0x86)			                    ///1st line of lcd
    {
    if(inc==0)
    {
    while(inc==0);
    ahour++;
    	blink_on();
    
    if(ahour>=13)
    ahour=0;
    Lcd8_decimal2(count1[cur1],ahour);
    }
    
    else if(dec==0)
    {
    while(!dec);
    ahour--;
    	blink_on();
    
    
    
    if(ahour==0)
    ahour=12;
    Lcd8_decimal2(count1[cur1],ahour);
    
    }
    
    }
    else if(count1[cur1]==0x89)
    {
    if(inc==0)
    {
    while(inc==0);
    amin++;
    	blink_on();
    
    if(amin>=60)
    amin=0;
    Lcd8_decimal2(count1[cur1],amin);
    }
    
    else if(dec==0)
    {
    while(!dec);
    amin--;
    		blink_on();
    
    
    if(amin==0)
    amin=59;
    Lcd8_decimal2(count1[cur1],amin);
    
    }
    
    }
    
    
    
    else if(count1[cur1]==0x8b)
    {
    if(inc==0)
    {
    while(inc==0);
    ampm=0;
    	d=0;
    display_ampm1();
    
    	blink_on();
    
    if(ampm>1)
    ampm=0;
    //Lcd8_decimal2(count1[cur1],ampm);
    }
    
    else if(dec==0)
    {
    while(!dec);
    ampm=1;
    	d=1;
    display_ampm1();
    
    
    		blink_on();
    
    
    //Lcd8_decimal2(count1[cur1],ampm);
    
    }
    
    }
    
    
    
    
    
    
    
    else if(count1[cur1]==0xc6)
    {
    if(inc==0)
    {
    while(inc==0);
    ahour1++;
    	blink_on();
    
    if(ahour1>=13)
    ahour1=0;
    Lcd8_decimal2(count1[cur1],ahour1);
    }
    
    else if(dec==0)
    {
    while(!dec);
    ahour1--;
    	blink_on();
    
    
    if(ahour1==0)
    ahour1=12;
    Lcd8_decimal2(count1[cur1],ahour1);
    
    }
    
    }
    else if(count1[cur1]==0xc9)
    {
    if(inc==0)
    {
    while(inc==0);
    amin1++;
    	blink_on();
    
    if(amin1>=60)
    amin1=0;
    Lcd8_decimal2(count1[cur1],amin1);
    }
    
    else if(dec==0)
    {
    while(!dec);
    amin1--;
    	blink_on();
    
    if(amin1==0)
    amin1=60;
    Lcd8_decimal2(count1[cur1],amin1);
    
    }
    
    }
    
    
    
    
    else if(count1[cur1]==0xcb)
    {
    if(inc==0)
    {
    while(inc==0);
    ampm1=0;
    	d=0;
    display_ampm2();
    
    	blink_on();
    
    
    //Lcd8_decimal2(count1[cur1],ampm1);
    }
    
    else if(dec==0)
    {
    while(!dec);
    ampm1=1;
    	d=1;
    	display_ampm2();
    
    	blink_on();
    
    //Lcd8_decimal2(count1[cur1],ampm1);
    
    }
    
    }
    
    
    
    
    
    
    
    }
    
    
    Lcd8_Command(0x0c);
    //Delay(65000);Delay(65000);
    cur4--;
    cur4--;
    cur4--;
    cur4--;
    cur4--;
    
    
    
     EEPROM_WriteByte(memo1[cur4], ahour);
    cur4++;
     EEPROM_WriteByte(memo1[cur4], amin);
    cur4++;
     EEPROM_WriteByte(memo1[cur4], ampm);
    cur4++;
     EEPROM_WriteByte(memo1[cur4], ahour1);
    cur4++;
     EEPROM_WriteByte(memo1[cur4], amin1);
     cur4++;
     EEPROM_WriteByte(memo1[cur4], ampm1);
    
     Lcd8_Command(0x01);	                                       
    
    if(cur4==66)
    	
    	nxta=1;
    
    }
    
    
    
    
    
    
    
    void alarm_set()
    	
    {
    
    
    	no_puls();
    	cur2++;
     ahour=EEPROM_ReadByte(memo[cur2]);
      cur2++;
     amin=EEPROM_ReadByte(memo[cur2]);
    	cur2++;
    	 ampm=EEPROM_ReadByte(memo[cur2]);
    	cur2++;
    
    ahour1=EEPROM_ReadByte(memo[cur2]);
    	  cur2++;
    amin1=EEPROM_ReadByte(memo[cur2]);
    		cur2++;
    ampm1=EEPROM_ReadByte(memo[cur2]);
    
    Delay(10000);
    	blink_on();
    
    
    	Lcd8_Command(Curser_Off);
    
    	Lcd8_Command(0x0f);
     
      Lcd8_Command(0x01);
    	Lcd8_Display(0x88,":",1);
      	Lcd8_Display(0xc8,":",1);
    		if(cur2<31)
    		{	
    						
      
      Lcd8_Display(0x82,"LB)",3);
    
    	Lcd8_decimal2(0x86,ahour);
      Lcd8_decimal2(0x89,amin);
    	Lcd8_decimal2(0x8b,ampm);
    if(ampm==0)
    {
    	d=0;
    	display_ampm1();
    
    }
    if(ampm==1)
    {
    	d=1;
    	display_ampm1();
    
    }
    	
      Lcd8_Display(0xc2,"LB)",3);
      Lcd8_decimal2(0xc6,ahour1);
      Lcd8_decimal2(0xc9,amin1);
    	Lcd8_decimal2(0xcb,ampm1);
    
    if(ampm1==0)
    {
    	d=0;
    	display_ampm2();
    
    }
    if(ampm1==1)
    {
    	d=1;
    	display_ampm2();
    
    }
    
    
    
    
    
    		}
    else
    		{	
      Lcd8_Display(0x82,"SB)",3);
      Lcd8_decimal2(0x86,ahour);
      Lcd8_decimal2(0x89,amin);
    	Lcd8_decimal2(0x8b,ampm);
    if(ampm==0)
    {
    	d=0;
    	display_ampm1();
    
    }
    if(ampm==1)
    {
    	d=1;
    	display_ampm1();
    
    }
    
    			
    			
      Lcd8_Display(0xc2,"SB)",3);
      Lcd8_decimal2(0xc6,ahour1);
      Lcd8_decimal2(0xc9,amin1);
    	Lcd8_decimal2(0xcb,ampm1);
    
    if(ampm1==0)
    {
    	d=0;
    	display_ampm2();
    
    }
    if(ampm1==1)
    {
    	d=1;
    	display_ampm2();
    
    }
    
    		}
    
    
    cur1=0;
    
    while(set)
    {
    Lcd8_Command(0x0f);
    Lcd8_Command(count1[cur1]);
    if(mov==0)
    {
    while(!mov);
    cur1++;
    if(cur1==6)
    cur1=0;
    }
    else if(count1[cur1]==0x86)			                    ///1st line of lcd
    {
    if(inc==0)
    {
    while(inc==0);
    ahour++;
    	blink_on();
    
    if(ahour>=13)
    ahour=0;
    Lcd8_decimal2(count1[cur1],ahour);
    }
    
    else if(dec==0)
    {
    while(!dec);
    ahour--;
    	blink_on();
    
    
    
    if(ahour==0)
    ahour=12;
    Lcd8_decimal2(count1[cur1],ahour);
    
    }
    
    }
    else if(count1[cur1]==0x89)
    {
    if(inc==0)
    {
    while(inc==0);
    amin++;
    	blink_on();
    
    if(amin>=60)
    amin=0;
    Lcd8_decimal2(count1[cur1],amin);
    }
    
    else if(dec==0)
    {
    while(!dec);
    amin--;
    		blink_on();
    
    
    if(amin==0)
    amin=59;
    Lcd8_decimal2(count1[cur1],amin);
    
    }
    
    }
    
    
    
    else if(count1[cur1]==0x8b)
    {
    if(inc==0)
    {
    while(inc==0);
    ampm=0;
    	d=0;
    display_ampm1();
    
    	blink_on();
    
    if(ampm>1)
    ampm=0;
    //Lcd8_decimal2(count1[cur1],ampm);
    }
    
    else if(dec==0)
    {
    while(!dec);
    ampm=1;
    	d=1;
    display_ampm1();
    
    
    		blink_on();
    
    
    //Lcd8_decimal2(count1[cur1],ampm);
    
    }
    
    }
    
    
    
    
    
    
    
    else if(count1[cur1]==0xc6)
    {
    if(inc==0)
    {
    while(inc==0);
    ahour1++;
    	blink_on();
    
    if(ahour1>=13)
    ahour1=0;
    Lcd8_decimal2(count1[cur1],ahour1);
    }
    
    else if(dec==0)
    {
    while(!dec);
    ahour1--;
    	blink_on();
    
    
    if(ahour1==0)
    ahour1=12;
    Lcd8_decimal2(count1[cur1],ahour1);
    
    }
    
    }
    else if(count1[cur1]==0xc9)
    {
    if(inc==0)
    {
    while(inc==0);
    amin1++;
    	blink_on();
    
    if(amin1>=60)
    amin1=0;
    Lcd8_decimal2(count1[cur1],amin1);
    }
    
    else if(dec==0)
    {
    while(!dec);
    amin1--;
    	blink_on();
    
    if(amin1==0)
    amin1=60;
    Lcd8_decimal2(count1[cur1],amin1);
    
    }
    
    }
    
    
    
    
    else if(count1[cur1]==0xcb)
    {
    if(inc==0)
    {
    while(inc==0);
    ampm1=0;
    	d=0;
    display_ampm2();
    
    	blink_on();
    
    
    //Lcd8_decimal2(count1[cur1],ampm1);
    }
    
    else if(dec==0)
    {
    while(!dec);
    ampm1=1;
    	d=1;
    	display_ampm2();
    
    	blink_on();
    
    //Lcd8_decimal2(count1[cur1],ampm1);
    
    }
    
    }
    
    
    
    
    
    
    
    }
    
    
    Lcd8_Command(0x0c);
    //Delay(65000);Delay(65000);
    cur2--;
    cur2--;
    cur2--;
    cur2--;
    cur2--;
    
    
    
     EEPROM_WriteByte(memo[cur2], ahour);
    cur2++;
     EEPROM_WriteByte(memo[cur2], amin);
    cur2++;
     EEPROM_WriteByte(memo[cur2], ampm);
    cur2++;
     EEPROM_WriteByte(memo[cur2], ahour1);
    cur2++;
     EEPROM_WriteByte(memo[cur2], amin1);
     cur2++;
     EEPROM_WriteByte(memo[cur2], ampm1);
    
     Lcd8_Command(0x01);	                                       
    
    if(cur2==78)
    	
    	nxt=1;
    
    }
    
    
    
    
    
    
    
    void alarm_chk1()
    {
    	for(cur2=1;cur2<31;cur2++)
    	{
    ahour=EEPROM_ReadByte(memo[cur2]);
    		cur2++;
    amin=EEPROM_ReadByte(memo[cur2]);
    		cur2++;
    ampm=EEPROM_ReadByte(memo[cur2]);
    
    		asec=00;
       if(hour==ahour)
    	 if(min==amin)
       if(sec==asec)
    	 if(clk_ampm==ampm)
    alarm_bell_HI();
    else
    rly=0;
    }
    }
    
    
    void alarm_chk2()
    {
    		for(cur2=31;cur2<79;cur2++)
    	{
    ahour=EEPROM_ReadByte(memo[cur2]);
    		cur2++;
    		amin=EEPROM_ReadByte(memo[cur2]);
    		cur2++;
    		ampm=EEPROM_ReadByte(memo[cur2]);
    
    		asec=00;
       if(hour==ahour)
    	 if(min==amin)
       if(sec==asec)
    	 if(clk_ampm==ampm)
    
    alarm_bell_LOW();
    else
    rly=0;
    }
    
    	
    }
    
    
    
    
    void alarm_chk3()
    {
    	for(cur4=1;cur4<25;cur4++)
    	{
    ahour=EEPROM_ReadByte(memo1[cur4]);
    		cur4++;
    		amin=EEPROM_ReadByte(memo1[cur4]);
    				cur4++;
    		ampm=EEPROM_ReadByte(memo1[cur4]);
    
    		asec=00;
     if(hour==ahour)
    	 if(min==amin)
       if(sec==asec)
    	 if(clk_ampm==ampm)
    
    alarm_bell_HI();
    else
    rly=0;
    }
    }
    
    
    void alarm_chk4()
    {
    		for(cur4=25;cur4<67;cur4++)
    	{
    ahour=EEPROM_ReadByte(memo1[cur4]);
    		cur4++;
    		amin=EEPROM_ReadByte(memo1[cur4]);
    						cur4++;
    		ampm=EEPROM_ReadByte(memo1[cur4]);
    
    		asec=00;
     if(hour==ahour)
    	 if(min==amin)
       if(sec==asec)
    	 if(clk_ampm==ampm)
    
    alarm_bell_LOW();
    else
    rly=0;
    }
    
    	
    }
    
    
    
    
    
    
    
    
    void alarm_bell_HI()
    {
    	Lcd8_Command(0x01);
    
    	Lcd8_Display(0xc0,"Bell ringing.. ",14);
    
    rly=1;
      delay_ms(8000);            
    
    rly=0;
    	 Delay(65000);
       Delay(65000);
    
    		 Lcd8_Command(0x01);
    
    }
    
    void alarm_bell_LOW()
    {
    			 Lcd8_Command(0x01);
    
    		Lcd8_Display(0xc0,"Bell ringing.. ",14);
    
    rly=1;
    delay_ms(4000);  
    	
    rly=0;
    	
    	 Delay(65000);
       Delay(65000);
    
    		 Lcd8_Command(0x01);
    
    }
    
    
    
    
    
    
    
    
    
    
    
    
    void Lcd8_Init()
    {
    Lcd8_Command(0x38); //to select function set
    Lcd8_Command(0x06); //entry mode set
    Lcd8_Command(0x0c); //display on
    Lcd8_Command(0x01); //clear display
    }
    
    void Lcd8_Command(unsigned char com)
    {
    Data_Port=com;
    Lcd_en=1;
    Lcd_rs=0;
    //Lcd_rw=0;
    
    Delay(125);
    Lcd_en=0;
    Delay(125);
    }
    
    void Lcd8_Write(unsigned char com,unsigned char lr)
    {
    Lcd8_Command(com);
    Data_Port=lr; // Data 
    Lcd_en=Lcd_rs=1;
    //Lcd_rw=0;
    Delay(125);
    Lcd_en=0;
    Delay(125);
    }
    
    void Lcd8_Display(unsigned char com,const unsigned char *word,unsigned int n)
    {
    unsigned char Lcd_i;
    for(Lcd_i=0;Lcd_i>4); /* Convert to ascii code */
    buf2 = value & 0x0F; /* Filter for low byte */
    buf=(buf1*10)+buf2;
    
    return buf;
    }
    
    
    void Rtc_Init()//lower order 256 bytes of the chip
    {
    Rtc_add_wr=0xd0;
    Rtc_add_rd=0xd1;
    }
    
    
    void Rtc_Write(unsigned char zig,unsigned char zag)// program to write to EEPROM
    {
    dat_rtc=zig;
    temp_rtc=zag;
    Rtc_rd_wr_sub();
    above:
    d_rtc=temp_rtc;
    Rtc_Tx();
    if (CY==1)goto above;
    CY=0;
    Rtc_Stop();
    }
    
    
    Rtc_Read(unsigned char zig)// program to read from EEPROM
    {
    dat_rtc=zig;
    Rtc_rd_wr_sub();
    Rtc_Start();
    be:
    d_rtc=Rtc_add_rd; // 0xd1 =Rtc_add_rd
    Rtc_Tx();
    if(CY==1)goto be;
    Rtc_Rx();
    Rtc_Ack();
    CY=0;
    Rtc_Stop();
    return(datain_rtc);
    }
    
    
    void Rtc_Start()// must for any operation on EEPROM
    {
    sda_rtc=1;
    scl_rtc=1;
    sda_rtc=0;
    scl_rtc=0;
    }
    
    
    void Rtc_Stop()// this is similar to the START operation whereas this should be performed after the completion of any operation
    {
    sda_rtc=0;
    scl_rtc=1;
    sda_rtc=1;
    }
    
    
    void Rtc_Tx()// program to send the device address, read/write address,data to be written
    {
    signed char i_rtc;
    for(i_rtc=7;i_rtc>=0;i_rtc--)// should necessarily be initialised as signed char.
    {
    CY=(d_rtc>>i_rtc)&0x01;
    sda_rtc=CY;
    scl_rtc=1;// clock is essential inorder to write or read
    scl_rtc=0;// clk should be alternated
    }
    sda_rtc=1;
    scl_rtc=1;
    CY=sda_rtc;
    scl_rtc=0;
    }
    
    
    void Rtc_Rx()// program read the data from the EEPROM
    {
    unsigned char l_rtc;
    sda_rtc=1;
    for (l_rtc=0;l_rtc<=7;l_rtc++)
    {
    scl_rtc=1;
    in_rtc=in_rtc<<1;
    in_rtc|=sda_rtc;
    scl_rtc=0;
    }
    datain_rtc=in_rtc;
    in_rtc=0;
    }
    
    
    void Rtc_Ack()// this is to intimate the EEPROM that the read operation is over
    {
    sda_rtc=1;
    scl_rtc=1;
    scl_rtc=0;
    }
    
    
    void Rtc_rd_wr_sub()// this routine will be used by both the read & write operations to send the device address & the address at which the corresponding action is to be taken
    {
    Rtc_Start();
    here1:
    d_rtc=Rtc_add_wr;// 0xd0 device address is passed
    Rtc_Tx();
    if(CY==1)goto here1;
    again1:
    d_rtc=dat_rtc;// the address from which data is to be read/written is to be passed
    Rtc_Tx();
    if(CY==1)goto again1;
    }
    
    void EEPROM_WriteByte(unsigned char eeprom_Address, unsigned char eeprom_Data)
    {
     
        I2C_Start();               // Start i2c communication
        I2C_Write(EEPROM_ID);      // connect to AT2404 by sending its ID on I2c Bus
        I2C_Ack();
        I2C_Write(eeprom_Address); // Select the Specified EEPROM address of AT2404
        I2C_Ack();
        I2C_Write(eeprom_Data);    // Write the data at specified address
        I2C_Ack();
        I2C_Stop();                // Stop i2c communication after Writing the data
        delay_ms(1);            // Write operation takes max 5ms, refer At2404 datasheet        
    }
     
    unsigned char EEPROM_ReadByte(unsigned char eeprom_Address)
    {
      unsigned char eeprom_Data;
     
        I2C_Start();               // Start i2c communication
        I2C_Write(EEPROM_ID);      // connect to AT2404(write) by sending its ID on I2c Bus
        I2C_Ack();
        I2C_Write(eeprom_Address); // Select the Specified EEPROM address of AT2404
        I2C_Ack();
     
        I2C_Start();               // Start i2c communication
        I2C_Write(0xA1);           // connect to AT2404(read) by sending its ID on I2c Bus
        I2C_Ack();
       eeprom_Data = I2C_Read();  // Read the data from specified address
        I2C_NoAck();
        I2C_Stop();                // Stop i2c communication after Reading the data
        delay_us(10);
      return eeprom_Data;          // Return the Read data
     
    }
    void I2C_Start()
    {
        SCL = 0;        // Pull SCL low
     
        SDA = 1;        // Pull SDA High
        delay_us(1);
     
        SCL = 1;        //Pull SCL high
        delay_us(1);
     
        SDA = 0;        //Now Pull SDA LOW, to generate the Start Condition
        delay_us(1);
     
        SCL = 0;        //Finally Clear the SCL to complete the cycle
    }
    
    void I2C_Stop(void)
    {
        SCL = 0;            // Pull SCL low
        delay_us(1);
     
        SDA = 0;            // Pull SDA  low
        delay_us(1);
     
        SCL = 1;            // Pull SCL High
        delay_us(1);
     
        SDA = 1;            // Now Pull SDA High, to generate the Stop Condition
    }
    
    void I2C_Ack()
    {
        SDA = 0;        //Pull SDA low to indicate Positive ACK
        I2C_Clock();    //Generate the Clock
        SDA = 1;        // Pull SDA back to High(IDLE state)
    }
    
    void I2C_NoAck()
    {
        SDA = 1;        //Pull SDA high to indicate Negative/NO ACK
       I2C_Clock();     // Generate the Clock  
        SCL = 1;        // Set SCL */
    }
    
    void I2C_Write(unsigned char dat)
    {
        unsigned char i;
     
        for(i=0;i<8;i++)         // loop 8 times to send 1-byte of data
         {
            SDA = dat & 0x80;    // Send Bit by Bit on SDA line
            I2C_Clock();         // Generate Clock at SCL
            dat = dat<<1;
          }
            SDA = 1;                 // Set SDA at last
    }
     
    
    unsigned char I2C_Read(void)
    {
        unsigned char i, dat=0x00;
     
           SDA=1;               //Make SDA as I/P
        for(i=0;i<8;i++)        // loop 8times to read 1-byte of data
         {
           delay_us(1);
            SCL = 1;            // Pull SCL High
            delay_us(1);
     
            dat = dat<<1;       //dat is Shifted each time and
            dat = dat | SDA;    //ORed with the received bit to pack into byte
     
            SCL = 0;            // Clear SCL to complete the Clock
           }
       return dat;               // Finally return the received Byte*
    }
     
     void delay_ms(unsigned int ms_count)
     {
            while(ms_count!=0)
             {
                delay_us(112);   //delay_us is called to generate 1ms delay
                 ms_count--;
             }
       }
     
    
      void delay_us(unsigned int us_count)
     {  
        while(us_count!=0)
          {
             us_count--;
          }												   
     }
     
     void I2C_Clock(void)
    {
        delay_us(1);
        SCL = 1;        // Wait for Some time and Pull the SCL line High
     
        delay_us(1);        // Wait for Some time
        SCL = 0;        // Pull back the SCL line low to Generate a clock pulse
    }

    Program becomes lengthy becouse of multiple alarms code.You can concider only RTC code.
    is there any error in code?
    or i have to change rtc model by ds3232?
    Please guide.
    Thanks
    Tepu

  2. #2
    Advanced Member level 1
    Points: 2,263, Level: 11

    Join Date
    Jan 2019
    Posts
    416
    Helped
    107 / 107
    Points
    2,263
    Level
    11

    Re: ds1307 Rtc clock running slow by 6 second per day.

    6 seconds per day is 6 seconds out of 86400 seconds per day
    or 6.944 x 10-5 or 6.944 x 10^-3 % or 0.007 % or 69 x10^-6 or 69 ppm

    alternatively,

    depending on your 32.768 kHz crystal, this is within its tolerance.

    6 seconds per day over 30 days is 180 seconds, or 3 minutes

    i think it is the tolerance of your crystal
    i suggest you get a better crystal

    mouser has a 10 ppm crystal for about $1.00 (US) Abracon ABS07W-32.768kHz-D-1-T



    •   AltAdvertisement

        
       

  3. #3
    Full Member level 4
    Points: 1,916, Level: 10

    Join Date
    Sep 2014
    Location
    india
    Posts
    225
    Helped
    1 / 1
    Points
    1,916
    Level
    10

    Re: ds1307 Rtc clock running slow by 6 second per day.

    Dear sir,I have changed 4 to 5 ds1307 and many crystel but problem is same.is there any error in rtc code?



  4. #4
    Advanced Member level 5
    Points: 15,042, Level: 29
    schmitt trigger's Avatar
    Join Date
    Apr 2013
    Posts
    2,499
    Helped
    807 / 807
    Points
    15,042
    Level
    29

    Re: ds1307 Rtc clock running slow by 6 second per day.

    And all of those crystals that you have changed, are they different vendors and part numbers?
    Did you purchase them from reputable vendors or from e-bay?

    Having said this: If you really require better accuracy though, the DS3231/DS3232 are far better alternatives.
    My batteries are recharged by "Helpful Post" ratings.
    If you feel that I've helped you, please indicate it as a Helpful Post



  5. #5
    Super Moderator
    Points: 78,516, Level: 68
    Achievements:
    7 years registered
    Awards:
    Most Frequent Poster 3rd Helpful Member

    Join Date
    Apr 2014
    Posts
    15,931
    Helped
    3610 / 3610
    Points
    78,516
    Level
    68

    Re: ds1307 Rtc clock running slow by 6 second per day.

    Hi,

    Datasheet says:
    Application Note 58: Crystal Considerations with Dallas Real-Time Clocks.
    Did you read and follow the application note?

    Klaus
    Please donīt contact me via PM, because there is no time to respond to them. No friend requests. Thank you.



  6. #6
    Full Member level 4
    Points: 1,916, Level: 10

    Join Date
    Sep 2014
    Location
    india
    Posts
    225
    Helped
    1 / 1
    Points
    1,916
    Level
    10

    Re: ds1307 Rtc clock running slow by 6 second per day.

    Thanks for replies.First I have tried by diffrent retailers available locally,then i purchased online by rhydolabs(shows original ds1307 on offer).But issue not solved.
    I Read the application note.But sometimes in past it shows good accuracy in same design.



    •   AltAdvertisement

        
       

  7. #7
    Advanced Member level 1
    Points: 2,263, Level: 11

    Join Date
    Jan 2019
    Posts
    416
    Helped
    107 / 107
    Points
    2,263
    Level
    11

    Re: ds1307 Rtc clock running slow by 6 second per day.

    what are the part numbers and the accuracy/tolerance specification for your crystals?
    you've used several from different sources? are all of them off 6 seconds per day? compared to what?



  8. #8
    Advanced Member level 5
    Points: 15,042, Level: 29
    schmitt trigger's Avatar
    Join Date
    Apr 2013
    Posts
    2,499
    Helped
    807 / 807
    Points
    15,042
    Level
    29

    Re: ds1307 Rtc clock running slow by 6 second per day.

    In a previous company we had product which sold in +200k/year volumes and which used a DS1307. Produced it for over 5 years, perhaps over a million units total.

    Following the app note considerations (that Klaus has already mentioned) and using a good quality crystal, the only time I remember that we would see a frequency error, was because the crystal had been physically damaged. They are fragile.

    We would check the crystal frequency by setting the DS1307 mode to output 32768 Khz, and checking the output with a highly precise Hewlett Packard frequency counter.
    My batteries are recharged by "Helpful Post" ratings.
    If you feel that I've helped you, please indicate it as a Helpful Post


    1 members found this post helpful.

  9. #9
    Super Moderator
    Points: 78,516, Level: 68
    Achievements:
    7 years registered
    Awards:
    Most Frequent Poster 3rd Helpful Member

    Join Date
    Apr 2014
    Posts
    15,931
    Helped
    3610 / 3610
    Points
    78,516
    Level
    68

    Re: ds1307 Rtc clock running slow by 6 second per day.

    Hi,

    Maybe a production problem
    .. some 32768 Hz XTALs donīt like ultrasonic cleaning.

    Klaus
    Please donīt contact me via PM, because there is no time to respond to them. No friend requests. Thank you.



  10. #10
    Advanced Member level 1
    Points: 2,263, Level: 11

    Join Date
    Jan 2019
    Posts
    416
    Helped
    107 / 107
    Points
    2,263
    Level
    11

    Re: ds1307 Rtc clock running slow by 6 second per day.

    according to the Ap Note 58, page 7, the clock can run slow if there is a capacitance mismatch
    or stray capacitance or a variety of other things.
    Have you followed the recommendations therein, as Klaus suggested?

    The DS3232 uses an internal MEMs device and no crystal
    the timing uncertainty is +/- 5 ppm
    as schmitt trigger suggested, you can try a DS3231 or DS3232

    i assume the program is ok, as it seems to work well enough for you to see the
    time change correctly except for the loss of 6 seconds a day
    (as you have not raised that as an issue)


    1 members found this post helpful.

    •   AltAdvertisement

        
       

  11. #11
    Advanced Member level 1
    Points: 2,263, Level: 11

    Join Date
    Jan 2019
    Posts
    416
    Helped
    107 / 107
    Points
    2,263
    Level
    11

    Re: ds1307 Rtc clock running slow by 6 second per day.

    if the previous discussion has not helped, reduce the program to only the clock
    leave out the alarms and anything else that is not directly clock
    see how that works

    then slowly add back one function at a time, testing after each
    it may be that the structure of your program has a loop that slows it down
    this process may determine that loop



  12. #12
    Advanced Member level 2
    Points: 5,258, Level: 17
    Achievements:
    7 years registered
    ZASto's Avatar
    Join Date
    Jan 2008
    Location
    Messy Corner, 44°47'31.56"N, 20°28'9.16"E
    Posts
    630
    Helped
    130 / 130
    Points
    5,258
    Level
    17

    Re: ds1307 Rtc clock running slow by 6 second per day.

    It is impossible to slow down the clock running a huge program.
    Accessing the DS1307 only reads secondary registers not influencing clock operation

    On a 2-wire START, the current time is transferred to a second set of registers. The time information is
    read from these secondary registers, while the clock may continue to run. This eliminates the need to re-
    read the registers in case of an update of the main registers during a read.
    Make no mistake between my personality and my attitude.
    My personality is who I am.
    My attitude depends on who you are.



  13. #13
    Advanced Member level 1
    Points: 5,352, Level: 17

    Join Date
    Jul 2004
    Location
    Hungary
    Posts
    480
    Helped
    189 / 189
    Points
    5,352
    Level
    17

    Re: ds1307 Rtc clock running slow by 6 second per day.

    @tapu:
    My suggestion: try to use a better 5V power supply with decoupling capacities (as close as possible to the RTC).
    The DS1307 is very sensitive to the quality of its power supply.



--[[ ]]--