LTlib LaurTec Library  4.0.1
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.0
5 Created on Date : 13/03/2015
6 Last update : 13/03/2015
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 }
64 
65 
66 //************************************************************
67 // RTCC_set_seconds function implementation
68 //************************************************************
69 signed char RTCC_set_seconds (unsigned char seconds) {
70 
71  seconds |= 0b10000000;
72 
74 }
75 
76 
77 //************************************************************
78 // RTCC_get_seconds function implementation
79 //************************************************************
80 unsigned char RTCC_get_seconds (void) {
81 
82  unsigned char seconds;
83 
85 
86  // I set to 0 the bits which are not significant
87  seconds = seconds & 0b01111111;
88  return (seconds);
89 }
90 
91 
92 
93 //************************************************************
94 // RTCC_set_minutes function implementation
95 //************************************************************
96 
97 signed char RTCC_set_minutes (unsigned char minutes) {
98 
100 
101 }
102 
103 
104 //************************************************************
105 // RTCC_get_minutes function implementation
106 //************************************************************
107 unsigned char RTCC_get_minutes (void) {
108 
109  unsigned char minutes;
111 
112  // I set to 0 the bits which are not significant
113  minutes = minutes & 0b01111111;
114  return (minutes);
115 }
116 
117 
118 //************************************************************
119 // RTCC_set_hours function implementation
120 //************************************************************
121 signed char RTCC_set_hours (unsigned char hours) {
122 
124 }
125 
126 
127 //************************************************************
128 // RTCC_get_hours function implementation
129 //************************************************************
130 unsigned char RTCC_get_hours (void){
131 
132  unsigned char hours;
134 
135  // I set to 0 the bits which are not significant
136  hours = hours & 0b00111111;
137  return (hours);
138 }
139 
140 
141 //************************************************************
142 // RTCC_get_time_seconds function implementation
143 //************************************************************
144 unsigned char* RTCC_get_time_seconds (void) {
145 
146  static unsigned char time[9];
147  unsigned char value;
148 
149  value = RTCC_get_hours();
150 
151  // with +48 I convert the number in ASCII number
152  time[1] = (value & 0b00001111)+48;
153  time[0] = (value >> 4)+48;
154 
155  time[2] = ':';
156 
157  value = RTCC_get_minutes();
158  time[4] = (value & 0b00001111)+48;
159  time[3] = (value >> 4)+48;
160 
161  time[5] = '.';
162 
163  value = RTCC_get_seconds();
164  time[7] = (value & 0b00001111)+48;
165  time[6] = (value >> 4)+48;
166 
167  time[8] = '\0';
168 
169  return (time);
170 
171 }
172 
173 
174 //************************************************************
175 // RTCC_get_time function implementation
176 //************************************************************
177 unsigned char* RTCC_get_time (void) {
178 
179  static unsigned char time[6];
180  unsigned char value;
181 
182  value = RTCC_get_hours();
183 
184  // with +48 I convert the number in ASCII number
185  time[1] = (value & 0b00001111)+48;
186  time[0] = (value >> 4)+48;
187 
188  time[2] = ':';
189 
190  value = RTCC_get_minutes();
191  time[4] = (value & 0b00001111)+48;
192  time[3] = (value >> 4)+48;
193 
194 
195  time[5] = '\0';
196 
197  return (time);
198 
199 }
200 
201 
202 //************************************************************
203 // RTCC_set_days function implementation
204 //************************************************************
205 signed char RTCC_set_days (unsigned char days) {
206 
208 }
209 
210 
211 //************************************************************
212 // RTCC_get_days function implementation
213 //************************************************************
214 unsigned char RTCC_get_days (void) {
215 
216  unsigned char days;
218 
219  // I set to 0 the bits which are not significant
220  days = days & 0b00111111;
221  return (days);
222 }
223 
224 
225 //************************************************************
226 // RTCC_set_months function implementation
227 //************************************************************
228 signed char RTCC_set_months (unsigned char months) {
229 
231 
232 }
233 
234 
235 //************************************************************
236 // RTCC_get_months function implementation
237 //************************************************************
238 unsigned char RTCC_get_months (void) {
239 
240  unsigned char months;
242 
243  // I set to 0 the bits which are not significant
244  months = months & 0b00011111;
245  return (months);
246 }
247 
248 
249 //************************************************************
250 // RTCC_set_years function implementation
251 //************************************************************
252 signed char RTCC_set_years (unsigned char years) {
253 
255 }
256 
257 
258 //************************************************************
259 // RTCC_get_years function implementation
260 //************************************************************
261 unsigned char RTCC_get_years (void) {
262 
263  unsigned char years;
265  return (years);
266 }
267 
268 
269 //************************************************************
270 // RTCC_get_date function implementation
271 //************************************************************
272 unsigned char* RTCC_get_date (void) {
273 
274  static unsigned char date[9];
275  unsigned char value;
276 
277  value = RTCC_get_days();
278 
279  // with +48 I convert the number in ASCII number
280  date[1] = (value & 0b00001111)+48;
281  date[0] = (value >> 4)+48;
282 
283  date[2] = '/';
284 
285  value = RTCC_get_months();
286  date[4] = (value & 0b00001111)+48;
287  date[3] = (value >> 4)+48;
288 
289  date[5] = '/';
290 
291 
292  value = RTCC_get_years();
293  date[7] = (value & 0b00001111)+48;
294  date[6] = (value >> 4)+48;
295 
296  date[8] = '\0';
297 
298  return (date);
299 
300 }
301 
302 
303 //************************************************************
304 // RTCC_set_seconds_alarm function implementation
305 //************************************************************
306 signed char RTCC_set_seconds_alarm (unsigned char seconds, unsigned char alarm_enable) {
307  unsigned char value;
308 
309  if (alarm_enable == RTCC_ENABLE_ON) {
310 
312  value = value | 0b00010000;
314  }
315 
317 
318 }
319 
320 
321 //************************************************************
322 // RTCC_set_minutes_alarm function implementation
323 //************************************************************
324 signed char RTCC_set_minutes_alarm (unsigned char minutes, unsigned char alarm_enable) {
325  unsigned char value;
326 
327  if (alarm_enable == RTCC_ENABLE_ON) {
328 
330  value = value | 0b00010000;
332  }
333 
335 }
336 
337 
338 //************************************************************
339 // RTCC_set_hours_alarm function implementation
340 //************************************************************
341 signed char RTCC_set_hours_alarm (unsigned char hours, unsigned char alarm_enable) {
342 
343  unsigned char value;
344 
345  if (alarm_enable == RTCC_ENABLE_ON) {
346 
348  value = value | 0b00010000;
350  }
351 
353 }
354 
355 
356 //************************************************************
357 // RTCC_set_days_alarm function implementation
358 //************************************************************
359 signed char RTCC_set_days_alarm (unsigned char days, unsigned char alarm_enable) {
360 
361  unsigned char value;
362 
363  if (alarm_enable == RTCC_ENABLE_ON) {
364 
366  value = value | 0b00010000;
368  }
369 
371 
372 }
373 
374 
375 //************************************************************
376 // RTCC_enable_alarm_interrupt function implementation
377 //************************************************************
378 signed char RTCC_enable_alarm_interrupt (void) {
379 
380  unsigned char value;
382  value = value | 0b10000000;
384 
385  return (0);
386 }
387 
388 
389 //************************************************************
390 // RTCC_disable_alarm_interrupt function implementation
391 //************************************************************
392 signed char RTCC_disable_alarm_interrupt (void) {
393 
394  unsigned char value;
395 
397  value = value & 0b01111111;
399 
400  return (0);
401 }
402 
403 
404 //************************************************************
405 // RTCC_is_alarm_ON function implementation
406 //************************************************************
407 unsigned char RTCC_is_alarm_ON (void) {
408 
409  //Code Under test
410  //Code removed
411 }
412 
413 
414 //************************************************************
415 // RTCC_increment_minutes function implementation
416 //************************************************************
417 signed char RTCC_increment_minutes (void) {
418 
419  unsigned char minutes;
420  signed char error;
421 
422  // Read the current minutes
423  minutes = RTCC_get_minutes ();
424 
425  // Increment the minutes
426  minutes++;
427 
428  // Check the minute limits
429 
430  if ((minutes&0x0F) > (unsigned char) 9 ) {
431  minutes &= 0xF0;
432  minutes += 0x10;
433  }
434 
435  if (minutes == (unsigned char) RTCC_MAX_MINUTES) {
436 
437  minutes = 0;
438  }
439 
440  // Update the minutes
441  error = RTCC_set_minutes (minutes);
442 
443  return (error);
444 
445 }
446 
447 
448 //************************************************************
449 // RTCC_increment_hours function implementation
450 //************************************************************
451 
452 signed char RTCC_increment_hours (void) {
453 
454  unsigned char hours;
455  signed char error;
456 
457  // Read the current hours
458  hours = RTCC_get_hours ();
459 
460  // Increment the hours
461  hours++;
462 
463  // Check the hour limits
464 
465  if ((hours&0x0F) > (unsigned char) 9 ) {
466  hours &= 0xF0;
467  hours += 0x10;
468  }
469 
470  if (hours == (unsigned char) RTCC_MAX_HOURS) {
471 
472  hours = 0;
473  }
474 
475  // Update the hours
476  error = RTCC_set_hours (hours);
477 
478  return (error);
479 
480 }
481 
482 
483 //************************************************************
484 // RTCC_increment_years function implementation
485 //************************************************************
486 
487 signed char RTCC_increment_years (void) {
488 
489  unsigned char years;
490  signed char error;
491 
492  // Read the current years
493  years = RTCC_get_years ();
494 
495  // Increment the years
496  years++;
497 
498  // Check the year limits
499 
500  if ((years&0x0F) > (unsigned char) 9 ) {
501  years &= 0xF0;
502  years += 0x10;
503  }
504 
505  if (years == (unsigned char) RTCC_MAX_YEARS) {
506 
507  years = 0;
508  }
509 
510  // Update the years
511  error = RTCC_set_years (years);
512 
513  return (error);
514 
515 }
516 
517 
518 //************************************************************
519 // RTCC_increment_months function implementation
520 //************************************************************
521 
522 signed char RTCC_increment_months (void) {
523 
524  unsigned char months;
525  signed char error;
526 
527  // Read the current months
528  months = RTCC_get_months ();
529 
530  // Increment the months
531  months++;
532 
533  // Check the month limits
534 
535  if ((months&0x0F) > (unsigned char) 9 ) {
536  months &= 0xF0;
537  months += 0x10;
538  }
539 
540  if (months == (unsigned char) RTCC_MAX_MONTHS) {
541 
542  months = 1;
543  }
544 
545  // Update the months
546  error = RTCC_set_months (months);
547 
548  return (error);
549 
550 }
551 
552 //************************************************************
553 // RTCC_increment_days function implementation
554 //************************************************************
555 
556 signed char RTCC_increment_days (void) {
557 
558  unsigned char days;
559  signed char error;
560 
561  // Read the current days
562  days = RTCC_get_days ();
563 
564  // Increment the days
565  days++;
566 
567  // Check the day limits
568 
569  if ((days&0x0F) > (unsigned char) 9 ) {
570  days &= 0xF0;
571  days += 0x10;
572  }
573 
574  if (days == (unsigned char) RTCC_MAX_DAYS) {
575 
576  days = 1;
577  }
578 
579  // Update the days
580  error = RTCC_set_days (days);
581 
582  return (error);
583 }
584