STM32CUBE+RTC万年历,注意事项

STM32CUBE+RTC万年历

配置过程参照微雪电子的基本可以实现日历走,但是微雪电子并没有给出怎么设置时间,
当然找到HAL_RTC_SetDate和HAL_RTC_SetTime这两个函数并不难
但是我在使用这两个函数的时候发现年份月份等会设置不正确,出现想不到的现象,现就这两个函数在使用时需要注意的地方进行说明:
这两个函数的第三个参数RTC_FORMAT_BIN 和 RTC_FORMAT_BCD的意思分别是

RTC_FORMAT_BIN 使用十进制 例如 18年你得到到是 18年
RTC_FORMAT_BCD 使用16进制 例如 18年你得到的是 0x18年

在这里我使用的是RTC_FORMAT_BIN

/*
*Rtc时间设置
*/
void rtc_Set(struct rtcStdType Rtcval)
{
    RTC_DateTypeDef stdatestructure;
    RTC_TimeTypeDef sttimestructure;
    s_Rtc = Rtcval;

    //下面三行需要加上
    stdatestructure.WeekDay = 0x01;
    sttimestructure.DayLightSaving = RTC_DAYLIGHTSAVING_NONE;
    sttimestructure.StoreOperation = RTC_STOREOPERATION_RESET;
    //上面三行需要加上

    stdatestructure.Year    = Rtcval.year ;
    stdatestructure.Month   =  Rtcval.month ;
    stdatestructure.Date    = Rtcval.day    ;
    sttimestructure.Hours   =  Rtcval.hour ;
    sttimestructure.Minutes =  Rtcval.minute ;
    sttimestructure.Seconds =  Rtcval.second ;


    HAL_RTC_SetTime(&hrtc, &sttimestructure, RTC_FORMAT_BIN);
    HAL_RTC_SetDate(&hrtc, &stdatestructure, RTC_FORMAT_BIN);

}

你可能感兴趣的:(STM32CUBE+RTC万年历,注意事项)