PIC18 LaurTec Library  3.1
Open Source C Library for PIC18 Microcontrollers based on C18 - XC8 Compilers
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros
DS1337.c
Go to the documentation of this file.
1 /*******************************************************************************
2 
3 Author : Mauro Laurenti
4 Version : 1.0
5 Created on Date : 02/02/2013
6 Last update : 02/02/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 "DS1337.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_months_RTCC function implementation
204 //************************************************************
205 signed char set_months_RTCC (unsigned char months) {
206 
207  return (EEByteWrite (WRITE_ADD,MONTHS_ADDR,months));
208 
209 }
210 
211 
212 //************************************************************
213 // get_months_RTCC function implementation
214 //************************************************************
215 unsigned char get_months_RTCC (void) {
216 
217  unsigned char months;
218  months = EERandomRead (WRITE_ADD,MONTHS_ADDR);
219 
220  // I set to 0 the not significant bits
221  months = months & 0b00011111;
222  return (months);
223 }
224 
225 
226 //************************************************************
227 // set_years_RTCC function implementation
228 //************************************************************
229 signed char set_years_RTCC (unsigned char years) {
230 
231  return (EEByteWrite (WRITE_ADD,YEARS_ADDR,years));
232 }
233 
234 
235 //************************************************************
236 // get_years_RTCC function implementation
237 //************************************************************
238 unsigned char get_years_RTCC (void) {
239 
240  unsigned char years;
241  years = EERandomRead (WRITE_ADD,YEARS_ADDR);
242  return (years);
243 }
244 
245 
246 //************************************************************
247 // get_date_RTCC function implementation
248 //************************************************************
249 unsigned char* get_date_RTCC (void) {
250 
251  static unsigned char date[9];
252  unsigned char value;
253 
254  value = get_days_RTCC();
255 
256  // with +48 I convert the number in ASCII number
257  date[1] = (value & 0b00001111)+48;
258  date[0] = (value >> 4)+48;
259 
260  date[2] = '/';
261 
262  value = get_months_RTCC();
263  date[4] = (value & 0b00001111)+48;
264  date[3] = (value >> 4)+48;
265 
266  date[5] = '/';
267 
268 
269  value = get_years_RTCC();
270  date[7] = (value & 0b00001111)+48;
271  date[6] = (value >> 4)+48;
272 
273  date[8] = '\0';
274 
275  return (date);
276 
277 }
278 
279 
280 //************************************************************
281 // set_seconds_alarm_RTCC function implementation
282 //************************************************************
283 signed char set_seconds_alarm_RTCC (unsigned char seconds, unsigned char alarm_enable) {
284 
285  //I activate AE if required
286  seconds = seconds + alarm_enable;
287  return (EEByteWrite (WRITE_ADD,SECONDS_ALARM_ADDR,seconds));
288 
289 }
290 
291 
292 //************************************************************
293 // set_minutes_alarm_RTCC function implementation
294 //************************************************************
295 signed char set_minutes_alarm_RTCC (unsigned char minutes, unsigned char alarm_enable) {
296 
297  //I activate AE if required
298  minutes = minutes + alarm_enable;
299 
300  return (EEByteWrite (WRITE_ADD,MINUTS_ALARM_ADDR,minutes));
301 
302 }
303 
304 
305 //************************************************************
306 // set_hours_alarm_RTCC function implementation
307 //************************************************************
308 signed char set_hours_alarm_RTCC (unsigned char hours, unsigned char alarm_enable) {
309 
310  //I activate AE if required
311  hours = hours + alarm_enable;
312  return (EEByteWrite (WRITE_ADD,HOURS_ALARM_ADDR,hours));
313 }
314 
315 
316 //************************************************************
317 // set_days_alarm_RTCC function implementation
318 //************************************************************
319 signed char set_days_alarm_RTCC (unsigned char days, unsigned char alarm_enable) {
320 
321  //I activate AE if required
322  days = days + alarm_enable;
323  return (EEByteWrite (WRITE_ADD,DAYS_ALARM_ADDR,days));
324 
325 }
326 
327 
328 //************************************************************
329 // enable_alarm_interrupt_RTCC function implementation
330 //************************************************************
331 signed char enable_alarm_interrupt_RTCC (void) {
332 
333  return (EEByteWrite (WRITE_ADD,CONTROL_REG_1_ADDR,0x01));
334 
335 }
336 
337 
338 //************************************************************
339 // disable_alarm_interrupt_RTCC function implementation
340 //************************************************************
341 signed char disable_alarm_interrupt_RTCC (void) {
342 
343  return (EEByteWrite (WRITE_ADD,CONTROL_REG_1_ADDR,0x00));
344 
345 }
346 
347 
348 //************************************************************
349 // is_alarm_ON_RTCC function implementation
350 //************************************************************
351 unsigned char is_alarm_ON_RTCC (void) {
352 
353  unsigned char value;
354  value = EERandomRead (WRITE_ADD,CONTROL_REG_2_ADDR);
355 
356  // Just A1F bit is controlled
357  if (value & 0x01) {
358 
359  value = value & 0xFE;
360 
361  // I clean AF bit without canging the other bits
362  EEByteWrite (WRITE_ADD,CONTROL_REG_2_ADDR,value);
363  return (1);
364 
365  } else {
366  return (0);
367  }
368 
369 }
370 
371 
372 //************************************************************
373 // increment_minutes_RTCC function implementation
374 //************************************************************
375 signed char increment_minutes_RTCC (void) {
376 
377  unsigned char minutes;
378  signed char error;
379 
380  // Read the current minutes
381  minutes = get_minutes_RTCC ();
382 
383  // Increment the minutes
384  minutes ++;
385 
386  // Check the minute limits
387 
388  if ((minutes&0x0F) > (unsigned char) 9 ) {
389  minutes &= 0xF0;
390  minutes += 0x10;
391  }
392 
393  if (minutes == (unsigned char) MAX_MINUTES) {
394 
395  minutes = 0;
396  }
397 
398  // Update the minutes
399  error = set_minutes_RTCC (minutes);
400 
401  return (error);
402 
403 }
404 
405 
406 //************************************************************
407 // increment_hours_RTCC function implementation
408 //************************************************************
409 
410 signed char increment_hours_RTCC (void) {
411 
412  unsigned char hours;
413  signed char error;
414 
415  // Read the current hours
416  hours = get_hours_RTCC ();
417 
418  // Increment the hours
419  hours ++;
420 
421  // Check the hour limits
422 
423  if ((hours&0x0F) > (unsigned char) 9 ) {
424  hours &= 0xF0;
425  hours += 0x10;
426  }
427 
428  if (hours == (unsigned char) MAX_HOURS) {
429 
430  hours = 0;
431  }
432 
433  // Update the hours
434  error = set_hours_RTCC (hours);
435 
436  return (error);
437 
438 }
439 
440 
441 //************************************************************
442 // increment_years_RTCC function implementation
443 //************************************************************
444 
445 signed char increment_years_RTCC (void) {
446 
447  unsigned char years;
448  signed char error;
449 
450  // Read the current years
451  years = get_years_RTCC ();
452 
453  // Increment the years
454  years ++;
455 
456  // Check the year limits
457 
458  if ((years&0x0F) > (unsigned char) 9 ) {
459  years &= 0xF0;
460  years += 0x10;
461  }
462 
463  if (years == (unsigned char) MAX_YEARS) {
464 
465  years = 0;
466  }
467 
468  // Update the years
469  error = set_years_RTCC (years);
470 
471  return (error);
472 
473 }
474 
475 
476 //************************************************************
477 // increment_months_RTCC function implementation
478 //************************************************************
479 
480 signed char increment_months_RTCC (void) {
481 
482  unsigned char months;
483  signed char error;
484 
485  // Read the current months
486  months = get_months_RTCC ();
487 
488  // Increment the months
489  months ++;
490 
491  // Check the month limits
492 
493  if ((months&0x0F) > (unsigned char) 9 ) {
494  months &= 0xF0;
495  months += 0x10;
496  }
497 
498  if (months == (unsigned char) MAX_MONTHS) {
499 
500  months = 1;
501  }
502 
503  // Update the months
504  error = set_months_RTCC (months);
505 
506  return (error);
507 
508 }
509 
510 //************************************************************
511 // increment_days_RTCC function implementation
512 //************************************************************
513 
514 signed char increment_days_RTCC (void) {
515 
516  unsigned char days;
517  signed char error;
518 
519  // Read the current days
520  days = get_days_RTCC ();
521 
522  // Increment the days
523  days ++;
524 
525  // Check the day limits
526 
527  if ((days&0x0F) > (unsigned char) 9 ) {
528  days &= 0xF0;
529  days += 0x10;
530  }
531 
532  if (days == (unsigned char) MAX_DAYS) {
533 
534  days = 1;
535  }
536 
537  // Update the days
538  error = set_days_RTCC (days);
539 
540  return (error);
541 
542 }
543