HAL库STM32L151 万年历

1、设置好RTC万年历后,睡眠唤醒不会影响时间;


//set time and date: year,        month,        date,        hours,        minutes,        seconds,


void setTimeandDate(uint16_t year_t, uint8_t month_t, uint8_t date_t, 
                                        uint8_t hours_t, uint8_t minutes_t, uint8_t seconds_t)
{
    
    //yeas high    (bin)  
    //eg1: 2019 -> yeasHigh = 20
    //eg2: 2345 -> yeasHigh = 23
    static uint8_t yeasHigh =0;        
    
  RTC_TimeTypeDef sTime = {0};
  RTC_DateTypeDef sDate = {0};

    //ÏÈ»ñȡʱ¼äºóÔÚ»ñÈ¡ÈÕÆÚ£¬hal¿âµÄbug ÍøÉÏ˵µÄ
    //first : get Time,
    //Second: get Date,
    
    if((hours_t >= 0) && (hours_t < 24))
    {
        sTime.Hours = hours_t;
    }
    if((minutes_t >= 0) && (minutes_t < 60))
    {
        sTime.Minutes = minutes_t;
    }
    if((seconds_t >= 0) && (seconds_t < 60))
    {
        sTime.Seconds = seconds_t;
    }
    
  sTime.DayLightSaving = RTC_DAYLIGHTSAVING_NONE;
  sTime.StoreOperation = RTC_STOREOPERATION_RESET;
  HAL_RTC_SetTime(&hrtc, &sTime, RTC_FORMAT_BIN);

  
    if((month_t >= 0) && (month_t <= 12))
    {
        sDate.Month = month_t;
    }
    if((date_t >= 0) && (date_t <= 31))
    {
         sDate.Date = date_t;
    }
    if((year_t >=0) && (year_t < 9999))
    {
        sDate.Year = year_t % 100;
    }
    
    yeasHigh = year_t /100;                //year high bit                (BIN)
    //sDate.Year = year_t % 100;    //year low bit                (BIN)
    
    yeas_high = yeasHigh;
    
  HAL_RTC_SetDate(&hrtc, &sDate, RTC_FORMAT_BIN);
    
}

发布了30 篇原创文章 · 获赞 12 · 访问量 6136

猜你喜欢

转载自blog.csdn.net/qq_39758638/article/details/103385975