void initializeTime(int GMT_zone) { GMT_hour_adding = GMT_zone; while(epoch<epochtime) { vTaskDelay(50); //Remember to enable the SNTP Client in the wizard. epoch=SNTPGetUTCSeconds(); } epoch=SNTPGetUTCSeconds(); now=(time_t)epoch; ts=localtime(&now); vTaskDelay(20); ts->tm_hour = (ts->tm_hour + GMT_hour_adding); // Correct if overflowed hour 0-24 format if(ts->tm_hour > 24) { ts->tm_hour = ts->tm_hour - 24; } else if(ts->tm_hour < 0) { ts->tm_hour = ts->tm_hour +24; } sprintf (epochString, "\nReceived date/time is: %s \r\n", asctime (ts) ); UARTWrite(1,epochString); RTCCSet(ts); //Set time };
/********************************************************************* * Function: RTCCInit * * Preconditions: RTCCInit must be called before. * * Overview: Enable the oscillator for the RTCC * * Input: None. * * Output: None. ********************************************************************/ void RTCCInit(void) { // Enables the LP OSC for RTCC operation asm("mov #OSCCON,W1"); // move address of OSCCON to W1 asm("mov.b #0x02, W0"); // move 8-bit literal to W0, 16-bit. asm("mov.b #0x46, W2"); // unlock byte 1 for OSCCONL(low byte) asm("mov.b #0x57, W3"); // unlock byte 2 for OSCCONL(low byte) // move 8-bit of Wn to OSCCON register asm("mov.b W2, [W1]"); // write unlock byte 1 asm("mov.b W3, [W1]"); // write unlock byte 2 asm("mov.b W0, [W1]"); // enable SOSCEN // Unlock sequence must take place for RTCEN to be written RCFGCAL = 0x0000; RTCCUnlock(); RCFGCALbits.RTCEN = 1; // bit15 //RTCC pin pad conected to RTCC second clock PADCFG1bits.RTSECSEL = 1; RCFGCALbits.RTCOE = 1; //RTCC Output Enable bit /* Enable the RTCC interrupt*/ IFS3bits.RTCIF = 0; //clear the RTCC interrupt flag IEC3bits.RTCIE = 1; //enable the RTCC interrupt // TO DO: Write the time and date to RTCC as follow. _time_chk.sec = mRTCCBin2Dec(0); _time_chk.min = mRTCCBin2Dec(1); // 40 minutes _time_chk.hr = mRTCCBin2Dec(2); //7PM _time_chk.wkd = 3; _time_chk.day = mRTCCBin2Dec(4); _time_chk.mth = mRTCCBin2Dec(5); _time_chk.yr = mRTCCBin2Dec(13); RTCCCalculateWeekDay(); // To calculate and confirm the weekday // Set it after you change the time and date. RTCCSet(); // Set alarm _alarm.sec = 0x01; _alarm.min = 0x01; _alarm.hr = 0x01; _alarm.wkd = 0x01; _alarm.day = 0x01; _alarm.mth = 0x01; RTCCALMSet(); }
WORD ProcessMessageTime( WORD translatedMsg, OBJ_HEADER* pObj, GOL_MSG* pMsg ) { signed char change; WORD controlID; signed char day; signed char dayMax; signed char hour; signed char minute; signed char month; signed char year; controlID = GetObjID(pObj); switch (controlID) { case ID_RTCC_BUTTON_NEXT: // Show the old control set as unselected. IndicateFocus( BLACK ); // Set the next control as active. currentControlSet ++; if (currentControlSet > CONTROL_SET_MAX) { currentControlSet = 0; } // Show the new control set as selected. IndicateFocus( WHITE ); return 0; // Hidden button break; case ID_RTCC_BUTTON_PREVIOUS: // Show the old control set as unselected. IndicateFocus( BLACK ); // Set the next control as active. currentControlSet --; if (currentControlSet < 0 ) { currentControlSet = CONTROL_SET_MAX; } // Show the new control set as selected. IndicateFocus( WHITE ); return 0; // Hidden button break; case ID_RTCC_BUTTON_HOME: screenState = SCREEN_DISPLAY_MAIN; return 0; // Hidden button break; case ID_DAY_PLUS: case ID_MONTH_PLUS: case ID_YEAR_PLUS: case ID_HOUR_PLUS: case ID_MINUTE_PLUS: if (translatedMsg == BTN_MSG_PRESSED) { change = +1; } else { return 1; } break; case ID_DAY_MINUS: case ID_MONTH_MINUS: case ID_YEAR_MINUS: case ID_HOUR_MINUS: case ID_MINUTE_MINUS: if (translatedMsg == BTN_MSG_PRESSED) { change = -1; } else { return 1; } break; default: return 0; } // Update the selected field. RTCCProcessEvents(); hour = RTCCGetBinHour(); minute = RTCCGetBinMin(); day = RTCCGetBinDay(); month = RTCCGetBinMonth(); year = RTCCGetBinYear(); dayMax = daysPerMonth[month-1]; // February has one day more for a leap year, unless it is on the thousands if ((month == 2) && ((year % 4) == 0) && (year != 0)) { dayMax ++; } // Change the appropriate setting. Allow the controls to roll over. switch (currentControlSet) { case CONTROL_SET_DAY: day += change; if (day < 1) { day = dayMax; } if (day > dayMax) { day = 1; } break; case CONTROL_SET_MONTH: month += change; if (month < 1) { month = 12; } if (month > 12) { month = 1; } break; case CONTROL_SET_YEAR: year += change; if (year < 0) { year = 99; } if (year > 99) { year = 0; } break; case CONTROL_SET_HOUR: hour += change; if (hour < 0) { hour = 23; } if (hour > 23) { hour = 0; } break; case CONTROL_SET_MINUTE: minute += change; if (minute < 0) { minute = 59; } if (minute > 59) { minute = 0; } break; default: return 0; } RTCCOff(); RTCCSetBinHour( hour ); RTCCSetBinMin( minute ); RTCCSetBinSec( 0 ); RTCCSetBinMonth( month ); RTCCSetBinYear( year ); RTCCSetBinDay( day ); RTCCCalculateWeekDay(); RTCCSet(); // Copy the new values to the RTCC registers UpdateDateTime(); // Update the display. return 1; // Call the default handler to show the button state. }