4.RTC模塊初始化,ErrorStatus 返回值為SUCCESS或ERROR
ErrorStatus RTC_Init(RTC_InitTypeDef* RTC_InitStruct)
{
CW_SYSCTRL->APBEN1_f.RTC = 1; // 啟動(dòng)RTC外設(shè)時(shí)鐘,使能RTC模塊
if ((RCC_GetAllRstFlag() & SYSCTRL_RESETFLAG_POR_Msk) != RCC_FLAG_PORRST) //不是上電復(fù)位,直接退出
{
RCC_ClearRstFlag(RCC_FLAG_ALLRST);
return SUCCESS;
}
RTC_Cmd(DISABLE); // 停止RTC,保證正確訪問RTC寄存器
RTC_SetClockSource(RTC_InitStruct->RTC_ClockSource); // 設(shè)置RTC時(shí)鐘源, 用戶需首先啟動(dòng)RTC時(shí)鐘源!!!
RTC_SetDate(&RTC_InitStruct->DateStruct);// 設(shè)置日期,DAY、MONTH、YEAR必須為BCD方,星期為0~6,代表星期日,星期一至星期六
RTC_SetTime(&RTC_InitStruct->TimeStruct); //時(shí)間,HOUR、MINIUTE、SECOND必須為BCD方式,用戶須保證HOUR、AMPM、H24之間的關(guān)聯(lián)正確性
RTC_Cmd(ENABLE);
RCC_ClearRstFlag(RCC_FLAG_ALLRST);
return SUCCESS;
}
5.RTC周期中斷時(shí)間設(shè)置
int RTC_SetInterval(uint8_t Period)
{
uint16_t timeout = 0xffff;
RTC_UNLOCK();
if (IS_RTC_START()) // 如果RTC正在運(yùn)行,則使用WINDOWS、ACCESS訪問
{
CW_RTC->CR1_f.ACCESS = 1;
while ((!CW_RTC->CR1_f.WINDOW) && timeout--);
if (timeout == 0) return 1;
}
CW_RTC->CR0_f.INTERVAL = Period;
CW_RTC->CR1_f.ACCESS = 0;
RTC_LOCK();
return 0;
}
6.設(shè)置時(shí)鐘中斷使能
int RTC_ITConfig(uint32_t RTC_IT, FunctionalState NewState)
{
uint16_t timeout = 0xffff;
RTC_UNLOCK();
CW_RTC->CR1_f.ACCESS = 1;
while ((!CW_RTC->CR1_f.WINDOW) && timeout--);
if (timeout == 0) return 1;
if (!NewState)
{
CW_RTC->IER &= ~RTC_IT;
}
else
{
CW_RTC->IER |= RTC_IT;
}
CW_RTC->CR1_f.ACCESS = 0;
RTC_LOCK();
return 0;
}
void RTC_IRQHandlerCallBack(void)
{
if (RTC_GetITState(RTC_IT_ALARMA))
{
RTC_ClearITPendingBit(RTC_IT_ALARMA);
printf("*********Alarm!!!!\\r\\n");
}
if (RTC_GetITState(RTC_IT_INTERVAL))
{
RTC_ClearITPendingBit(RTC_IT_INTERVAL);
ShowTime();
}
void NVIC_Configuration(void)
{
__disable_irq();
NVIC_EnableIRQ(RTC_IRQn);
__enable_irq();
}
7.RTC時(shí)鐘測(cè)試,初始化日歷,使用間隔中斷0.5秒通過Log輸出日期時(shí)間
int32_t main(void)
{
RTC_InitTypeDef RTC_InitStruct = {0};
RTC_AlarmTypeDef RTC_AlarmStruct = {0};
/*系統(tǒng)時(shí)鐘配置*/
RCC_Configuration();
/* GPIO 口配置*/
GPIO_Configuration();
LogInit();//配置輸出時(shí)間所需GPIO口以及串口UART配置
printf("RTC Init...\\r\\n");
printf(" (RTC CR0:%04x,CR1:%04x,CR2:%04x,RESET FLAG:0x%08x)\\r\\n",CW_RTC->CR0,CW_RTC->CR1,CW_RTC->CR2,CW_SYSCTRL->RESETFLAG);
RCC_LSE_Enable(RCC_LSE_MODE_OSC, RCC_LSE_AMP_NORMAL, RCC_LSE_DRIVER_NORMAL); // 選擇LSE為RTC時(shí)鐘
RTC_InitStruct.DateStruct.Day = 0x21; //日
RTC_InitStruct.DateStruct.Month = RTC_Month_June;//月
RTC_InitStruct.DateStruct.Week = RTC_Weekday_Monday;//星期
RTC_InitStruct.DateStruct.Year = 0x21; //年
//設(shè)置日期,DAY、MONTH、YEAR必須為BCD方式,星期為0~6,代表星期日,星期一至星期六
printf("-------Set Date as 20%x/%x/%x\\r\\n", RTC_InitStruct.DateStruct.Year,RTC_InitStruct.DateStruct.Month,RTC_InitStruct.DateStruct.Day);
//打印日期
RTC_InitStruct.TimeStruct.Hour = 0x11; //時(shí)
RTC_InitStruct.TimeStruct.Minute = 0x58;//分
RTC_InitStruct.TimeStruct.Second = 0x59;//秒
RTC_InitStruct.TimeStruct.AMPM = 0;
RTC_InitStruct.TimeStruct.H24 = 0; //采用12小時(shí)設(shè)置
//設(shè)置時(shí)間,HOUR、MINIUTE、SECOND必須為BCD方式,用戶須保證HOUR、AMPM、H24之間的關(guān)聯(lián)正確性
printf("-------Set Time as %02x:%02x:%02x\\r\\n", RTC_InitStruct.TimeStruct.Hour,RTC_InitStruct.TimeStruct.Minute,RTC_InitStruct.TimeStruct.Second);//打印時(shí)間
RTC_InitStruct.RTC_ClockSource = RTC_RTCCLK_FROM_LSE;
RTC_Init(&RTC_InitStruct); // RTC模塊初始化, 用戶需選定需要使用的時(shí)鐘源
printf("=====Set interval period as 0.5s...\\r\\n");
RTC_SetInterval(RTC_INTERVAL_EVERY_0_5S);
//鬧鐘為工作日上午的6:45
RTC_AlarmStruct.RTC_AlarmMask = RTC_AlarmMask_WeekMON | RTC_AlarmMask_WeekTUE |
RTC_AlarmMask_WeekWED | RTC_AlarmMask_WeekTHU |RTC_AlarmMask_WeekFRI;
//設(shè)定時(shí)間為周一到周五
RTC_AlarmStruct.RTC_AlarmTime.Hour = 6;
RTC_AlarmStruct.RTC_AlarmTime.Minute = 0x45;
RTC_AlarmStruct.RTC_AlarmTime.Second = 0;
RTC_SetAlarm(RTC_Alarm_A, &RTC_AlarmStruct); // 設(shè)置鬧鐘,BCD格式
RTC_AlarmCmd(RTC_Alarm_A, ENABLE);//使能鬧鐘
printf("=====Enable ALRAMA and INTERVAL IT...\\r\\n");
RTC_ITConfig(RTC_IT_ALARMA | RTC_IT_INTERVAL, ENABLE);
//設(shè)置中斷使能
While(1){}
}
8.通過UART串口驗(yàn)證RTC工作正常
-
實(shí)時(shí)時(shí)鐘
+關(guān)注
關(guān)注
4文章
314瀏覽量
67026 -
計(jì)數(shù)器
+關(guān)注
關(guān)注
32文章
2291瀏覽量
96334 -
中斷
+關(guān)注
關(guān)注
5文章
905瀏覽量
42723 -
定時(shí)器
+關(guān)注
關(guān)注
23文章
3298瀏覽量
118560 -
CW32
+關(guān)注
關(guān)注
1文章
255瀏覽量
1215 -
武漢芯源
+關(guān)注
關(guān)注
1文章
67瀏覽量
462
發(fā)布評(píng)論請(qǐng)先 登錄
STM32 RTC實(shí)時(shí)時(shí)鐘(一)

什么是實(shí)時(shí)時(shí)鐘(RTC)?如何更改RTC的時(shí)間?
實(shí)時(shí)時(shí)鐘(RTC)簡(jiǎn)要介紹
RTC是什么?RTC實(shí)時(shí)時(shí)鐘實(shí)驗(yàn)
RTC實(shí)時(shí)時(shí)鐘怎么使用?
RTC實(shí)時(shí)時(shí)鐘實(shí)驗(yàn)的相關(guān)資料分享
HT49 MCU RTC(實(shí)時(shí)時(shí)鐘)的使用介紹
一文看懂rtc實(shí)時(shí)時(shí)鐘和單片機(jī)時(shí)鐘的區(qū)別
淺談RTC實(shí)時(shí)時(shí)鐘特征與原理
STM32CubeMX | 40 - 實(shí)時(shí)時(shí)鐘RTC的使用(日歷和鬧鐘)

STM32CubeMX系列|RTC實(shí)時(shí)時(shí)鐘

DA1468x SoC 的實(shí)時(shí)時(shí)鐘(RTC) 概念

CW32實(shí)時(shí)時(shí)鐘(RTC)介紹(上)

DA1468x SoC 的實(shí)時(shí)時(shí)鐘(RTC) 概念

評(píng)論