00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #ifndef __WPROGRAM_H__
00025
00026 #include <WaspClasses.h>
00027 #endif
00028
00029
00030
00031
00032 WaspRTC::WaspRTC()
00033 {
00034
00035 }
00036
00037
00038
00039
00040
00041
00042
00043
00044 void WaspRTC::ON(void)
00045 {
00046 begin();
00047 }
00048
00049
00050
00051
00052
00053
00054
00055 void WaspRTC::OFF(void)
00056 {
00057 close();
00058 setMode(RTC_OFF);
00059 }
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070 void WaspRTC::begin()
00071 {
00072
00073 setMode(RTC_ON);
00074 clearAlarmFlag();
00075
00076 Wire.begin();
00077
00078
00079 resetVars();
00080 readRTC(RTC_ALARM2_ADDRESS);
00081 }
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092 void WaspRTC::close()
00093 {
00094 PWR.closeI2C();
00095 }
00096
00097
00098
00099
00100
00101 void WaspRTC::setMode(uint8_t mode)
00102 {
00103 _pwrMode=mode;
00104 pinMode(RTC_PW,OUTPUT);
00105 switch(_pwrMode)
00106 {
00107 case RTC_ON : digitalWrite(RTC_PW,HIGH);
00108 break;
00109 case RTC_OFF : digitalWrite(RTC_PW,LOW);
00110 break;
00111 }
00112 }
00113
00114
00115
00116
00117
00118
00119 uint8_t WaspRTC::getMode()
00120 {
00121 return _pwrMode;
00122 }
00123
00124
00125
00126
00127
00128 void WaspRTC::resetVars()
00129 {
00130 year = 0;
00131 month = 0;
00132 day = 0;
00133 hour = 0;
00134 minute = 0;
00135 second = 0;
00136 date = 0;
00137 second_alarm1 = 0;
00138 minute_alarm1 = 0;
00139 hour_alarm1 = 0;
00140 day_alarm1 = 0;
00141 minute_alarm2 = 0;
00142 hour_alarm2 = 0;
00143 day_alarm2 = 0;
00144 alarm1Mode = 0;
00145 alarm2Mode = 0;
00146 temp=0;
00147 tempNegative=false;
00148 }
00149
00150
00151
00152
00153
00154
00155
00156
00157 char* WaspRTC::getRTCarray()
00158 {
00159 char aux[60];
00160 for(uint8_t i = 0; i < RTC_DATA_SIZE; i++) {
00161 sprintf(aux, "%u", registersRTC[i]);
00162 }
00163 return aux;
00164 }
00165
00166
00167
00168
00169
00170
00171 char* WaspRTC::getTimestamp()
00172 {
00173 char* aux="\0 " ;
00174 switch (day) {
00175 case 1:
00176 sprintf (aux, "%s, %d/%d/%d - %d:%d.%d", DAY_1, year, month, date, hour, minute, second);
00177 break;
00178 case 2:
00179 sprintf (aux, "%s, %d/%d/%d - %d:%d.%d", DAY_2, year, month, date, hour, minute, second);
00180 break;
00181 case 3:
00182 sprintf (aux, "%s, %d/%d/%d - %d:%d.%d", DAY_3, year, month, date, hour, minute, second);
00183 break;
00184 case 4:
00185 sprintf (aux, "%s, %d/%d/%d - %d:%d.%d", DAY_4, year, month, date, hour, minute, second);
00186 break;
00187 case 5:
00188 sprintf (aux, "%s, %d/%d/%d - %d:%d.%d", DAY_5, year, month, date, hour, minute, second);
00189 break;
00190 case 6:
00191 sprintf (aux, "%s, %d/%d/%d - %d:%d.%d", DAY_6, year, month, date, hour, minute, second);
00192 break;
00193 case 7:
00194 sprintf (aux, "%s, %d/%d/%d - %d:%d.%d", DAY_7, year, month, date, hour, minute, second);
00195 break;
00196 }
00197 return aux;
00198 }
00199
00200
00201
00202
00203
00204
00205
00206
00207
00208
00209
00210
00211
00212
00213 void WaspRTC::readRTC(uint8_t endAddress)
00214 {
00215 uint16_t timecount = 0;
00216
00217 Wire.beginTransmission(RTC_ADDRESS);
00218
00219
00220 Wire.send(RTC_START_ADDRESS);
00221 Wire.endTransmission();
00222
00223
00224 Wire.requestFrom(RTC_ADDRESS, RTC_DATA_SIZE);
00225
00226
00227
00228 while(timecount <= endAddress)
00229 {
00230 if (Wire.available()) {
00231 uint8_t c = Wire.receive();
00232 registersRTC[timecount] = c;
00233 switch (timecount) {
00234 case 0:
00235 second = BCD2byte(c>>4, c&B00001111);
00236 break;
00237 case 1:
00238 minute = BCD2byte(c>>4, c&B00001111);
00239 break;
00240 case 2:
00241 hour = BCD2byte(c>>4, c&B00001111);
00242 break;
00243 case 3:
00244 day = c;
00245 break;
00246 case 4:
00247 date = BCD2byte(c>>4, c&B00001111);
00248 break;
00249 case 5:
00250 month = BCD2byte(c>>4, c&B00001111);
00251 break;
00252 case 6:
00253 year = BCD2byte(c>>4, c&B00001111);
00254 break;
00255 case 7:
00256 second_alarm1 = BCD2byte((c>>4)&B00000111, c&B00001111);
00257 break;
00258 case 8:
00259 minute_alarm1 = BCD2byte((c>>4)&B00000111, c&B00001111);
00260 break;
00261 case 9:
00262 hour_alarm1 = BCD2byte((c>>4)&B00000011, c&B00001111);
00263 break;
00264 case 10:
00265 day_alarm1 = BCD2byte((c>>4)&B00000011, c&B00001111);
00266 break;
00267 case 11:
00268 minute_alarm2 = BCD2byte((c>>4)&B00000111, c&B00001111);
00269 break;
00270 case 12:
00271 hour_alarm2 = BCD2byte((c>>4)&B00000011, c&B00001111);
00272 break;
00273 case 13:
00274 day_alarm2 = BCD2byte((c>>4)&B00000011, c&B00001111);
00275 break;
00276 }
00277 timecount++;
00278 }
00279 }
00280
00281 timecount = 0;
00282 }
00283
00284
00285
00286
00287
00288
00289
00290 void WaspRTC::writeRTC()
00291 {
00292 int timecount = 0;
00293 Wire.beginTransmission(RTC_ADDRESS);
00294
00295
00296 Wire.send(RTC_START_ADDRESS);
00297
00298 registersRTC[RTC_SECONDS_ADDRESS] = byte2BCD(second);
00299 registersRTC[RTC_MINUTES_ADDRESS] = byte2BCD(minute);
00300 registersRTC[RTC_HOURS_ADDRESS] = byte2BCD(hour);
00301 registersRTC[RTC_DAYS_ADDRESS] = day;
00302 registersRTC[RTC_DATE_ADDRESS] = byte2BCD(date);
00303 registersRTC[RTC_MONTH_ADDRESS] = byte2BCD(month);
00304 registersRTC[RTC_YEAR_ADDRESS] = byte2BCD(year);
00305
00306 for(timecount = 0; timecount <= 0x06; timecount++) {
00307 Wire.send(registersRTC[timecount]);
00308 }
00309
00310 Wire.endTransmission();
00311 }
00312
00313
00314
00315
00316
00317
00318
00319 void WaspRTC::writeRTCalarm1()
00320 {
00321 byte timecount = 0;
00322 Wire.beginTransmission(RTC_ADDRESS);
00323
00324
00325 Wire.send(RTC_ALM1_START_ADDRESS);
00326
00327 registersRTC[RTC_ALM1_SECONDS_ADDRESS] = 0x7F & byte2BCD(second_alarm1);
00328 registersRTC[RTC_ALM1_MINUTES_ADDRESS] = 0x7F & byte2BCD(minute_alarm1);
00329 registersRTC[RTC_ALM1_HOURS_ADDRESS] = 0x7F & byte2BCD(hour_alarm1);
00330 registersRTC[RTC_ALM1_DAYS_ADDRESS] = 0X3F & byte2BCD(day_alarm1);
00331
00332 for(timecount = 7; timecount <= 0x0A; timecount++) {
00333 Wire.send(registersRTC[timecount]);
00334 }
00335
00336 Wire.endTransmission();
00337 }
00338
00339
00340
00341
00342
00343
00344
00345 void WaspRTC::writeRTCalarm2()
00346 {
00347 byte timecount = 0;
00348 Wire.beginTransmission(RTC_ADDRESS);
00349
00350
00351 Wire.send(RTC_ALM2_START_ADDRESS);
00352
00353 registersRTC[RTC_ALM2_MINUTES_ADDRESS] = 0x7F & byte2BCD(minute_alarm2);
00354 registersRTC[RTC_ALM2_HOURS_ADDRESS] = 0x7F & byte2BCD(hour_alarm2);
00355 registersRTC[RTC_ALM2_DAYS_ADDRESS] = 0x3F & byte2BCD(day_alarm2);
00356
00357
00358 for(timecount = 0X0B; timecount <= 0x0D; timecount++) {
00359 Wire.send(registersRTC[timecount]);
00360 }
00361
00362 Wire.endTransmission();
00363 }
00364
00365
00366
00367
00368
00369
00370
00371
00372
00373
00374
00375
00376
00377
00378
00379
00380
00381
00382
00383
00384
00385
00386
00387 void WaspRTC::configureAlarmMode (uint8_t alarmNum, uint8_t alarmMode)
00388 {
00389 if (alarmNum == 1){
00390
00391 registersRTC[RTC_CONTROL_ADDRESS] &= B11111101;
00392 registersRTC[RTC_CONTROL_ADDRESS] |= B00000101;
00393 writeRTCregister(RTC_CONTROL_ADDRESS);
00394 registersRTC[RTC_STATUS_ADDRESS] &= B11111100;
00395 writeRTCregister(RTC_STATUS_ADDRESS);
00396
00397 switch (alarmMode){
00398 case 0:
00399 registersRTC[RTC_ALM1_SECONDS_ADDRESS] &= B01111111;
00400 writeRTCregister(RTC_ALM1_SECONDS_ADDRESS);
00401
00402 registersRTC[RTC_ALM1_MINUTES_ADDRESS] &= B01111111;
00403 writeRTCregister(RTC_ALM1_MINUTES_ADDRESS);
00404
00405 registersRTC[RTC_ALM1_HOURS_ADDRESS] &= B01111111;
00406 writeRTCregister(RTC_ALM1_HOURS_ADDRESS);
00407
00408 registersRTC[RTC_ALM1_DAYS_ADDRESS] &= B01111111;
00409 registersRTC[RTC_ALM1_DAYS_ADDRESS] |= B01000000;
00410 writeRTCregister(RTC_ALM1_DAYS_ADDRESS);
00411
00412 break;
00413
00414 case 1:
00415 registersRTC[RTC_ALM1_SECONDS_ADDRESS] &= B01111111;
00416 writeRTCregister(RTC_ALM1_SECONDS_ADDRESS);
00417
00418 registersRTC[RTC_ALM1_MINUTES_ADDRESS] &= B01111111;
00419 writeRTCregister(RTC_ALM1_MINUTES_ADDRESS);
00420
00421 registersRTC[RTC_ALM1_HOURS_ADDRESS] &= B01111111;
00422 writeRTCregister(RTC_ALM1_HOURS_ADDRESS);
00423
00424 registersRTC[RTC_ALM1_DAYS_ADDRESS] &= B00111111;
00425
00426 writeRTCregister(RTC_ALM1_DAYS_ADDRESS);
00427
00428 break;
00429 case 2:
00430 registersRTC[RTC_ALM1_SECONDS_ADDRESS] &= B01111111;
00431 writeRTCregister(RTC_ALM1_SECONDS_ADDRESS);
00432
00433 registersRTC[RTC_ALM1_MINUTES_ADDRESS] &= B01111111;
00434 writeRTCregister(RTC_ALM1_MINUTES_ADDRESS);
00435
00436 registersRTC[RTC_ALM1_HOURS_ADDRESS] &= B01111111;
00437 writeRTCregister(RTC_ALM1_HOURS_ADDRESS);
00438
00439 registersRTC[RTC_ALM1_DAYS_ADDRESS] |= B10000000;
00440 writeRTCregister(RTC_ALM1_DAYS_ADDRESS);
00441
00442 break;
00443 case 3:
00444 registersRTC[RTC_ALM1_SECONDS_ADDRESS] &= B01111111;
00445 writeRTCregister(RTC_ALM1_SECONDS_ADDRESS);
00446
00447 registersRTC[RTC_ALM1_MINUTES_ADDRESS] &= B01111111;
00448 writeRTCregister(RTC_ALM1_MINUTES_ADDRESS);
00449
00450 registersRTC[RTC_ALM1_HOURS_ADDRESS] |= B10000000;
00451 writeRTCregister(RTC_ALM1_HOURS_ADDRESS);
00452
00453 registersRTC[RTC_ALM1_DAYS_ADDRESS] |= B10000000;
00454 writeRTCregister(RTC_ALM1_DAYS_ADDRESS);
00455 break;
00456 case 4:
00457 registersRTC[RTC_ALM1_SECONDS_ADDRESS] &= B01111111;
00458 writeRTCregister(RTC_ALM1_SECONDS_ADDRESS);
00459
00460 registersRTC[RTC_ALM1_MINUTES_ADDRESS] |= B10000000;
00461 writeRTCregister(RTC_ALM1_MINUTES_ADDRESS);
00462
00463 registersRTC[RTC_ALM1_HOURS_ADDRESS] |= B10000000;
00464 writeRTCregister(RTC_ALM1_HOURS_ADDRESS);
00465
00466 registersRTC[RTC_ALM1_DAYS_ADDRESS] |= B10000000;
00467 writeRTCregister(RTC_ALM1_DAYS_ADDRESS);
00468 break;
00469 case 5:
00470 registersRTC[RTC_ALM1_SECONDS_ADDRESS] |= B10000000;
00471 writeRTCregister(RTC_ALM1_SECONDS_ADDRESS);
00472
00473 registersRTC[RTC_ALM1_MINUTES_ADDRESS] |= B10000000;
00474 writeRTCregister(RTC_ALM1_MINUTES_ADDRESS);
00475
00476 registersRTC[RTC_ALM1_HOURS_ADDRESS] |= B10000000;
00477 writeRTCregister(RTC_ALM1_HOURS_ADDRESS);
00478
00479 registersRTC[RTC_ALM1_DAYS_ADDRESS] |= B10000000;
00480 writeRTCregister(RTC_ALM1_DAYS_ADDRESS);
00481 break;
00482 case 6:
00483 registersRTC[RTC_CONTROL_ADDRESS] &= B11111000;
00484 writeRTCregister(RTC_CONTROL_ADDRESS);
00485 detachInt();
00486 break;
00487 }
00488
00489 }
00490
00491 if (alarmNum == 2){
00492
00493 registersRTC[RTC_CONTROL_ADDRESS] &= B11111110;
00494 registersRTC[RTC_CONTROL_ADDRESS] |= B00000110;
00495 writeRTCregister(RTC_CONTROL_ADDRESS);
00496 registersRTC[RTC_STATUS_ADDRESS] &= B11111100;
00497 writeRTCregister(RTC_STATUS_ADDRESS);
00498
00499 switch (alarmMode){
00500 case 0:
00501 registersRTC[RTC_ALM2_MINUTES_ADDRESS] &= B01111111;
00502 writeRTCregister(RTC_ALM2_MINUTES_ADDRESS);
00503
00504 registersRTC[RTC_ALM2_HOURS_ADDRESS] &= B01111111;
00505 writeRTCregister(RTC_ALM2_HOURS_ADDRESS);
00506
00507 registersRTC[RTC_ALM2_DAYS_ADDRESS] &= B01111111;
00508 registersRTC[RTC_ALM2_DAYS_ADDRESS] |= B01000000;
00509 writeRTCregister(RTC_ALM2_DAYS_ADDRESS);
00510
00511 break;
00512
00513 case 1:
00514 registersRTC[RTC_ALM2_MINUTES_ADDRESS] &= B01111111;
00515 writeRTCregister(RTC_ALM2_MINUTES_ADDRESS);
00516
00517 registersRTC[RTC_ALM2_HOURS_ADDRESS] &= B01111111;
00518 writeRTCregister(RTC_ALM2_HOURS_ADDRESS);
00519
00520 registersRTC[RTC_ALM2_DAYS_ADDRESS] &= B00111111;
00521 writeRTCregister(RTC_ALM2_DAYS_ADDRESS);
00522
00523 break;
00524 case 2:
00525 registersRTC[RTC_ALM2_MINUTES_ADDRESS] &= B01111111;
00526 writeRTCregister(RTC_ALM2_MINUTES_ADDRESS);
00527
00528 registersRTC[RTC_ALM2_HOURS_ADDRESS] &= B01111111;
00529 writeRTCregister(RTC_ALM2_HOURS_ADDRESS);
00530
00531 registersRTC[RTC_ALM2_DAYS_ADDRESS] |= B10000000;
00532 writeRTCregister(RTC_ALM2_DAYS_ADDRESS);
00533
00534 break;
00535 case 3:
00536 registersRTC[RTC_ALM2_MINUTES_ADDRESS] &= B01111111;
00537 writeRTCregister(RTC_ALM2_MINUTES_ADDRESS);
00538
00539 registersRTC[RTC_ALM2_HOURS_ADDRESS] |= B10000000;
00540 writeRTCregister(RTC_ALM2_HOURS_ADDRESS);
00541
00542 registersRTC[RTC_ALM2_DAYS_ADDRESS] |= B10000000;
00543 writeRTCregister(RTC_ALM2_DAYS_ADDRESS);
00544
00545 break;
00546 case 4:
00547 registersRTC[RTC_ALM2_MINUTES_ADDRESS] |= B10000000;
00548 writeRTCregister(RTC_ALM2_MINUTES_ADDRESS);
00549
00550 registersRTC[RTC_ALM2_HOURS_ADDRESS] |= B10000000;
00551 writeRTCregister(RTC_ALM2_HOURS_ADDRESS);
00552
00553 registersRTC[RTC_ALM2_DAYS_ADDRESS] |= B10000000;
00554 writeRTCregister(RTC_ALM2_DAYS_ADDRESS);
00555
00556 break;
00557 case 5:
00558 registersRTC[RTC_CONTROL_ADDRESS] &= B11111000;
00559 writeRTCregister(RTC_CONTROL_ADDRESS);
00560 detachInt();
00561 break;
00562 }
00563
00564 }
00565
00566 attachInt();
00567 }
00568
00569
00570
00571
00572
00573
00574
00575
00576 void WaspRTC::writeRTCregister(uint8_t theAddress)
00577 {
00578
00579 Wire.beginTransmission(RTC_ADDRESS);
00580
00581
00582 Wire.send(theAddress);
00583
00584
00585 Wire.send(registersRTC[theAddress]);
00586 Wire.endTransmission();
00587 }
00588
00589
00590
00591
00592
00593
00594
00595
00596 void WaspRTC::readRTCregister(uint8_t theAddress)
00597 {
00598
00599 Wire.beginTransmission(RTC_ADDRESS);
00600
00601
00602 Wire.send(theAddress);
00603 Wire.endTransmission();
00604
00605
00606 Wire.requestFrom(RTC_ADDRESS, 0x01);
00607
00608
00609 while(!Wire.available()) {};
00610 registersRTC[theAddress] = Wire.receive();
00611 Wire.endTransmission();
00612 }
00613
00614
00615
00616
00617
00618
00619
00620
00621
00622
00623 void WaspRTC::setTime(const char* time)
00624 {
00625 uint8_t aux=0, aux2=0;
00626
00627 aux=(uint8_t) time[0] - 48;
00628 aux2=(uint8_t) time[1] - 48;
00629 year = RTC.BCD2byte(aux, aux2);
00630 aux=(uint8_t) time[3] - 48;
00631 aux2=(uint8_t) time[4] - 48;
00632 month = RTC.BCD2byte(aux, aux2);
00633 aux=(uint8_t) time[6] - 48;
00634 aux2=(uint8_t) time[7] - 48;
00635 date = RTC.BCD2byte(aux, aux2);
00636 aux=(uint8_t) time[9] - 48;
00637 aux2=(uint8_t) time[10] - 48;
00638 day = RTC.BCD2byte(aux, aux2);
00639 aux=(uint8_t) time[12] - 48;
00640 aux2=(uint8_t) time[13] - 48;
00641 hour = RTC.BCD2byte(aux, aux2);
00642 aux=(uint8_t) time[15] - 48;
00643 aux2=(uint8_t) time[16] - 48;
00644 minute = RTC.BCD2byte(aux, aux2);
00645 aux=(uint8_t) time[18] - 48;
00646 aux2=(uint8_t) time[19] - 48;
00647 second = RTC.BCD2byte(aux, aux2);
00648 writeRTC();
00649 }
00650
00651
00652
00653
00654
00655
00656
00657
00658
00659 void WaspRTC::setTime(uint8_t _year, uint8_t _month, uint8_t _date, uint8_t day_week, uint8_t _hour, uint8_t _minute, uint8_t _second)
00660 {
00661 uint8_t aux=0, aux2=0;
00662 if(_year<10)
00663 {
00664 aux=0;
00665 aux2=_year;
00666 year = RTC.BCD2byte(aux, aux2);
00667 }
00668 else if(_year>=10)
00669 {
00670 aux2=_year%10;
00671 aux=_year/10;
00672 year = RTC.BCD2byte(aux, aux2);
00673 }
00674 if(_month<10)
00675 {
00676 aux=0;
00677 aux2=_month;
00678 month = RTC.BCD2byte(aux, aux2);
00679 }
00680 else if(_month>=10)
00681 {
00682 aux2=_month%10;
00683 aux=_month/10;
00684 month = RTC.BCD2byte(aux, aux2);
00685 }
00686 if(_date<10)
00687 {
00688 aux=0;
00689 aux2=_date;
00690 date = RTC.BCD2byte(aux, aux2);
00691 }
00692 else if(_date>=10)
00693 {
00694 aux2=_date%10;
00695 aux=_date/10;
00696 date = RTC.BCD2byte(aux, aux2);
00697 }
00698 if(day_week<10)
00699 {
00700 aux=0;
00701 aux2=day_week;
00702 day = RTC.BCD2byte(aux, aux2);
00703 }
00704 else if(day_week>=10)
00705 {
00706 aux2=day_week%10;
00707 aux=day_week/10;
00708 day = RTC.BCD2byte(aux, aux2);
00709 }
00710 if(_hour<10)
00711 {
00712 aux=0;
00713 aux2=_hour;
00714 hour = RTC.BCD2byte(aux, aux2);
00715 }
00716 else if(_hour>=10)
00717 {
00718 aux2=_hour%10;
00719 aux=_hour/10;
00720 hour = RTC.BCD2byte(aux, aux2);
00721 }
00722 if(_minute<10)
00723 {
00724 aux=0;
00725 aux2=_minute;
00726 minute = RTC.BCD2byte(aux, aux2);
00727 }
00728 else if(_minute>=10)
00729 {
00730 aux2=_minute%10;
00731 aux=_minute/10;
00732 minute = RTC.BCD2byte(aux, aux2);
00733 }
00734 if(_second<10)
00735 {
00736 aux=0;
00737 aux2=_second;
00738 second = RTC.BCD2byte(aux, aux2);
00739 }
00740 else if(_second>=10)
00741 {
00742 aux2=_second%10;
00743 aux=_second/10;
00744 second = RTC.BCD2byte(aux, aux2);
00745 }
00746 writeRTC();
00747 }
00748
00749
00750
00751
00752
00753
00754
00755
00756 char* WaspRTC::getTime()
00757 {
00758 readRTC(RTC_DATE_ADDRESS_2);
00759 return getTimestamp();
00760 }
00761
00762
00763
00764
00765
00766
00767
00768
00769 void WaspRTC::setTimeFromGPS()
00770 {
00771 int day, month, year, hour, minute, second = 0;
00772
00773 day = (GPS.dateGPS[0]-'0')*10 + (GPS.dateGPS[1]-'0');
00774
00775 month = (GPS.dateGPS[2]-'0')*10 + (GPS.dateGPS[3]-'0');
00776
00777 year = (GPS.dateGPS[4]-'0')*10 + (GPS.dateGPS[5]-'0');
00778
00779 hour = (GPS.timeGPS[0]-'0')*10 + (GPS.timeGPS[1]-'0');
00780
00781 minute = (GPS.timeGPS[2]-'0')*10 + (GPS.timeGPS[3]-'0');
00782
00783 second = (GPS.timeGPS[4]-'0')*10 + (GPS.timeGPS[5]-'0');
00784
00785 RTC.setTime(year, month, day, 1, hour, minute, second);
00786 }
00787
00788
00789
00790
00791
00792
00793
00794
00795
00796 uint8_t WaspRTC::getTemperature()
00797 {
00798 readRTCregister(RTC_MSB_TEMP_ADDRESS);
00799 readRTCregister(RTC_LSB_TEMP_ADDRESS);
00800 temp = (registersRTC[RTC_MSB_TEMP_ADDRESS]&B01111111) + (registersRTC[RTC_LSB_TEMP_ADDRESS]>>6);
00801
00802 if (registersRTC[RTC_MSB_TEMP_ADDRESS]>>7 == 1)
00803 {
00804 tempNegative=true;
00805 ~(temp);
00806 temp++;
00807 }
00808 else tempNegative=false;
00809
00810 return temp;
00811 }
00812
00813
00814
00815
00816
00817
00818
00819
00820
00821
00822
00823
00824
00825
00826 void WaspRTC::setAlarm1(const char* time, uint8_t offset, uint8_t mode)
00827 {
00828 uint8_t aux=0, aux2=0;
00829 aux=(uint8_t) time[0] - 48;
00830 aux2=(uint8_t) time[1] - 48;
00831 day_alarm1 = BCD2byte(aux, aux2);
00832 aux=(uint8_t) time[3] - 48;
00833 aux2=(uint8_t) time[4] - 48;
00834 hour_alarm1 = BCD2byte(aux, aux2);
00835 aux=(uint8_t) time[6] - 48;
00836 aux2=(uint8_t) time[7] - 48;
00837 minute_alarm1 = BCD2byte(aux, aux2);
00838 aux=(uint8_t) time[9] - 48;
00839 aux2=(uint8_t) time[10] - 48;
00840 second_alarm1 = BCD2byte(aux, aux2);
00841
00842 if(offset==RTC_OFFSET)
00843 {
00844 readRTC(RTC_DATE_ADDRESS_2);
00845 second_alarm1+=second;
00846 if(second_alarm1>=60)
00847 {
00848 second_alarm1-=60;
00849 minute_alarm1++;
00850 }
00851 minute_alarm1+=minute;
00852 if(minute_alarm1>=60)
00853 {
00854 minute_alarm1-=60;
00855 hour_alarm1++;
00856 }
00857 hour_alarm1+=hour;
00858 if(hour_alarm1>=24)
00859 {
00860 hour_alarm1-=24;
00861 day_alarm1++;
00862 }
00863 if(mode==RTC_ALM1_MODE1) day_alarm1+=day;
00864 else day_alarm1+=date;
00865 }
00866
00867 RTC.writeRTCalarm1();
00868 RTC.configureAlarmMode(1,mode);
00869 }
00870
00871
00872
00873
00874
00875
00876
00877
00878
00879
00880
00881
00882
00883 void WaspRTC::setAlarm1(uint8_t day_date, uint8_t _hour, uint8_t _minute, uint8_t _second, uint8_t offset, uint8_t mode)
00884 {
00885 uint8_t aux=0, aux2=0;
00886
00887 if(day_date<10)
00888 {
00889 aux=0;
00890 aux2=day_date;
00891 day_alarm1 = BCD2byte(aux, aux2);
00892 }
00893 else if(day_date>=10)
00894 {
00895 aux2=day_date%10;
00896 aux=day_date/10;
00897 day_alarm1 = BCD2byte(aux, aux2);
00898 }
00899 if(_hour<10)
00900 {
00901 aux=0;
00902 aux2=_hour;
00903 hour_alarm1 = BCD2byte(aux, aux2);
00904 }
00905 else if(_hour>=10)
00906 {
00907 aux2=_hour%10;
00908 aux=_hour/10;
00909 hour_alarm1 = BCD2byte(aux, aux2);
00910 }
00911 if(_minute<10)
00912 {
00913 aux=0;
00914 aux2=_minute;
00915 minute_alarm1 = BCD2byte(aux, aux2);
00916 }
00917 else if(_minute>=10)
00918 {
00919 aux2=_minute%10;
00920 aux=_minute/10;
00921 minute_alarm1 = BCD2byte(aux, aux2);
00922 }
00923 if(_second<10)
00924 {
00925 aux=0;
00926 aux2=_second;
00927 second_alarm1 = BCD2byte(aux, aux2);
00928 }
00929 else if(_second>=10)
00930 {
00931 aux2=_second%10;
00932 aux=_second/10;
00933 second_alarm1 = BCD2byte(aux, aux2);
00934 }
00935
00936 if(offset==RTC_OFFSET)
00937 {
00938 readRTC(RTC_DATE_ADDRESS_2);
00939 second_alarm1+=second;
00940 if(second_alarm1>=60)
00941 {
00942 second_alarm1-=60;
00943 minute_alarm1++;
00944 }
00945 minute_alarm1+=minute;
00946 if(minute_alarm1>=60)
00947 {
00948 minute_alarm1-=60;
00949 hour_alarm1++;
00950 }
00951 hour_alarm1+=hour;
00952 if(hour_alarm1>=24)
00953 {
00954 hour_alarm1-=24;
00955 day_alarm1++;
00956 }
00957 if(mode==RTC_ALM1_MODE1) day_alarm1+=day;
00958 else day_alarm1+=date;
00959 }
00960
00961 RTC.writeRTCalarm1();
00962 RTC.configureAlarmMode(1,mode);
00963 }
00964
00965
00966
00967
00968
00969
00970
00971
00972 char* WaspRTC::getAlarm1()
00973 {
00974 char aux[40];
00975
00976 readRTC(RTC_ALARM1_ADDRESS);
00977 sprintf (aux, "%d, %d:%d:%d - %d:%d:%d", day, year, month, day_alarm1, hour_alarm1, minute_alarm1, second_alarm1);
00978 return aux;
00979 }
00980
00981
00982
00983
00984
00985
00986
00987
00988
00989
00990
00991
00992
00993 void WaspRTC::setAlarm2(const char* time, uint8_t offset, uint8_t mode)
00994 {
00995 uint8_t aux=0, aux2=0;
00996
00997 aux=(uint8_t) time[0] - 48;
00998 aux2=(uint8_t) time[1] - 48;
00999 day_alarm2 = BCD2byte(aux, aux2);
01000 aux=(uint8_t) time[3] - 48;
01001 aux2=(uint8_t) time[4] - 48;
01002 hour_alarm2 = BCD2byte(aux, aux2);
01003 aux=(uint8_t) time[6] - 48;
01004 aux2=(uint8_t) time[7] - 48;
01005 minute_alarm2 = BCD2byte(aux, aux2);
01006
01007 if(offset==RTC_OFFSET)
01008 {
01009 readRTC(RTC_DATE_ADDRESS_2);
01010 minute_alarm2+=minute;
01011 if(minute_alarm2>=60)
01012 {
01013 minute_alarm2-=60;
01014 hour_alarm2++;
01015 }
01016 hour_alarm2+=hour;
01017 if(hour_alarm2>=24)
01018 {
01019 hour_alarm2-=24;
01020 day_alarm2++;
01021 }
01022 if(mode==RTC_ALM2_MODE1) day_alarm2+=day;
01023 else day_alarm2+=date;
01024 }
01025
01026 RTC.writeRTCalarm2();
01027 RTC.configureAlarmMode(2,mode);
01028 }
01029
01030
01031
01032
01033
01034
01035
01036
01037
01038
01039
01040
01041
01042 void WaspRTC::setAlarm2(uint8_t day_date, uint8_t _hour, uint8_t _minute, uint8_t offset, uint8_t mode)
01043 {
01044 uint8_t aux=0, aux2=0;
01045 if(day_date<10)
01046 {
01047 aux=0;
01048 aux2=day_date;
01049 day_alarm2 = BCD2byte(aux, aux2);
01050 }
01051 else if(day_date>=10)
01052 {
01053 aux2=day_date%10;
01054 aux=day_date/10;
01055 day_alarm2 = BCD2byte(aux, aux2);
01056 }
01057 if(_hour<10)
01058 {
01059 aux=0;
01060 aux2=_hour;
01061 hour_alarm2 = BCD2byte(aux, aux2);
01062 }
01063 else if(_hour>=10)
01064 {
01065 aux2=_hour%10;
01066 aux=_hour/10;
01067 hour_alarm2 = BCD2byte(aux, aux2);
01068 }
01069 if(_minute<10)
01070 {
01071 aux=0;
01072 aux2=_minute;
01073 minute_alarm2 = BCD2byte(aux, aux2);
01074 }
01075 else if(_minute>=10)
01076 {
01077 aux2=_minute%10;
01078 aux=_minute/10;
01079 minute_alarm2 = BCD2byte(aux, aux2);
01080 }
01081
01082 if(offset==RTC_OFFSET)
01083 {
01084 readRTC(RTC_DATE_ADDRESS_2);
01085 minute_alarm2+=minute;
01086 if(minute_alarm2>=60)
01087 {
01088 minute_alarm2-=60;
01089 hour_alarm2++;
01090 }
01091 hour_alarm2+=hour;
01092 if(hour_alarm2>=24)
01093 {
01094 hour_alarm2-=24;
01095 day_alarm2++;
01096 }
01097 if(mode==RTC_ALM2_MODE1) day_alarm2+=day;
01098 else day_alarm2+=date;
01099 }
01100
01101 RTC.writeRTCalarm2();
01102 RTC.configureAlarmMode(2,mode);
01103 }
01104
01105
01106
01107
01108
01109
01110
01111
01112 char* WaspRTC::getAlarm2()
01113 {
01114 char aux[35];
01115
01116 readRTC(RTC_ALARM2_ADDRESS);
01117 sprintf (aux, "%d, %d:%d:%d - %d:%d", day, year, month, day_alarm2, hour_alarm2, minute_alarm2);
01118 return aux;
01119 }
01120
01121
01122
01123
01124
01125
01126
01127 void WaspRTC::clearAlarmFlag()
01128 {
01129 RTC.registersRTC[RTC_STATUS_ADDRESS] &= B11111100;
01130 RTC.writeRTCregister(RTC_STATUS_ADDRESS);
01131 }
01132
01133
01134
01135
01136
01137 uint8_t WaspRTC::BCD2byte(uint8_t number)
01138 {
01139 return (number>>4)*10 | (number & 0x0F);
01140 }
01141
01142
01143
01144 uint8_t WaspRTC::BCD2byte(uint8_t high, uint8_t low)
01145 {
01146 return high*10 + low;
01147 }
01148
01149
01150
01151 uint8_t WaspRTC::byte2BCD(uint8_t theNumber)
01152 {
01153 return (theNumber%10 | ((theNumber-theNumber%10)/10)<<4);
01154 }
01155
01156
01157
01158
01159
01160
01161
01162
01163
01164
01165
01166 void WaspRTC::attachInt(void)
01167 {
01168 enableInterrupts(RTC_INT);
01169 }
01170
01171
01172
01173
01174
01175
01176 void WaspRTC::detachInt(void)
01177 {
01178 disableInterrupts(RTC_INT);
01179 clearAlarmFlag();
01180 }
01181
01182
01183
01184
01185
01186
01187 WaspRTC RTC = WaspRTC();
01188