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
PCF8563.c
Go to the documentation of this file.
1 /*******************************************************************************
2 
3 Author : Mauro Laurenti
4 Version : 1.2
5 Created on Date : 4/9/2006
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 PCF8563 Library supports only PIC18 devices
40  #endif
41 #endif
42 
43 
44 #include "PCF8563.h"
45 
46 //************************************************************
47 // PCF8563_initialize function implementation
48 //************************************************************
49 void PCF8563_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 signed char RTCC_set_minutes (unsigned char minutes) {
87 
88  return (EEByteWrite (RTCC_WRITE_ADD, RTCC_MINUTES_ADDR,minutes));
89 
90 }
91 
92 
93 //************************************************************
94 // RTCC_get_minutes function implementation
95 //************************************************************
96 unsigned char RTCC_get_minutes (void) {
97 
98  unsigned char minutes;
99  minutes = EERandomRead (RTCC_WRITE_ADD, RTCC_MINUTES_ADDR);
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 
112  return (EEByteWrite (RTCC_WRITE_ADD, RTCC_HOURS_ADDR,hours));
113 }
114 
115 
116 //************************************************************
117 // RTCC_get_hours function implementation
118 //************************************************************
119 unsigned char RTCC_get_hours (void)
120 
121 { unsigned char hours;
122  hours = EERandomRead (RTCC_WRITE_ADD, RTCC_HOURS_ADDR);
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 = get_hours_RTCC();
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 = get_minutes_RTCC();
147  time[4] = (value & 0b00001111)+48;
148  time[3] = (value >> 4)+48;
149 
150  time[5] = '.';
151 
152  value = get_seconds_RTCC();
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 = get_hours_RTCC();
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 = get_minutes_RTCC();
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 // RTCC_set_days function implementation
192 //************************************************************
193 signed char RTCC_set_days (unsigned char days) {
194 
195  return (EEByteWrite (RTCC_WRITE_ADD, RTCC_DAYS_ADDR,days));
196 }
197 
198 
199 //************************************************************
200 // RTCC_get_days function implementation
201 //************************************************************
202 unsigned char RTCC_get_days (void) {
203 
204  unsigned char days;
205  days = EERandomRead (RTCC_WRITE_ADD, RTCC_DAYS_ADDR);
206 
207  // I set to 0 the not significant bits
208  days = days & 0b00111111;
209  return (days);
210 }
211 
212 
213 //************************************************************
214 // RTCC_set_day_of_the_week function implementation
215 //************************************************************
216 signed char RTCC_set_day_of_the_week (unsigned char day_of_the_week) {
217 
218  return (EEByteWrite (RTCC_WRITE_ADD, RTCC_DAY_WEEK_ADDR,day_of_the_week));
219 }
220 
221 
222 //************************************************************
223 // RTCC_get_week_days function implementation
224 //************************************************************
225 unsigned char RTCC_get_week_days (void){
226 
227  unsigned char day_of_the_week;
228  day_of_the_week = EERandomRead (RTCC_WRITE_ADD, RTCC_DAY_WEEK_ADDR);
229 
230  // I set to 0 the not significant bits
231  day_of_the_week = day_of_the_week & 0b00000111;
232  return (day_of_the_week);
233 }
234 
235 
236 //************************************************************
237 // RTCC_set_months function implementation
238 //************************************************************
239 signed char RTCC_set_months (unsigned char months) {
240 
241  return (EEByteWrite (RTCC_WRITE_ADD, RTCC_MONTHS_ADDR,months));
242 
243 }
244 
245 
246 //************************************************************
247 // RTCC_get_months function implementation
248 //************************************************************
249 unsigned char RTCC_get_months (void) {
250 
251  unsigned char months;
252  months = EERandomRead (RTCC_WRITE_ADD, RTCC_MONTHS_ADDR);
253 
254  // I set to 0 the not significant bits
255  months = months & 0b00011111;
256  return (months);
257 }
258 
259 
260 //************************************************************
261 // RTCC_set_years function implementation
262 //************************************************************
263 signed char RTCC_set_years (unsigned char years) {
264 
265  return (EEByteWrite (RTCC_WRITE_ADD, RTCC_YEARS_ADDR,years));
266 }
267 
268 
269 //************************************************************
270 // RTCC_get_years function implementation
271 //************************************************************
272 unsigned char RTCC_get_years (void) {
273 
274  unsigned char years;
275  years = EERandomRead (RTCC_WRITE_ADD, RTCC_YEARS_ADDR);
276  return (years);
277 }
278 
279 
280 //************************************************************
281 // RTCC_get_date function implementation
282 //************************************************************
283 unsigned char* RTCC_get_date (void) {
284 
285  static unsigned char date[9];
286  unsigned char value;
287 
288  value = get_days_RTCC();
289 
290  // with +48 I convert the number in ASCII number
291  date[1] = (value & 0b00001111)+48;
292  date[0] = (value >> 4)+48;
293 
294  date[2] = '/';
295 
296  value = get_months_RTCC();
297  date[4] = (value & 0b00001111)+48;
298  date[3] = (value >> 4)+48;
299 
300  date[5] = '/';
301 
302 
303  value = get_years_RTCC();
304  date[7] = (value & 0b00001111)+48;
305  date[6] = (value >> 4)+48;
306 
307  date[8] = '\0';
308 
309  return (date);
310 
311 }
312 
313 
314 //************************************************************
315 // RTCC_set_minutes_alarm function implementation
316 //************************************************************
317 signed char RTCC_set_minutes_alarm (unsigned char minutes, unsigned char alarm_enable) {
318 
319  //I activate AE if required
320  minutes = minutes + alarm_enable;
321 
322  return (EEByteWrite (RTCC_WRITE_ADD, RTCC_MINUTS_ALARM_ADDR,minutes));
323 
324 }
325 
326 
327 //************************************************************
328 // RTCC_set_hours_alarm function implementation
329 //************************************************************
330 signed char RTCC_set_hours_alarm (unsigned char hours, unsigned char alarm_enable) {
331 
332  //I activate AE if required
333  hours = hours + alarm_enable;
334  return (EEByteWrite (RTCC_WRITE_ADD, RTCC_HOURS_ALARM_ADDR,hours));
335 }
336 
337 
338 //************************************************************
339 // RTCC_set_days_alarm function implementation
340 //************************************************************
341 signed char RTCC_set_days_alarm (unsigned char days, unsigned char alarm_enable) {
342 
343  //I activate AE if required
344  days = days + alarm_enable;
345  return (EEByteWrite (RTCC_WRITE_ADD, RTCC_DAYS_ALARM_ADDR,days));
346 
347 }
348 
349 
350 //************************************************************
351 // RTCC_set_day_of_the_week_alarm function implementation
352 //************************************************************
353 signed char RTCC_set_day_of_the_week_alarm (unsigned char day_of_the_week_alarm, unsigned char alarm_enable) {
354 
355  //I activate AE if required
356  day_of_the_week_alarm = day_of_the_week_alarm + alarm_enable;
357  return (EEByteWrite (RTCC_WRITE_ADD, RTCC_DAY_WEEK_ALARM_ADDR,day_of_the_week_alarm));
358 
359 }
360 
361 
362 //************************************************************
363 // RTCC_enable_alarm_interrupt function implementation
364 //************************************************************
365 signed char RTCC_enable_alarm_interrupt (void) {
366 
367  return (EEByteWrite (RTCC_WRITE_ADD, RTCC_CONTROL_REG_2_ADDR,0x02));
368 
369 }
370 
371 
372 //************************************************************
373 // RTCC_disable_alarm_interrupt function implementation
374 //************************************************************
375 signed char RTCC_disable_alarm_interrupt (void) {
376 
377  return (EEByteWrite (RTCC_WRITE_ADD, RTCC_CONTROL_REG_2_ADDR,0x00));
378 
379 }
380 
381 
382 //************************************************************
383 // RTCC_is_alarm_ON function implementation
384 //************************************************************
385 unsigned char RTCC_is_alarm_ON (void) {
386 
387  unsigned char value;
388  value = EERandomRead (RTCC_WRITE_ADD, RTCC_CONTROL_REG_2_ADDR);
389 
390  // Just AF bit is controlled
391  if (value & 0x08) {
392 
393  value = value & 0xF7;
394 
395  // I clean AF bit without canging the other bits
396  EEByteWrite (RTCC_WRITE_ADD, RTCC_CONTROL_REG_2_ADDR,value);
397  return (1);
398 
399  } else {
400  return (0);
401  }
402 
403 }
404 
405 
406 //************************************************************
407 // RTCC_increment_minutes function implementation
408 //************************************************************
409 signed char RTCC_increment_minutes (void) {
410 
411  unsigned char minutes;
412  signed char error;
413 
414  // Read the current minutes
415  minutes = get_minutes_RTCC ();
416 
417  // Increment the minutes
418  minutes ++;
419 
420  // Check the minute limits
421 
422  if ((minutes&0x0F) > (unsigned char) 9 ) {
423  minutes &= 0xF0;
424  minutes += 0x10;
425  }
426 
427  if (minutes == (unsigned char) RTCC_MAX_MINUTES) {
428 
429  minutes = 0;
430  }
431 
432  // Update the minutes
433  error = set_minutes_RTCC (minutes);
434 
435  return (error);
436 
437 }
438 
439 
440 //************************************************************
441 // RTCC_increment_hours function implementation
442 //************************************************************
443 signed char RTCC_increment_hours (void) {
444 
445  unsigned char hours;
446  signed char error;
447 
448  // Read the current hours
449  hours = get_hours_RTCC ();
450 
451  // Increment the hours
452  hours ++;
453 
454  // Check the hour limits
455 
456  if ((hours&0x0F) > (unsigned char) 9 ) {
457  hours &= 0xF0;
458  hours += 0x10;
459  }
460 
461  if (hours == (unsigned char) RTCC_MAX_HOURS) {
462 
463  hours = 0;
464  }
465 
466  // Update the hours
467  error = set_hours_RTCC (hours);
468 
469  return (error);
470 
471 }
472 
473 
474 //************************************************************
475 // RTCC_increment_years function implementation
476 //************************************************************
477 signed char RTCC_increment_years (void) {
478 
479  unsigned char years;
480  signed char error;
481 
482  // Read the current years
483  years = get_years_RTCC ();
484 
485  // Increment the years
486  years ++;
487 
488  // Check the year limits
489 
490  if ((years&0x0F) > (unsigned char) 9 ) {
491  years &= 0xF0;
492  years += 0x10;
493  }
494 
495  if (years == (unsigned char) RTCC_MAX_YEARS) {
496 
497  years = 0;
498  }
499 
500  // Update the years
501  error = set_years_RTCC (years);
502 
503  return (error);
504 
505 }
506 
507 
508 //************************************************************
509 // RTCC_increment_months function implementation
510 //************************************************************
511 signed char RTCC_increment_months (void) {
512 
513  unsigned char months;
514  signed char error;
515 
516  // Read the current months
517  months = get_months_RTCC ();
518 
519  // Increment the months
520  months ++;
521 
522  // Check the month limits
523 
524  if ((months&0x0F) > (unsigned char) 9 ) {
525  months &= 0xF0;
526  months += 0x10;
527  }
528 
529  if (months == (unsigned char) RTCC_MAX_MONTHS) {
530 
531  months = 1;
532  }
533 
534  // Update the months
535  error = set_months_RTCC (months);
536 
537  return (error);
538 
539 }
540 
541 //************************************************************
542 // RTCC_increment_days function implementation
543 //************************************************************
544 signed char RTCC_increment_days (void) {
545 
546  unsigned char days;
547  signed char error;
548 
549  // Read the current days
550  days = get_days_RTCC ();
551 
552  // Increment the days
553  days ++;
554 
555  // Check the day limits
556 
557  if ((days&0x0F) > (unsigned char) 9 ) {
558  days &= 0xF0;
559  days += 0x10;
560  }
561 
562  if (days == (unsigned char) RTCC_MAX_DAYS) {
563 
564  days = 1;
565  }
566 
567  // Update the days
568  error = set_days_RTCC (days);
569 
570  return (error);
571 }
572