久久久久久久999_99精品久久精品一区二区爱城_成人欧美一区二区三区在线播放_国产精品日本一区二区不卡视频_国产午夜视频_欧美精品在线观看免费

 找回密碼
 立即注冊

QQ登錄

只需一步,快速開始

搜索
查看: 1489|回復: 0
打印 上一主題 下一主題
收起左側

關于MCU rtc程序的理解問題

[復制鏈接]
回帖獎勵 3 黑幣 回復本帖可獲得 3 黑幣獎勵! 每人限 1 次
跳轉到指定樓層
樓主
ID:248659 發表于 2020-3-26 11:40 | 只看該作者 回帖獎勵 |倒序瀏覽 |閱讀模式
這里有一段程序,但是有一個函數我是死活沒有理解,希望大佬們能指點下,在開頭的代碼中,例程注釋了:* 設置時間:* Step1. tm_now.xxx = xxxxxxxxx;
* Step2. Time_SetCalendarTime(tm_now);
這樣一段代碼,但是這段代碼如何使用呢。。。。實在沒整明白,如果我寫成 now.tm_year = 2008;然后Time_SetCalendarTime(now);程序不報錯,但是運行會卡住,也不會顯示東西,整個人處于懵逼的狀態



/*******************************************************************************
* 本文件實現基于RTC的日期功能,提供年月日的讀寫。(基于ANSI-C的time.h)
*

* RTC中保存的時間格式,是UNIX時間戳格式的。即一個32bit的time_t變量(實為u32)
*
* ANSI-C的標準庫中,提供了兩種表示時間的數據  型:
* time_t:           UNIX時間戳(從1970-1-1起到某時間經過的秒數)
*         typedef unsigned int time_t;
*
* struct tm:        Calendar格式(年月日形式)
*   tm結構如下:
*   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,yday可以自動產生,軟件直接讀取
*         mon的取值為0-11
*        ***注意***:
*        tm_year:在time.h庫中定義為1900年起的年份,即2008年應表示為2008-1900=108
*         這種表示方法對用戶來說不是十分友好,與現實有較大差異。
*         所以在本文件中,屏蔽了這種差異。
*         即外部調用本文件的函數時,tm結構體類型的日期,tm_year即為2008
*         注意:若要調用系統庫time.c中的函數,需要自行將tm_year-=1900
*
* 成員函數說明:
* struct tm Time_ConvUnixToCalendar(time_t t);
*         輸入一個Unix時間戳(time_t),返回Calendar格式日期
* time_t Time_ConvCalendarToUnix(struct tm t);
*         輸入一個Calendar格式日期,返回Unix時間戳(time_t)
* time_t Time_GetUnixTime(void);
*         從RTC取當前時間的Unix時間戳值
* struct tm Time_GetCalendarTime(void);
*         從RTC取當前時間的日歷時間
* void Time_SetUnixTime(time_t);
*         輸入UNIX時間戳格式時間,設置為當前RTC時間
* void Time_SetCalendarTime(struct tm t);
*         輸入Calendar格式時間,設置為當前RTC時間
*
* 外部調用實例:
* 定義一個Calendar格式的日期變量:
* struct tm now;
* now.tm_year = 2008;
* now.tm_mon = 11;                //12月
* now.tm_mday = 20;
* now.tm_hour = 20;
* now.tm_min = 12;
* now.tm_sec = 30;
*
* 獲取當前日期時間:
* tm_now = Time_GetCalendarTime();
* 然后可以直接讀tm_now.tm_wday獲取星期數
*
* 設置時間:
* Step1. tm_now.xxx = xxxxxxxxx;
* Step2. Time_SetCalendarTime(tm_now);
*
* 計算兩個時間的差
* struct tm t1,t2;
* t1_t = Time_ConvCalendarToUnix(t1);
* t2_t = Time_ConvCalendarToUnix(t2);
* dt = t1_t - t2_t;
* dt就是兩個時間差的秒數
* dt_tm = mktime(dt);        //注意dt的年份匹配,ansi庫中函數為相對年份,注意超限
* 另可以參考相關資料,調用ansi-c庫的格式化輸出等功能,ctime,strftime等
*
*******************************************************************************/

/* Includes ------------------------------------------------------------------*/
#include "RTC_Time.h"
#include "oled.h"
/* Private define ------------------------------------------------------------*/
//#define RTCClockOutput_Enable  /* RTC Clock/64 is output on tamper pin(PC.13) */  

/* Private function prototypes -----------------------------------------------*/
void Time_Set(u32 t);

/*******************************************************************************
* Function Name  : Time_ConvUnixToCalendar
* Description    : 轉換UNIX時間戳為日歷時間
* Input          : - t: 當前時間的UNIX時間戳
* Output         : None
* Return         : struct tm
* Attention                 : None
*******************************************************************************/
struct tm Time_ConvUnixToCalendar(time_t t)
{
        struct tm *t_tm;
        t_tm = localtime(&t);
        t_tm->tm_year += 1900;        /* localtime轉換結果的tm_year是相對值,需要轉成絕對值 */
        return *t_tm;
}

/*******************************************************************************
* Function Name  : Time_ConvCalendarToUnix
* Description    : 寫入RTC時鐘當前時間
* Input          : - t: struct tm
* Output         : None
* Return         : time_t
* Attention                 : None
*******************************************************************************/
time_t Time_ConvCalendarToUnix(struct tm t)
{
        t.tm_year -= 1900;  /* 外部tm結構體存儲的年份為2008格式        */
                                                /* 而time.h中定義的年份格式為1900年開始的年份 */
                                                /* 所以,在日期轉換時要考慮到這個因素。*/
        return mktime(&t);
}


/*******************************************************************************
* Function Name  : Time_GetUnixTime
* Description    : 從RTC取當前時間的Unix時間戳值
* Input          : None
* Output         : None
* Return         : time_t
* Attention                 : None
*******************************************************************************/
time_t Time_GetUnixTime(void)
{
        return (time_t)RTC_GetCounter();
}

/*******************************************************************************
* Function Name  : Time_GetCalendarTime
* Description    : 從RTC取當前時間的日歷時間(struct tm)
* Input          : None
* Output         : None
* Return         : struct tm
* Attention                 : None
*******************************************************************************/
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    : 將給定的Unix時間戳寫入RTC
* Input          : - t: time_t
* Output         : None
* Return         : None
* Attention                 : None
*******************************************************************************/
void Time_SetUnixTime(time_t t)
{
        RTC_WaitForLastTask();
        RTC_SetCounter((u32)t);
        RTC_WaitForLastTask();
        return;
}

/*******************************************************************************
* Function Name  : Time_SetCalendarTime
* Description    : 將給定的Calendar格式時間轉換成UNIX時間戳寫入RTC
* Input          : - t: struct tm
* Output         : None
* Return         : None
* Attention                 : None
*******************************************************************************/
void Time_SetCalendarTime(struct tm t)
{
        Time_SetUnixTime(Time_ConvCalendarToUnix(t));
        return;
}

/*******************************************************************************
* Function Name  : NVIC_Configuration
* Description    : Configures the nested vectored interrupt controller.
* Input          : None
* Output         : None
* Return         : None
* Attention                 : None
*******************************************************************************/
static void NVIC_Configuration(void)
{
  NVIC_InitTypeDef NVIC_InitStructure;

  /* Configure one bit for preemption priority */
  NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);

  /* Enable the RTC Interrupt */
  NVIC_InitStructure.NVIC_IRQChannel = RTC_IRQn;
  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  NVIC_Init(&NVIC_InitStructure);
}

/*******************************************************************************
* Function Name  : NVIC_Configuration
* Description    : Configures the RTC.
* Input          : None
* Output         : None
* Return         : None
* Attention                 : None
*******************************************************************************/
static void RTC_Configuration(void)
{
  /* Enable PWR and BKP clocks */
  RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR | RCC_APB1Periph_BKP, ENABLE);

  /* Allow access to BKP Domain */
  PWR_BackupAccessCmd(ENABLE);

  /* Reset Backup Domain */
  BKP_DeInit();

  /* Enable LSE */
  RCC_LSEConfig(RCC_LSE_ON);
  /* Wait till LSE is ready */
  while (RCC_GetFlagStatus(RCC_FLAG_LSERDY) == RESET)
  {}

  /* Select LSE as RTC Clock Source */
  RCC_RTCCLKConfig(RCC_RTCCLKSource_LSE);

  /* Enable RTC Clock */
  RCC_RTCCLKCmd(ENABLE);

  /* Wait for RTC registers synchronization */
  RTC_WaitForSynchro();

  /* Wait until last write operation on RTC registers has finished */
  RTC_WaitForLastTask();

  /* Enable the RTC Second */
  RTC_ITConfig(RTC_IT_SEC, ENABLE);

  /* Wait until last write operation on RTC registers has finished */
  RTC_WaitForLastTask();

  /* Set RTC prescaler: set RTC period to 1sec */
  RTC_SetPrescaler(32767); /* RTC period = RTCCLK/RTC_PR = (32.768 KHz)/(32767+1) */

  /* Wait until last write operation on RTC registers has finished */
  RTC_WaitForLastTask();
}
/*******************************************************************************
* Function Name  : Time_Regulate
* Description    : None
* Input          : None
* Output         : None
* Return         : None
* Attention                 : None
*******************************************************************************/
void Time_Regulate(void)
{
  struct tm time;

  memset(&time, 0 , sizeof(time) );        /* 清空結構體 */

  printf("=======================RTC 時間日期設置==========================\r\n");
  printf("請輸入年份在1970到2037之間\r\n");
                time.tm_year = 2020;
         time.tm_mon= 2;
        time.tm_mday=26;
        time.tm_hour=01;
         time.tm_min =06;
        time.tm_sec =00;
  Time_SetCalendarTime(time);  
}

/*******************************************************************************
* Function Name  : RTC_Init
* Description    : RTC Initialization
* Input          : None
* Output         : None
* Return         : None
* Attention                 : None        
*******************************************************************************/
void RTC_Init(void)
{

  if (BKP_ReadBackupRegister(BKP_DR1) != 0xA5A5)
  {
    /* Backup data register value is not correct or not yet programmed (when
       the first time the program is executed) */

    printf("RTC 還沒有被設置....\r\n");

    /* RTC Configuration */
    RTC_Configuration();

        Time_Regulate();

        /* Adjust time by values entred by the user on the hyperterminal */

    printf("RTC 初始化....\r\n");

    BKP_WriteBackupRegister(BKP_DR1, 0xA5A5);
  }
  else
  {
    /* Check if the Power On Reset flag is set */
    if (RCC_GetFlagStatus(RCC_FLAG_PORRST) != RESET)
    {
      printf("發生一次電源上電重啟....\r\n");
    }
    /* Check if the Pin Reset flag is set */
    else if (RCC_GetFlagStatus(RCC_FLAG_PINRST) != RESET)
    {
      printf("發生一次外部復位....\r\n");
    }

    printf("不需要設置RCT....\r\n");
    /* Wait for RTC registers synchronization */
    RTC_WaitForSynchro();

    /* Enable the RTC Second */
    RTC_ITConfig(RTC_IT_SEC, ENABLE);
    /* Wait until last write operation on RTC registers has finished */
    RTC_WaitForLastTask();
  }

   /* NVIC configuration */
   NVIC_Configuration();

#ifdef RTCClockOutput_Enable
  /* Enable PWR and BKP clocks */
  RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR | RCC_APB1Periph_BKP, ENABLE);

  /* Allow access to BKP Domain */
  PWR_BackupAccessCmd(ENABLE);

  /* Disable the Tamper Pin */
  BKP_TamperPinCmd(DISABLE); /* To output RTCCLK/64 on Tamper pin, the tamper
                                 functionality must be disabled */

  /* Enable RTC Clock Output on Tamper Pin */
  BKP_RTCOutputConfig(BKP_RTCOutputSource_CalibClock);
#endif

   /* Clear reset flags */
  RCC_ClearFlag();
  return;
}

/*******************************************************************************
* Function Name  : Time_Display
* Description    : Printf Time
* Input          : None
* Output         : None
* Return         : None
* Attention                 : None
*******************************************************************************/
void Time_Display(void)
{
   struct tm time;
   time = Time_GetCalendarTime();
   printf("Time: %d-%d-%d   %02d:%02d:%02d \r\n", time.tm_year, \
                   time.tm_mon+1, time.tm_mday,\
                   time.tm_hour, time.tm_min, time.tm_sec);
}



分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏 分享淘帖 頂 踩
回復

使用道具 舉報

您需要登錄后才可以回帖 登錄 | 立即注冊

本版積分規則

小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術交流QQ群281945664

Powered by 單片機教程網

快速回復 返回頂部 返回列表
主站蜘蛛池模板: 亚洲欧美一区二区三区国产精品 | 成人影院一区二区三区 | 国产精品99久久久久久www | 精品国产一区二区三区久久久蜜月 | 亚洲高清久久 | 九九免费在线视频 | 玖玖国产精品视频 | 欧美啪啪网站 | 午夜三级网站 | 日韩在线视频免费观看 | 亚洲精品久久久久久国产精华液 | 午夜亚洲 | 亚洲精品在线视频 | 91在线电影 | 91av在线免费看 | 欧美精品在线观看 | 一区二区av| 国产成人精品a视频一区www | 日韩一区二区三区视频 | 亚洲成在线观看 | jav成人av免费播放 | 欧美精品一区在线 | 免费国产视频 | 国产免费一区二区 | 一区视频 | 91精品久久久久久久久 | 麻豆精品一区二区三区在线观看 | 欧洲av一区 | 国产免费一区二区三区 | 亚洲精品久久久 | 欧美日韩成人影院 | 免费小视频在线观看 | 亚洲成人一区二区在线 | 国产精品免费高清 | 国产一区二区三区四区在线观看 | 午夜小电影 | 免费视频二区 | 日韩三级在线观看 | 日韩精品一区二区三区视频播放 | 色五月激情五月 | 超碰97免费观看 |