这个作者很懒,想到什么就写什么!
写在前面:这个是记录学习LL库的踩坑系列,这个资料很少,想要找例程可以在电脑用户文件下的STM32Cube\Repository里,前提是下载好那个单片机的pack,部分没有例程的可以借鉴同系列的文件。
本次踩坑RTC实时时钟作为Calender使用。
打开cubemx,配置好时钟模块,选择外部LSE。
然后选择生成LL库,编译,添加代码,一个是设置,一个读取。
/* Define used to indicate date/time updated */
#define RTC_BKP_DATE_TIME_UPDTATED ((uint32_t)0x32F2)
void RTC_Set(LL_RTC_DateTypeDef *date,LL_RTC_TimeTypeDef *time)
{
LL_RTC_TIME_Init(RTC, LL_RTC_FORMAT_BIN, time);
LL_RTC_DATE_Init(RTC, LL_RTC_FORMAT_BIN, date);
LL_RTC_BAK_SetRegister(RTC, LL_RTC_BKP_DR0, RTC_BKP_DATE_TIME_UPDTATED);
}
void RTC_Get(LL_RTC_DateTypeDef *date,LL_RTC_TimeTypeDef *time)
{
time->Hours = __LL_RTC_CONVERT_BCD2BIN(LL_RTC_TIME_GetHour(RTC));
time->Minutes = __LL_RTC_CONVERT_BCD2BIN(LL_RTC_TIME_GetMinute(RTC));
time->Seconds = __LL_RTC_CONVERT_BCD2BIN(LL_RTC_TIME_GetSecond(RTC));
date->Year = __LL_RTC_CONVERT_BCD2BIN(LL_RTC_DATE_GetYear(RTC));
date->Month = __LL_RTC_CONVERT_BCD2BIN(LL_RTC_DATE_GetMonth(RTC));
date->Day = __LL_RTC_CONVERT_BCD2BIN(LL_RTC_DATE_GetDay(RTC));
}
添加测试代码。
unsigned char const table_week[12]={0,3,3,6,1,4,6,2,5,0,3,5}; //月修正数据表
unsigned char RTC_Get_Week(unsigned short int year,unsigned char month, unsigned char day)
{
unsigned short int temp2;
unsigned char yearH, yearL;
yearH = year/100; yearL = year%100;
// 如果为21世纪,年份数加100
if (yearH>19) yearL+=100;
// 所过闰年数只算1900年之后的
temp2 = yearL + yearL/4;
temp2 = temp2 % 7;
temp2 = temp2 + day + table_week[month-1];
if (yearL%4 == 0&&month<3) temp2--;
return(temp2 % 7);
}
void TestRtc(void)
{
LL_RTC_DateTypeDef curData;
LL_RTC_TimeTypeDef curTime;
RTC_Get(&curData, &curTime);
curData.WeekDay = RTC_Get_Week((curData.Year+2000), curData.Month, curData.Day );
curData.Year = 20;
curData.Month = 7;
curData.Day = 4;
curData.WeekDay = RTC_Get_Week((curData.Year+2000), curData.Month, curData.Day );
curTime.Hours = 9;
curTime.Minutes = 29;
curTime.Seconds = 1;
RTC_Set(&curData, &curTime);
RTC_Get(&curData, &curTime);
RTC_Get(&curData, &curTime);
}
进入调试,会发现备份寄存器写入值后,重启居然变成功0了。进入void SystemClock_Config(void)函数查看
LL_RCC_HSI_SetCalibTrimming(16);
LL_PWR_EnableBkUpAccess();
LL_RCC_ForceBackupDomainReset();
LL_RCC_ReleaseBackupDomainReset();
LL_RCC_LSE_Enable();
/* Wait till LSE is ready */
while(LL_RCC_LSE_IsReady() != 1)
{
}
LL_RCC_SetRTCClockSource(LL_RCC_RTC_CLKSOURCE_LSE);
LL_RCC_EnableRTC();
这里居然有LL_RCC_ForceBackupDomainReset(); LL_RCC_ReleaseBackupDomainReset();两条函数,卧槽!!!!!!!!!!!!!
屏蔽了就正常了,复位备份寄存器个锤儿。