Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

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

Status
Not open for further replies.

Teja.p

Member level 2
Joined
May 29, 2016
Messages
44
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,286
Activity points
1,727
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));
 
                }
                }

 

Attachments

  • New Text Document (2).txt
    3.5 KB · Views: 54

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.
 

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

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.
 

Dear sir Thank u for your reply

"simulation is not running in real time" is not coming in proteus simulation msg window
RTC.png
 

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
 

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 ..
 

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.
 

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 ...
 


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.
 

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.
 

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
 

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.
 

The OP has not responded in over a week.
He/she either corrected the problem or lost interest.
 

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.

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.
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top