您尚未登录。

楼主 #1 2020-05-12 15:25:48

BugActiveDaughter
会员
注册时间: 2017-10-17
已发帖子: 118
积分: 117.5

stm32 获取时间戳

stm32f407,从rtc的结构体转换为时间戳代码如下,注意包含头文件#include <time.h>

/**
  ******************************************************************************
  * @file    rtc.c
  * @author  yedasheng
  * @version V1.0
  * @date    20200512
  * @brief    rtc 
  ******************************************************************************
  * @attention
  *
  *
  ******************************************************************************
  */

/* Includes ------------------------------------------------------------------*/



#include <time.h>
#include "rtc.h"


//=====================================================

RTC_DateTypeDef Sdatestructure;
RTC_TimeTypeDef Stimestructure;

RTC_HandleTypeDef hrtc;

//==========================================================

extern uint32_t  ErrorCode;

extern SYSPARA   GSystemPara;  // ??????
//==============================
/* RTC init function */
void MX_RTC_Init(void)
{

    RTC_DateTypeDef Sdate;
    RTC_TimeTypeDef Stime;


    /* Enable Power Clock*/
    __HAL_RCC_PWR_CLK_ENABLE();

    /* Allow Access to RTC Backup domaine */
    HAL_PWR_EnableBkUpAccess();

    hrtc.Instance = RTC;

    /* Check if Pin Reset flag is set */
    if ( (__HAL_RCC_GET_FLAG(RCC_FLAG_IWDGRST) != RESET))
    {
        print(0, "\n reset \n ");
    }
    else
    {

        /**Initialize RTC and set the Time and Date
        */
        hrtc.Instance = RTC;
        hrtc.Init.HourFormat = RTC_HOURFORMAT_24;
        hrtc.Init.AsynchPrediv = RTC_ASYNCH_PREDIV;
        hrtc.Init.SynchPrediv = RTC_SYNCH_PREDIV;
        hrtc.Init.OutPut = RTC_OUTPUT_DISABLE;
        hrtc.Init.OutPutPolarity = RTC_OUTPUT_POLARITY_HIGH;
        hrtc.Init.OutPutType = RTC_OUTPUT_TYPE_OPENDRAIN;

        if (HAL_RTC_Init(&hrtc) != HAL_OK)
        {
            /* Initialization Error */
            ErrorCode |= ERRORMASK_RTC;
            print(0, "\r\nHAL_RTC_Init Fail  hrtc.State=%u\n", hrtc.State );
        }

        /*##-2- Check if Data stored in BackUp register0: No Need to reconfigure RTC#*/
        /* Read the Back Up Register 0 Data */
        if (HAL_RTCEx_BKUPRead(&hrtc, RTC_BKP_DR0) != 0x32F2)

        {
            /*##-1- Configure the Date #################################################*/
            /* Set Date: Tuesday February 18th 2015*/
            Sdate.Year = 16;
            Sdate.Month = RTC_MONTH_MARCH;
            Sdate.Date = 2;
            Sdate.WeekDay = RTC_WEEKDAY_WEDNESDAY;

            /*##-2- Configure the Time #################################################*/
            /* Set Time: 02:00:00 */
            Stime.Hours = 14;
            Stime.Minutes = 17;
            Stime.Seconds = 41;
            Stime.TimeFormat = RTC_HOURFORMAT12_PM;
            Stime.DayLightSaving = RTC_DAYLIGHTSAVING_NONE ;
            Stime.StoreOperation = RTC_STOREOPERATION_RESET;
            /* Configure RTC Calendar */
            RTC_CalendarConfig(Sdate, Stime, RTC_FORMAT_BIN);

            ErrorCode |= ERRORMASK_RTC;
        }

        else
        {
            /* Check if the Power On Reset flag is set */
            if (__HAL_RCC_GET_FLAG(RCC_FLAG_PORRST) != RESET)
            {

            }
            /* Check if Pin Reset flag is set */
            if (__HAL_RCC_GET_FLAG(RCC_FLAG_PINRST) != RESET)
            {

            }

        }

    }
    /* Clear source Reset Flag */
    __HAL_RCC_CLEAR_RESET_FLAGS();
}


/**
  * @brief  Configure the current time and date.
  * @param  None
  * @retval None
  */
void RTC_CalendarConfig( RTC_DateTypeDef sdatestructure, RTC_TimeTypeDef  stimestructure, uint32_t Format)
{


    if(HAL_RTC_SetDate(&hrtc, &sdatestructure, Format) != HAL_OK)
    {
        /* Initialization Error */
        ErrorCode |= ERRORMASK_RTC;
        print(0, "\r\nHAL_RTC_SetDate Fail");
    }


    if (HAL_RTC_SetTime(&hrtc, &stimestructure, Format) != HAL_OK)
    {
        /* Initialization Error */
        ErrorCode |= ERRORMASK_RTC;
        print(0, "\r\nHAL_RTC_SetTime Fail");
    }

    /*##-3- Writes a data in a RTC Backup data Register0 #######################*/
    HAL_RTCEx_BKUPWrite(&hrtc, RTC_BKP_DR0, 0x32F2);
}


/**
  * @brief  Display the current time and date.
  * @retval None
  */
void RTC_CalendarShow(void)
{

    /* Get the RTC current Time */
    HAL_RTC_GetTime(&hrtc, &Stimestructure, RTC_FORMAT_BIN);
    /* Get the RTC current Date */
    HAL_RTC_GetDate(&hrtc, &Sdatestructure, RTC_FORMAT_BIN);
    /* Display time Format : hh:mm:ss */
    //print(0, "\r\n%02u-%02u-%02u   %02u:%02u:%02u",  Sdatestructure.Year, Sdatestructure.Month, Sdatestructure.Date, Stimestructure.Hours, Stimestructure.Minutes, Stimestructure.Seconds);
    //  print(0, "  %02u:%02u:%02u\n", Stimestructure.Hours, Stimestructure.Minutes, Stimestructure.Seconds);


}
/*******************************************************************************
* Function Name  : Time_ConvCalendarToUnix(struct tm t)
* Description    : 写入RTC时钟当前时间
* Input 		 : struct tm t
* Output		 : None
* Return		 : time_t
*******************************************************************************/
uint32_t Time_ConvCalendarToUnix(void)
{
    struct tm 	 time_now;
    uint32_t  now;
    // t.tm_year -= 1900;  //外部tm结构体存储的年份为2008格式
    //而time.h中定义的年份格式为1900年开始的年份
    //所以,在日期转换时要考虑到这个因素。

    time_now.tm_year=Sdatestructure.Year+2000-1900;
    time_now.tm_mon=Sdatestructure.Month-1;
    time_now.tm_mday=Sdatestructure.Date;
    time_now.tm_wday= Sdatestructure.WeekDay-1;
    time_now.tm_hour=Stimestructure.Hours;
    time_now.tm_min=Stimestructure.Minutes;
    time_now.tm_sec=Stimestructure.Seconds;

    now= mktime(&time_now)-8*60*60;  /* 标准时间戳和北京时间差值 8小时 */
    print(0,"time:%04u-%02u-%02u   %02u:%02u:%02u",  Sdatestructure.Year + 2000, Sdatestructure.Month, Sdatestructure.Date, Stimestructure.Hours, Stimestructure.Minutes, Stimestructure.Seconds);
    print(0,"uinix: %u\n", now);
    return  now;
}

离线

#2 2020-05-29 11:25:06

lzh666
会员
注册时间: 2020-05-29
已发帖子: 7
积分: 7

Re: stm32 获取时间戳

这个有用,输出日志的时候可以使用这个,很方便

离线

页脚

工信部备案:粤ICP备20025096号 Powered by FluxBB

感谢为中文互联网持续输出优质内容的各位老铁们。 QQ: 516333132, 微信(wechat): whycan_cn (哇酷网/挖坑网/填坑网) service@whycan.cn