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