STM32 RTC calendar program

Publisher:钱币之歌Latest update time:2016-10-10 Source: eefocusKeywords:STM32  RTC Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
The RTC of STM32 is just a 32-bit counter, without information such as year, month, day, and week, which is far inferior to the dedicated RTC chip. To realize the time and date function, you need to implement it in a program. I remember that the time under Linux is also a 32-bit counter. After checking, it turns out that many experts on the Internet have already implemented it. And it is very simple to implement it using this method. There is no need to consider leap years, big and small months, etc. The key function is mktime, a standard function library function.
 
The following program is written by "jjldc (九九)"
 
rtc_time.h

#ifndef _RTC_TIME_H_
#define _RTC_TIME_H_
#include

extern struct tm Time_ConvUnixToCalendar(time_t t);
extern time_t Time_ConvCalendarToUnix(struct tm t);
extern time_t Time_GetUnixTime(void);
extern struct tm Time_GetCalendarTime(void);
extern void Time_SetUnixTime(time_t);
extern void Time_SetCalendarTime(struct tm t);

#endif

 
rtc_time.c

/*******************************************************************************
* This file implements the date function based on RTC, providing reading and writing of year, month and day. (Based on ANSI-C's time.h)
*
* Author: jjldc (九九)
* QQ: 77058617
*
* The time format saved in RTC is the UNIX timestamp format. That is, a 32-bit time_t variable (actually u32)
*
* The ANSI-C standard library provides two data types to represent time:
* time_t: UNIX timestamp (the number of seconds since 1970-1-1 to a certain time)
* typedef unsigned int time_t;
*
* struct tm: Calendar format (year-month-day format)
* The tm structure is as follows:
* struct tm {
* int tm_sec; // seconds after the minute, 0 to 60
* (0 - 60 allows for the occasional leap second)
* int tm_min; // minutes after the hour, 0 to 59
* int tm_hour; // hours since midnight, 0 to 23
* int tm_mday; // day of the month, 1 to 31
* int tm_mon; // months since January, 0 to 11
* int tm_year; // years since 1900
* int tm_wday; // days since Sunday, 0 to 6
* int tm_yday; // days since January 1, 0 to 365
* int tm_isdst; // Daylight Savings Time flag
* ...
* }
* wday and yday can be automatically generated, and the software can directly read
* the value of mon is 0-11
* ***Note***:
* tm_year: defined in the time.h library as the year since 1900, that is, 2008 should be expressed as 2008-1900=108
* This representation method is not very user-friendly and is quite different from reality.
* Therefore, this difference is blocked in this document.
* That is, when the functions of this file are called externally, the date of the tm structure type, tm_year is 2008
* Note: If you want to call the functions in the system library time.c, you need to set tm_year-=1900 yourself
*
* Member function description:
* struct tm Time_ConvUnixToCalendar(time_t t);
* Input a Unix timestamp (time_t) and return a date in Calendar format
* time_t Time_ConvCalendarToUnix(struct tm t);
* Input a date in Calendar format and return a Unix timestamp (time_t)
* time_t Time_GetUnixTime(void);
* Get the Unix timestamp value of the current time from the RTC
* struct tm Time_GetCalendarTime(void);
* Get the calendar time of the current time from the RTC
* void Time_SetUnixTime(time_t);
* Input a time in UNIX timestamp format and set it to the current RTC time
* void Time_SetCalendarTime(struct tm t);
* Enter the time in Calendar format and set it to the current RTC time
*
* External call example:
* Define a date variable in Calendar format:
* struct tm now;
* now.tm_year = 2008;
* now.tm_mon = 11; //December
* now.tm_mday = 20;
* now.tm_hour = 20;
* now.tm_min = 12;
* now.tm_sec = 30;
*
* Get the current date and time:
* tm_now = Time_GetCalendarTime();
* Then you can directly read tm_now.tm_wday to get the week number
*
* Set the time:
* Step1. tm_now.xxx = xxxxxxxxx;
* Step2. Time_SetCalendarTime(tm_now);
*
* Calculate the difference between two times
* struct tm t1,t2;
* t1_t = Time_ConvCalendarToUnix(t1);
* t2_t = Time_ConvCalendarToUnix(t2);
* dt = t1_t - t2_t;
* dt is the number of seconds between the two times
void SetTime(u32 t); /* Private functions ---------------------------------------------------------*/ /* Private define ------------------------------------------------------------*/ /* Private macro -------------------------------------------------------------*/ /* Private variables ---------------------------------------------------------*/
/* Private function prototypes ---------------------------------------------------------------*/ void SetTime(u32 t); /* Private functions --------------------------------------------------------- */ /* Private
define ------------------------------------------------------------*/ / * Private macro -------------------------------------------------------------*/ /* Private variables ---------------------------------------------------------*/ /* Private function prototypes ---------------------------------------------------------------*/ void SetTime(u32 t); /* Private functions ---------------------------------------------------------*/ /* Private define ------------------------------------------------------------*/ /* Private macro -------------------------------------------------------------*/ /* Private variables ------------------------------------------------------------- * / / * Private function prototypes --------------------------------------------------------------- */ Time_ConvUnixToCalendar(time_t t) * Description : Convert UNIX timestamp to calendar time * Input : u32 t current UNIX timestamp * Output : None * Return : struct tm ******************************************************************************/ struct tm Time_ConvUnixToCalendar(time_t t) { struct tm *t_tm; t_tm = localtime(&t); t_tm->tm_year += 1900; //the tm_year of the localtime conversion result is a relative value and needs to be converted to an absolute valuereturn *t_tm; } /*********************************************************************************** * Function Name : Time_ConvCalendarToUnix(struct tm t) * Description : Write the current time of the RTC clock * Input : struct tm t * Output : None * Return : time_t ******************************************************************************/ time_t Time_ConvCalendarToUnix(struct tm t) { t.tm_year -= 1900; //The year stored in the external tm structure is in 2008 format . //The year format defined in time.h is the year starting from 1900. //So, this factor should be taken into account when converting dates. return mktime(&t); } /*********************************************************************************** * Function Name : Time_GetUnixTime() * Description : Get the current Unix timestamp value from RTC * Input : None * Output : None * Return : time_t t *******************************************************************************/ time_t Time_GetUnixTime(void) { return (time_t)RTC_GetCounter(); } /*********************************************************************************** * Function Name : Time_GetCalendarTime() * Description : Get the current calendar time (struct tm) from RTC



























































* Input: None
* Output: None
* Return: time_t t
************************************* ******************************************/
struct tm Time_GetCalendarTime(void)
{
time_t t_t;
struct tm t_tm;

t_t = (time_t)RTC_GetCounter();
t_tm = Time_ConvUnixToCalendar(t_t);
return t_tm;
}

/********************** *************************************************** *******
* Function Name: Time_SetUnixTime()
* Description: Write the given Unix timestamp to RTC
* Input: time_t t
* Output: None
* Return: None
********** *************************************************** *******************/
void Time_SetUnixTime(time_t t)
{
PWR_BackupAccessCmd(ENABLE);
RTC_WaitForLastTask();
RTC_SetCounter((u32)t);
RTC_WaitForLastTask();
PWR_BackupAccessCmd(DISABLE);
return;
}

/************************* ************************************************** ****
* Function Name : Time_SetCalendarTime()
* Description : Convert the given Calendar format time into UNIX timestamp and write it to RTC
* Input : struct tm t
* Output : None
* Return : None
******* ************************************************** **********************/
void Time_SetCalendarTime(struct tm t)
{
Time_SetUnixTime(Time_ConvCalendarToUnix(t));
return;
}

 

Keywords:STM32  RTC Reference address:STM32 RTC calendar program

Previous article:USART sends and receives interrupts without intentional nesting
Next article:stm32 stack setting size causes error

Recommended ReadingLatest update time:2024-11-16 13:40

Uip transplantation in STM32 platform to establish UDP link
Data transmission is achieved by establishing a UDP connection on STM32. In the previous section, it was mentioned that the lightweight TCP/IP protocol stack Uip is used. To create a UDP connection in the Uip protocol, the following steps are required: The first step is to open the configuration items that support the
[Microcontroller]
STM32---Key learning
#include "stm32f10x.h"   GPIO_InitTypeDef GPIO_InitStructure;//声明GPIO_InitStructure   void LED_Init() {  RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE); //Turn on GPIOA clock   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //Push-pull output  GPIO_InitStructure.GPIO_S
[Microcontroller]
STM32 GPIO Application Notes
1                      The input and output pins of STM32 have the following 8 possible configurations: (4 inputs + 2 outputs + 2 multiplexed outputs) ①Floating  input_IN_FLOATING ②With  pull-up input_IPU    ③With  pull-down input_IPD                      ④Analog  input_AIN ⑤Open  drain output_OUT_OD         
[Microcontroller]
stm32 advanced timer relearning
I recently used timers in a project, so I decided to relearn them. I used them only for simple pwm generation and interrupt processing before, and never studied them in depth. Today, I took this opportunity to relearn advanced timers. Once you learn advanced timers, you will have no problem with basic timers. In genera
[Microcontroller]
stm32 advanced timer relearning
STM32 simulates I2C program
/*******************************************************************************   Test platform: STM32F103ZET6 minimum system *******************************************************************************/ static void i2cDelay() {     volatile int i = 7;     while (i)     i--; } // During the SCL high level period
[Microcontroller]
Summary of problems encountered in STM32 IAP upgrade
1.Serial communication problem 1. The TTL levels at both ends of the serial communication must be consistent. Depending on the selected chip, they should be either 3.3V or 5V. When the levels at both ends are inconsistent, data cannot be received. When there is no problem with the detection program, etc. but data ca
[Microcontroller]
Summary of problems encountered in STM32 IAP upgrade
stm32 FSMC-TFTLCD display
TFTLCD  The commonly used communication modes for TFT LCD screens are 6800 mode and 8080 mode. For TFT color screens, 8080 parallel port (abbreviated as 80 parallel port) mode is usually used. The read and write timing of 8080 mode is actually similar to that of LCD1602 or LCD12864. The 8080 interface has 5 basic c
[Microcontroller]
stm32 FSMC-TFTLCD display
LPC213x internal RTC power supply problem
The focus of everyone's question is: Can the LPC213x internal RTC work properly with a backup battery and an external crystal oscillator when the CPU is in power-down mode or the chip is not powered on? This article does not discuss how to charge the backup battery, which belongs to peripheral circuit design. //Clo
[Microcontroller]
Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号