C18 LaurTec Library
2.5
Open Source C Library for PIC18 Microcontrollers
|
00001 /*********************************************************************************************** 00002 00003 Author : Mauro Laurenti 00004 Version : 1.0 00005 Date : 4/9/2006 00006 00007 CopyRight 2006 all rights are reserved 00008 00009 00010 ******************************************************** 00011 SOFTWARE LICENSE AGREEMENT 00012 ******************************************************** 00013 00014 The usage of the supplied software imply the acceptance of the following license. 00015 00016 The software supplied herewith by Mauro Laurenti (the Author) 00017 is intended for use solely and exclusively on Microchip PIC Microcontroller (registered mark). 00018 The software is owned by the Author, and is protected under applicable copyright laws. 00019 All rights are reserved. 00020 Any use in violation of the foregoing restrictions may subject the 00021 user to criminal sanctions under applicable laws (Italian or International ones), as well as to 00022 civil liability for the breach of the terms and conditions of this license. 00023 Commercial use is forbidden without a written acknowledgment with the Author. 00024 Personal or educational use is allowed if the application containing the following 00025 software doesn't aim to commercial use or monetary earning of any kind. 00026 00027 THIS SOFTWARE IS PROVIDED IN AN "AS IS" CONDITION. NO WARRANTIES, 00028 WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING, BUT NOT LIMITED 00029 TO, IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A 00030 PARTICULAR PURPOSE APPLY TO THIS SOFTWARE. THE AUTHOR SHALL NOT, 00031 IN ANY CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL OR 00032 CONSEQUENTIAL DAMAGES, FOR ANY REASON WHATSOEVER. 00033 00034 ******************************************************** 00035 PURPOSES 00036 ******************************************************** 00037 00038 This libary is supposed to be used to drive the PCF8563 or compatible 00039 devices 00040 00041 00042 //********************************************************************************************** 00043 00044 */ 00045 00046 #include "PCF8563.h" 00047 00048 00049 00050 //************************************************************ 00051 // set_seconds_RTCC function implementation 00052 //************************************************************ 00053 signed char set_seconds_RTCC (unsigned char seconds) { 00054 00055 return (EEByteWrite (WRITE_ADD,SECONDS_ADDR,seconds)); 00056 00057 } 00058 00059 00060 //************************************************************ 00061 // get_seconds_RTCC function implementation 00062 //************************************************************ 00063 unsigned char get_seconds_RTCC (void) { 00064 00065 unsigned char seconds; 00066 00067 seconds = EERandomRead (WRITE_ADD,SECONDS_ADDR); 00068 00069 // I set to 0 the not significant bits 00070 seconds = seconds & 0b01111111; 00071 return (seconds); 00072 } 00073 00074 00075 00076 //************************************************************ 00077 // set_minutes_RTCC function implementation 00078 //************************************************************ 00079 00080 signed char set_minutes_RTCC (unsigned char minutes) { 00081 00082 return (EEByteWrite (WRITE_ADD,MINUTES_ADDR,minutes)); 00083 00084 } 00085 00086 00087 //************************************************************ 00088 // get_minutes_RTCC function implementation 00089 //************************************************************ 00090 unsigned char get_minutes_RTCC (void) { 00091 00092 unsigned char minutes; 00093 minutes = EERandomRead (WRITE_ADD,MINUTES_ADDR); 00094 00095 // I set to 0 the not significant bits 00096 minutes = minutes & 0b01111111; 00097 return (minutes); 00098 } 00099 00100 00101 //************************************************************ 00102 // set_hours_RTCC function implementation 00103 //************************************************************ 00104 signed char set_hours_RTCC (unsigned char hours) { 00105 00106 return (EEByteWrite (WRITE_ADD,HOURS_ADDR,hours)); 00107 } 00108 00109 00110 //************************************************************ 00111 // get_hours_RTCC function implementation 00112 //************************************************************ 00113 unsigned char get_hours_RTCC (void) 00114 00115 { unsigned char hours; 00116 hours = EERandomRead (WRITE_ADD,HOURS_ADDR); 00117 00118 // I set to 0 the not significant bits 00119 hours = hours & 0b00111111; 00120 return (hours); 00121 } 00122 00123 00124 //************************************************************ 00125 // get_time_seconds_RTCC function implementation 00126 //************************************************************ 00127 unsigned char* get_time_seconds_RTCC (void) { 00128 00129 static unsigned char time[9]; 00130 unsigned char value; 00131 00132 value = get_hours_RTCC(); 00133 00134 // with +48 I convert the number in ASCII number 00135 time[1] = (value & 0b00001111)+48; 00136 time[0] = (value >> 4)+48; 00137 00138 time[2] = ':'; 00139 00140 value = get_minutes_RTCC(); 00141 time[4] = (value & 0b00001111)+48; 00142 time[3] = (value >> 4)+48; 00143 00144 time[5] = '.'; 00145 00146 value = get_seconds_RTCC(); 00147 time[7] = (value & 0b00001111)+48; 00148 time[6] = (value >> 4)+48; 00149 00150 time[8] = '\0'; 00151 00152 return (time); 00153 00154 } 00155 00156 00157 //************************************************************ 00158 // get_time_RTCC function implementation 00159 //************************************************************ 00160 unsigned char* get_time_RTCC (void) { 00161 00162 static unsigned char time[6]; 00163 unsigned char value; 00164 00165 value = get_hours_RTCC(); 00166 00167 // with +48 I convert the number in ASCII number 00168 time[1] = (value & 0b00001111)+48; 00169 time[0] = (value >> 4)+48; 00170 00171 time[2] = ':'; 00172 00173 value = get_minutes_RTCC(); 00174 time[4] = (value & 0b00001111)+48; 00175 time[3] = (value >> 4)+48; 00176 00177 00178 time[5] = '\0'; 00179 00180 return (time); 00181 00182 } 00183 00184 00185 //************************************************************ 00186 // set_days_RTCC function implementation 00187 //************************************************************ 00188 signed char set_days_RTCC (unsigned char days) { 00189 00190 return (EEByteWrite (WRITE_ADD,DAYS_ADDR,days)); 00191 } 00192 00193 00194 //************************************************************ 00195 // get_days_RTCC function implementation 00196 //************************************************************ 00197 unsigned char get_days_RTCC (void) { 00198 00199 unsigned char days; 00200 days = EERandomRead (WRITE_ADD,DAYS_ADDR); 00201 00202 // I set to 0 the not significant bits 00203 days = days & 0b00111111; 00204 return (days); 00205 } 00206 00207 00208 //************************************************************ 00209 // set_day_of_the_week_RTCC function implementation 00210 //************************************************************ 00211 signed char set_day_of_the_week_RTCC (unsigned char day_of_the_week) { 00212 00213 return (EEByteWrite (WRITE_ADD,DAY_WEEK_ADDR,day_of_the_week)); 00214 } 00215 00216 00217 //************************************************************ 00218 // get_week_days_RTCC function implementation 00219 //************************************************************ 00220 unsigned char get_week_days_RTCC (void){ 00221 00222 unsigned char day_of_the_week; 00223 day_of_the_week = EERandomRead (WRITE_ADD,DAY_WEEK_ADDR); 00224 00225 // I set to 0 the not significant bits 00226 day_of_the_week = day_of_the_week & 0b00000111; 00227 return (day_of_the_week); 00228 } 00229 00230 00231 //************************************************************ 00232 // set_months_RTCC function implementation 00233 //************************************************************ 00234 signed char set_months_RTCC (unsigned char months) { 00235 00236 return (EEByteWrite (WRITE_ADD,MONTHS_ADDR,months)); 00237 00238 } 00239 00240 00241 //************************************************************ 00242 // get_months_RTCC function implementation 00243 //************************************************************ 00244 unsigned char get_months_RTCC (void) { 00245 00246 unsigned char months; 00247 months = EERandomRead (WRITE_ADD,MONTHS_ADDR); 00248 00249 // I set to 0 the not significant bits 00250 months = months & 0b00011111; 00251 return (months); 00252 } 00253 00254 00255 //************************************************************ 00256 // set_years_RTCC function implementation 00257 //************************************************************ 00258 signed char set_years_RTCC (unsigned char years) { 00259 00260 return (EEByteWrite (WRITE_ADD,YEARS_ADDR,years)); 00261 } 00262 00263 00264 //************************************************************ 00265 // get_years_RTCC function implementation 00266 //************************************************************ 00267 unsigned char get_years_RTCC (void) { 00268 00269 unsigned char years; 00270 years = EERandomRead (WRITE_ADD,YEARS_ADDR); 00271 return (years); 00272 } 00273 00274 00275 //************************************************************ 00276 // get_date_RTCC function implementation 00277 //************************************************************ 00278 unsigned char* get_date_RTCC (void) { 00279 00280 static unsigned char date[9]; 00281 unsigned char value; 00282 00283 value = get_days_RTCC(); 00284 00285 // with +48 I convert the number in ASCII number 00286 date[1] = (value & 0b00001111)+48; 00287 date[0] = (value >> 4)+48; 00288 00289 date[2] = '/'; 00290 00291 value = get_months_RTCC(); 00292 date[4] = (value & 0b00001111)+48; 00293 date[3] = (value >> 4)+48; 00294 00295 date[5] = '/'; 00296 00297 00298 value = get_years_RTCC(); 00299 date[7] = (value & 0b00001111)+48; 00300 date[6] = (value >> 4)+48; 00301 00302 date[8] = '\0'; 00303 00304 return (date); 00305 00306 } 00307 00308 00309 //************************************************************ 00310 // set_minutes_alarm_RTCC function implementation 00311 //************************************************************ 00312 signed char set_minutes_alarm_RTCC (unsigned char minutes, unsigned char alarm_enable) { 00313 00314 //I activate AE if required 00315 minutes = minutes + alarm_enable; 00316 00317 return (EEByteWrite (WRITE_ADD,MINUTS_ALARM_ADDR,minutes)); 00318 00319 } 00320 00321 00322 //************************************************************ 00323 // set_hours_alarm_RTCC function implementation 00324 //************************************************************ 00325 signed char set_hours_alarm_RTCC (unsigned char hours, unsigned char alarm_enable) { 00326 00327 //I activate AE if required 00328 hours = hours + alarm_enable; 00329 return (EEByteWrite (WRITE_ADD,HOURS_ALARM_ADDR,hours)); 00330 } 00331 00332 00333 //************************************************************ 00334 // set_days_alarm_RTCC function implementation 00335 //************************************************************ 00336 signed char set_days_alarm_RTCC (unsigned char days, unsigned char alarm_enable) { 00337 00338 //I activate AE if required 00339 days = days + alarm_enable; 00340 return (EEByteWrite (WRITE_ADD,DAYS_ALARM_ADDR,days)); 00341 00342 } 00343 00344 00345 //************************************************************ 00346 // set_day_of_the_week_alarm_RTCC function implementation 00347 //************************************************************ 00348 signed char set_day_of_the_week_alarm_RTCC (unsigned char day_of_the_week_alarm, unsigned char alarm_enable) { 00349 00350 //I activate AE if required 00351 day_of_the_week_alarm = day_of_the_week_alarm + alarm_enable; 00352 return (EEByteWrite (WRITE_ADD,DAY_WEEK_ALARM_ADDR,day_of_the_week_alarm)); 00353 00354 } 00355 00356 00357 //************************************************************ 00358 // enable_alarm_interrupt_RTCC function implementation 00359 //************************************************************ 00360 signed char enable_alarm_interrupt_RTCC (void) { 00361 00362 return (EEByteWrite (WRITE_ADD,CONTROL_REG_2_ADDR,0x02)); 00363 00364 } 00365 00366 00367 //************************************************************ 00368 // disable_alarm_interrupt_RTCC function implementation 00369 //************************************************************ 00370 signed char disable_alarm_interrupt_RTCC (void) { 00371 00372 return (EEByteWrite (WRITE_ADD,CONTROL_REG_2_ADDR,0x00)); 00373 00374 } 00375 00376 00377 //************************************************************ 00378 // is_alarm_ON_RTCC function implementation 00379 //************************************************************ 00380 unsigned char is_alarm_ON_RTCC (void) { 00381 00382 unsigned char value; 00383 value = EERandomRead (WRITE_ADD,CONTROL_REG_2_ADDR); 00384 00385 // Just AF bit is controlled 00386 if (value & 0x08) { 00387 00388 value = value & 0xF7; 00389 00390 // I clean AF bit without canging the other bits 00391 EEByteWrite (WRITE_ADD,0x01,value); 00392 return (1); 00393 00394 } else { 00395 return (0); 00396 } 00397 00398 } 00399 00400 00401 //************************************************************ 00402 // increment_minutes_RTCC function implementation 00403 //************************************************************ 00404 signed char increment_minutes_RTCC (void) { 00405 00406 unsigned char minutes; 00407 signed char error; 00408 00409 // Read the current minutes 00410 minutes = get_minutes_RTCC (); 00411 00412 // Increment the minutes 00413 minutes ++; 00414 00415 // Check the minute limits 00416 00417 if ((minutes&0x0F) > (unsigned char) 9 ) { 00418 minutes &= 0xF0; 00419 minutes += 0x10; 00420 } 00421 00422 if (minutes == (unsigned char) MAX_MINUTES) { 00423 00424 minutes = 0; 00425 } 00426 00427 // Update the minutes 00428 error = set_minutes_RTCC (minutes); 00429 00430 return (error); 00431 00432 } 00433 00434 00435 //************************************************************ 00436 // increment_hours_RTCC function implementation 00437 //************************************************************ 00438 00439 signed char increment_hours_RTCC (void) { 00440 00441 unsigned char hours; 00442 signed char error; 00443 00444 // Read the current hours 00445 hours = get_hours_RTCC (); 00446 00447 // Increment the hours 00448 hours ++; 00449 00450 // Check the hour limits 00451 00452 if ((hours&0x0F) > (unsigned char) 9 ) { 00453 hours &= 0xF0; 00454 hours += 0x10; 00455 } 00456 00457 if (hours == (unsigned char) MAX_HOURS) { 00458 00459 hours = 0; 00460 } 00461 00462 // Update the hours 00463 error = set_hours_RTCC (hours); 00464 00465 return (error); 00466 00467 } 00468 00469 00470 //************************************************************ 00471 // increment_years_RTCC function implementation 00472 //************************************************************ 00473 00474 signed char increment_years_RTCC (void) { 00475 00476 unsigned char years; 00477 signed char error; 00478 00479 // Read the current years 00480 years = get_years_RTCC (); 00481 00482 // Increment the years 00483 years ++; 00484 00485 // Check the year limits 00486 00487 if ((years&0x0F) > (unsigned char) 9 ) { 00488 years &= 0xF0; 00489 years += 0x10; 00490 } 00491 00492 if (years == (unsigned char) MAX_YEARS) { 00493 00494 years = 0; 00495 } 00496 00497 // Update the years 00498 error = set_years_RTCC (years); 00499 00500 return (error); 00501 00502 } 00503 00504 00505 //************************************************************ 00506 // increment_months_RTCC function implementation 00507 //************************************************************ 00508 00509 signed char increment_months_RTCC (void) { 00510 00511 unsigned char months; 00512 signed char error; 00513 00514 // Read the current months 00515 months = get_months_RTCC (); 00516 00517 // Increment the months 00518 months ++; 00519 00520 // Check the month limits 00521 00522 if ((months&0x0F) > (unsigned char) 9 ) { 00523 months &= 0xF0; 00524 months += 0x10; 00525 } 00526 00527 if (months == (unsigned char) MAX_MONTHS) { 00528 00529 months = 1; 00530 } 00531 00532 // Update the months 00533 error = set_months_RTCC (months); 00534 00535 return (error); 00536 00537 } 00538 00539 //************************************************************ 00540 // increment_days_RTCC function implementation 00541 //************************************************************ 00542 00543 signed char increment_days_RTCC (void) { 00544 00545 unsigned char days; 00546 signed char error; 00547 00548 // Read the current days 00549 days = get_days_RTCC (); 00550 00551 // Increment the days 00552 days ++; 00553 00554 // Check the day limits 00555 00556 if ((days&0x0F) > (unsigned char) 9 ) { 00557 days &= 0xF0; 00558 days += 0x10; 00559 } 00560 00561 if (days == (unsigned char) MAX_DAYS) { 00562 00563 days = 1; 00564 } 00565 00566 // Update the days 00567 error = set_days_RTCC (days); 00568 00569 return (error); 00570 00571 } 00572