/*设置系统时间*/ void SysSetTime(FUN_BLOCK **funBlock) { DIS_SING *sing_temp; GetSubFunBlock(&pfunSubBlock); sing_temp = (DIS_SING *)(pfunSubBlock->current_display); pfunSubBlock->data_type = ZINUM;//显示属性 RTC_ITConfig(RTC_IT_SEC, DISABLE); //关RTC中断,防止systmtime被修改 strcpy(sing_temp->y2 , "请输入时间"); /*取得VIRTUAL_NUM_LENGTH位数的箱门数*/ if(CANCEL == KeyDisplay(pfunSubBlock , 10)) { FreeMem(); return; } /*读取时间值*/ systmtime.tm_year = 2000 + (sing_temp->y3[0]-'0')*10 + sing_temp->y3[1]-'0'; systmtime.tm_mon = (sing_temp->y3[2]-'0')*10 + sing_temp->y3[3]-'0'; systmtime.tm_mday = (sing_temp->y3[4]-'0')*10 + sing_temp->y3[5]-'0'; systmtime.tm_hour = (sing_temp->y3[6]-'0')*10 + sing_temp->y3[7]-'0'; systmtime.tm_min = (sing_temp->y3[8]-'0')*10 + sing_temp->y3[9]-'0'; memset(sing_temp->y3 , 0 , 10); /*写入时间*/ RTC_Configuration(); Time_Adjust(&systmtime);//写入系统时间 BKP_WriteBackupRegister(BKP_DR1, 0xA5A5); strcpy(sing_temp->y2 , "系统时间修改成功"); DisplaySubBlock(pfunSubBlock); FreeMem(); }
/** * @brief Setting up the time by Serial USART1. * @param None * @retval None */ void RTC_SetTimeBySerial(void) { if (BKP_ReadBackupRegister(BKP_DR1) != 0xA5A5) { /* Backup data register value is not correct or not yet programmed (when the first time the program is executed) */ printf("\r\n * Begin RTC initialization"); /* RTC Configuration */ RTC_Configuration(); printf("\r\n * Please set calendar"); /* setup year, month, date in 4, 2, 2 digits each */ Date_Regulate(); printf("\r\n * Please set time"); /* Adjust time by values entered by the user on the hyperterminal */ Time_Adjust(); BKP_WriteBackupRegister(BKP_DR1, 0xA5A5); } else { /* Check if the Power On Reset flag is set */ if (RCC_GetFlagStatus(RCC_FLAG_PORRST) != RESET) { printf("\r\n * Power On Reset occurred...."); } /* Check if the Pin Reset flag is set */ else if (RCC_GetFlagStatus(RCC_FLAG_PINRST) != RESET) { printf("\r\n\n * External Reset occurred...."); } printf("\r\n * No need to configure RTC...."); /* Check whether we've written year or month or day value before */ if(BKP_ReadBackupRegister(BKP_DR2) == 0x0000 || BKP_ReadBackupRegister(BKP_DR3) == 0x0000) { /* setup year, month, date in 4, 2, 2 digits each */ Date_Regulate(); } else { uint16_t YY, MD; YY = BKP_ReadBackupRegister(BKP_DR2); MD = BKP_ReadBackupRegister(BKP_DR3); int month, day; if( (MD / 1000) % 10 == 0) { month = (MD / 100) % 10; } else { month = (MD / 1000) % 10 + (MD / 100) % 10 ; } if( (MD / 10) % 10 == 0 ) { day = MD % 10; } else { day = MD - (MD / 100) * 100; } printf("\r\n\n Previous written calendar data found !"); printf("\r\n Written values are as follows :"); printf("\r\n Year : %d, Month : %d, Day : %d", YY, month, day); printf("\r\n Above calendar datas will be used to set current calendar automatically\r\n"); TranslateIntoYear(YY); TranslateIntoMonth(month); TranslateIntoDay(day); } /* NVIC MUST BE CONFIGURED before branch into power on reset */ NVIC_InitTypeDef NVIC_InitStructure; EXTI_InitTypeDef EXTI_InitStructure; /* Configure one bit for preemption priority */ NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1); /* Enable the RTC Interrupt */ NVIC_InitStructure.NVIC_IRQChannel = RTC_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); NVIC_InitStructure.NVIC_IRQChannel = RTCAlarm_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0xFF; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); /* Configure EXTI Line17 (RTC Alarm)to generate an interrupt on rising edge */ EXTI_ClearITPendingBit(EXTI_Line17); EXTI_InitStructure.EXTI_Line = EXTI_Line17; EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising; EXTI_Init(&EXTI_InitStructure); /* Wait for RTC registers synchronization */ RTC_WaitForSynchro(); /* Wait until last write operation on RTC registers has finished */ RTC_WaitForLastTask(); /* Alarm in 3 second */ //RTC_SetAlarm(3); /* Wait until last write operation on RTC registers has finished */ RTC_WaitForLastTask(); /* Enable the RTC Second, RTC Alarm interrupt */ RTC_ITConfig(RTC_IT_SEC || RTC_IT_ALR, ENABLE); /* Wait until last write operation on RTC registers has finished */ RTC_WaitForLastTask(); } #ifdef RTCClockOutput_Enable /* Enable PWR and BKP clocks */ RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR | RCC_APB1Periph_BKP, ENABLE); /* Allow access to BKP Domain */ PWR_BackupAccessCmd(ENABLE); /* Disable the Tamper Pin */ BKP_TamperPinCmd(DISABLE); /* To output RTCCLK/64 on Tamper pin, the tamper functionality must be disabled */ /* Enable RTC Clock Output on Tamper Pin */ BKP_RTCOutputConfig(BKP_RTCOutputSource_Second); #endif /* Clear reset flags */ RCC_ClearFlag(); }
/** * @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_stm32f10x_xx.s) before to branch to application main. To reconfigure the default setting of SystemInit() function, refer to system_stm32f10x.c file */ /* Initialize LED1 mounted on STM3210X-EVAL board */ RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 | RCC_APB2Periph_GPIOA , ENABLE); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10 ; // USART 2 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOA, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9 ; // USART 2 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_OD; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOA, &GPIO_InitStructure); /* USARTx configured as follow: - BaudRate = 115200 baud - Word Length = 8 Bits - One Stop Bit - No parity - Hardware flow control disabled (RTS and CTS signals) - Receive and transmit enabled */ USART_InitStructure.USART_BaudRate = 115200; USART_InitStructure.USART_WordLength = USART_WordLength_8b; USART_InitStructure.USART_StopBits = USART_StopBits_1; USART_InitStructure.USART_Parity = USART_Parity_No; USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; USART_Init(USART1,&USART_InitStructure); USART_Cmd(USART1, ENABLE); /* NVIC configuration */ NVIC_Configuration(); if (BKP_ReadBackupRegister(BKP_DR1) != 0xA5A5) { /* Backup data register value is not correct or not yet programmed (when the first time the program is executed) */ printf("\r\n\n RTC not yet configured...."); /* RTC Configuration */ RTC_Configuration(); printf("\r\n RTC configured...."); /* Adjust time by values entered by the user on the hyperterminal */ Time_Adjust(); BKP_WriteBackupRegister(BKP_DR1, 0xA5A5); } else { /* Check if the Power On Reset flag is set */ if (RCC_GetFlagStatus(RCC_FLAG_PORRST) != RESET) { printf("\r\n\n Power On Reset occurred...."); } /* Check if the Pin Reset flag is set */ else if (RCC_GetFlagStatus(RCC_FLAG_PINRST) != RESET) { printf("\r\n\n External Reset occurred...."); } printf("\r\n No need to configure RTC...."); /* Wait for RTC registers synchronization */ RTC_WaitForSynchro(); /* Enable the RTC Second */ RTC_ITConfig(RTC_IT_SEC, ENABLE); /* Wait until last write operation on RTC registers has finished */ RTC_WaitForLastTask(); } #ifdef RTCClockOutput_Enable /* Enable PWR and BKP clocks */ RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR | RCC_APB1Periph_BKP, ENABLE); /* Allow access to BKP Domain */ PWR_BackupAccessCmd(ENABLE); /* Disable the Tamper Pin */ BKP_TamperPinCmd(DISABLE); /* To output RTCCLK/64 on Tamper pin, the tamper functionality must be disabled */ /* Enable RTC Clock Output on Tamper Pin */ BKP_RTCOutputConfig(BKP_RTCOutputSource_CalibClock); #endif /* Clear reset flags */ RCC_ClearFlag(); /* Display time in infinite loop */ Time_Show(); }
/** * @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_stm32f2xx.s) before to branch to application main. To reconfigure the default setting of SystemInit() function, refer to system_stm32f2xx.c file */ NVIC_InitTypeDef NVIC_InitStructure; EXTI_InitTypeDef EXTI_InitStructure; /* Configure the external interrupt "WAKEUP", "KEY" and "TAMPER" buttons */ STM_EVAL_PBInit(BUTTON_KEY, BUTTON_MODE_GPIO); STM_EVAL_PBInit(BUTTON_TAMPER , BUTTON_MODE_GPIO); STM_EVAL_PBInit(BUTTON_WAKEUP , BUTTON_MODE_GPIO); /* Initialize the LCD */ STM322xG_LCD_Init(); /* Configure the LCD Log Module */ LCD_LOG_Init(); LCD_LOG_SetHeader("RTC Backup Domain Example"); LCD_LOG_SetFooter (" Copyright (c) STMicroelectronics" ); /* Display the default RCC BDCR and RTC TAFCR Registers */ LCD_UsrLog ("Entry Point \n"); LCD_UsrLog ("RCC BDCR = 0x%x\n", RCC->BDCR); LCD_UsrLog ("RTC TAFCR = 0x%x\n", RTC->TAFCR); /* Enable the PWR APB1 Clock Interface */ RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE); /* Allow access to BKP Domain */ PWR_BackupAccessCmd(ENABLE); /* Configure one bit for preemption priority */ NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1); /* Enable the RTC Interrupt */ NVIC_InitStructure.NVIC_IRQChannel = RTC_WKUP_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); /* EXTI configuration *******************************************************/ EXTI_ClearITPendingBit(EXTI_Line22); EXTI_InitStructure.EXTI_Line = EXTI_Line22; EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt; EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising; EXTI_InitStructure.EXTI_LineCmd = ENABLE; EXTI_Init(&EXTI_InitStructure); if(RTC_ReadBackupRegister(RTC_BKP_DR0) != FIRST_DATA) { LCD_UsrLog ("RTC Config PLZ Wait. \n"); /* RTC Configuration */ RTC_Config(); /* Adjust Current Time */ Time_Adjust(); /* Adjust Current Date */ Date_Adjust(); } else { /* Wait for RTC APB registers synchronisation */ RTC_WaitForSynchro(); RTC_ClearITPendingBit(RTC_IT_WUT); EXTI_ClearITPendingBit(EXTI_Line22); /* Backup SRAM ***************************************************************/ /* Enable BKPSRAM Clock */ RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_BKPSRAM, ENABLE); /* Check the written Data */ for (i = 0x0; i < 0x1000; i += 4) { if ((*(__IO uint32_t *) (BKPSRAM_BASE + i)) != i) { errorindex++; } } if(errorindex) { LCD_ErrLog ("BKP SRAM Number of errors = %d\n", errorindex); } else { LCD_UsrLog ("BKP SRAM Content OK \n"); } /* RTC Backup Data Registers **************************************************/ /* Check if RTC Backup DRx registers data are correct */ if (CheckBackupReg(FIRST_DATA) == 0x00) { /* OK, RTC Backup DRx registers data are correct */ LCD_UsrLog ("OK, RTC Backup DRx registers data are correct. \n"); } else { /* Error, RTC Backup DRx registers data are not correct */ LCD_ErrLog ("RTC Backup DRx registers data are not correct\n"); } } /* Infinite loop */ Calendar_Show(); while (1) { } }
/**************************************************************/ //程 序 名: RTC_Init //开 发 者: MingH //入口参数: 无 //功能说明: RTC 相关寄存器配置初始化, 外部调用 //**************************************************************/ void RTC_Init(void) { RTC_NVIC_Configuration(); // RTC 中断配置 if (BKP_ReadBackupRegister(BKP_DR1) != 0xA5A5) { /* Backup data register value is not correct or not yet programmed (when the first time the program is executed) */ printf("\r\n\n RTC not yet configured...."); /* RTC Configuration */ RTC_Configuration(); printf("\r\n RTC configured...."); /* Adjust time by values entered by the user on the hyperterminal */ Time_Adjust(); BKP_WriteBackupRegister(BKP_DR1, 0xA5A5); } else { /* Check if the Power On Reset flag is set */ if (RCC_GetFlagStatus(RCC_FLAG_PORRST) != RESET) { printf("\r\n\n Power On Reset occurred...."); } /* Check if the Pin Reset flag is set */ else if (RCC_GetFlagStatus(RCC_FLAG_PINRST) != RESET) { printf("\r\n\n External Reset occurred...."); } printf("\r\n No need to configure RTC...."); /* Wait for RTC registers synchronization */ RTC_WaitForSynchro(); /* Enable the RTC Second */ RTC_ITConfig(RTC_IT_SEC, ENABLE); /* Wait until last write operation on RTC registers has finished */ RTC_WaitForLastTask(); } #ifdef RTCClockOutput_Enable /* Enable PWR and BKP clocks */ RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR | RCC_APB1Periph_BKP, ENABLE); /* Allow access to BKP Domain */ PWR_BackupAccessCmd(ENABLE); /* Disable the Tamper Pin */ BKP_TamperPinCmd(DISABLE); /* To output RTCCLK/64 on Tamper pin, the tamper functionality must be disabled */ /* Enable RTC Clock Output on Tamper Pin */ BKP_RTCOutputConfig(BKP_RTCOutputSource_CalibClock); #endif /* Clear reset flags */ RCC_ClearFlag(); }
/* * 函数名:RTC_CheckAndConfig * 描述 :检查并配置RTC * 输入 :用于读取RTC时间的结构体指针 * 输出 :无 * 调用 :外部调用 */ void RTC_CheckAndConfig(struct rtc_time *tm) { /*在启动时检查备份寄存器BKP_DR1,如果内容不是0xA5A5, 则需重新配置时间并询问用户调整时间*/ if (BKP_ReadBackupRegister(BKP_DR1) != 0xA5A5) { printf("\r\n\r\n RTC not yet configured...."); /* RTC Configuration */ RTC_Configuration(); printf("\r\n\r\n RTC configured...."); /* Adjust time by users typed on the hyperterminal */ Time_Adjust(tm); BKP_WriteBackupRegister(BKP_DR1, 0xA5A5); } else { /*启动无需设置新时钟*/ /*检查是否掉电重启*/ if (RCC_GetFlagStatus(RCC_FLAG_PORRST) != RESET) { printf("\r\n\r\n Power On Reset occurred...."); } /*检查是否Reset复位*/ else if (RCC_GetFlagStatus(RCC_FLAG_PINRST) != RESET) { printf("\r\n\r\n External Reset occurred...."); } printf("\r\n No need to configure RTC...."); /*等待寄存器同步*/ RTC_WaitForSynchro(); /*允许RTC秒中断*/ RTC_ITConfig(RTC_IT_SEC, ENABLE); /*等待上次RTC寄存器写操作完成*/ RTC_WaitForLastTask(); } /*定义了时钟输出宏,则配置校正时钟输出到PC13*/ #ifdef RTCClockOutput_Enable /* Enable PWR and BKP clocks */ RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR | RCC_APB1Periph_BKP, ENABLE); /* Allow access to BKP Domain */ PWR_BackupAccessCmd(ENABLE); /* Disable the Tamper Pin */ BKP_TamperPinCmd(DISABLE); /* To output RTCCLK/64 on Tamper pin, the tamper functionality must be disabled */ /* Enable RTC Clock Output on Tamper Pin */ BKP_RTCOutputConfig(BKP_RTCOutputSource_CalibClock); #endif /* Clear reset flags */ RCC_ClearFlag(); }