LTlib LaurTec Library  4.0.3
Open Source C Library for Microchip Microcontrollers based on XC8 Compiler
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros
DS1337.c
Go to the documentation of this file.
1 /*******************************************************************************
2 
3 Author : Mauro Laurenti
4 Version : 1.4
5 Created on Date : 02/02/2013
6 Last update : 30/04/2016
7 
8 CopyRight 2006-2016 all rights are reserved
9 
10 ********************************************************
11 SOFTWARE LICENSE AGREEMENT
12 ********************************************************
13 
14 The usage of the supplied software imply the acceptance of the following license.
15 
16 The software supplied herewith by Mauro Laurenti (the Author) is intended for
17 use solely and exclusively on Microchip PIC Microcontroller (registered mark).
18 The software is owned by the Author, and is protected under applicable
19 copyright laws. All rights are reserved.
20 Any use in violation of the foregoing restrictions may subject the
21 user to criminal sanctions under applicable laws, as well as to civil liability
22 for the breach of the terms and conditions of this license.
23 Commercial use is forbidden without a written acknowledgement with the Author.
24 Personal or educational use is allowed if the application containing the
25 following software doesn't aim to commercial use or monetary earning of any kind.
26 
27 THIS SOFTWARE IS PROVIDED IN AN "AS IS" CONDITION. NO WARRANTIES,
28 WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING, BUT NOT LIMITED
29 TO, IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
30 PARTICULAR PURPOSE APPLY TO THIS SOFTWARE. THE AUTHOR SHALL NOT,
31 IN ANY CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL OR
32 CONSEQUENTIAL DAMAGES, FOR ANY REASON WHATSOEVER.
33 
34 *******************************************************************************/
35 
36 #ifdef __XC8
37  #include <xc.h>
38 #endif
39 
40 #include "DS1337.h"
41 
42 
43 //************************************************************
44 // RTCC_initialize function implementation
45 //************************************************************
46 void RTCC_initialize (unsigned int baud_rate_KHz){
47 
49  DS1337_I2C_baud_rate(baud_rate_KHz);
50 
51  //short delay to make sure that the 32KHz crystal is oscillating
52  delay_ms (500);
53 }
54 
55 
56 //************************************************************
57 // RTCC_set_seconds function implementation
58 //************************************************************
59 signed char RTCC_set_seconds (unsigned char seconds) {
60 
62 
63 }
64 
65 
66 //************************************************************
67 // RTCC_get_seconds function implementation
68 //************************************************************
69 unsigned char RTCC_get_seconds (void) {
70 
71  unsigned char seconds;
72 
74 
75  // I set to 0 the not significant bits
76  seconds = seconds & 0b01111111;
77  return (seconds);
78 }
79 
80 
81 
82 //************************************************************
83 // RTCC_set_minutes function implementation
84 //************************************************************
85 
86 signed char RTCC_set_minutes (unsigned char minutes) {
87 
89 
90 }
91 
92 
93 //************************************************************
94 // RTCC_get_minutes function implementation
95 //************************************************************
96 unsigned char RTCC_get_minutes (void) {
97 
98  unsigned char minutes;
100 
101  // I set to 0 the not significant bits
102  minutes = minutes & 0b01111111;
103  return (minutes);
104 }
105 
106 
107 //************************************************************
108 // RTCC_set_hours function implementation
109 //************************************************************
110 signed char RTCC_set_hours (unsigned char hours) {
111 
113 }
114 
115 
116 //************************************************************
117 // RTCC_get_hours function implementation
118 //************************************************************
119 unsigned char RTCC_get_hours (void){
120 
121  unsigned char hours;
123 
124  // I set to 0 the not significant bits
125  hours = hours & 0b00111111;
126  return (hours);
127 }
128 
129 
130 //************************************************************
131 // RTCC_get_time_seconds function implementation
132 //************************************************************
133 unsigned char* RTCC_get_time_seconds (void) {
134 
135  static unsigned char time[9];
136  unsigned char value;
137 
138  value = RTCC_get_hours();
139 
140  // with +48 I convert the number in ASCII number
141  time[1] = (value & 0b00001111)+48;
142  time[0] = (value >> 4)+48;
143 
144  time[2] = ':';
145 
146  value = RTCC_get_minutes();
147  time[4] = (value & 0b00001111)+48;
148  time[3] = (value >> 4)+48;
149 
150  time[5] = '.';
151 
152  value = RTCC_get_seconds();
153  time[7] = (value & 0b00001111)+48;
154  time[6] = (value >> 4)+48;
155 
156  time[8] = '\0';
157 
158  return (time);
159 
160 }
161 
162 
163 //************************************************************
164 // RTCC_get_time function implementation
165 //************************************************************
166 unsigned char* RTCC_get_time (void) {
167 
168  static unsigned char time[6];
169  unsigned char value;
170 
171  value = RTCC_get_hours();
172 
173  // with +48 I convert the number in ASCII number
174  time[1] = (value & 0b00001111)+48;
175  time[0] = (value >> 4)+48;
176 
177  time[2] = ':';
178 
179  value = RTCC_get_minutes();
180  time[4] = (value & 0b00001111)+48;
181  time[3] = (value >> 4)+48;
182 
183 
184  time[5] = '\0';
185 
186  return (time);
187 
188 }
189 
190 
191 //************************************************************
192 // RTCC_set_days function implementation
193 //************************************************************
194 signed char RTCC_set_days (unsigned char days) {
195 
197 }
198 
199 
200 //************************************************************
201 // RTCC_get_days function implementation
202 //************************************************************
203 unsigned char RTCC_get_days (void) {
204 
205  unsigned char days;
207 
208  // I set to 0 the not significant bits
209  days = days & 0b00111111;
210  return (days);
211 }
212 
213 
214 //************************************************************
215 // RTCC_set_months function implementation
216 //************************************************************
217 signed char RTCC_set_months (unsigned char months) {
218 
220 
221 }
222 
223 
224 //************************************************************
225 // RTCC_get_months function implementation
226 //************************************************************
227 unsigned char RTCC_get_months (void) {
228 
229  unsigned char months;
231 
232  // I set to 0 the not significant bits
233  months = months & 0b00011111;
234  return (months);
235 }
236 
237 
238 //************************************************************
239 // RTCC_set_years function implementation
240 //************************************************************
241 signed char RTCC_set_years (unsigned char years) {
242 
244 }
245 
246 
247 //************************************************************
248 // RTCC_get_years function implementation
249 //************************************************************
250 unsigned char RTCC_get_years (void) {
251 
252  unsigned char years;
254  return (years);
255 }
256 
257 
258 //************************************************************
259 // RTCC_get_date function implementation
260 //************************************************************
261 unsigned char* RTCC_get_date (void) {
262 
263  static unsigned char date[9];
264  unsigned char value;
265 
266  value = RTCC_get_days();
267 
268  // with +48 I convert the number in ASCII number
269  date[1] = (value & 0b00001111)+48;
270  date[0] = (value >> 4)+48;
271 
272  date[2] = '/';
273 
274  value = RTCC_get_months();
275  date[4] = (value & 0b00001111)+48;
276  date[3] = (value >> 4)+48;
277 
278  date[5] = '/';
279 
280 
281  value = RTCC_get_years();
282  date[7] = (value & 0b00001111)+48;
283  date[6] = (value >> 4)+48;
284 
285  date[8] = '\0';
286 
287  return (date);
288 
289 }
290 
291 
292 //************************************************************
293 // RTCC_set_seconds_alarm function implementation
294 //************************************************************
295 signed char RTCC_set_seconds_alarm (unsigned char seconds, unsigned char alarm_enable) {
296 
297  //I activate AE if required
298  seconds = seconds + alarm_enable;
300 
301 }
302 
303 
304 //************************************************************
305 // RTCC_set_minutes_alarm function implementation
306 //************************************************************
307 signed char RTCC_set_minutes_alarm (unsigned char minutes, unsigned char alarm_enable) {
308 
309  //I activate AE if required
310  minutes = minutes + alarm_enable;
311 
313 
314 }
315 
316 
317 //************************************************************
318 // RTCC_set_hours_alarm function implementation
319 //************************************************************
320 signed char RTCC_set_hours_alarm (unsigned char hours, unsigned char alarm_enable) {
321 
322  //I activate AE if required
323  hours = hours + alarm_enable;
325 }
326 
327 
328 //************************************************************
329 // RTCC_set_days_alarm function implementation
330 //************************************************************
331 signed char RTCC_set_days_alarm (unsigned char days, unsigned char alarm_enable) {
332 
333  //I activate AE if required
334  days = days + alarm_enable;
336 
337 }
338 
339 
340 //************************************************************
341 // RTCC_enable_alarm_interrupt function implementation
342 //************************************************************
343 signed char RTCC_enable_alarm_interrupt (void) {
344 
345  //Reset interrutp flags
346  //EEByteWrite (WRITE_ADD,CONTROL_REG_2_ADDR,0x00);
347 
349 
350 }
351 
352 
353 //************************************************************
354 // RTCC_disable_alarm_interrupt function implementation
355 //************************************************************
356 signed char RTCC_disable_alarm_interrupt (void) {
357 
359 
360 }
361 
362 
363 //************************************************************
364 // RTCC_is_alarm_ON function implementation
365 //************************************************************
366 unsigned char RTCC_is_alarm_ON (void) {
367 
368  unsigned char value;
370 
371  // Just A1F bit is controlled
372  if (value & 0x01) {
373 
374  value = value & 0xFE;
375 
376  // I clean AF bit without changing the other bits
378  return (1);
379 
380  } else {
381  return (0);
382  }
383 
384 }
385 
386 
387 //************************************************************
388 // RTCC_increment_minutes function implementation
389 //************************************************************
390 signed char RTCC_increment_minutes (void) {
391 
392  unsigned char minutes;
393  signed char error;
394 
395  // Read the current minutes
396  minutes = RTCC_get_minutes ();
397 
398  // Increment the minutes
399  minutes ++;
400 
401  // Check the minute limits
402 
403  if ((minutes&0x0F) > (unsigned char) 9 ) {
404  minutes &= 0xF0;
405  minutes += 0x10;
406  }
407 
408  if (minutes == (unsigned char) RTCC_MAX_MINUTES) {
409 
410  minutes = 0;
411  }
412 
413  // Update the minutes
414  error = RTCC_set_minutes (minutes);
415 
416  return (error);
417 
418 }
419 
420 
421 //************************************************************
422 // RTCC_increment_hours function implementation
423 //************************************************************
424 
425 signed char RTCC_increment_hours (void) {
426 
427  unsigned char hours;
428  signed char error;
429 
430  // Read the current hours
431  hours = RTCC_get_hours ();
432 
433  // Increment the hours
434  hours ++;
435 
436  // Check the hour limits
437 
438  if ((hours&0x0F) > (unsigned char) 9 ) {
439  hours &= 0xF0;
440  hours += 0x10;
441  }
442 
443  if (hours == (unsigned char) RTCC_MAX_HOURS) {
444 
445  hours = 0;
446  }
447 
448  // Update the hours
449  error = RTCC_set_hours (hours);
450 
451  return (error);
452 
453 }
454 
455 
456 //************************************************************
457 // RTCC_increment_years function implementation
458 //************************************************************
459 
460 signed char RTCC_increment_years (void) {
461 
462  unsigned char years;
463  signed char error;
464 
465  // Read the current years
466  years = RTCC_get_years ();
467 
468  // Increment the years
469  years ++;
470 
471  // Check the year limits
472 
473  if ((years&0x0F) > (unsigned char) 9 ) {
474  years &= 0xF0;
475  years += 0x10;
476  }
477 
478  if (years == (unsigned char) RTCC_MAX_YEARS) {
479 
480  years = 0;
481  }
482 
483  // Update the years
484  error = RTCC_set_years (years);
485 
486  return (error);
487 
488 }
489 
490 
491 //************************************************************
492 // RTCC_increment_months function implementation
493 //************************************************************
494 
495 signed char RTCC_increment_months (void) {
496 
497  unsigned char months;
498  signed char error;
499 
500  // Read the current months
501  months = RTCC_get_months ();
502 
503  // Increment the months
504  months ++;
505 
506  // Check the month limits
507 
508  if ((months&0x0F) > (unsigned char) 9 ) {
509  months &= 0xF0;
510  months += 0x10;
511  }
512 
513  if (months == (unsigned char) RTCC_MAX_MONTHS) {
514 
515  months = 1;
516  }
517 
518  // Update the months
519  error = RTCC_set_months (months);
520 
521  return (error);
522 
523 }
524 
525 //************************************************************
526 // RTCC_increment_days function implementation
527 //************************************************************
528 
529 signed char RTCC_increment_days (void) {
530 
531  unsigned char days;
532  signed char error;
533 
534  // Read the current days
535  days = RTCC_get_days ();
536 
537  // Increment the days
538  days ++;
539 
540  // Check the day limits
541 
542  if ((days&0x0F) > (unsigned char) 9 ) {
543  days &= 0xF0;
544  days += 0x10;
545  }
546 
547  if (days == (unsigned char) RTCC_MAX_DAYS) {
548 
549  days = 1;
550  }
551 
552  // Update the days
553  error = RTCC_set_days (days);
554 
555  return (error);
556 }
557