SetDate将年份设置为错误的值

SetDate将年份设置为错误的值

本文介绍了HAL_SetDate将年份设置为错误的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将STM32F030RCT6与CubeMX一起使用.设备是数据记录器,而RTC是不会失败的主要事物.在勘误表中,有一些有关RTC影子寄存器的信息.

I'm using STM32F030RCT6 with CubeMX. Device is a datalogger and RTC is the main thing that cannot fail. On Errata Sheet there is something about RTC Shadow Register.

我将CubeMX配置为不生成MX_RTC_Init()函数,并且到目前为止它一直可以正常工作. (我正在使用LSE)

I configured CubeMX to not generate MX_RTC_Init() function and it has been working normally so far. (I'm using LSE)

我需要从GSM时间更新时间/日期,但是当我使用HAL_SetDate()将年份设置为 18 时,经过一小段延迟后,我用HAL_GetDate()进行了阅读,sDate.Year给了我 20 .除了Year之外,其他值都是正确的.

I need to update the time/date from GSM time but when I set the year to 18 with HAL_SetDate() and after a small delay I read with HAL_GetDate(), sDate.Year gave me 20. Apart from Year, the other values are correct.

我尝试过的事情:

  • 二手LSI
  • 同时(HAL_SetDate!= HAL_GetDate)HAL_SetDate(ActualDate)
  • 首先HAL_GetDate然后HAL_SetDate
  • Used LSI
  • while (HAL_SetDate != HAL_GetDate) HAL_SetDate(ActualDate)
  • First HAL_GetDate then HAL_SetDate

我没有进步,甚至变得更糟,例如Month = 56,Day = 45等.

I got no progress and thing even got worse like Month = 56, Day = 45 etc.

先谢谢了.最好的问候.

Thanks in advance.Best regards.

推荐答案

WeekDay必须设置为0到7之间的值

我有同样的问题.我发现问题是:未将值设置为WeekDay .在函数作用域中创建结构RTC_DateTypeDef时,字段WeekDay获得随机值.我发现:WeekDay必须设置为0到7之间的值,如果超出此范围,则可以更改年份.

The value WeekDay must be set to a value between 0 to 7

I had the same problem. I found that the problem was: Not setting a value to WeekDay. When creating a struct RTC_DateTypeDef in a functions scope, the field WeekDay gets a random value. I discovered that: The value WeekDay must be set to a value between 0 to 7, if it is out of this range, it can change the year.

在功能HAL_RTC_SetDate中设置日期的代码:

The code for setting the date in function HAL_RTC_SetDate:

if (Format == RTC_FORMAT_BIN)
{
    assert_param(IS_RTC_YEAR(sDate->Year));
    assert_param(IS_RTC_MONTH(sDate->Month));
    assert_param(IS_RTC_DATE(sDate->Date));

    datetmpreg = (((uint32_t)RTC_ByteToBcd2(sDate->Year) << 16U) | \
                  ((uint32_t)RTC_ByteToBcd2(sDate->Month) << 8U) | \
                  ((uint32_t)RTC_ByteToBcd2(sDate->Date)) | \
                  ((uint32_t)sDate->WeekDay << 13U));
}
else
{
    assert_param(IS_RTC_YEAR(RTC_Bcd2ToByte(sDate->Year)));
    assert_param(IS_RTC_MONTH(RTC_Bcd2ToByte(sDate->Month)));
    assert_param(IS_RTC_DATE(RTC_Bcd2ToByte(sDate->Date)));

    datetmpreg = ((((uint32_t)sDate->Year) << 16U) | \
                  (((uint32_t)sDate->Month) << 8U) | \
                  ((uint32_t)sDate->Date) | \
                  (((uint32_t)sDate->WeekDay) << 13U));
}

  • Date占据0-7位(8位):两个BCD数字.
  • Month占用8-12位(5位):两个BCD数字,但左边的数字可以只能是0或1-> 5位就够了.
  • WeekDay占据位13-15(3位):1个BCD数字,其值范围为1-7-> 3位够了.
  • Year占据16-24位(9位).
    • Date occupies bits 0-7 (8 bits): two BCD digits.
    • Month occupies bits 8-12 (5 bits): two BCD digits but the left digit canonly be 0 or 1 -> 5 bits is enough.
    • WeekDay occupies bits 13-15 (3 bits): one BCD digit with value range 1-7 -> 3 bitsis enough.
    • Year occupies bits 16-24 (9 bits).
    • WeekDay大于7时,MSB为1,并且与Year的LSB重叠并且可以更改(如果LSB为0).

      When WeekDay is greater than 7, the MSB is 1 and it overlaps with the LSB of Year and can change it (if the LSB is 0).

      这篇关于HAL_SetDate将年份设置为错误的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 15:16