[51] RTC DS1307 Interfacing with 8051 problem

Status
Not open for further replies.

MANO.5

Member level 1
Joined
Mar 15, 2013
Messages
36
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,286
Visit site
Activity points
1,618
hai friends,
I am new to embedded field. I have working circuit with 8051-micro and ds1307 rtc with time set function.But it was not working properly.when starting rtc it give correct time.Then rtc time will decreases than actual time.crystal for microcontroller -12Mhz and for-DS1307 is 32.768khz.Circuit Diagram is


please help anybody.as soon as possible

my code is


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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
#include<reg51.h>
#include<ds1307.h>
#include<string.h>
sbit rs=P1^0;
sbit rw=P1^1;
sbit en=P1^2;
sbit  sda_rtc =P1^3;
sbit  scl_rtc =P1^4;
 
sfr ldata=0xA0;    //p2
sfr KEYPRT=0xB0;  // Port for keypad        
 
    
sbit ROW0 = P3^4;
sbit ROW1 = P3^5;
sbit ROW2 = P3^6;
sbit ROW3 = P3^7;
sbit COL0 = P3^0;
sbit COL1 = P3^1;
sbit COL2 = P3^2;
sbit COL3 = P3^3; 
 
 
DS1307_get(unsigned char);
unsigned char KEYGET(void);
void lcdcommnd(unsigned char value);
void lcddata(unsigned char value);
void lcd_putc(unsigned char ascii);
void MSDelay(int ms);
void LcdLine1(unsigned char *value);
void LcdLine2(unsigned char *value);
 send2lcd(unsigned char value);
 
unsigned int Rtc_add_wr,Rtc_add_rd;
unsigned char d_rtc,datain_rtc,in_rtc,temp_rtc,dat_rtc,flag_rtc;
void MSDelay(int ms)
{
unsigned int i,j;
for(i=0;i<ms;i++)
{
for(j=0;j<1275;j++);
}
}
void lcdcommnd(unsigned char value)
{
    ldata= value;
    rs= 0;
    rw=0;
    en=1;
    MSDelay(1);
    en=0;
    return;
 
} 
void lcddata(unsigned char value)
{
 
    ldata = value;
    rs= 1;
    rw=0;
    en=1;
    MSDelay(1);
    en=0;
    return;
 
 
}
void lcd_string(unsigned char *value)   // Function to send string to LCD
{
     int x;
    for(x=0;value[x]!=0;x++)
    {
        lcddata(value[x]);
    }
 
}
void lcd_init()
{
        lcdcommnd(0x38);
    MSDelay(100);       
    lcdcommnd(0x0e);
    MSDelay(100);      
    lcdcommnd(0x01);
    MSDelay(100);
 
}
void lcd_decimal2(unsigned char val)
{
    unsigned char d,d1,d2;
 d=val%100;
d1=d/10;
d2=d%10;
 lcddata(d1+48);
lcddata(d2+48);
}
 
send2lcd(unsigned char value)
{
     unsigned char buf1,buf2,buf = 0;
 
  buf1 = value & 0xF0; /* Filter for high byte */
  buf1 = (buf1>>4); /* Convert  to ascii code */
   buf2 = value & 0x0F; /* Filter for low byte */
   buf=(buf1*10)+buf2;
   return buf;
} 
 
   unsigned char KEYGET() 
{
unsigned char colloc, rowloc, keyvalue;
unsigned char keypad[4] [4] = {'0', '1', '2','3',
'4', '5', '6', '7',
'8', '9', 'A', 'B',
'C', 'D', 'E', 'F'};
do
{ 
ROW0 = 0;
ROW1 = 0;
ROW2 = 0;
ROW3 = 0; // ground all rows
colloc = KEYPRT; // read port0
colloc &= 0x0F; //mask used bits 
} while ( colloc != 0X0F); // check until all keys released
 
do
{ do
{
MSDelay(5);
colloc = KEYPRT; // read port0
colloc &= 0x0F; //mask used bits
} while ( colloc == 0X0F); //keep checking for keypress
 
MSDelay(5); //delay for debounce
colloc = KEYPRT; // read port0
colloc &= 0x0F; //mask used bits
} while ( colloc == 0X0F); //keep checking for keypress
 
while (1)
 
{
ROW0 = 0; // ground row0
ROW1 = 1;
ROW2 = 1;
ROW3 = 1;
colloc = KEYPRT; // read port0
colloc &= 0x0F; //mask used bits
if( colloc!=0x0F)
{
rowloc = 0;
break; 
}
 
ROW0 = 1; // ground row1
ROW1 = 0;
ROW2 = 1;
ROW3 = 1;
colloc = KEYPRT; // read port0
colloc &= 0x0F; //mask used bits
if( colloc!=0x0F)
{
rowloc = 1;
break; 
}
 
ROW0 = 1; // ground row2
ROW1 = 1;
ROW2 = 0;
ROW3 = 1;
colloc = KEYPRT; // read port0
colloc &= 0x0F; //mask used bits
if( colloc!=0x0F)
{
rowloc = 2;
break; 
}
ROW0 = 1; // ground row3
ROW1 = 1;
ROW2 = 1;
ROW3 = 0;
colloc = KEYPRT; // read port0
colloc &= 0x0F; //mask used bits
rowloc = 3;
break; 
}
 
if( colloc ==0x0E)
keyvalue = keypad [rowloc] [0];
else if ( colloc ==0x0D)
keyvalue = keypad [rowloc] [1];
else if ( colloc ==0x0B)
keyvalue = keypad [rowloc] [2];
else
keyvalue = keypad [rowloc] [3];
return (keyvalue);
}
 
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_Ack()// this is to intimate the EEPROM that the read operation is over
{
sda_rtc=1;
scl_rtc=1;
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_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_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;// 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;
}
 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;
Rtc_Tx();
if(CY==1)goto be;
Rtc_Rx();
Rtc_Ack();
CY=0;
Rtc_Stop();
return(datain_rtc);
}
 DS1307_get(unsigned char addr)
{
unsigned char ret;
 
Rtc_Start();            
ret = Rtc_Read(addr);
Rtc_Stop();
 return ret;
}
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();
}
void DS1307_settime(unsigned char hh, unsigned char mm, unsigned char ss)
{
Rtc_Start(); 
 
Rtc_Write(0x00,ss); /* Write sec on RAM address 00H */
Rtc_Write(0x01,mm); /* Write min on RAM address 01H */
Rtc_Write(0x02,hh); /* Write hour on RAM address 02H */
Rtc_Stop();           /* Stop i2c bus */
}
void DS1307_setdate(unsigned char dd, unsigned char mm, unsigned char yy)
{
  Rtc_Start();
Rtc_Write(0x04,dd); /* Write date on RAM address 04H */
Rtc_Write(0x05,mm); /* Write month on RAM address 05H */
Rtc_Write(0x06,yy); /* Write year on RAM address 06H */
Rtc_Stop(); /* Stop i2c bus */
}
void main()
{
unsigned int y,tens,ones;
unsigned char sec, min, hour, date, month, year;
//unsigned char a,b,c,d,e,hr1,hr2,sec1,sec2,min1,min2,dt1,dt2,mon1,mon2;
lcd_init();
MSDelay(200);
Rtc_Init();
lcdcommnd(0x80);
lcd_string("1->SET DATE&TIME"); 
lcdcommnd(0xC0);
lcd_string ("Any other-> SKIP");
COL0 = 1; // make cols input port
COL1 = 1;
COL2 = 1;
 
y = KEYGET();
lcdcommnd(0x01); // Clear Command
MSDelay(20);
lcddata(y);
MSDelay(20);
if (y == '1')
{
        lcdcommnd(0x01);       
lcdcommnd(0x80);
lcd_string("HR:MN:SC");
////////////////hour/////////////////////
y = KEYGET();
tens = y & 0x0F;
tens = (tens << 4) & 0xF0;
lcdcommnd(0XC0);
lcddata(y);
y = KEYGET();
ones = y & 0x0F;
lcdcommnd(0XC1);
lcddata(y);
hour = (tens |ones );
lcdcommnd(0XC2);
lcddata(':');
////////////////min/////////////////////
y = KEYGET();
tens = y & 0x0F;
tens = (tens << 4) & 0xF0;
lcdcommnd(0XC3);
lcddata(y);
y = KEYGET();
ones = y & 0x0F;
lcdcommnd(0XC4);
lcddata(y);
min = (tens |ones );
lcdcommnd(0XC5);
lcddata(':');
////////////////sec/////////////////////
y = KEYGET();
tens = y & 0x0F;
tens = (tens << 4) & 0xF0;
lcdcommnd(0XC6);
lcddata(y); 
y = KEYGET();
ones = y & 0x0F;
lcdcommnd(0XC7);
lcddata(y);
sec = (tens |ones );
///////////////////////////////////////
MSDelay(20);
////////////////////////////////////////////
lcdcommnd(0x01); // Clear Command
MSDelay(20);
lcdcommnd(0x80);
lcd_string("DD:MM:YY");
////////////////date/////////////////////
y = KEYGET();
tens = y & 0x0F;
tens = (tens << 4) & 0xF0;
lcdcommnd(0XC0);
lcddata(y);
y = KEYGET();
ones = y & 0x0F;
lcdcommnd(0XC1);
lcddata(y);
date = (tens |ones );
lcdcommnd(0XC2);
lcddata(':');
//////////////////month/////////////////
y = KEYGET();
tens = y & 0x0F;
tens = (tens << 4) & 0xF0;
lcdcommnd(0XC3);
lcddata(y);
y = KEYGET();
ones = y & 0x0F;
lcdcommnd(0XC4);
lcddata(y);
month = (tens |ones );
lcdcommnd(0XC5);
lcddata(':');
///////////////////////////////////////
//////////////////year/////////////////
y = KEYGET();
tens = y & 0x0F;
tens = (tens << 4) & 0xF0;
lcdcommnd(0XC6);
lcddata(y);
y = KEYGET();
ones = y & 0x0F;
lcdcommnd(0XC7);
lcddata(y);
year = (tens |ones );
///////////////////////////////////////
/////////////////////////////////////////
 
DS1307_settime(hour,min,sec); /* Set Time (hh:mm:ss) */       
DS1307_setdate(date,month,year); /* Set Date (dd/mm/yy) */
MSDelay(200);
lcdcommnd(0x01); // Clear Command       
lcdcommnd(0x80);
lcd_string("Date:"); 
lcdcommnd(0xC0);
 lcd_string("Time:"); 
 sec = DS1307_get(0x00); 
min = DS1307_get(0x01); 
hour = DS1307_get(0x02);
date = DS1307_get(0x04);
month = DS1307_get(0x05);
year = DS1307_get(0x06);
sec=send2lcd(sec);
min=send2lcd(min);
hour=send2lcd(hour);
date=send2lcd(date);
month=send2lcd(month);
year=send2lcd(year);
 lcdcommnd(0x80);
lcd_string("Date:"); 
lcdcommnd(0xC0);
 lcd_string("Time:"); 
lcdcommnd(0x85); 
lcd_decimal2(date);
 lcdcommnd(0x87);
lcddata('/');
lcdcommnd(0x88); 
lcd_decimal2(month);
lcdcommnd(0x8A);
lcddata('/');
lcdcommnd(0x8B); 
lcd_decimal2(year); 
lcdcommnd(0xC5); 
lcd_decimal2(hour);
 lcdcommnd(0xC7);
lcddata('.');
lcdcommnd(0xC8);
lcd_decimal2(min); 
lcdcommnd(0xCA);
lcddata('.');
lcdcommnd(0xCB);
lcd_decimal2(sec);
MSDelay(100);
 
}
 
while(1) 
{
lcdcommnd(0x01);
 
/////////////////////////////rtc///////////////////////////////
  Rtc_Init();
sec = DS1307_get(0x00); 
min = DS1307_get(0x01); 
hour = DS1307_get(0x02);
date = DS1307_get(0x04);
month = DS1307_get(0x05);
year = DS1307_get(0x06);
sec=send2lcd(sec);
min=send2lcd(min);
hour=send2lcd(hour);
date=send2lcd(date);
month=send2lcd(month);
year=send2lcd(year);
 lcdcommnd(0x80);
lcd_string("Date:"); 
lcdcommnd(0xC0);
 lcd_string("Time:"); 
lcdcommnd(0x85); 
lcd_decimal2(date);
lcdcommnd(0x87);
lcddata('/');
lcdcommnd(0x88); 
lcd_decimal2(month);
 lcdcommnd(0x8A);
lcddata('/');
lcdcommnd(0x8B); 
lcd_decimal2(year); 
lcdcommnd(0xC5); 
lcd_decimal2(hour);
 lcdcommnd(0xC7);
lcddata('.');
lcdcommnd(0xC8);
lcd_decimal2(min); 
lcdcommnd(0xCA);
lcddata('.');
lcdcommnd(0xCB);
lcd_decimal2(sec);
MSDelay(100);
}
}

 
Last edited by a moderator:

Place

Code C - [expand]
1
Rtc_Init();

in main() function before

Code C - [expand]
1
while(1)

loop. RTC should be initialized only once. Have you compiled the code and tested the hex file. Does it really work or are you trying the hex file that came with the project without compiling?
 


hai jayanth,
Thank you for your reply.After making changes also it didn't work.I just simulate the circuit and program in proteus6.9.The clock was updated very slow means updated for 4or 5 seconds delay.So is there any problem with crystal oscillator or delay??.....
 

Which key has to be pressed to run the time. Here the current time is shown but time doesn't run.

if key 2(means 1) is pressed then time set mode is displaying.anything other than 1 will display current time.
it was looking like clock seconds register was updated after 1.33 seconds or more when starting and even more after some time
 

I ran the simulation for 15 minutes and saw only 1 sec difference between LCD time and Proteus DS1307 time throughout the simulation duration. Remove MSDelay(100) which is the last line of while(1) loop. Try after that. Post the new compiled hex file.



Edit: Use timer interrupt to update LCD data.
 
Last edited:

Hello sir,
i start the simulation at 1.40pm(13.40) today after 30 minutes it show 1.57 pm(23.57) instead of 2.10(14.10). I already tried with 10 ms delay even without delay also.nothing changed.is anything problem with proteus version. I was using proteus 6.9...
 

hello sir,
i tried in proteus 7.6 also .same result.when compiling this code it will give warning
c(2): warning C318: can't open file 'ds1307.h'

is this a problem??how to solve it
 

when compiling this code it will give warning
c(2): warning C318: can't open file 'ds1307.h'

is this a problem??how to solve it

Yes, it is a problem.

If the ds1307.h header file is located with the other C source files of your project, you may have to change the <> to "" as such:

Code:
#include<reg51.h>
#include"ds1307.h"
#include<string.h>
sbit rs=P1^0;
sbit rw=P1^1;
sbit en=P1^2;
sbit  sda_rtc =P1^3;
sbit  scl_rtc =P1^4;
...
...

Typically, the quotes in the #include directive indicate to the compilers preprocessor to begin searching for the header file in the current project directory, rather than the default include directory of the compiler installation or other specified header file locations.

What compiler are you using? KEIL C51?

KEIL Documentation - #include

BigDog
 


Upload your COMPLETE project, not just the Proteus project (DSN) file and the HEX file, include ALL C source (main.c), ds1307.h and other source files (ds1307.c) required.

What compiler and version are you utilizing to compile the project?


BigDog
 

hello sir ,
I posted the program in my first post of this thread. I am using trial version of keil IDE v4 with limitation of program size within 8 kb..

keil IDE-Version:
µVision V4.24.00.0
C Compiler: C51.Exe V9.06
Assembler: A51.Exe V8.02a
Linker/Locator: BL51.Exe V6.22
Librarian: LIB51.Exe V4.29
Hex Converter: OH51.Exe V2.6
CPU DLL: S8051.DLL V3.79.0.1
Dialog DLL: DP51.DLL V2.62.0.1
 

You have only posted the main C source file in your original post.

Please post ds1307.h header file and ds1397.c if it exists.

BigDog
 

don't have any other files.i think all the rtc functions are in that program....
 

Then you need to remove the following preprocessor statement:

Code:
#include<ds1307.h>

You cannot include a header file which does not exist.

BigDog
 

Status
Not open for further replies.

Similar threads

Cookies are required to use this site. You must accept them to continue using the site. Learn more…