L151 RTC读取BUG

L151 RTC写入后读取异常,年份总是差几个数,有几点要注意:

1. weekday设成0

2. 网上有些文章说写要先日期,后时间,读取是先时间后日期,亲测无用,不过还是加上

3. 实测如果一条写指令后马上读会出错,延时1秒,正常

所以以上三点都加上,代码:

void setlocaltime(uint8_t *timestring){
RTC_DateTypeDef setDate;
RTC_TimeTypeDef setTime;
    
    setDate.Year=char2num(timestring[0],timestring[1]);
    setDate.Month=char2num(timestring[2],timestring[3]);
    setDate.Date=char2num(timestring[4],timestring[5]);
    setDate.WeekDay=0x00;
    setTime.Hours=char2num(timestring[6],timestring[7]);
    setTime.Minutes=char2num(timestring[8],timestring[9]);
    setTime.Seconds=char2num(timestring[10],timestring[11]);
    
    while(HAL_RTC_SetDate(&hrtc,&setDate,RTC_FORMAT_BIN)!= HAL_OK);
    while(HAL_RTC_SetTime(&hrtc,&setTime,RTC_FORMAT_BCD)!= HAL_OK);
    // debug the RTC write and read
//     while(HAL_RTC_GetTime(&hrtc,&setTime,RTC_FORMAT_BCD)!= HAL_OK);
    osDelay(1000);
    while(HAL_RTC_GetDate(&hrtc,&setDate,RTC_FORMAT_BIN)!= HAL_OK);


}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

海里的鱼2022

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值