PIC18 LaurTec Library  3.2.0
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.1
5 Created on Date : 4/9/2006
6 Last update : 25/01/2013
7 
8 CopyRight 2006-2013 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 // initialize_PCF8563 function implementation
48 //************************************************************
49 void initialize_PCF8563 (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 // set_seconds_RTCC function implementation
59 //************************************************************
60 signed char set_seconds_RTCC (unsigned char seconds) {
61 
62  return (EEByteWrite (WRITE_ADD,SECONDS_ADDR,seconds));
63 
64 }
65 
66 
67 //************************************************************
68 // get_seconds_RTCC function implementation
69 //************************************************************
70 unsigned char get_seconds_RTCC (void) {
71 
72  unsigned char seconds;
73 
74  seconds = EERandomRead (WRITE_ADD,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 // set_minutes_RTCC function implementation
85 //************************************************************
86 
87 signed char set_minutes_RTCC (unsigned char minutes) {
88 
89  return (EEByteWrite (WRITE_ADD,MINUTES_ADDR,minutes));
90 
91 }
92 
93 
94 //************************************************************
95 // get_minutes_RTCC function implementation
96 //************************************************************
97 unsigned char get_minutes_RTCC (void) {
98 
99  unsigned char minutes;
100  minutes = EERandomRead (WRITE_ADD,MINUTES_ADDR);
101 
102  // I set to 0 the not significant bits
103  minutes = minutes & 0b01111111;
104  return (minutes);
105 }
106 
107 
108 //************************************************************
109 // set_hours_RTCC function implementation
110 //************************************************************
111 signed char set_hours_RTCC (unsigned char hours) {
112 
113  return (EEByteWrite (WRITE_ADD,HOURS_ADDR,hours));
114 }
115 
116 
117 //************************************************************
118 // get_hours_RTCC function implementation
119 //************************************************************
120 unsigned char get_hours_RTCC (void)
121 
122 { unsigned char hours;
123  hours = EERandomRead (WRITE_ADD,HOURS_ADDR);
124 
125  // I set to 0 the not significant bits
126  hours = hours & 0b00111111;
127  return (hours);
128 }
129 
130 
131 //************************************************************
132 // get_time_seconds_RTCC function implementation
133 //************************************************************
134 unsigned char* get_time_seconds_RTCC (void) {
135 
136  static unsigned char time[9];
137  unsigned char value;
138 
139  value = get_hours_RTCC();
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 = get_minutes_RTCC();
148  time[4] = (value & 0b00001111)+48;
149  time[3] = (value >> 4)+48;
150 
151  time[5] = '.';
152 
153  value = get_seconds_RTCC();
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 // get_time_RTCC function implementation
166 //************************************************************
167 unsigned char* get_time_RTCC (void) {
168 
169  static unsigned char time[6];
170  unsigned char value;
171 
172  value = get_hours_RTCC();
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 = get_minutes_RTCC();
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 // set_days_RTCC function implementation
194 //************************************************************
195 signed char set_days_RTCC (unsigned char days) {
196 
197  return (EEByteWrite (WRITE_ADD,DAYS_ADDR,days));
198 }
199 
200 
201 //************************************************************
202 // get_days_RTCC function implementation
203 //************************************************************
204 unsigned char get_days_RTCC (void) {
205 
206  unsigned char days;
207  days = EERandomRead (WRITE_ADD,DAYS_ADDR);
208 
209  // I set to 0 the not significant bits
210  days = days & 0b00111111;
211  return (days);
212 }
213 
214 
215 //************************************************************
216 // set_day_of_the_week_RTCC function implementation
217 //************************************************************
218 signed char set_day_of_the_week_RTCC (unsigned char day_of_the_week) {
219 
220  return (EEByteWrite (WRITE_ADD,DAY_WEEK_ADDR,day_of_the_week));
221 }
222 
223 
224 //************************************************************
225 // get_week_days_RTCC function implementation
226 //************************************************************
227 unsigned char get_week_days_RTCC (void){
228 
229  unsigned char day_of_the_week;
230  day_of_the_week = EERandomRead (WRITE_ADD,DAY_WEEK_ADDR);
231 
232  // I set to 0 the not significant bits
233  day_of_the_week = day_of_the_week & 0b00000111;
234  return (day_of_the_week);
235 }
236 
237 
238 //************************************************************
239 // set_months_RTCC function implementation
240 //************************************************************
241 signed char set_months_RTCC (unsigned char months) {
242 
243  return (EEByteWrite (WRITE_ADD,MONTHS_ADDR,months));
244 
245 }
246 
247 
248 //************************************************************
249 // get_months_RTCC function implementation
250 //************************************************************
251 unsigned char get_months_RTCC (void) {
252 
253  unsigned char months;
254  months = EERandomRead (WRITE_ADD,MONTHS_ADDR);
255 
256  // I set to 0 the not significant bits
257  months = months & 0b00011111;
258  return (months);
259 }
260 
261 
262 //************************************************************
263 // set_years_RTCC function implementation
264 //************************************************************
265 signed char set_years_RTCC (unsigned char years) {
266 
267  return (EEByteWrite (WRITE_ADD,YEARS_ADDR,years));
268 }
269 
270 
271 //************************************************************
272 // get_years_RTCC function implementation
273 //************************************************************
274 unsigned char get_years_RTCC (void) {
275 
276  unsigned char years;
277  years = EERandomRead (WRITE_ADD,YEARS_ADDR);
278  return (years);
279 }
280 
281 
282 //************************************************************
283 // get_date_RTCC function implementation
284 //************************************************************
285 unsigned char* get_date_RTCC (void) {
286 
287  static unsigned char date[9];
288  unsigned char value;
289 
290  value = get_days_RTCC();
291 
292  // with +48 I convert the number in ASCII number
293  date[1] = (value & 0b00001111)+48;
294  date[0] = (value >> 4)+48;
295 
296  date[2] = '/';
297 
298  value = get_months_RTCC();
299  date[4] = (value & 0b00001111)+48;
300  date[3] = (value >> 4)+48;
301 
302  date[5] = '/';
303 
304 
305  value = get_years_RTCC();
306  date[7] = (value & 0b00001111)+48;
307  date[6] = (value >> 4)+48;
308 
309  date[8] = '\0';
310 
311  return (date);
312 
313 }
314 
315 
316 //************************************************************
317 // set_minutes_alarm_RTCC function implementation
318 //************************************************************
319 signed char set_minutes_alarm_RTCC (unsigned char minutes, unsigned char alarm_enable) {
320 
321  //I activate AE if required
322  minutes = minutes + alarm_enable;
323 
324  return (EEByteWrite (WRITE_ADD,MINUTS_ALARM_ADDR,minutes));
325 
326 }
327 
328 
329 //************************************************************
330 // set_hours_alarm_RTCC function implementation
331 //************************************************************
332 signed char set_hours_alarm_RTCC (unsigned char hours, unsigned char alarm_enable) {
333 
334  //I activate AE if required
335  hours = hours + alarm_enable;
336  return (EEByteWrite (WRITE_ADD,HOURS_ALARM_ADDR,hours));
337 }
338 
339 
340 //************************************************************
341 // set_days_alarm_RTCC function implementation
342 //************************************************************
343 signed char set_days_alarm_RTCC (unsigned char days, unsigned char alarm_enable) {
344 
345  //I activate AE if required
346  days = days + alarm_enable;
347  return (EEByteWrite (WRITE_ADD,DAYS_ALARM_ADDR,days));
348 
349 }
350 
351 
352 //************************************************************
353 // set_day_of_the_week_alarm_RTCC function implementation
354 //************************************************************
355 signed char set_day_of_the_week_alarm_RTCC (unsigned char day_of_the_week_alarm, unsigned char alarm_enable) {
356 
357  //I activate AE if required
358  day_of_the_week_alarm = day_of_the_week_alarm + alarm_enable;
359  return (EEByteWrite (WRITE_ADD,DAY_WEEK_ALARM_ADDR,day_of_the_week_alarm));
360 
361 }
362 
363 
364 //************************************************************
365 // enable_alarm_interrupt_RTCC function implementation
366 //************************************************************
367 signed char enable_alarm_interrupt_RTCC (void) {
368 
369  return (EEByteWrite (WRITE_ADD,CONTROL_REG_2_ADDR,0x02));
370 
371 }
372 
373 
374 //************************************************************
375 // disable_alarm_interrupt_RTCC function implementation
376 //************************************************************
377 signed char disable_alarm_interrupt_RTCC (void) {
378 
379  return (EEByteWrite (WRITE_ADD,CONTROL_REG_2_ADDR,0x00));
380 
381 }
382 
383 
384 //************************************************************
385 // is_alarm_ON_RTCC function implementation
386 //************************************************************
387 unsigned char is_alarm_ON_RTCC (void) {
388 
389  unsigned char value;
390  value = EERandomRead (WRITE_ADD,CONTROL_REG_2_ADDR);
391 
392  // Just AF bit is controlled
393  if (value & 0x08) {
394 
395  value = value & 0xF7;
396 
397  // I clean AF bit without canging the other bits
398  EEByteWrite (WRITE_ADD,CONTROL_REG_2_ADDR,value);
399  return (1);
400 
401  } else {
402  return (0);
403  }
404 
405 }
406 
407 
408 //************************************************************
409 // increment_minutes_RTCC function implementation
410 //************************************************************
411 signed char increment_minutes_RTCC (void) {
412 
413  unsigned char minutes;
414  signed char error;
415 
416  // Read the current minutes
417  minutes = get_minutes_RTCC ();
418 
419  // Increment the minutes
420  minutes ++;
421 
422  // Check the minute limits
423 
424  if ((minutes&0x0F) > (unsigned char) 9 ) {
425  minutes &= 0xF0;
426  minutes += 0x10;
427  }
428 
429  if (minutes == (unsigned char) MAX_MINUTES) {
430 
431  minutes = 0;
432  }
433 
434  // Update the minutes
435  error = set_minutes_RTCC (minutes);
436 
437  return (error);
438 
439 }
440 
441 
442 //************************************************************
443 // increment_hours_RTCC function implementation
444 //************************************************************
445 
446 signed char increment_hours_RTCC (void) {
447 
448  unsigned char hours;
449  signed char error;
450 
451  // Read the current hours
452  hours = get_hours_RTCC ();
453 
454  // Increment the hours
455  hours ++;
456 
457  // Check the hour limits
458 
459  if ((hours&0x0F) > (unsigned char) 9 ) {
460  hours &= 0xF0;
461  hours += 0x10;
462  }
463 
464  if (hours == (unsigned char) MAX_HOURS) {
465 
466  hours = 0;
467  }
468 
469  // Update the hours
470  error = set_hours_RTCC (hours);
471 
472  return (error);
473 
474 }
475 
476 
477 //************************************************************
478 // increment_years_RTCC function implementation
479 //************************************************************
480 
481 signed char increment_years_RTCC (void) {
482 
483  unsigned char years;
484  signed char error;
485 
486  // Read the current years
487  years = get_years_RTCC ();
488 
489  // Increment the years
490  years ++;
491 
492  // Check the year limits
493 
494  if ((years&0x0F) > (unsigned char) 9 ) {
495  years &= 0xF0;
496  years += 0x10;
497  }
498 
499  if (years == (unsigned char) MAX_YEARS) {
500 
501  years = 0;
502  }
503 
504  // Update the years
505  error = set_years_RTCC (years);
506 
507  return (error);
508 
509 }
510 
511 
512 //************************************************************
513 // increment_months_RTCC function implementation
514 //************************************************************
515 
516 signed char increment_months_RTCC (void) {
517 
518  unsigned char months;
519  signed char error;
520 
521  // Read the current months
522  months = get_months_RTCC ();
523 
524  // Increment the months
525  months ++;
526 
527  // Check the month limits
528 
529  if ((months&0x0F) > (unsigned char) 9 ) {
530  months &= 0xF0;
531  months += 0x10;
532  }
533 
534  if (months == (unsigned char) MAX_MONTHS) {
535 
536  months = 1;
537  }
538 
539  // Update the months
540  error = set_months_RTCC (months);
541 
542  return (error);
543 
544 }
545 
546 //************************************************************
547 // increment_days_RTCC function implementation
548 //************************************************************
549 
550 signed char increment_days_RTCC (void) {
551 
552  unsigned char days;
553  signed char error;
554 
555  // Read the current days
556  days = get_days_RTCC ();
557 
558  // Increment the days
559  days ++;
560 
561  // Check the day limits
562 
563  if ((days&0x0F) > (unsigned char) 9 ) {
564  days &= 0xF0;
565  days += 0x10;
566  }
567 
568  if (days == (unsigned char) MAX_DAYS) {
569 
570  days = 1;
571  }
572 
573  // Update the days
574  error = set_days_RTCC (days);
575 
576  return (error);
577 
578 }
579