Files
2025-05-13 04:55:13 +03:00

401 lines
12 KiB
C

/**
******************************************************************************
* @file main.c
* @author MCD Application Team
* @version V1.0.0
* @date 18-April-2011
* @brief Main program body
******************************************************************************
* @attention
*
* THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
* WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
* TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY
* DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
* FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
* CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
*
* <h2><center>&copy; COPYRIGHT 2011 STMicroelectronics</center></h2>
******************************************************************************
*/
/* Includes ------------------------------------------------------------------*/
#include "stm32f4xx.h"
#include "usart.h"
#include <string.h>
#include <stdio.h>
/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Uncomment the corresponding line to select the RTC Clock source */
#define RTC_CLOCK_SOURCE_LSE /* LSE used as RTC source clock */
/* #define RTC_CLOCK_SOURCE_LSI */ /* LSI used as RTC source clock. The RTC Clock
may varies due to LSI frequency dispersion. */
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
RTC_InitTypeDef RTC_InitStructure;
RTC_TimeTypeDef RTC_TimeStructure;
RTC_DateTypeDef RTC_DateStructure;
RTC_TimeTypeDef RTC_TimeStampStructure;
RTC_DateTypeDef RTC_TimeStampDateStructure;
__IO uint32_t AsynchPrediv = 0, SynchPrediv = 0;
/* Private function prototypes -----------------------------------------------*/
/* Private functions ---------------------------------------------------------*/
void RTC_Config(void);
void RTC_TimeRegulate(void);
void RTC_TimeShow(void);
void RTC_DateShow(void);
void RTC_TimeStampShow(void);
uint8_t USART_Scanf(uint32_t MinValue, uint32_t MaxValue);
/**
* @brief Main program
* @param None
* @retval None
*/
int main(void)
{
uint8_t Rtctmp=0;
USART_Configuration();
/* Output a message on Hyperterminal using printf function */
printf("\n\r *********************** RTC Time Stamp 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 ********/!\\ \r\n");
}
/* 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....\r\n");
}
/* Check if the Pin Reset flag is set */
else if (RCC_GetFlagStatus(RCC_FLAG_PINRST) != RESET)
{
printf("\r\n External Reset occurred....\r\n");
}
printf("\r\n No need to configure RTC....\r\n");
/* 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);
/* Display the RTC Time/Date and TimeStamp Time/Date */
RTC_TimeShow();
RTC_DateShow();
RTC_TimeStampShow();
}
while (1)
{
RTC_GetTime(RTC_Format_BIN, &RTC_TimeStructure);
RTC_GetDate(RTC_Format_BIN, &RTC_DateStructure);
if(Rtctmp != RTC_TimeStructure.RTC_Seconds)
{
printf("The Date : Y:20%0.2d - M:%0.2d - D:%0.2d - W:%0.2d\r\n", RTC_DateStructure.RTC_Year,RTC_DateStructure.RTC_Month, RTC_DateStructure.RTC_Date,RTC_DateStructure.RTC_WeekDay);
printf("The Time : %0.2d:%0.2d:%0.2d \r\n\r\n", RTC_TimeStructure.RTC_Hours, RTC_TimeStructure.RTC_Minutes, RTC_TimeStructure.RTC_Seconds);
(void)RTC->DR;
}
Rtctmp = RTC_TimeStructure.RTC_Seconds;
}
}
/**
* @brief Configure the RTC peripheral by selecting the clock source.
* @param None
* @retval None
*/
void RTC_Config(void)
{
/* Enable the PWR clock */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE);
/* Allow access to RTC */
PWR_BackupAccessCmd(ENABLE);
#if defined (RTC_CLOCK_SOURCE_LSI) /* LSI used as RTC source clock*/
/* The RTC Clock may varies due to LSI frequency dispersion. */
/* Enable the LSI OSC */
RCC_LSICmd(ENABLE);
/* Wait till LSI is ready */
while(RCC_GetFlagStatus(RCC_FLAG_LSIRDY) == RESET)
{
}
/* Select the RTC Clock Source */
RCC_RTCCLKConfig(RCC_RTCCLKSource_LSI);
SynchPrediv = 0xFF;
AsynchPrediv = 0x7F;
#elif defined (RTC_CLOCK_SOURCE_LSE) /* LSE used as RTC source clock */
/* Enable the LSE OSC */
RCC_LSEConfig(RCC_LSE_ON);
/* Wait till LSE is ready */
while(RCC_GetFlagStatus(RCC_FLAG_LSERDY) == RESET)
{
}
/* Select the RTC Clock Source */
RCC_RTCCLKConfig(RCC_RTCCLKSource_LSE);
SynchPrediv = 0xFF;
AsynchPrediv = 0x7F;
#else
#error Please select the RTC Clock source inside the main.c file
#endif /* RTC_CLOCK_SOURCE_LSI */
/* Enable the RTC Clock */
RCC_RTCCLKCmd(ENABLE);
/* Wait for RTC APB registers synchronisation */
RTC_WaitForSynchro();
/* Enable The TimeStamp */
RTC_TimeStampCmd(RTC_TimeStampEdge_Falling, ENABLE);
}
/**
* @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("\r\n==============Time Settings=====================================\r\n");
RTC_TimeStructure.RTC_H12 = RTC_H12_AM;
printf("\r\n Please Set Hours");
while (tmp_hh == 0xFF)
{
tmp_hh = USART_Scanf(0, 23);
RTC_TimeStructure.RTC_Hours = tmp_hh;
}
printf(": %0.2d\n", tmp_hh);
printf("\r\n Please Set Minutes");
while (tmp_mm == 0xFF)
{
tmp_mm = USART_Scanf(0, 59);
RTC_TimeStructure.RTC_Minutes = tmp_mm;
}
printf(": %0.2d\n", tmp_mm);
printf("\r\n Please Set Seconds");
while (tmp_ss == 0xFF)
{
tmp_ss = USART_Scanf(0, 59);
RTC_TimeStructure.RTC_Seconds = tmp_ss;
}
printf(": %0.2d\n", tmp_ss);
/* Configure the RTC time register */
if(RTC_SetTime(RTC_Format_BIN, &RTC_TimeStructure) == ERROR)
{
printf("\r\n>> !! RTC Set Time failed. !! <<\r\n");
}
else
{
printf("\r\n>> !! RTC Set Time success. !! <<\r\n");
RTC_TimeShow();
/* Indicator for the RTC configuration */
RTC_WriteBackupRegister(RTC_BKP_DR0, 0x32F2);
}
tmp_hh = 0xFF;
tmp_mm = 0xFF;
tmp_ss = 0xFF;
printf("\r\n==============Date Settings=====================================\r\n");
printf("\r\n Please Set WeekDay (01-07)");
while (tmp_hh == 0xFF)
{
tmp_hh = USART_Scanf(1, 7);
RTC_DateStructure.RTC_WeekDay = tmp_hh;
}
printf(": %0.2d\n", tmp_hh);
tmp_hh = 0xFF;
printf("\r\n Please Set Date (01-31)");
while (tmp_hh == 0xFF)
{
tmp_hh = USART_Scanf(1, 31);
RTC_DateStructure.RTC_Date = tmp_hh;
}
printf(": %0.2d\n", tmp_hh);
printf("\r\n Please Set Month (01-12)");
while (tmp_mm == 0xFF)
{
tmp_mm = USART_Scanf(1, 12);
RTC_DateStructure.RTC_Month = tmp_mm;
}
printf(": %0.2d\n", tmp_mm);
printf("\r\n Please Set Year (00-99)");
while (tmp_ss == 0xFF)
{
tmp_ss = USART_Scanf(0, 99);
RTC_DateStructure.RTC_Year = tmp_ss;
}
printf(": %0.2d\n", tmp_ss);
/* Configure the RTC date register */
if(RTC_SetDate(RTC_Format_BIN, &RTC_DateStructure) == ERROR)
{
printf("\r\n>> !! RTC Set Date failed. !! <<\r\n");
}
else
{
printf("\r\n>> !! RTC Set Date success. !! <<\r\n");
RTC_DateShow();
/* Indicator for the RTC configuration */
RTC_WriteBackupRegister(RTC_BKP_DR0, 0x32F2);
}
}
/**
* @brief Display the current time on the Hyperterminal.
* @param None
* @retval None
*/
void RTC_TimeShow(void)
{
/* Get the current Time and Date */
RTC_GetTime(RTC_Format_BIN, &RTC_TimeStructure);
printf("\r\n\r\n============== Current Time Display ============================\n");
printf("\r\n The current time (Hour-Minute-Second) is : %0.2d:%0.2d:%0.2d \n", RTC_TimeStructure.RTC_Hours, RTC_TimeStructure.RTC_Minutes, RTC_TimeStructure.RTC_Seconds);
/* Unfreeze the RTC DR Register */
(void)RTC->DR;
}
/**
* @brief Display the current date on the Hyperterminal.
* @param None
* @retval None
*/
void RTC_DateShow(void)
{
/* Get the current Date */
RTC_GetDate(RTC_Format_BIN, &RTC_DateStructure);
printf("\r\n============== Current Date Display ============================\n");
printf("\r\n The current date (WeekDay-Date-Month-Year) is : %0.2d-%0.2d-%0.2d-%0.2d \n", RTC_DateStructure.RTC_WeekDay, RTC_DateStructure.RTC_Date, RTC_DateStructure.RTC_Month, RTC_DateStructure.RTC_Year);
}
/**
* @brief Display the current TimeStamp (time and date) on the Hyperterminal.
* @param None
* @retval None
*/
void RTC_TimeStampShow(void)
{
/* Get the current TimeStamp */
RTC_GetTimeStamp(RTC_Format_BIN, &RTC_TimeStampStructure, &RTC_TimeStampDateStructure);
printf("\r\n==============TimeStamp Display (Time and Date)=================\n");
printf("\r\n The current time stamp time (Hour-Minute-Second) is : %0.2d:%0.2d:%0.2d \n", RTC_TimeStampStructure.RTC_Hours, RTC_TimeStampStructure.RTC_Minutes, RTC_TimeStampStructure.RTC_Seconds);
printf("\r\n The current timestamp date (WeekDay-Date-Month) is : %0.2d-%0.2d-%0.2d \n", RTC_TimeStampDateStructure.RTC_WeekDay, RTC_TimeStampDateStructure.RTC_Date, RTC_TimeStampDateStructure.RTC_Month);
}
/**
* @brief Gets numeric values from the hyperterminal.
* @param MinValue: minimum value to be used.
* @param MaxValue: maximum value to be used.
* @retval None
*/
uint8_t USART_Scanf(uint32_t MinValue, uint32_t MaxValue)
{
uint32_t index = 0;
uint32_t tmp[2] = {0, 0};
while (index < 2)
{
/* Loop until RXNE = 1 */
while (USART_GetFlagStatus(Open_USART, USART_FLAG_RXNE) == RESET)
{}
tmp[index++] = (USART_ReceiveData(Open_USART));
if ((tmp[index - 1] < 0x30) || (tmp[index - 1] > 0x39))
{
printf("\r\n Please enter valid number between 0 and 9\n");
index--;
}
}
/* Calculate the Corresponding value */
index = (tmp[1] - 0x30) + ((tmp[0] - 0x30) * 10);
/* Checks the value */
if ((index < MinValue) || (index > MaxValue))
{
printf("\r\n Please enter valid number between %d and %d\n", MinValue, MaxValue);
return 0xFF;
}
return index;
}
#ifdef USE_FULL_ASSERT
/**
* @brief Reports the name of the source file and the source line number
* where the assert_param error has occurred.
* @param file: pointer to the source file name
* @param line: assert_param error line source number
* @retval None
*/
void assert_failed(uint8_t* file, uint32_t line)
{
/* User can add his own implementation to report the file name and line number,
ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
/* Infinite loop */
while (1)
{
}
}
#endif
/**
* @}
*/
/**
* @}
*/
/******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/