stm32 RTC time 함수 구현

1633 단어
플랫폼: STM32F4 컴파일러:arm-none-eabi-gcc
  • 글로벌 변수 정의
  • time_t time_dat;
    
  • time() 함수 재정의
  • time_t time (time_t *_timer)
    {
        struct tm *ts;
        if(_timer != NULL)
        {
            time_dat = *_timer;
            ts = localtime (_timer);
            RTC_Set_Time(ts->tm_hour, ts->tm_min, ts->tm_sec);
            RTC_Set_Date(ts->tm_year+1900-2000, ts->tm_mon+1, ts->tm_mday);
        }
        return time_dat;
    }
    
  • 초 인터럽트 중 타임 누적dat WakeUp 인터럽트 리셋 함수에 누적
  • void HAL_RTCEx_WakeUpTimerEventCallback(RTC_HandleTypeDef *hrtc)
    {
        time_dat ++;
    }
    
  • 읽기 RTC 초기화 timedate 주의해야 할 것은 tm 구조체 중 월 tmmon은 0에서 시작, tm예아는 1900년 이후 연수다.
  • void timeInit()
    {
        time_t settime;
        struct tm orig;
        RTC_TimeTypeDef RTC_TimeStruct;
        RTC_DateTypeDef RTC_DateStruct;
    
        HAL_RTC_GetTime(&RTC_Handler, &RTC_TimeStruct, RTC_FORMAT_BIN);
        HAL_RTC_GetDate(&RTC_Handler, &RTC_DateStruct, RTC_FORMAT_BIN);
    
        orig.tm_sec = RTC_TimeStruct.Seconds;
        orig.tm_min = RTC_TimeStruct.Minutes;
        orig.tm_hour = RTC_TimeStruct.Hours;
        orig.tm_mday = RTC_DateStruct.Date;
        orig.tm_mon = RTC_DateStruct.Month-1;
        orig.tm_year = 2000 + RTC_DateStruct.Year - 1900;
        orig.tm_isdst = -1;
        settime = mktime (&orig);
        time(&settime);
    }
    
  • time
  • 사용
        time_t now;
        struct tm *ts;
        char buf [80];
    
        now = time (NULL);
        ts = localtime (& now);
        strftime (buf, sizeof (buf), "%a %Y-%m-%d %H:%M:%S %Z", ts);
        printf_safe("%s 
    ", buf);

    좋은 웹페이지 즐겨찾기