+ Post New Thread
Results 1 to 17 of 17
  1. #1
    Member level 2
    Points: 939, Level: 6

    Join Date
    May 2016
    Posts
    43
    Helped
    0 / 0
    Points
    939
    Level
    6

    PIC16F877 interface with ds1307 ...RTC running 10 sec slow

    sir,

    i trying to interface the DS1307 with PIC16F877A using I2C.

    RTC running 10sec slow per MINUTE .. please help mee..

    i have WRITE the RTC with some defaults ...please find attached code.


    Code C - [expand]
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    
    #define _XTAL_FREQ 20000000 //We are running on 20MHz crystal
      
          /*Set the current value of date and time below*/
    int sec = 00;
    int min = 04;
    int hour = 05;
    int date = 00;
    int month = 00;
    int year = 00;
    /*Set the current value of date and time below*/
    unsigned int sec1 = 0;
    unsigned int min1 = 0;
    unsigned int hour1 = 0;
    unsigned int date1 = 0;
    unsigned int month1 = 0;
    unsigned int year1 = 0;
    /*Time and Date Set*/
     
    void I2C_Initialize(const unsigned long feq_K) //Begin IIC as master
    {
      TRISC3_bit = 1;  TRISC4_bit = 1;  //Set SDA and SCL pins as input pins
      SSPCON  = 0b00101000;
      SSPCON2 = 0b00000000;
      SSPADD = (_XTAL_FREQ/(4*feq_K*100))-1; //Setting Clock Speed pg99/234
      SSPSTAT = 0b10000000;
      }
     
    //I2C_Hold() function checks the status of the bus. It checks either I2C bus is busy or free. is It completed the last operation successfully?
    void I2C_Hold()
    {
    while((SSPCON2 & 0b00011111)||(SSPSTAT & 0b00000100)) ; //check the bis on registers to make sure the IIC is not in progress
    }
     
     
    void I2C_start()
    {
      I2C_Hold();  //Hold the program is I2C is busy
      SEN_bit = 1;     //Begin IIC pg85/234
    }
     
      //input parameter to this function can be a byte ( either address or data)
    unsigned char I2C_Write(unsigned char Data)
    {
    I2C_Hold();   // wait untill I2C_Bus of PIC18F4550 microcontroller becomes free
    SSPBUF = Data; // store data inside SSPBUF register of PIC18F4550 microcontroller
    return ACKSTAT; //return status of data or address transmission
    }
     
    unsigned char I2C_Read_byte(unsigned short ack)
    {
      unsigned short incoming;
      //I2C_Hold();
      RCEN_bit = 1;
     
      I2C_Hold();
     incoming = SSPBUF;      //get the data saved in SSPBUF
     
      I2C_Hold();
      ACKDT_bit = (ack)?0:1;    //check if ack bit received
      ACKEN_bit = 1;          //pg 85/234
      return incoming ;
    }
     
     
     
     
    void write_ds1307()
    {
    I2C_start();
       I2C_Write(0xD0);
       I2C_Write(0);
       I2C_Write(DEC_2_BCD (sec)); //update sec
       I2C_Write(DEC_2_BCD (min)); //update min
       I2C_Write(DEC_2_BCD (hour)); //update hour
       I2C_Write(1); //ignore updating day
       I2C_Write(date); //update date
       I2C_Write(month); //update month
       I2C_Write(year); //update year
       I2C_Write(0x13); //update year
       I2C_Stop();
    }
     
    void Update_Current_Date_Time()
    {
    //START to Read
       I2C_start();
       I2C_Write(0xD0);
       I2C_Write(0);
       I2C_Stop();
     
      //READ
     
       I2C_start();
       I2C_Write(0xD1);                              // Initialize data read
       sec1 = BCD_2_DEC(I2C_Read_Byte(1));
       min1 = BCD_2_DEC(I2C_Read_Byte(1));   // Read sec from register
       hour1 = BCD_2_DEC(I2C_Read_Byte(1));
    I2C_Read_Byte(1);
       date1 = BCD_2_DEC(I2C_Read_Byte(1));
       month1 = BCD_2_DEC(I2C_Read_Byte(1));
       year1 = BCD_2_DEC(I2C_Read_Byte(1));
       I2C_Stop();
     
      //END Reading
        I2C_start();
        I2C_Write(0xD1);                              // Initialize data read
       I2C_Read_Byte(1);
        I2C_Stop();
    }
     
    int  BCD_2_DEC(int convert)
    {  char r=0;
       r=((convert >> 4) * 10 + (convert & 0x0F));
       return r;
    }
     
    int DEC_2_BCD (int to_convert)
    {
       return ((to_convert / 10) << 4) + (to_convert % 10);
    }
     
     
    void main()
    {
             TRISB=0X00;
             TRISD=0X00;
             PORTB=0X00;
             PORTD=0X00;
             PORTC=0X00;
            TRISC0_bit = 0;  TRISC1_bit =0;  //Set rs and en pins as o/p pins  for lcd
                   
            I2C_Initialize(100);
            I2C_start();
            write_ds1307();
                    while(1)
                    {
                     Update_Current_Date_Time(); // Bcd2Dec
     
                         lcdcmd(0xc0);
                            //Lcd_dec1(hour1);
                             //lcdcmd(0xc5);
                            // Lcd_dec1((min1));
                            // lcdcmd(0x0c9);
                             Lcd_dec1((sec1));
     
                    }
                    }

    •   AltAdvertisement

        
       

  2. #2
    Super Moderator
    Points: 85,268, Level: 71
    Achievements:
    7 years registered
    Awards:
    2nd Helpful Member
    betwixt's Avatar
    Join Date
    Jul 2009
    Location
    Aberdyfi, West Wales, UK
    Posts
    14,032
    Helped
    4671 / 4671
    Points
    85,268
    Level
    71

    Re: PIC16F877 interface with ds1307 ...RTC running 10 sec slow

    Timekeeping is internal to the DS1307 so the interface code is unlikely to be the problem. Taking 70 seconds to complete one minute implies the RTC crystal is running at 28.086KHz instead of 32.768KHz, it is unlikely a normal crystal would 'pull' that far. Check you are using a 32.768KHz crystal and you are following the layout recommendation in the data sheet. Also make sure you have a decoupling capacitor directly across the supply and ground pins, very close to the IC.

    Brian.
    PLEASE - no friends requests or private emails, I simply don't have time to reply to them all.
    It's better to share your questions and answers on Edaboard so we can all benefit from each others experiences.



  3. #3
    Member level 2
    Points: 939, Level: 6

    Join Date
    May 2016
    Posts
    43
    Helped
    0 / 0
    Points
    939
    Level
    6

    Re: PIC16F877 interface with ds1307 ...RTC running 10 sec slow

    thanks for u r reply
    actual i am simulating circuit using PROTEUS software
    i have check DS1307 Clock frequency
    it was fine..



  4. #4
    Super Moderator
    Points: 30,356, Level: 42
    andre_teprom's Avatar
    Join Date
    Nov 2006
    Location
    Brazil
    Posts
    9,105
    Helped
    1143 / 1143
    Points
    30,356
    Level
    42
    Blog Entries
    9

    Re: PIC16F877 interface with ds1307 ...RTC running 10 sec slow

    You didn't mention before this was in a simulation environment; check messages on the log tab, or at status bar, it clearly states something like "simulation is not running in real time". I leave you the task of wondering the why.

    - - - Updated - - -

    If you had spent few more words to depict the issue with few more details, you could have the answer 2 days earlier.
    --------------------------------------------------------------------------------------------------
    Part of the world that you live in, You are the part that you're giving ( Renaissance )



  5. #5
    Member level 2
    Points: 939, Level: 6

    Join Date
    May 2016
    Posts
    43
    Helped
    0 / 0
    Points
    939
    Level
    6

    Re: PIC16F877 interface with ds1307 ...RTC running 10 sec slow

    Dear sir Thank u for your reply

    "simulation is not running in real time" is not coming in proteus simulation msg window
    Click image for larger version. 

Name:	RTC.png 
Views:	6 
Size:	107.4 KB 
ID:	158527



    •   AltAdvertisement

        
       

  6. #6
    Super Moderator
    Points: 83,894, Level: 70
    Achievements:
    7 years registered
    Awards:
    Most Frequent Poster 3rd Helpful Member

    Join Date
    Apr 2014
    Posts
    17,062
    Helped
    3854 / 3854
    Points
    83,894
    Level
    70

    Re: PIC16F877 interface with ds1307 ...RTC running 10 sec slow

    Hi,

    Honestly, I see absolutely no use in simulating the accuracy of an RTC.
    Simply trust the datasheet. The datsheet specifications should be more reliable than any simulation.

    And even if the simulation does work ... it does not mean that the real circuit works the same way, when you not read and not follow the datasheet recommendations.

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



  7. #7
    Member level 2
    Points: 939, Level: 6

    Join Date
    May 2016
    Posts
    43
    Helped
    0 / 0
    Points
    939
    Level
    6

    Re: PIC16F877 interface with ds1307 ...RTC running 10 sec slow

    Hi
    this is first time i am trying the I2C protocol ...by study ...
    Please read my code i2c code and DS1307 code check is there any errors in that ..


    i have google ...and check one DS1307 code with i2c in proteus simulation..
    it's working fine...with accuracy..

    please help ..



  8. #8
    Super Moderator
    Points: 30,356, Level: 42
    andre_teprom's Avatar
    Join Date
    Nov 2006
    Location
    Brazil
    Posts
    9,105
    Helped
    1143 / 1143
    Points
    30,356
    Level
    42
    Blog Entries
    9

    Re: PIC16F877 interface with ds1307 ...RTC running 10 sec slow

    it's working fine...with accuracy..
    Congratulations, you just realized that there are no problems regarding to the original issue.

    Please read my code i2c code and DS1307 code check is there any errors in that ..
    No, you point out exactly the problem, if any.
    --------------------------------------------------------------------------------------------------
    Part of the world that you live in, You are the part that you're giving ( Renaissance )



    •   AltAdvertisement

        
       

  9. #9
    Member level 2
    Points: 939, Level: 6

    Join Date
    May 2016
    Posts
    43
    Helped
    0 / 0
    Points
    939
    Level
    6

    Re: PIC16F877 interface with ds1307 ...RTC running 10 sec slow

    Hii sir....
    i already told my problem is my RTC showing 10Sec delay per min in proteus simulation...
    i am was not able identified problem in code ...This is first time i am doing the i2c with RTC ...

    i have searched in google ..I2C with RTC example applications with pic...their applications are running with accurecy in proteus simulation...

    please help what is the issue ...

    by this was suspect my code i2c receive functions ...



  10. #10
    Advanced Member level 2
    Points: 5,541, 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
    679
    Helped
    137 / 137
    Points
    5,541
    Level
    17

    Re: PIC16F877 interface with ds1307 ...RTC running 10 sec slow

    Your issue is simulator.
    I never trust simulators, all checks, especially with RTC should be done on real hardware. For example:
    https://www.youtube.com/watch?v=v_vNuGR10pk
    or
    https://www.youtube.com/watch?v=Vz2xHZoQKCI
    or
    https://www.youtube.com/watch?v=Pj88g6BBGHg

    All working perfectly (DS3231 used as RTC)
    Make no mistake between my personality and my attitude.
    My personality is who I am.
    My attitude depends on who you are.



  11. #11
    Super Moderator
    Points: 30,356, Level: 42
    andre_teprom's Avatar
    Join Date
    Nov 2006
    Location
    Brazil
    Posts
    9,105
    Helped
    1143 / 1143
    Points
    30,356
    Level
    42
    Blog Entries
    9

    Re: PIC16F877 interface with ds1307 ...RTC running 10 sec slow

    i already told my problem is my RTC showing 10Sec delay per min in proteus simulation...
    As said, regardless of whether the simulator exhibit the message "non-realtime simulation" or not (even because only Informations, not Warnings messages were displayed in your log), trust us; Simulation much often - not to say, always - DO NOT occurs at the same speed as it is performed in the real world.
    --------------------------------------------------------------------------------------------------
    Part of the world that you live in, You are the part that you're giving ( Renaissance )



  12. #12
    Advanced Member level 5
    Points: 15,687, Level: 30
    Achievements:
    7 years registered
    schmitt trigger's Avatar
    Join Date
    Apr 2013
    Posts
    2,606
    Helped
    834 / 834
    Points
    15,687
    Level
    30

    Re: PIC16F877 interface with ds1307 ...RTC running 10 sec slow

    I've used the DS1307 and DS3231 in dozens of projects.
    As long as the proper decimal to BCD conversions are made, which appear correct in your code, and the time/date registers are written and read in the exact order as shown in the datasheet, the displayed values are ALWAYS, ALWAYS, ALWAYS an exact ratio of the crystal frequency.

    Both the 1307 and 3231 have a control register which allows you to output a replica of the crystal frequency on the SQW/OUT pin (pin 7 on the 1307). Read the datasheet on how to set up that control register.
    If the frequency at that pin is exactly 32768 Hz, then all of the timing register values will be correct.

    As simple as that.
    My batteries are recharged by "Helpful Post" ratings.
    If you feel that I've helped you, please indicate it as a Helpful Post



  13. #13
    Advanced Member level 2
    Points: 5,541, 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
    679
    Helped
    137 / 137
    Points
    5,541
    Level
    17

    Re: PIC16F877 interface with ds1307 ...RTC running 10 sec slow

    The point is that the OP is running the code in SIMULATOR! :D
    Make no mistake between my personality and my attitude.
    My personality is who I am.
    My attitude depends on who you are.



  14. #14
    Advanced Member level 5
    Points: 20,634, Level: 34
    Achievements:
    7 years registered
    pranam77's Avatar
    Join Date
    Apr 2008
    Location
    INDIA
    Posts
    2,785
    Helped
    627 / 627
    Points
    20,634
    Level
    34

    Re: PIC16F877 interface with ds1307 ...RTC running 10 sec slow

    As i understand you are from India and here we have a lot sources with spurious and fake Chinese components. The DS1307 runs with an external crystal of 32.768khz which is problem in this case. The crystal will not be accurate compared to its marking. Next the temperature too will act a lot to the crystal accuracy. Using DS3231 in place of the DS1307 will drastically increase accuracy as it has inbuilt crystal and has better accuracy than the later. But still i presume that the atmospheric temperature or the temperature inside the enclosure is quite normal in your case. Also i would suggest if you aim at accuracy, buy components only from reputed sources like RS delivers, Element14, Mouser, etc



  15. #15
    Super Moderator
    Points: 30,356, Level: 42
    andre_teprom's Avatar
    Join Date
    Nov 2006
    Location
    Brazil
    Posts
    9,105
    Helped
    1143 / 1143
    Points
    30,356
    Level
    42
    Blog Entries
    9

    Re: PIC16F877 interface with ds1307 ...RTC running 10 sec slow

    Also i would suggest if you aim at accuracy, buy components only from reputed sources
    Again, there is no issue with real world components, OP is claiming inaccuracy of the code running on simulation environment, which do not work real time.
    --------------------------------------------------------------------------------------------------
    Part of the world that you live in, You are the part that you're giving ( Renaissance )



  16. #16
    Advanced Member level 5
    Points: 15,687, Level: 30
    Achievements:
    7 years registered
    schmitt trigger's Avatar
    Join Date
    Apr 2013
    Posts
    2,606
    Helped
    834 / 834
    Points
    15,687
    Level
    30

    Re: PIC16F877 interface with ds1307 ...RTC running 10 sec slow

    The OP has not responded in over a week.
    He/she either corrected the problem or lost interest.
    My batteries are recharged by "Helpful Post" ratings.
    If you feel that I've helped you, please indicate it as a Helpful Post



    •   AltAdvertisement

        
       

  17. #17
    Advanced Member level 5
    Points: 20,634, Level: 34
    Achievements:
    7 years registered
    pranam77's Avatar
    Join Date
    Apr 2008
    Location
    INDIA
    Posts
    2,785
    Helped
    627 / 627
    Points
    20,634
    Level
    34

    Re: PIC16F877 interface with ds1307 ...RTC running 10 sec slow

    Quote Originally Posted by andre_teprom View Post
    Again, there is no issue with real world components, OP is claiming inaccuracy of the code running on simulation environment, which do not work real time.
    Quote Originally Posted by Teja.p View Post
    thanks for u r reply
    actual i am simulating circuit using PROTEUS software
    i have check DS1307 Clock frequency
    it was fine..
    Oh..i just saw the first post where he didn't mention about the simulation. Proteus is not 100% accurate and cannot trust i always. Only results on real hardware are practical and accurate. Try assembling the circuit and re-check.



--[[ ]]--