-
Notifications
You must be signed in to change notification settings - Fork 0
/
RTC.c
725 lines (657 loc) · 20 KB
/
RTC.c
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
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
//-----------------------------------------------------------------------------
//RTC.c
//-----------------------------------------------------------------------------
//
// Program Description:
//
// This program interfaces with DS1307 ( Real time clock ) via the i2c communication protocal.
// It sets the time & calender registers and runs the clock.
//
// How To Test:
//
// 1) Download code to the Pt-51 board
// 2) Connect SCL,SDA,5v and gnd to the respective pins on board.
// 3) You should see a normal working clock set to the following LCD display:
// 10:00:00
// 10|06|2k13 MON
// 4) Press the set key and enter the set mode to set the time
//
// Keypad numbering : With respect to the port given the first 3x3 matrix represents thr numbers from 1-9
// Row 1 last coloumn is the set key ( refernce : port facing us, the left column is the the first coloumn )
// Row 2 last coloumn is the number 0
//
//
// Author: P.Anusri
// Target: C8051F38x
// Tool chain: Keil / Raisonance
// Command Line: None
//
//
// Release 1.0 - 10-JUNE-2013 (PKC)
// -Initial Revision
//
//-----------------------------------------------------------------------------
// Includes
//-----------------------------------------------------------------------------
#include<at89c5131.h> // SFR declarations
//-----------------------------------------------------------------------------
// Global Constants
//-----------------------------------------------------------------------------
Sbit ( RW , P0 , 1 ) ;
Sbit ( RS , P0 , 0 ) ;
Sbit ( EN , P0 , 2 ) ;
Sfr ( Dt , 0xA0 ) ;
//-----------------------------------------------------------------------------
// Global Variables
//-----------------------------------------------------------------------------te
bit j;
bit p;
bit flag;
unsigned char i;
unsigned char temp;
unsigned char R_W;
unsigned long ph;
unsigned char seconds;
unsigned char minutes;
unsigned char hours;
unsigned char day;
unsigned char date;
unsigned char month;
unsigned char year;
unsigned char stored_word;
unsigned char prsnt_word;
//-----------------------------------------------------------------------------
// Function Prototypes
//-----------------------------------------------------------------------------
void delay();
void status ();
void command ( unsigned char a );
void display (unsigned char a);
void ascii (unsigned char a);
void i2c_interrupt ();
void scan();
void back();
void check_set();
void check();
void set_mode();
//-----------------------------------------------------------------------------
// main() Routine
//-----------------------------------------------------------------------------
void main()
{
command(0x38); // function set command
command(0x0f); // display switch command
command(0x06); // input set command
command(0x01); // clear screen command
seconds = 0x00; // set initial time values
minutes=0x00;
hours=0x10;
day=0x07;
date=0x16;
month=0x06;
year=0x13;
p = 0; // variable used to differentiate between write and read paths
j = 0; // variable used to either start or continue with the protocol
R_W = 0; // write mode set
IEN1 = IEN1 | 0X02; // i2c interrupt enabled
EA = 1; // Enabling all interrupts
SSCON = SSCON | 0x81; // set bit frequency to 100kHz
SSCON = SSCON | 0x40; // synchronous serial enable bit
SSCON = SSCON & 0xc5; // clear start,stop and serial interrupt fla
while(1)
{
if(j==0)
{
SSCON = SSCON | 0x20; // Set to send a START condition on the bus
j = 1; // set to enter loop
}
while(j==1) {} // further protocol on interruption
}
}
//-----------------------------------------------------------------------------
// Initialization Subroutines
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
// status
//-----------------------------------------------------------------------------
//
// Return Value : None
// Parameters : None
//
// This function checks if the LCD is busy.
//
//-----------------------------------------------------------------------------
void status()
{
EN = 0 ;
RS = 0 ;
RW = 1 ; // read mode of LCD
Dt = 0xFF ; // configuring port pins as inputs
EN = 1 ;
while (P2_7==1) {} ; // wait if the lcd uC is busy
EN=0 ;
}
//-----------------------------------------------------------------------------
// command
//-----------------------------------------------------------------------------
//
// Return Value : None
// Parameters :
// 1) Unsigned char a - 8 bit command value to the LCD
//
// This function transmits the desired command to the LCD uC
//
//-----------------------------------------------------------------------------
void command ( unsigned char a )
{
status(); // checks the status of the LCD
EN = 0;
RS = 0;
RW = 0; // write mode of LCD
EN = 1;
Dt = a; // command transfered to port pins
EN = 0;
}
//-----------------------------------------------------------------------------
// display
//-----------------------------------------------------------------------------
//
// Return Value : None
// Parameters :
// 1) Unsigned char a - 8 bit ascii value (hex value) of the character to be displayed
//
// This function transmits the ascii value of the character to be displayed ti LCD uC
//
//-----------------------------------------------------------------------------
void display (unsigned char a)
{
status(); // checks the status of the LCD
EN = 0;
RS = 1;
RW = 0; // write mode of LCD
EN = 1;
Dt = a; // 8 bit hex value of character transfered to port pins
EN = 0;
}
//-----------------------------------------------------------------------------
// ascii
//-----------------------------------------------------------------------------
//
// Return Value : None
// Parameters :
// 1) Unsigned char a - 8 bit value read from the DS1307 registers
//
// This function displays the BCD value read from the DS1307 registers
//
//-----------------------------------------------------------------------------
void ascii (unsigned char a)
{
temp = a & 0xf0; // masking the lower-order nibble
temp = (temp>>4); // bit shift to find the value of high-order nibble
display(temp+0X30); // display the high-order nibble of byte
temp = a & 0x00f; // masking the high-order nibble
display(temp+0X30); // display lower-order nibble of byte
}
//-----------------------------------------------------------------------------
// delay
//-----------------------------------------------------------------------------
//
// Return Value : None
// Parameters :
// 1) Unsigned char a - 8 bit value to be multiplied to 10ms delay
//
// This function produces a delay of multiples of 10ms
//
//-----------------------------------------------------------------------------
void delay (unsigned char a)
{
for(ph=0;ph<(300*a);ph++) {}
}
//-----------------------------------------------------------------------------
// back
//-----------------------------------------------------------------------------
//
// Return Value : None
// Parameters : None
//
//
// This function eliminates the key press and held case
//
//-----------------------------------------------------------------------------
void back()
{
P3=0xf0; // configuring higher nibble as input and lower nibble as output
while ( P3 != 0xf0 ); // wait till no key pressed
P3 = 0x0f; // configuring lower nibble as input and higher nibble as output
while (P3 != 0x0f ); // wait till no key pressed
delay(10); // debouncing time
P3=0xf0; // repeat the process
while ( P3 != 0xf0 );
P3 = 0x0f;
while (P3 != 0x0f );
}
//-----------------------------------------------------------------------------
// scan
//-----------------------------------------------------------------------------
//
// Return Value : None
// Parameters : None
//
//
// This function waits till a key is pressed
//
//-----------------------------------------------------------------------------
void scan()
{
P3=0x0f; // configuring lower nibble as input and higher nibble as output
while(P3==0x0f); // wait till some key is pressed
stored_word=P3; // save the row status pressed
P3=0xf0; // configuring lower nibble as input and higher nibble as output
while(P3==0xf0); // wait till some key is pressed
stored_word = (stored_word | P3); // save the coloumn status pressed
delay(1); // debouncing time
P3=0x0f; // repeat the above procedure and save the keypad status in prsnt_word
while(P3==0x0f);
prsnt_word=P3;
P3=0xf0;
while(P3==0xf0);
prsnt_word = (prsnt_word | P3);
if(stored_word==prsnt_word) // check if the same key pressed before and after debouncing time
{
flag=1; // valid input
}
}
//-----------------------------------------------------------------------------
// check_set
//-----------------------------------------------------------------------------
//
// Return Value : None
// Parameters : None
//
//
// This function decides whether or not to enter the set mode
//
//-----------------------------------------------------------------------------
void check_set()
{
if(stored_word==0x7e && flag==1)
set_mode(); // if set key pressed enter set mode
else
return; // if set key not pressed return back to normal RTC
}
//-----------------------------------------------------------------------------
// check
//-----------------------------------------------------------------------------
//
// Return Value : None
// Parameters : None
//
//
// This function identifies the key pressed with the corresponding number
//-----------------------------------------------------------------------------
void check()
{
switch(stored_word)
{
case 0xee :
{
stored_word = 1;
break;
}
case 0xde :
{
stored_word = 2;
break;
}
case 0xbe :
{
stored_word = 3;
break;
}
case 0xed :
{
stored_word = 4;
break;
}
case 0xdd :
{
stored_word = 5;
break;
}
case 0xbd :
{
stored_word = 6;
break;
}
case 0xeb :
{
stored_word = 7;
break;
}
case 0xdb :
{
stored_word = 8;
break;
}
case 0xbb :
{
stored_word = 9;
break;
}
case 0x7d :
{
stored_word = 0;
break;
}
}
}
//-----------------------------------------------------------------------------
// set_mode
//-----------------------------------------------------------------------------
//
// Return Value : None
// Parameters : None
//
//
// This function allows the user to enter set mode and set the calender
//
//-----------------------------------------------------------------------------
void set_mode()
{
command(0x01); // clear screen command
command(0x81); // set cursor
display('S'); // display SET MODE
display('E');
display('T');
display(' ');
display('M');
display('0');
display('D');
display('E');
back(); // wait for the user to leave the set key
delay(100); // wait for 1 sec
command(0x01); // clear screen command
command(0x81); // set cursor
for(i=0;i<7;i=i+1)
{
P3=0xff;
flag=0; // high flag indicates a valid keypress
while(flag==0)
{
scan(); // wait till key is pressed
}
check(); // check which key has been pressed
display(stored_word+0x30); // diplay the key number pressed
back(); // check if the key is held pressed
temp=stored_word; // store the higher BCD digit to update the time register
temp=temp<<4;
delay(10);
P3=0xff;
flag=0;
while(flag==0)
{
scan(); // wait till key is pressed
}
check(); // check which key has been pressed
display(stored_word+0x30); // diplay the key number pressed
back(); // check if the key is held pressed
temp=temp+stored_word; // store the lower BCD digit to update the time register
if(i==0)
{
hours=temp; // update hours register
display(':');
}
else if ( i==1)
{
minutes=temp; // update minutes register
display(':');
}
else if ( i==2)
{
seconds=temp; // update seconds register
command(0xc1); // set cursor position
}
else if ( i==3)
{
date=temp; // update dates register
display('|');
}
else if ( i==4)
{
month=temp; // update months register
display('|');
display('2');
display('k');
}
else if ( i==5)
{
year=temp; // update year register
display(' ');
}
else if ( i==6)
day=temp; // update day register
}
p=0; // set the write registers path
i=0;
}
//-----------------------------------------------------------------------------
// Interrupt Service Routines
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
//i2c_interrupt
//-----------------------------------------------------------------------------
//
// This routine decides further protocol to be executed
//
//-----------------------------------------------------------------------------
void i2c_interrupt () interrupt 8
{
switch(SSCS)
{
case (0x08) : // start condition has been transmitted
{
SSDAT = 0XD0 + R_W; // slave address (SLA) + write (W) transmitted
SSCON &= 0XE7; // clear interrupt and stop flag
break;
}
case (0x20) : // SLA+W has been transmitted NOT ACK has been received
{
SSCON = 0Xd1; // stop condition will be transmitted and interrupt flag cleared
j=0; // escape loop and start condition transmitted ( in main )
i=0;
break;
}
case 0x18 : // SLA+W has been transmitted ACK has been received
{
SSDAT = 0x00; // set pointer address to 00h
SSCON &= 0Xc7; // Data byte will be transmitted
break;
}
case 0x28 : // data byte has been transmitted ACK has been received
{
if (p==0) // enter when pointer address set to write
{
if (i==0)
{
SSDAT = seconds; // setting seconds to 00
SSCON &= 0xc7; // data byte will be transmitted
}
else if (i==1)
{
SSDAT = minutes; // setting minutes to 00
SSCON &= 0xc7; // data byte will be transmitted
}
else if (i==2)
{
SSDAT = hours; // setting hours to 10
SSCON &= 0xc7; // data byte will be transmitted
}
else if (i==3)
{
SSDAT = day; // setting day to 01
SSCON &= 0xc7; // data byte will be transmitted
}
else if (i==4)
{
SSDAT = date; // setting date to 10
SSCON &= 0xc7; // data byte will be tranmitted
}
else if (i==5)
{
SSDAT = month; // setting month to 06
SSCON &= 0xc7; // data byte will be transmitted
}
else if (i==6)
{
SSDAT = year; // setting year to 2k'13'
SSCON &= 0xc7; // data byte will be transmitted
}
else if (i==7)
{
SSDAT = 0X10; // using control register to enable a 1 Hz square wave output
SSCON &= 0xc7; // data byte will be transmitted
}
else if(i==8)
{
SSCON = 0xd1; // stop condition will be transmitted and interrupt flag cleared
j=0; // escape loop and start condition transmitted ( in main )
p=1;
}
i=i+1;
}
if ( p==1 && j==1 ) // enter when pointer address set to read
{
R_W = 0x01 ; // set read mode
SSCON = 0xe1; // repeated start will be transmitted
}
break;
}
case 0X30 : // data byte has been transmitted and NOT ACK has been received
{
SSCON = 0Xd1; // stop condition will be transmitted and interrupt flag cleared
j=0; // escape loop and send start condition ( in main )
i=0;
break;
}
case 0x10 : // repeated start condition has been transmitted
{
SSDAT = 0XD0 + R_W; // slave address (SLA) + read (R) transmitted
SSCON &= 0XE7; // clear interrupt and stop flag
break;
}
case 0X40 : // SLA+R has been transmitted ACK has been transmitted
{
i=0;
SSCON = 0xC5; // data byte will be received and ACK will be returned
break;
}
case 0X48 : // SLA+R has been transmitted NOT ACK has been transmitted
{
SSCON &= 0Xd1; // stop condition will be transmitted and interrupt flag cleared
j=0; // escape loop and send start condition ( in main )
i=0;
break;
}
case 0x50 : // data byte has been received, ACK has been returned
{
if(i==0)
{
seconds = SSDAT; // read seconds register
SSCON = 0xc5; // data byte will be received and ACK will be returned
}
if(i==1)
{
minutes = SSDAT; // read minutes register
SSCON = 0xc5; // data byte will be received and ACK will be returned
}
if(i==2)
{
hours = SSDAT; // read hours register
SSCON = 0xc5; // data byte will be received and ACK will be returned
}
if(i==3)
{
day = SSDAT; // read days register
SSCON = 0xc5; // data byte will be received and ACK will be returned
}
if(i==4)
{
date = SSDAT; // read date register
SSCON = 0xc5; // data byte will be received and ACK will be returned
}
if(i==5)
{
month = SSDAT; // read months register
SSCON &= 0xc3; // data byte will be received and NOT ACK will be returned
}
i=i+1;
break;
}
case 0X58 : // data byte has been received,NOT ACK has been returned
{
year = SSDAT ; // read years register
command(0x81); // set cursor position ( first line )
ascii(hours); // display data from hours register
display(':');
ascii(minutes); // display data from minutes register
display(':');
ascii(seconds); // display data from seconds register
command(0xc1); // set cursor position ( second line )
ascii(date); // display data frm date register
display('|');
ascii(month); // display data frm months register
display('|');
display('2');
display('k');
ascii(year); // display data frm years register ( format 2k13 )
command(0xcc); // set cursor position
switch(day)
{
case 0x01 : {display('M'); // based on the days register-display the day in characters
display('0');
display('N');break;}
case 0x02 : {display('T');
display('U');
display('E');break;}
case 0x03 : {display('W');
display('E');
display('D');break;}
case 0x04 : {display('T');
display('H');
display('U');break;}
case 0x05 : {display('F');
display('R');
display('I');break;}
case 0x06 : {display('S');
display('A');
display('T');break;}
case 0x07 : {display('S');
display('U');
display('N');break;}
}
SSCON = 0xd1; // stop condition will be transmitted and interrupt flag cleared
j = 0; // escape loop and send start condition ( in main )
i = 0;
R_W = 0; // set write mode
P3=0xff;
flag=0;
P3=0x0f; // configuring lower nibble as input and higher nibble as output
delay(1);
stored_word=P3; // save the row status pressed
P3=0xf0; // configuring higher nibble as input and lower nibble as output
stored_word = (stored_word | P3); // save the coloumn status pressed
delay(1); // debouncing time
P3=0x0f; // repeat the same process
delay(1);
prsnt_word=P3;
P3=0xf0;
prsnt_word = (prsnt_word | P3);
if(stored_word==prsnt_word)
{
flag=1; // check if valid key pressed before and after debouncing time
}
check_set(); // check if the set key is pressed
break;
}
}
}