static long rtcTimeNow() { RTC_TimeTypeDef time; RTC_DateTypeDef date; RTC_TimeTypeDef time2; RTC_DateTypeDef date2; uint32_t ss; uint32_t ss2; struct tm t; // RTC is in BYPSHAD mode. Read the registers twice. RTC_GetTime(RTC_Format_BIN, &time); RTC_GetDate(RTC_Format_BIN, &date); ss = RTC_GetSubSecond(); RTC_GetTime(RTC_Format_BIN, &time2); RTC_GetDate(RTC_Format_BIN, &date2); ss2 = RTC_GetSubSecond(); // Check read values. If they match, read was ok. Otherwise read third time. if (ss != ss2 || time.RTC_Seconds != time2.RTC_Seconds || time.RTC_Minutes != time2.RTC_Minutes || time.RTC_Hours != time2.RTC_Hours || date.RTC_Date != date2.RTC_Date || date.RTC_Month != date2.RTC_Month || date.RTC_Year != date2.RTC_Year) { RTC_GetTime(RTC_Format_BIN, &time); RTC_GetDate(RTC_Format_BIN, &date); ss = RTC_GetSubSecond(); } memset(&t, '\0', sizeof(t)); t.tm_sec = time.RTC_Seconds; t.tm_min = time.RTC_Minutes; t.tm_hour = time.RTC_Hours; t.tm_mday = date.RTC_Date; t.tm_mon = date.RTC_Month - 1; t.tm_year = date.RTC_Year + 2000 - 1900; time_t secs; long msecs; secs = mktime(&t); msecs = 1000 - (ss * 1000 / 1024); return 1000 * secs + msecs; }
void Time_Show(uint8_t Line, uint8_t pos) { /* Wait until the calendar is synchronized */ while (RTC_WaitForSynchro() != SUCCESS); /* Get the current subsecond Time*/ Subsecondvalue = RTC_GetSubSecond(); /* Wait until the calendar is synchronized */ while (RTC_WaitForSynchro() != SUCCESS); /* Get the current Time*/ RTC_GetTime(RTC_Format_BCD, &RTC_TimeStr); Mstime = 999 - ((uint32_t)((uint32_t)Subsecondvalue * 1000) / (uint32_t)RTC_InitStr.RTC_SynchPrediv); Ms100 = (uint8_t)(Mstime / 100); Ms10 = (uint8_t)((Mstime % 100 ) / 10); Ms1 = (uint8_t)(Mstime % 10); /* Fill the LCDString fields with the current Time : second and milliseconds*/ LCDStringTime[pos] = (uint8_t)(((uint8_t)(RTC_TimeStr.RTC_Seconds & 0xF0) >> 4) + ASCII_NUM_0); LCDStringTime[pos+1] = (uint8_t)(((uint8_t)(RTC_TimeStr.RTC_Seconds & 0x0F)) + ASCII_NUM_0); LCDStringTime[pos+3] = (uint8_t)((uint8_t)(Ms100 + ASCII_NUM_0)); LCDStringTime[pos+4] = (uint8_t)((uint8_t)(Ms10 + ASCII_NUM_0)); LCDStringTime[pos+5] = (uint8_t)((uint8_t)(Ms1 + ASCII_NUM_0)); /* Print the Time Calendar on the LCD*/ LCD_SetCursorPos(Line, 0); LCD_Print((uint8_t*)LCDStringTime); }
int64_t get_current_time(){ int64_t ret; RTC_TimeTypeDef time; ret = RTC_GetSubSecond(); RTC_GetTime( RTC_Format_BIN, &time ); ret += time.RTC_Hours*60*60*1000; ret += time.RTC_Minutes*60*1000; ret += time.RTC_Seconds*1000; return ret; }
void Time_SaveCurrentTime(uint8_t Id) { /* Wait until the calendar is synchronized */ while (RTC_WaitForSynchro() != SUCCESS); /* Get the current subsecond Time*/ SaveSubsecond[Id] = RTC_GetSubSecond(); /* Wait until the calendar is synchronized */ while (RTC_WaitForSynchro() != SUCCESS); /* Get the current Time*/ RTC_GetTime(RTC_Format_BCD, &RTC_SaveTimeStr[Id]); }
void get_iec_time(cp56time2a* TM_cp56_time){ RTC_TimeTypeDef RTC_TimeStruct; RTC_DateTypeDef RTC_DateStruct; RTC_GetTime(RTC_Format_BIN, &RTC_TimeStruct); RTC_GetDate(RTC_Format_BIN, &RTC_DateStruct); TM_cp56_time->hour = RTC_TimeStruct.RTC_Hours; TM_cp56_time->min = RTC_TimeStruct.RTC_Minutes; TM_cp56_time->msec = RTC_TimeStruct.RTC_Seconds*0x03E8 + RTC_GetSubSecond()/8; TM_cp56_time->mday = RTC_DateStruct.RTC_Date; TM_cp56_time->month = RTC_DateStruct.RTC_Month; TM_cp56_time->wday = RTC_DateStruct.RTC_WeekDay; TM_cp56_time->year = RTC_DateStruct.RTC_Year; }
void Time_Show(uint8_t Line) { /* Wait until the calendar is synchronized */ while (RTC_WaitForSynchro() != SUCCESS); /* Get the current subsecond Time*/ subsecondvalue = RTC_GetSubSecond(); /* Wait until the calendar is synchronized */ while (RTC_WaitForSynchro() != SUCCESS); /* Get the current Time*/ RTC_GetTime(RTC_Format_BCD, &RTC_TimeStr); mstime = 1000 - ((uint32_t)((uint32_t)subsecondvalue * 1000) / (uint32_t)RTC_InitStr.RTC_SynchPrediv); ms100 = (uint8_t)(mstime / 100); ms10 = (uint8_t)((mstime % 100 ) / 10); ms1 = (uint8_t)(mstime % 10); /* Fill the LCDString fields with the current Time*/ LCDStringTime[SHOW_POINT] = (uint8_t)(((uint8_t)(RTC_TimeStr.RTC_Hours & 0xF0) >> 4) + ASCII_NUM_0); LCDStringTime[SHOW_POINT+1] = (uint8_t)(((uint8_t)(RTC_TimeStr.RTC_Hours & 0x0F)) + ASCII_NUM_0); LCDStringTime[SHOW_POINT+3] = (uint8_t)(((uint8_t)(RTC_TimeStr.RTC_Minutes & 0xF0) >> 4) + ASCII_NUM_0); LCDStringTime[SHOW_POINT+4] = (uint8_t)(((uint8_t)(RTC_TimeStr.RTC_Minutes & 0x0F)) + (uint8_t)ASCII_NUM_0); LCDStringTime[SHOW_POINT+6] = (uint8_t)(((uint8_t)(RTC_TimeStr.RTC_Seconds & 0xF0) >> 4) + ASCII_NUM_0); LCDStringTime[SHOW_POINT+7] = (uint8_t)(((uint8_t)(RTC_TimeStr.RTC_Seconds & 0x0F)) + ASCII_NUM_0); LCDStringTime[SHOW_POINT+9] = (uint8_t)((uint8_t)(ms100 + ASCII_NUM_0)); LCDStringTime[SHOW_POINT+10] = (uint8_t)((uint8_t)(ms10 + ASCII_NUM_0)); LCDStringTime[SHOW_POINT+11] = (uint8_t)((uint8_t)(ms1 + ASCII_NUM_0)); /* Print the Time Calendar on the LCD*/ LCD_SetCursorPos(Line, 0); LCD_Print((uint8_t *)LCDStringTime); }
/** * @brief Main program. * @param None * @retval None */ int main(void) { /*!< At this stage the microcontroller clock setting is already configured, this is done through SystemInit() function which is called from startup file (startup_stm32f0xx.s) before to branch to application main. To reconfigure the default setting of SystemInit() function, refer to system_stm32f0xx.c file */ RTC_TimeTypeDef RTC_TimeStruct; /* Initialize the LCD */ STM320518_LCD_Init(); /* Clear the LCD */ LCD_Clear(LCD_COLOR_WHITE); /* Set the LCD Back Color */ LCD_SetBackColor(Blue); /* Set the LCD Text Color */ LCD_SetTextColor(White); /* Displays MESSAGE1 on line 1 */ LCD_DisplayStringLine(LINE(0), (uint8_t *)MESSAGE1); /* Set the LCD Text Color */ LCD_SetTextColor(Red); /* Set the LCD Back Color */ LCD_SetBackColor(Red); LCD_DrawFullRect(31, 292,264,34); /* Set the LCD Back Color */ LCD_SetBackColor(White); LCD_DrawFullRect(33, 290 ,260,30); /* Configure the external interrupt "RIGHT" and "LEFT" buttons */ STM_EVAL_PBInit(BUTTON_RIGHT,BUTTON_MODE_EXTI); STM_EVAL_PBInit(BUTTON_LEFT,BUTTON_MODE_EXTI); /* Configure the RTC peripheral by selecting the clock source.*/ RTC_Config(); /* Configure the RTC tamper register : To Clear all the Backup data register */ RTC_TamperConfig(); /* Set the LCD Text, Back Colors and Text size */ LCD_SetTextColor(Black); LCD_SetBackColor(Cyan); LCD_SetFont(&Font12x12); LCD_DisplayStringLine(LINE(18), (uint8_t *)MESSAGE2); /* Set the LCD Back Color */ LCD_SetBackColor(Blue); LCD_SetTextColor(White); LCD_DisplayStringLine(LINE(19), (uint8_t *)MESSAGE3); /* Set text size */ LCD_SetFont(&Font16x24); /* Set the LCD Text Color */ LCD_SetTextColor(Blue); /* Initialize time Stucture */ RTC_TimeStructInit(&RTC_TimeStruct); /* Infinite loop */ while (1) { /* Set the LCD Back Color and text size */ LCD_SetFont(&Font16x24); LCD_SetBackColor(White); /* Check on the event 'start' */ if(StartEvent != 0x0) { /* Get the RTC sub second fraction */ SecondFraction = (((256 - (uint32_t)RTC_GetSubSecond()) * 1000) / 256); /* Get the Curent time */ RTC_GetTime(RTC_Format_BIN, &RTC_TimeStruct); /* Refresh : Display the curent time and the sub second on the LCD */ RTC_Time_Display(37, Black , RTC_Get_Time(SecondFraction , &RTC_TimeStruct)); } else { /* Re-initialize the Display time on the LCD */ RTC_Time_InitDisplay(); } } }
/** * @brief This function handles External lines 15 to 10 interrupt request. * @param None * @retval None */ void EXTI15_10_IRQHandler(void) { if((EXTI_GetITStatus(TAMPER_BUTTON_EXTI_LINE) != RESET) && (ubStartevent !=0) && (uwBackupindex < 11)) { uint16_t Colorx; uint8_t index = 0; if (uwBackupindex < 10) { /* Increment counter */ ubRTCCount++; /* Set LCD background color*/ if((uint8_t)(ubRTCCount % 2) != 0) { LCD_SetBackColor(Blue2); Colorx = White; } else { LCD_SetBackColor(Cyan); Colorx = Black; } /* Get the Current sub seconds and time */ ubSSecondfraction = 1000 - ((uint32_t)((uint32_t)RTC_GetSubSecond() * 1000) / (uint32_t)0x3FF); RTC_GetTime(RTC_Format_BCD, &RTC_StampTimeStruct); LCD_SetFont(&Font12x12); /* Display result on the LCD */ RTC_Time_display(LINE(7 + uwBackupindex), Colorx, RTC_Get_Time(ubSSecondfraction , &RTC_StampTimeStruct)); /* Save time register to Backup register (the first 10 registers are reserved for time) */ RTC_WriteBackupRegister(aBKPDataReg[uwBackupindex],(uint32_t)RTC->TR); /* Save sub second time stamp register (the latest 10 registers are reserved for sub second) */ RTC_WriteBackupRegister(aBKPDataReg[uwBackupindex + 10], ubSSecondfraction); } else { /* Set the LCD Background Color */ LCD_SetBackColor(White); LCD_SetFont(&Font12x12); /* Clear all LCD lines from 7 to 19 */ for (index = 0; index < 13; index++) { LCD_ClearLine(LINE(7 + index)); } /* Reset Counters */ ubRTCCount = 0; uwBackupindex = 0 ; /* Enter to idle */ ubStartevent =0; /* Set the time to 00h 00mn 00s AM */ RTC_TimeStructure.RTC_H12 = RTC_H12_AM; RTC_TimeStructure.RTC_Hours = 0; RTC_TimeStructure.RTC_Minutes = 0; RTC_TimeStructure.RTC_Seconds = 0; RTC_SetTime(RTC_Format_BCD, &RTC_TimeStructure); /* Disable the RTC Clock */ RCC_RTCCLKCmd(DISABLE); LCD_SetFont(&Font16x24); /* Set the LCD Text Color */ LCD_SetTextColor(Blue); /* Display message to the LCD */ LCD_DisplayStringLine(LINE(5), (uint8_t *)MESSAGE1); /* Clear EXTI line 21 */ EXTI_ClearITPendingBit(EXTI_Line21); /* Clear Tamper pin interrupt pending bit */ RTC_ClearITPendingBit(RTC_IT_TAMP1); } uwBackupindex++; } /* Clear the TAMPER EXTI pending bit */ EXTI_ClearITPendingBit(TAMPER_BUTTON_EXTI_LINE); }
/** * @brief Main program. * @param None * @retval None */ int main(void) { /*!< At this stage the microcontroller clock setting is already configured, this is done through SystemInit() function which is called from startup file (startup_stm32f4xx.s) before to branch to application main. To reconfigure the default setting of SystemInit() function, refer to system_stm32f4xx.c file */ /* Initialize the LCD */ STM324xG_LCD_Init(); /* Clear the LCD */ LCD_Clear(White); /* Set the LCD Back Color */ LCD_SetBackColor(Blue); /* Set the LCD Text Color */ LCD_SetTextColor(White); /* Displays MESSAGE1 on line 1 */ LCD_DisplayStringLine(LINE(0), (uint8_t *)MESSAGE1); /* RTC configuration */ RTC_Config(); /* Set the LCD Text Color */ LCD_SetTextColor(Red); /* Set the LCD Back Color */ LCD_SetBackColor(White); /* Draw rectangle on the LCD */ LCD_DrawRect(43, 300, 30, 260); /* Configure the external interrupt "WAKEUP" and "KEY" buttons */ STM_EVAL_PBInit(BUTTON_WAKEUP,BUTTON_MODE_EXTI); STM_EVAL_PBInit(BUTTON_KEY,BUTTON_MODE_EXTI); /* Configure the RTC tamper register */ RTC_TamperConfig(); /* Set the LCD Text Color */ LCD_SetTextColor(Blue); /* Displays MESSAGE2 on line 5 */ LCD_DisplayStringLine(LINE(5), (uint8_t *)MESSAGE2); /* Infinite loop */ while (1) { /* Set The LCD font size */ LCD_SetFont(&Font16x24); /* Set the LCD Back Color */ LCD_SetBackColor(White); /* Check on the event 'start'*/ if(Startevent != 0x0) { /* Get the RTC sub second fraction */ Secondfraction = 1000 - ((uint32_t)((uint32_t)RTC_GetSubSecond() * 1000) / (uint32_t)0x3FF); } else { /* Idle */ Secondfraction =0x0; } /* Get the Curent time */ RTC_GetTime(RTC_Format_BCD, &RTC_TimeStruct); /* Display the curent time and the sub second on the LCD */ RTC_Time_display(Line2, Black , RTC_Get_Time(Secondfraction , &RTC_TimeStruct)); } }
/** * @brief Main program. * @param None * @retval None */ int main(void) { /*!< At this stage the microcontroller clock setting is already configured, this is done through SystemInit() function which is called from startup file (startup_stm32f0xx.s) before to branch to application main. To reconfigure the default setting of SystemInit() function, refer to system_stm32f0xx.c file */ RTC_TimeTypeDef RTC_TimeStruct; RTC_TimeTypeDef RTC_TimeStructureInit; RTC_TimeTypeDef RTC_StampTimeStruct; /* Configure the external interrupt "RIGHT" and "LEFT" buttons */ STM_EVAL_PBInit(BUTTON_RIGHT,BUTTON_MODE_EXTI); STM_EVAL_PBInit(BUTTON_LEFT,BUTTON_MODE_EXTI); /* Configure the RTC peripheral by selecting the clock source.*/ RTC_Config(); /* LCD Display init */ Display_Init(); /* Configure the RTC tamper register : To Clear all the Backup data register */ RTC_TamperConfig(); /* Initialize time Stucture */ RTC_TimeStructInit(&RTC_TimeStruct); /* Infinite loop */ while (1) { /* Set the LCD Back Color and text size */ LCD_SetFont(&Font16x24); LCD_SetBackColor(White); /* Check on the event 'start' */ if(StartEvent != 0x0) { /* Get the RTC sub second fraction */ SecondFraction = (((256 - (uint32_t)RTC_GetSubSecond()) * 1000) / 256); /* Get the Curent time */ RTC_GetTime(RTC_Format_BIN, &RTC_TimeStruct); /* Refresh : Display the curent time and the sub second on the LCD */ RTC_Time_Display(37, Black , RTC_Get_Time(SecondFraction , &RTC_TimeStruct)); } else { /* Re-initialize the Display time on the LCD */ RTC_Time_InitDisplay(); } /* Left button is pressed */ if (Button_State == LEFT_ON) { /* Enable Tamper interrupt */ RTC_ITConfig(RTC_IT_TAMP, ENABLE); /* Enabale the tamper 1 */ RTC_TamperCmd(RTC_Tamper_1 , ENABLE); /* Set the LCD Back Color */ LCD_SetBackColor(White); /* Clear the LCD line 5 */ LCD_ClearLine(Line5); /* Get the current time */ RTC_GetTime(RTC_Format_BIN, &RTC_TimeStructureInit); CurrentTimeSec = (RTC_TimeStructureInit.RTC_Hours * 3600) + (RTC_TimeStructureInit.RTC_Minutes * 60) + RTC_TimeStructureInit.RTC_Seconds; /* start count */ StartEvent = 0x1; /* Reinitialize Button_State variable */ Button_State = 0; } /* Right button is pressed */ else if (Button_State == RIGHT_ON) { uint16_t Colorx; if (BackupIndex < 5) { if((uint8_t)(BackupIndex% 2) != 0x0) { /* Set LCD backcolor*/ LCD_SetBackColor(Blue2); Colorx = White; } else { /* Set LCD backcolor*/ LCD_SetBackColor(Cyan); Colorx = Black; } SubSecFrac = 0; /* Get the Current sub second and time */ SubSecFrac = (((256 - (uint32_t)RTC_GetSubSecond()) * 1000) / 256); RTC_GetTime(RTC_Format_BIN, &RTC_StampTimeStruct); LCD_SetFont(&Font16x24); /* Display result on the LCD */ RTC_Time_Display( LINE(3 + BackupIndex), Colorx, RTC_Get_Time(SubSecFrac , &RTC_StampTimeStruct) ); if (BackupIndex < 2) { /* Save time register to Backup register ( the first 5 register is reserved for time) */ RTC_WriteBackupRegister(BKPDataReg[BackupIndex],(uint32_t)RTC->TR); /* Save sub second time stamp register ( the latest 6 register is reserved for time) */ RTC_WriteBackupRegister(BKPDataReg[BackupIndex + 2], SubSecFrac); } } else { /* the backup register is full with 10trials */ /* Set the LCD Back Color */ LCD_SetBackColor(White); LCD_SetFont(&Font12x12); /* Set the LCD Text Color */ LCD_SetTextColor(Red); LCD_DisplayStringLine(LINE(16), (uint8_t *)MESSAGE4); LCD_DisplayStringLine(LINE(17), (uint8_t *)MESSAGE5); } BackupIndex++; /* Set the LCD Back Color */ LCD_SetBackColor(White); /* Reinitialize Button_State variable */ Button_State = 0; } /* TAMPER button is pressed */ else if (Button_RTC == RTC_TAMP) { /* Set the LCD Back Color */ LCD_SetBackColor(White); LCD_SetFont(&Font16x24); /* Clear LCD line 5 to 9 */ for (i=0; i < 5; i++) { /* Clear all the LCD lignes from 3 to 7 */ LCD_ClearLine(LINE(3+i)); } /* reset Counter */ BackupIndex = 0 ; /* Enetr to idle */ StartEvent =0x0; LCD_SetFont(&Font12x12); RTC_Time_InitDisplay(); LCD_SetFont(&Font12x12); LCD_ClearLine(LINE(16)); LCD_ClearLine(LINE(17)); /* Enable Tamper interrupt */ RTC_ITConfig(RTC_IT_TAMP, DISABLE); /* Enabale the tamper 1 */ RTC_TamperCmd(RTC_Tamper_1 , DISABLE); /* Reinitialize Button_RTC variable */ Button_RTC = 0; } } }