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
MCP7940.c
Go to the documentation of this file.
1 /*******************************************************************************
2 
3 Author : Mauro Laurenti
4 Version : 1.1
5 Created on Date : 13/03/2015
6 Last update : 16/03/2016
7 
8 CopyRight 2006-2015 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 PURPOSES
36 ********************************************************
37 
38 This library supports the Real Time Clock Calendar MCP7940 or compatible
39 devices. The library is kept compatible with the PCF8563
40 
41 The following functions have been added:
42 
43  set_seconds_alarm_RTCC (seconds associated to the first alarm)
44 
45  The second alarm is not handled but it can be easily implemented.
46 
47 *******************************************************************************/
48 
49 
50 #ifdef __XC8
51  #include <xc.h>
52 #endif
53 
54 #include "MCP7940.h"
55 
56 
57 //************************************************************
58 // RTCC_initialize function implementation
59 //************************************************************
60 void RTCC_initialize (unsigned int baud_rate_KHz){
62  MCP7940_I2C_baud_rate(baud_rate_KHz);
63 
65 }
66 
67 
68 //************************************************************
69 // RTCC_start_oscillator function implementation
70 //************************************************************
72 
73  // A dummy write is required to let the 32KHz oscillator start
74  RTCC_set_seconds (0);
75 
76  //short delay to make sure that the 32KHz crystal is oscillating
77  delay_ms (500);
78 }
79 
80 
81 //************************************************************
82 // RTCC_set_seconds function implementation
83 //************************************************************
84 signed char RTCC_set_seconds (unsigned char seconds) {
85 
86  //This or indirectly enable the internal oscillator buffer
87  seconds |= 0b10000000;
88 
90 }
91 
92 
93 //************************************************************
94 // RTCC_get_seconds function implementation
95 //************************************************************
96 unsigned char RTCC_get_seconds (void) {
97 
98  unsigned char seconds;
99 
101 
102  // I set to 0 the bits which are not significant
103  seconds = seconds & 0b01111111;
104  return (seconds);
105 }
106 
107 
108 
109 //************************************************************
110 // RTCC_set_minutes function implementation
111 //************************************************************
112 
113 signed char RTCC_set_minutes (unsigned char minutes) {
114 
116 
117 }
118 
119 
120 //************************************************************
121 // RTCC_get_minutes function implementation
122 //************************************************************
123 unsigned char RTCC_get_minutes (void) {
124 
125  unsigned char minutes;
127 
128  // I set to 0 the bits which are not significant
129  minutes = minutes & 0b01111111;
130  return (minutes);
131 }
132 
133 
134 //************************************************************
135 // RTCC_set_hours function implementation
136 //************************************************************
137 signed char RTCC_set_hours (unsigned char hours) {
138 
140 }
141 
142 
143 //************************************************************
144 // RTCC_get_hours function implementation
145 //************************************************************
146 unsigned char RTCC_get_hours (void){
147 
148  unsigned char hours;
150 
151  // I set to 0 the bits which are not significant
152  hours = hours & 0b00111111;
153  return (hours);
154 }
155 
156 
157 //************************************************************
158 // RTCC_get_time_seconds function implementation
159 //************************************************************
160 unsigned char* RTCC_get_time_seconds (void) {
161 
162  static unsigned char time[9];
163  unsigned char value;
164 
165  value = RTCC_get_hours();
166 
167  // with +48 I convert the number in ASCII number
168  time[1] = (value & 0b00001111)+48;
169  time[0] = (value >> 4)+48;
170 
171  time[2] = ':';
172 
173  value = RTCC_get_minutes();
174  time[4] = (value & 0b00001111)+48;
175  time[3] = (value >> 4)+48;
176 
177  time[5] = '.';
178 
179  value = RTCC_get_seconds();
180  time[7] = (value & 0b00001111)+48;
181  time[6] = (value >> 4)+48;
182 
183  time[8] = '\0';
184 
185  return (time);
186 
187 }
188 
189 
190 //************************************************************
191 // RTCC_get_time function implementation
192 //************************************************************
193 unsigned char* RTCC_get_time (void) {
194 
195  static unsigned char time[6];
196  unsigned char value;
197 
198  value = RTCC_get_hours();
199 
200  // with +48 I convert the number in ASCII number
201  time[1] = (value & 0b00001111)+48;
202  time[0] = (value >> 4)+48;
203 
204  time[2] = ':';
205 
206  value = RTCC_get_minutes();
207  time[4] = (value & 0b00001111)+48;
208  time[3] = (value >> 4)+48;
209 
210 
211  time[5] = '\0';
212 
213  return (time);
214 
215 }
216 
217 
218 //************************************************************
219 // RTCC_set_days function implementation
220 //************************************************************
221 signed char RTCC_set_days (unsigned char days) {
222 
224 }
225 
226 
227 //************************************************************
228 // RTCC_get_days function implementation
229 //************************************************************
230 unsigned char RTCC_get_days (void) {
231 
232  unsigned char days;
234 
235  // I set to 0 the bits which are not significant
236  days = days & 0b00111111;
237  return (days);
238 }
239 
240 
241 //************************************************************
242 // RTCC_set_months function implementation
243 //************************************************************
244 signed char RTCC_set_months (unsigned char months) {
245 
247 
248 }
249 
250 
251 //************************************************************
252 // RTCC_get_months function implementation
253 //************************************************************
254 unsigned char RTCC_get_months (void) {
255 
256  unsigned char months;
258 
259  // I set to 0 the bits which are not significant
260  months = months & 0b00011111;
261  return (months);
262 }
263 
264 
265 //************************************************************
266 // RTCC_set_years function implementation
267 //************************************************************
268 signed char RTCC_set_years (unsigned char years) {
269 
271 }
272 
273 
274 //************************************************************
275 // RTCC_get_years function implementation
276 //************************************************************
277 unsigned char RTCC_get_years (void) {
278 
279  unsigned char years;
281  return (years);
282 }
283 
284 
285 //************************************************************
286 // RTCC_get_date function implementation
287 //************************************************************
288 unsigned char* RTCC_get_date (void) {
289 
290  static unsigned char date[9];
291  unsigned char value;
292 
293  value = RTCC_get_days();
294 
295  // with +48 I convert the number in ASCII number
296  date[1] = (value & 0b00001111)+48;
297  date[0] = (value >> 4)+48;
298 
299  date[2] = '/';
300 
301  value = RTCC_get_months();
302  date[4] = (value & 0b00001111)+48;
303  date[3] = (value >> 4)+48;
304 
305  date[5] = '/';
306 
307 
308  value = RTCC_get_years();
309  date[7] = (value & 0b00001111)+48;
310  date[6] = (value >> 4)+48;
311 
312  date[8] = '\0';
313 
314  return (date);
315 
316 }
317 
318 
319 //************************************************************
320 // RTCC_set_seconds_alarm function implementation
321 //************************************************************
322 signed char RTCC_set_seconds_alarm (unsigned char seconds, unsigned char alarm_enable) {
323  unsigned char value;
324 
325  if (alarm_enable == RTCC_ENABLE_ON) {
326 
328  value = value | 0b00010000;
330  }
331 
333 
334 }
335 
336 
337 //************************************************************
338 // RTCC_set_minutes_alarm function implementation
339 //************************************************************
340 signed char RTCC_set_minutes_alarm (unsigned char minutes, unsigned char alarm_enable) {
341  unsigned char value;
342 
343  if (alarm_enable == RTCC_ENABLE_ON) {
344 
346  value = value | 0b00010000;
348  }
349 
351 }
352 
353 
354 //************************************************************
355 // RTCC_set_hours_alarm function implementation
356 //************************************************************
357 signed char RTCC_set_hours_alarm (unsigned char hours, unsigned char alarm_enable) {
358 
359  unsigned char value;
360 
361  if (alarm_enable == RTCC_ENABLE_ON) {
362 
364  value = value | 0b00010000;
366  }
367 
369 }
370 
371 
372 //************************************************************
373 // RTCC_set_days_alarm function implementation
374 //************************************************************
375 signed char RTCC_set_days_alarm (unsigned char days, unsigned char alarm_enable) {
376 
377  unsigned char value;
378 
379  if (alarm_enable == RTCC_ENABLE_ON) {
380 
382  value = value | 0b00010000;
384  }
385 
387 
388 }
389 
390 
391 //************************************************************
392 // RTCC_enable_alarm_interrupt function implementation
393 //************************************************************
394 signed char RTCC_enable_alarm_interrupt (void) {
395 
396  unsigned char value;
398  value = value | 0b10000000;
400 
401  return (0);
402 }
403 
404 
405 //************************************************************
406 // RTCC_disable_alarm_interrupt function implementation
407 //************************************************************
408 signed char RTCC_disable_alarm_interrupt (void) {
409 
410  unsigned char value;
411 
413  value = value & 0b01111111;
415 
416  return (0);
417 }
418 
419 
420 //************************************************************
421 // RTCC_is_alarm_ON function implementation
422 //************************************************************
423 unsigned char RTCC_is_alarm_ON (void) {
424 
425  unsigned char value;
427 
428  //value = MCP7940_I2C_read_byte_from_external_device (RTCC_WRITE_ADD, RTCC_ALARM_CONTROL_REG_1_ADDR);
429  // 0b00001000
430 
431  // Just ALM1 and ALM2 bits are controlled
432  //If one alarm is set I reset both flag. Currently only one alarm is suspported
433  if (value & 0b00110000) {
434 
436  //Reset the flags
437  value = value & 0b11110111;
438  // I clean the flag bits without changing the other bits
440 
442  //Reset the flags
443  value = value & 0b11110111;
444  // I clean the flag bits without changing the other bits
446 
447  return (1);
448 
449  } else {
450  return (0);
451  }
452 
453 }
454 
455 
456 //************************************************************
457 // RTCC_increment_minutes function implementation
458 //************************************************************
459 signed char RTCC_increment_minutes (void) {
460 
461  unsigned char minutes;
462  signed char error;
463 
464  // Read the current minutes
465  minutes = RTCC_get_minutes ();
466 
467  // Increment the minutes
468  minutes++;
469 
470  // Check the minute limits
471 
472  if ((minutes&0x0F) > (unsigned char) 9 ) {
473  minutes &= 0xF0;
474  minutes += 0x10;
475  }
476 
477  if (minutes == (unsigned char) RTCC_MAX_MINUTES) {
478 
479  minutes = 0;
480  }
481 
482  // Update the minutes
483  error = RTCC_set_minutes (minutes);
484 
485  return (error);
486 
487 }
488 
489 
490 //************************************************************
491 // RTCC_increment_hours function implementation
492 //************************************************************
493 
494 signed char RTCC_increment_hours (void) {
495 
496  unsigned char hours;
497  signed char error;
498 
499  // Read the current hours
500  hours = RTCC_get_hours ();
501 
502  // Increment the hours
503  hours++;
504 
505  // Check the hour limits
506 
507  if ((hours&0x0F) > (unsigned char) 9 ) {
508  hours &= 0xF0;
509  hours += 0x10;
510  }
511 
512  if (hours == (unsigned char) RTCC_MAX_HOURS) {
513 
514  hours = 0;
515  }
516 
517  // Update the hours
518  error = RTCC_set_hours (hours);
519 
520  return (error);
521 
522 }
523 
524 
525 //************************************************************
526 // RTCC_increment_years function implementation
527 //************************************************************
528 
529 signed char RTCC_increment_years (void) {
530 
531  unsigned char years;
532  signed char error;
533 
534  // Read the current years
535  years = RTCC_get_years ();
536 
537  // Increment the years
538  years++;
539 
540  // Check the year limits
541 
542  if ((years&0x0F) > (unsigned char) 9 ) {
543  years &= 0xF0;
544  years += 0x10;
545  }
546 
547  if (years == (unsigned char) RTCC_MAX_YEARS) {
548 
549  years = 0;
550  }
551 
552  // Update the years
553  error = RTCC_set_years (years);
554 
555  return (error);
556 
557 }
558 
559 
560 //************************************************************
561 // RTCC_increment_months function implementation
562 //************************************************************
563 
564 signed char RTCC_increment_months (void) {
565 
566  unsigned char months;
567  signed char error;
568 
569  // Read the current months
570  months = RTCC_get_months ();
571 
572  // Increment the months
573  months++;
574 
575  // Check the month limits
576 
577  if ((months&0x0F) > (unsigned char) 9 ) {
578  months &= 0xF0;
579  months += 0x10;
580  }
581 
582  if (months == (unsigned char) RTCC_MAX_MONTHS) {
583 
584  months = 1;
585  }
586 
587  // Update the months
588  error = RTCC_set_months (months);
589 
590  return (error);
591 
592 }
593 
594 //************************************************************
595 // RTCC_increment_days function implementation
596 //************************************************************
597 
598 signed char RTCC_increment_days (void) {
599 
600  unsigned char days;
601  signed char error;
602 
603  // Read the current days
604  days = RTCC_get_days ();
605 
606  // Increment the days
607  days++;
608 
609  // Check the day limits
610 
611  if ((days&0x0F) > (unsigned char) 9 ) {
612  days &= 0xF0;
613  days += 0x10;
614  }
615 
616  if (days == (unsigned char) RTCC_MAX_DAYS) {
617 
618  days = 1;
619  }
620 
621  // Update the days
622  error = RTCC_set_days (days);
623 
624  return (error);
625 }
626