PIC18 LaurTec Library  3.3.1
Open Source C Library for PIC18 Microcontrollers based on C18 - XC8 Compilers
 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.1
5 Created on Date : 02/02/2013
6 Last update : 15/12/2014
7 
8 CopyRight 2006-2014 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  #ifndef _PIC18
39  #error The DS1337 Library supports only PIC18 devices
40  #endif
41 #endif
42 
43 #include "DS1337.h"
44 
45 
46 //************************************************************
47 // DS1337_initialize function implementation
48 //************************************************************
49 void DS1337_initialize (unsigned char crystal_frequency_MHz, unsigned int baud_rate_KHz){
50 
51  OpenI2C(MASTER, SLEW_ON);
52 
53  SSPADD = (((crystal_frequency_MHz *1000)/4)/baud_rate_KHz)-1;
54 }
55 
56 
57 //************************************************************
58 // RTCC_set_seconds function implementation
59 //************************************************************
60 signed char RTCC_set_seconds (unsigned char seconds) {
61 
62  return (EEByteWrite (RTCC_WRITE_ADD,RTCC_SECONDS_ADDR,seconds));
63 
64 }
65 
66 
67 //************************************************************
68 // RTCC_get_seconds function implementation
69 //************************************************************
70 unsigned char RTCC_get_seconds (void) {
71 
72  unsigned char seconds;
73 
74  seconds = EERandomRead (RTCC_WRITE_ADD,RTCC_SECONDS_ADDR);
75 
76  // I set to 0 the not significant bits
77  seconds = seconds & 0b01111111;
78  return (seconds);
79 }
80 
81 
82 
83 //************************************************************
84 // RTCC_set_minutes function implementation
85 //************************************************************
86 
87 signed char RTCC_set_minutes (unsigned char minutes) {
88 
89  return (EEByteWrite (RTCC_WRITE_ADD,RTCC_MINUTES_ADDR,minutes));
90 
91 }
92 
93 
94 //************************************************************
95 // RTCC_get_minutes function implementation
96 //************************************************************
97 unsigned char RTCC_get_minutes (void) {
98 
99  unsigned char minutes;
100  minutes = EERandomRead (RTCC_WRITE_ADD,RTCC_MINUTES_ADDR);
101 
102  // I set to 0 the not significant bits
103  minutes = minutes & 0b01111111;
104  return (minutes);
105 }
106 
107 
108 //************************************************************
109 // RTCC_set_hours function implementation
110 //************************************************************
111 signed char RTCC_set_hours (unsigned char hours) {
112 
113  return (EEByteWrite (RTCC_WRITE_ADD,RTCC_HOURS_ADDR,hours));
114 }
115 
116 
117 //************************************************************
118 // RTCC_get_hours function implementation
119 //************************************************************
120 unsigned char RTCC_get_hours (void){
121 
122  unsigned char hours;
123  hours = EERandomRead (RTCC_WRITE_ADD,RTCC_HOURS_ADDR);
124 
125  // I set to 0 the not significant bits
126  hours = hours & 0b00111111;
127  return (hours);
128 }
129 
130 
131 //************************************************************
132 // RTCC_get_time_seconds function implementation
133 //************************************************************
134 unsigned char* RTCC_get_time_seconds (void) {
135 
136  static unsigned char time[9];
137  unsigned char value;
138 
139  value = RTCC_get_hours();
140 
141  // with +48 I convert the number in ASCII number
142  time[1] = (value & 0b00001111)+48;
143  time[0] = (value >> 4)+48;
144 
145  time[2] = ':';
146 
147  value = RTCC_get_minutes();
148  time[4] = (value & 0b00001111)+48;
149  time[3] = (value >> 4)+48;
150 
151  time[5] = '.';
152 
153  value = RTCC_get_seconds();
154  time[7] = (value & 0b00001111)+48;
155  time[6] = (value >> 4)+48;
156 
157  time[8] = '\0';
158 
159  return (time);
160 
161 }
162 
163 
164 //************************************************************
165 // RTCC_get_time function implementation
166 //************************************************************
167 unsigned char* RTCC_get_time (void) {
168 
169  static unsigned char time[6];
170  unsigned char value;
171 
172  value = RTCC_get_hours();
173 
174  // with +48 I convert the number in ASCII number
175  time[1] = (value & 0b00001111)+48;
176  time[0] = (value >> 4)+48;
177 
178  time[2] = ':';
179 
180  value = RTCC_get_minutes();
181  time[4] = (value & 0b00001111)+48;
182  time[3] = (value >> 4)+48;
183 
184 
185  time[5] = '\0';
186 
187  return (time);
188 
189 }
190 
191 
192 //************************************************************
193 // RTCC_set_days function implementation
194 //************************************************************
195 signed char RTCC_set_days (unsigned char days) {
196 
197  return (EEByteWrite (RTCC_WRITE_ADD,RTCC_DAYS_ADDR,days));
198 }
199 
200 
201 //************************************************************
202 // RTCC_get_days function implementation
203 //************************************************************
204 unsigned char RTCC_get_days (void) {
205 
206  unsigned char days;
207  days = EERandomRead (RTCC_WRITE_ADD,RTCC_DAYS_ADDR);
208 
209  // I set to 0 the not significant bits
210  days = days & 0b00111111;
211  return (days);
212 }
213 
214 
215 //************************************************************
216 // RTCC_set_months function implementation
217 //************************************************************
218 signed char RTCC_set_months (unsigned char months) {
219 
220  return (EEByteWrite (RTCC_WRITE_ADD,RTCC_MONTHS_ADDR,months));
221 
222 }
223 
224 
225 //************************************************************
226 // RTCC_get_months function implementation
227 //************************************************************
228 unsigned char RTCC_get_months (void) {
229 
230  unsigned char months;
231  months = EERandomRead (RTCC_WRITE_ADD,RTCC_MONTHS_ADDR);
232 
233  // I set to 0 the not significant bits
234  months = months & 0b00011111;
235  return (months);
236 }
237 
238 
239 //************************************************************
240 // RTCC_set_years function implementation
241 //************************************************************
242 signed char RTCC_set_years (unsigned char years) {
243 
244  return (EEByteWrite (RTCC_WRITE_ADD,RTCC_YEARS_ADDR,years));
245 }
246 
247 
248 //************************************************************
249 // RTCC_get_years function implementation
250 //************************************************************
251 unsigned char RTCC_get_years (void) {
252 
253  unsigned char years;
254  years = EERandomRead (RTCC_WRITE_ADD,RTCC_YEARS_ADDR);
255  return (years);
256 }
257 
258 
259 //************************************************************
260 // RTCC_get_date function implementation
261 //************************************************************
262 unsigned char* RTCC_get_date (void) {
263 
264  static unsigned char date[9];
265  unsigned char value;
266 
267  value = RTCC_get_days();
268 
269  // with +48 I convert the number in ASCII number
270  date[1] = (value & 0b00001111)+48;
271  date[0] = (value >> 4)+48;
272 
273  date[2] = '/';
274 
275  value = RTCC_get_months();
276  date[4] = (value & 0b00001111)+48;
277  date[3] = (value >> 4)+48;
278 
279  date[5] = '/';
280 
281 
282  value = RTCC_get_years();
283  date[7] = (value & 0b00001111)+48;
284  date[6] = (value >> 4)+48;
285 
286  date[8] = '\0';
287 
288  return (date);
289 
290 }
291 
292 
293 //************************************************************
294 // RTCC_set_seconds_alarm function implementation
295 //************************************************************
296 signed char RTCC_set_seconds_alarm (unsigned char seconds, unsigned char alarm_enable) {
297 
298  //I activate AE if required
299  seconds = seconds + alarm_enable;
300  return (EEByteWrite (RTCC_WRITE_ADD,RTCC_SECONDS_ALARM_ADDR,seconds));
301 
302 }
303 
304 
305 //************************************************************
306 // RTCC_set_minutes_alarm function implementation
307 //************************************************************
308 signed char RTCC_set_minutes_alarm (unsigned char minutes, unsigned char alarm_enable) {
309 
310  //I activate AE if required
311  minutes = minutes + alarm_enable;
312 
313  return (EEByteWrite (RTCC_WRITE_ADD,RTCC_MINUTS_ALARM_ADDR,minutes));
314 
315 }
316 
317 
318 //************************************************************
319 // RTCC_set_hours_alarm function implementation
320 //************************************************************
321 signed char RTCC_set_hours_alarm (unsigned char hours, unsigned char alarm_enable) {
322 
323  //I activate AE if required
324  hours = hours + alarm_enable;
325  return (EEByteWrite (RTCC_WRITE_ADD,RTCC_HOURS_ALARM_ADDR,hours));
326 }
327 
328 
329 //************************************************************
330 // RTCC_set_days_alarm function implementation
331 //************************************************************
332 signed char RTCC_set_days_alarm (unsigned char days, unsigned char alarm_enable) {
333 
334  //I activate AE if required
335  days = days + alarm_enable;
336  return (EEByteWrite (RTCC_WRITE_ADD,RTCC_DAYS_ALARM_ADDR,days));
337 
338 }
339 
340 
341 //************************************************************
342 // RTCC_enable_alarm_interrupt function implementation
343 //************************************************************
344 signed char RTCC_enable_alarm_interrupt (void) {
345 
346  //Reset interrutp flags
347  //EEByteWrite (WRITE_ADD,CONTROL_REG_2_ADDR,0x00);
348 
349  return (EEByteWrite (RTCC_WRITE_ADD,RTCC_CONTROL_REG_1_ADDR,0x01));
350 
351 }
352 
353 
354 //************************************************************
355 // RTCC_disable_alarm_interrupt function implementation
356 //************************************************************
357 signed char RTCC_disable_alarm_interrupt (void) {
358 
359  return (EEByteWrite (RTCC_WRITE_ADD,RTCC_CONTROL_REG_1_ADDR,0x00));
360 
361 }
362 
363 
364 //************************************************************
365 // RTCC_is_alarm_ON function implementation
366 //************************************************************
367 unsigned char RTCC_is_alarm_ON (void) {
368 
369  unsigned char value;
370  value = EERandomRead (RTCC_WRITE_ADD,RTCC_CONTROL_REG_2_ADDR);
371 
372  // Just A1F bit is controlled
373  if (value & 0x01) {
374 
375  value = value & 0xFE;
376 
377  // I clean AF bit without canging the other bits
378  EEByteWrite (RTCC_WRITE_ADD,RTCC_CONTROL_REG_2_ADDR,value);
379  return (1);
380 
381  } else {
382  return (0);
383  }
384 
385 }
386 
387 
388 //************************************************************
389 // RTCC_increment_minutes function implementation
390 //************************************************************
391 signed char RTCC_increment_minutes (void) {
392 
393  unsigned char minutes;
394  signed char error;
395 
396  // Read the current minutes
397  minutes = RTCC_get_minutes ();
398 
399  // Increment the minutes
400  minutes ++;
401 
402  // Check the minute limits
403 
404  if ((minutes&0x0F) > (unsigned char) 9 ) {
405  minutes &= 0xF0;
406  minutes += 0x10;
407  }
408 
409  if (minutes == (unsigned char) RTCC_MAX_MINUTES) {
410 
411  minutes = 0;
412  }
413 
414  // Update the minutes
415  error = RTCC_set_minutes (minutes);
416 
417  return (error);
418 
419 }
420 
421 
422 //************************************************************
423 // RTCC_increment_hours function implementation
424 //************************************************************
425 
426 signed char RTCC_increment_hours (void) {
427 
428  unsigned char hours;
429  signed char error;
430 
431  // Read the current hours
432  hours = RTCC_get_hours ();
433 
434  // Increment the hours
435  hours ++;
436 
437  // Check the hour limits
438 
439  if ((hours&0x0F) > (unsigned char) 9 ) {
440  hours &= 0xF0;
441  hours += 0x10;
442  }
443 
444  if (hours == (unsigned char) RTCC_MAX_HOURS) {
445 
446  hours = 0;
447  }
448 
449  // Update the hours
450  error = RTCC_set_hours (hours);
451 
452  return (error);
453 
454 }
455 
456 
457 //************************************************************
458 // RTCC_increment_years function implementation
459 //************************************************************
460 
461 signed char RTCC_increment_years (void) {
462 
463  unsigned char years;
464  signed char error;
465 
466  // Read the current years
467  years = RTCC_get_years ();
468 
469  // Increment the years
470  years ++;
471 
472  // Check the year limits
473 
474  if ((years&0x0F) > (unsigned char) 9 ) {
475  years &= 0xF0;
476  years += 0x10;
477  }
478 
479  if (years == (unsigned char) RTCC_MAX_YEARS) {
480 
481  years = 0;
482  }
483 
484  // Update the years
485  error = RTCC_set_years (years);
486 
487  return (error);
488 
489 }
490 
491 
492 //************************************************************
493 // RTCC_increment_months function implementation
494 //************************************************************
495 
496 signed char RTCC_increment_months (void) {
497 
498  unsigned char months;
499  signed char error;
500 
501  // Read the current months
502  months = RTCC_get_months ();
503 
504  // Increment the months
505  months ++;
506 
507  // Check the month limits
508 
509  if ((months&0x0F) > (unsigned char) 9 ) {
510  months &= 0xF0;
511  months += 0x10;
512  }
513 
514  if (months == (unsigned char) RTCC_MAX_MONTHS) {
515 
516  months = 1;
517  }
518 
519  // Update the months
520  error = RTCC_set_months (months);
521 
522  return (error);
523 
524 }
525 
526 //************************************************************
527 // RTCC_increment_days function implementation
528 //************************************************************
529 
530 signed char RTCC_increment_days (void) {
531 
532  unsigned char days;
533  signed char error;
534 
535  // Read the current days
536  days = RTCC_get_days ();
537 
538  // Increment the days
539  days ++;
540 
541  // Check the day limits
542 
543  if ((days&0x0F) > (unsigned char) 9 ) {
544  days &= 0xF0;
545  days += 0x10;
546  }
547 
548  if (days == (unsigned char) RTCC_MAX_DAYS) {
549 
550  days = 1;
551  }
552 
553  // Update the days
554  error = RTCC_set_days (days);
555 
556  return (error);
557 }
558