/** * @brief This function handles External line 0 interrupt request. * @param None * @retval None */ void EXTI0_IRQHandler(void) { if(EXTI_GetITStatus(WAKEUP_BUTTON_EXTI_LINE) != RESET) { /* Display the current alarm */ RTC_AlarmShow(); /* Clear the Wakeup Button EXTI line pending bit */ EXTI_ClearITPendingBit(WAKEUP_BUTTON_EXTI_LINE); } }
/** * @brief This function handles External line 9 to 5 interrupt request. * @param None * @retval None */ void EXTI9_5_IRQHandler(void) { if(EXTI_GetITStatus(KEY_BUTTON_EXTI_LINE) != RESET) { /* Display the current time on the Hyperterminal */ RTC_TimeShow(); /* Clear the Key Button EXTI line pending bit */ EXTI_ClearITPendingBit(KEY_BUTTON_EXTI_LINE); } if(EXTI_GetITStatus(UP_BUTTON_EXTI_LINE) != RESET) { /* Display the current alarm on the Hyperterminal */ RTC_AlarmShow(); /* Clear the UP Button EXTI line pending bit */ EXTI_ClearITPendingBit(UP_BUTTON_EXTI_LINE); } }
void RTC_Configuration(void) { EXTI_InitTypeDef EXTI_InitStructure; NVIC_InitTypeDef NVIC_InitStructure; /* If this is first time turn on the board */ if (RTC_ReadBackupRegister(RTC_BKP_DR0) != 0x32F2) { /* RTC configuration */ RTC_Config(); /* Display the RTC Time and Alarm */ RTC_TimeShow(); RTC_AlarmShow(); } else { // If this is not the first time turn on the board /* Check if the Power On Reset flag is set */ if (RCC_GetFlagStatus(RCC_FLAG_PORRST) != RESET) { /* Power On Reset occurred */ printf("\r\n * Power On Reset occurred"); /* Check if the Pin Reset flag is set */ } else if (RCC_GetFlagStatus(RCC_FLAG_PINRST) != RESET) { /* External Reset occurred */ printf("\r\n * External Reset occurred"); } /* Enable the PWR clock */ RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE); /* Allow access to RTC */ PWR_BackupAccessCmd(ENABLE); /* Wait for RTC APB registers synchronisation */ RTC_WaitForSynchro(); /* Clear the RTC Alarm Flag */ RTC_ClearFlag(RTC_FLAG_ALRAF); /* Clear the EXTI Line 17 Pending bit (Connected internally to RTC Alarm) */ EXTI_ClearITPendingBit(EXTI_Line17); /* Display the RTC Time and Alarm */ RTC_TimeShow(); RTC_AlarmShow(); } /* RTC Alarm A Interrupt Configuration */ /* EXTI configuration *********************************************************/ EXTI_ClearITPendingBit(EXTI_Line17); EXTI_InitStructure.EXTI_Line = EXTI_Line17; EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt; EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising; EXTI_InitStructure.EXTI_LineCmd = ENABLE; EXTI_Init(&EXTI_InitStructure); 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); /* Enable the RTC Wakeup Interrupt */ NVIC_InitStructure.NVIC_IRQChannel = RTC_WKUP_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); /* Enable the RTC Alarm Interrupt */ NVIC_InitStructure.NVIC_IRQChannel = RTC_Alarm_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); }
/** * @brief Main program. * @param None * @retval None */ void RTC_Calendar_Example(void) { /*!< At this stage the microcontroller clock setting is already configured, this is done through SystemInit() function which is called from startup file (startup_stm32f30x.s) before to branch to application main. To reconfigure the default setting of SystemInit() function, refer to system_stm32f30x.c file */ NVIC_InitTypeDef NVIC_InitStructure; EXTI_InitTypeDef EXTI_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; STM_EVAL_COMInit(COM1, &USART_InitStructure); /* Output a message on Hyperterminal using printf function */ printf("\n\r *********************** RTC Hardware Calendar Example ***********************\n\r"); #if defined (RTC_CLOCK_SOURCE_LSI) RCC_LSICmd(ENABLE); #endif if (RTC_ReadBackupRegister(RTC_BKP_DR0) != 0x32F2) { /* RTC configuration */ RTC_Config(); /* Configure the RTC data register and RTC prescaler */ RTC_InitStructure.RTC_AsynchPrediv = AsynchPrediv; RTC_InitStructure.RTC_SynchPrediv = SynchPrediv; RTC_InitStructure.RTC_HourFormat = RTC_HourFormat_24; /* Check on RTC init */ if (RTC_Init(&RTC_InitStructure) == ERROR) { printf("\n\r /!\\***** RTC Prescaler Config failed ********/!\\ \n\r"); } /* Configure the time register */ RTC_TimeRegulate(); } else { /* Check if the Power On Reset flag is set */ if (RCC_GetFlagStatus(RCC_FLAG_PORRST) != RESET) { printf("\r\n Power On Reset occurred....\n\r"); } /* Check if the Pin Reset flag is set */ else if (RCC_GetFlagStatus(RCC_FLAG_PINRST) != RESET) { printf("\r\n External Reset occurred....\n\r"); } printf("\n\r No need to configure RTC....\n\r"); /* Enable the PWR clock */ RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE); /* Allow access to RTC */ PWR_BackupAccessCmd(ENABLE); /* Wait for RTC APB registers synchronisation */ RTC_WaitForSynchro(); /* Clear the RTC Alarm Flag */ RTC_ClearFlag(RTC_FLAG_ALRAF); /* Clear the EXTI Line 17 Pending bit (Connected internally to RTC Alarm) */ EXTI_ClearITPendingBit(EXTI_Line17); /* Display the RTC Time and Alarm */ RTC_TimeShow(); RTC_AlarmShow(); } /* Configure the external interrupt "KEY", "SEL" and "UP" buttons */ STM_EVAL_PBInit(BUTTON_KEY, BUTTON_MODE_EXTI); STM_EVAL_PBInit(BUTTON_SEL, BUTTON_MODE_EXTI); STM_EVAL_PBInit(BUTTON_UP, BUTTON_MODE_EXTI); /* Configure LEDs */ STM_EVAL_LEDInit(LED1); STM_EVAL_LEDInit(LED2); /* Turn LED2 ON */ STM_EVAL_LEDOn(LED2); /* RTC Alarm A Interrupt Configuration */ /* EXTI configuration *******************************************************/ EXTI_ClearITPendingBit(EXTI_Line17); EXTI_InitStructure.EXTI_Line = EXTI_Line17; EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt; EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising; EXTI_InitStructure.EXTI_LineCmd = ENABLE; EXTI_Init(&EXTI_InitStructure); /* Enable the RTC Alarm Interrupt */ NVIC_InitStructure.NVIC_IRQChannel = RTC_Alarm_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); /* Infinite loop */ while (1) { } }
/** * @brief Returns the time entered by user, using Hyperterminal. * @param None * @retval None */ void RTC_TimeRegulate(void) { uint32_t tmp_hh = 0xFF, tmp_mm = 0xFF, tmp_ss = 0xFF; printf("\n\r==============Time Settings=====================================\n\r"); RTC_TimeStructure.RTC_H12 = RTC_H12_AM; printf(" Please Set Hours:\n\r"); while (tmp_hh == 0xFF) { tmp_hh = USART_Scanf(23); RTC_TimeStructure.RTC_Hours = tmp_hh; } printf(" %u\n\r", tmp_hh); printf(" Please Set Minutes:\n\r"); while (tmp_mm == 0xFF) { tmp_mm = USART_Scanf(59); RTC_TimeStructure.RTC_Minutes = tmp_mm; } printf(" %0.2d\n\r", tmp_mm); printf(" Please Set Seconds:\n\r"); while (tmp_ss == 0xFF) { tmp_ss = USART_Scanf(59); RTC_TimeStructure.RTC_Seconds = tmp_ss; } printf(" %0.2d\n\r", tmp_ss); /* Configure the RTC time register */ if(RTC_SetTime(RTC_Format_BIN, &RTC_TimeStructure) == ERROR) { printf("\n\r>> !! RTC Set Time failed. !! <<\n\r"); } else { printf("\n\r>> !! RTC Set Time success. !! <<\n\r"); RTC_TimeShow(); /* Indicator for the RTC configuration */ RTC_WriteBackupRegister(RTC_BKP_DR0, 0x32F2); } tmp_hh = 0xFF; tmp_mm = 0xFF; tmp_ss = 0xFF; /* Disable the Alarm A */ RTC_AlarmCmd(RTC_Alarm_A, DISABLE); printf("\n\r==============Alarm A Settings=====================================\n\r"); RTC_AlarmStructure.RTC_AlarmTime.RTC_H12 = RTC_H12_AM; printf(" Please Set Alarm Hours:\n\r"); while (tmp_hh == 0xFF) { tmp_hh = USART_Scanf(23); RTC_AlarmStructure.RTC_AlarmTime.RTC_Hours = tmp_hh; } printf(" %0.2d\n\r", tmp_hh); printf(" Please Set Alarm Minutes:\n\r"); while (tmp_mm == 0xFF) { tmp_mm = USART_Scanf(59); RTC_AlarmStructure.RTC_AlarmTime.RTC_Minutes = tmp_mm; } printf(" %0.2d\n\r", tmp_mm); printf(" Please Set Alarm Seconds:\n\r"); while (tmp_ss == 0xFF) { tmp_ss = USART_Scanf(59); RTC_AlarmStructure.RTC_AlarmTime.RTC_Seconds = tmp_ss; } printf(" %0.2d", tmp_ss); /* Set the Alarm A */ RTC_AlarmStructure.RTC_AlarmDateWeekDay = RTC_Weekday_Monday; RTC_AlarmStructure.RTC_AlarmDateWeekDaySel = RTC_AlarmDateWeekDaySel_Date; RTC_AlarmStructure.RTC_AlarmMask = RTC_AlarmMask_DateWeekDay; /* Configure the RTC Alarm A register */ RTC_SetAlarm(RTC_Format_BIN, RTC_Alarm_A, &RTC_AlarmStructure); printf("\n\r>> !! RTC Set Alarm success. !! <<\n\r"); RTC_AlarmShow(); /* Enable the RTC Alarm A Interrupt */ RTC_ITConfig(RTC_IT_ALRA, ENABLE); /* Enable the alarm A */ RTC_AlarmCmd(RTC_Alarm_A, ENABLE); }
/** * @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 */ NVIC_InitTypeDef NVIC_InitStructure; EXTI_InitTypeDef EXTI_InitStructure; LEDGpio_Init(); EXTILine0_Config(); EXTILine6_Config(); uart_init(); /* Output a message on Hyperterminal using printf function */ printf("\n\r *********************** RTC Hardware Calendar Example ***********************\n\r"); if (RTC_ReadBackupRegister(RTC_BKP_DR0) != 0x32F2) { /* RTC configuration */ RTC_Config(); /* Configure the RTC data register and RTC prescaler */ RTC_InitStructure.RTC_AsynchPrediv = AsynchPrediv; RTC_InitStructure.RTC_SynchPrediv = SynchPrediv; RTC_InitStructure.RTC_HourFormat = RTC_HourFormat_24; /* Check on RTC init */ if (RTC_Init(&RTC_InitStructure) == ERROR) { printf("\n\r /!\\***** RTC Prescaler Config failed ********/!\\ \n\r"); } /* Configure the time register */ RTC_TimeRegulate(); } else { /* Check if the Power On Reset flag is set */ if (RCC_GetFlagStatus(RCC_FLAG_PORRST) != RESET) { printf("\r\n Power On Reset occurred....\n\r"); } /* Check if the Pin Reset flag is set */ else if (RCC_GetFlagStatus(RCC_FLAG_PINRST) != RESET) { printf("\r\n External Reset occurred....\n\r"); } printf("\n\r No need to configure RTC....\n\r"); /* Enable the PWR clock */ RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE); /* Allow access to RTC */ PWR_BackupAccessCmd(ENABLE); /* Wait for RTC APB registers synchronisation */ RTC_WaitForSynchro(); /* Clear the RTC Alarm Flag */ RTC_ClearFlag(RTC_FLAG_ALRAF); /* Clear the EXTI Line 17 Pending bit (Connected internally to RTC Alarm) */ EXTI_ClearITPendingBit(EXTI_Line17); /* Display the RTC Time and Alarm */ RTC_TimeShow(); RTC_AlarmShow(); } // /* Configure the external interrupt "WAKEUP", "KEY" and "TAMPER" buttons */ // STM_EVAL_PBInit(BUTTON_KEY, BUTTON_MODE_EXTI); // STM_EVAL_PBInit(BUTTON_TAMPER , BUTTON_MODE_EXTI); // STM_EVAL_PBInit(BUTTON_WAKEUP , BUTTON_MODE_EXTI); // // /* Configure LEDx */ // STM_EVAL_LEDInit(LED1); // STM_EVAL_LEDInit(LED2); // // /* Turn LED1 ON */ // STM_EVAL_LEDOn(LED2); /* RTC Alarm A Interrupt Configuration */ /* EXTI configuration *********************************************************/ EXTI_ClearITPendingBit(EXTI_Line17); EXTI_InitStructure.EXTI_Line = EXTI_Line17; EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt; EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising; EXTI_InitStructure.EXTI_LineCmd = ENABLE; EXTI_Init(&EXTI_InitStructure); /* Enable the RTC Alarm Interrupt */ NVIC_InitStructure.NVIC_IRQChannel = RTC_Alarm_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); while (1) { } }