From 1702ce6ce430a66bb7af51644b91b7c196e719d9 Mon Sep 17 00:00:00 2001 From: Alexander Date: Wed, 29 Jun 2022 11:03:02 +0300 Subject: =?UTF-8?q?=D0=A1=D0=BE=D0=B7=D0=B4=D0=B0=D1=8E=20=D0=BD=D0=BE?= =?UTF-8?q?=D0=B2=D1=8B=D0=B9=20=D1=80=D0=B5=D0=BF=D0=BE=D0=B7=D0=B8=D1=82?= =?UTF-8?q?=D0=BE=D1=80=D0=B8=D0=B9.=20=D0=9F=D1=80=D0=BE=D0=B3=D1=80?= =?UTF-8?q?=D0=B0=D0=BC=D0=BC=D0=B0=20=D0=B4=D0=BB=D1=8F=20=D0=B2=D0=B5?= =?UTF-8?q?=D1=80=D1=81=D0=B8=D0=B8=20NixieClock=5Fv2.=20=D0=A0=D0=B0?= =?UTF-8?q?=D0=B1=D0=BE=D1=82=D0=B0=D0=B5=D1=82,=20=D1=87=D0=B0=D1=81?= =?UTF-8?q?=D1=8B=20=D1=82=D0=B8=D0=BA=D0=B0=D1=8E=D1=82.=20=D0=95=D1=81?= =?UTF-8?q?=D1=82=D1=8C=20=D0=BF=D1=80=D0=BE=D0=B1=D0=BB=D0=B5=D0=BC=D0=B0?= =?UTF-8?q?,=20=D1=87=D1=82=D0=BE=20=D1=81=D0=BA=D0=B0=D1=87=D0=B5=D1=82?= =?UTF-8?q?=20=D0=B2=D1=82=D0=BE=D1=80=D0=B0=D1=8F=20=D1=81=D0=B5=D0=BD?= =?UTF-8?q?=D0=BE=D1=81=D1=80=D0=BD=D0=B0=D1=8F=20=D0=BA=D0=BD=D0=BE=D0=BF?= =?UTF-8?q?=D0=BA=D0=B0=20(=D0=BE=D0=BD=D0=B0=20=D0=B2=20=D0=B4=D1=80?= =?UTF-8?q?=D1=83=D0=B3=D0=BE=D0=BC=20=D0=BA=D0=B0=D0=BD=D0=B0=D0=BB=D0=B5?= =?UTF-8?q?).=20=D0=9F=D0=BE=D1=8D=D1=82=D0=BE=D0=BC=D1=83=20=D0=BD=D0=B0?= =?UTF-8?q?=20=D0=BD=D0=B5=D0=B5=20=D1=81=D0=BE=D0=B1=D0=B8=D1=80=D0=B0?= =?UTF-8?q?=D1=8E=D1=81=D1=8C=20=D1=81=D0=B4=D0=B5=D0=BB=D0=B0=D1=82=D1=8C?= =?UTF-8?q?=20=D0=B0=D0=BD=D1=82=D0=B8=D0=B4=D1=80=D0=B5=D0=B1=D0=B5=D0=B7?= =?UTF-8?q?=D0=B3.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Libraries/Time/time.c | 176 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 176 insertions(+) create mode 100644 Libraries/Time/time.c (limited to 'Libraries/Time/time.c') diff --git a/Libraries/Time/time.c b/Libraries/Time/time.c new file mode 100644 index 0000000..ab5cbde --- /dev/null +++ b/Libraries/Time/time.c @@ -0,0 +1,176 @@ + +#include "time.h" +#include "stm32f0xx_conf.h" + +/* 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 + +#define BKP_VALUE 0x32F0 +/* Private macro -------------------------------------------------------------*/ +/* Private variables ---------------------------------------------------------*/ +__IO uint32_t AsynchPrediv = 0, SynchPrediv = 0; + +/* Private function prototypes -----------------------------------------------*/ +static void RTC_Config(void); +static void RTC_TimeRegulate(void); + + +// - калибровку RTC как делать? + + +// ---------------------------------------------------------------------------- +// +// ---------------------------------------------------------------------------- +void TimeInit (void) +{ + RTC_InitTypeDef RTC_InitStructure; + + if (RTC_ReadBackupRegister(RTC_BKP_DR0) != BKP_VALUE) + //if (SynchPrediv == 0) + { + /* 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); + + #ifdef RTC_CLOCK_SOURCE_LSI + /* Enable the LSI OSC */ + RCC_LSICmd(ENABLE); + #endif /* RTC_CLOCK_SOURCE_LSI */ + + /* 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(); + } +} + + +// ---------------------------------------------------------------------------- +// +// ---------------------------------------------------------------------------- +static 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 = 0x18F; + AsynchPrediv = 0x63; + +#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(); +} + + +// ---------------------------------------------------------------------------- +// +// ---------------------------------------------------------------------------- +static void RTC_TimeRegulate ( void ) +{ + RTC_TimeTypeDef RTC_TimeStructure; + + RTC_TimeStructure.RTC_Hours = 14; + RTC_TimeStructure.RTC_Minutes = 01; + RTC_TimeStructure.RTC_Seconds = 00; + + RTC_SetTime(RTC_Format_BIN, &RTC_TimeStructure); + /* Indicator for the RTC configuration */ + RTC_WriteBackupRegister(RTC_BKP_DR0, BKP_VALUE); +} + + + + + + + + + + + + + + + + -- cgit v1.2.3