基于stm32f103zet6的RTC學(xué)習(xí)
RTC配置
一、秒中斷的配置,RTC就是一個(gè)定時(shí)器而已,沒什么大不了的!
1、NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
NVIC_PriorityGroup,: specifies the priority grouping bits length.
This parameter can be one of the following values:
NVIC_PriorityGroup_0: 0 bits for pre-emption priority 4 bits for subpriority
NVIC_PriorityGroup_1: 1 bits for pre-emption priority 3 bits for subpriority
NVIC_PriorityGroup_2: 2 bits for pre-emption priority 2 bits for subpriority
NVIC_PriorityGroup_3: 3 bits for pre-emption priority 1 bits for subpriority
NVIC_PriorityGroup_4: 4 bits for pre-emption priority 0 bits for subpriority
//意思很明朗,唯一需要理解的就是pre-emption(主優(yōu)先級(jí))subpriority(副優(yōu)先級(jí));
2、NVIC_InitTypeDef NVIC_InitStructure;
一個(gè)這樣的結(jié)構(gòu)體,找到手冊(cè)
數(shù)據(jù)成員
uint8_t NVIC_IRQChannel
FunctionalState NVIC_IRQChannelCmd
uint8_t NVIC_IRQChannelPreemptionPriority
uint8_t NVIC_IRQChannelSubPriority
3、NVIC_IRQChannel他的描述如下
Specifies the IRQ channel to be enabled or disabled.
取值
NonMaskableInt_IRQn 2 Non Maskable Interrupt
MemoryManagement_IRQn 4 Cortex-M3 Memory Management Interrupt
BusFault_IRQn 5 Cortex-M3 Bus Fault Interrupt
UsageFault_IRQn 6 Cortex-M3 Usage Fault Interrupt
SVCall_IRQn 11 Cortex-M3 SV Call Interrupt
DebugMonitor_IRQn 12 Cortex-M3 Debug Monitor Interrupt
PendSV_IRQn 14 Cortex-M3 Pend SV Interrupt
SysTick_IRQn 15 Cortex-M3 System Tick Interrupt
WWDG_IRQn Window WatchDog Interrupt
PVD_IRQn PVD through EXTI Line detection Interrupt
TAMPER_IRQn Tamper Interrupt
RTC_IRQn RTC global Interrupt
FLASH_IRQn FLASH global Interrupt
RCC_IRQn RCC global Interrupt
EXTI0_IRQn EXTI Line0 Interrupt
EXTI1_IRQn EXTI Line1 Interrupt
EXTI2_IRQn EXTI Line2 Interrupt
EXTI3_IRQn EXTI Line3 Interrupt
EXTI4_IRQn EXTI Line4 Interrupt
DMA1_Channel1_IRQn DMA1 Channel 1 global Interrupt
DMA1_Channel2_IRQn DMA1 Channel 2 global Interrupt
DMA1_Channel3_IRQn DMA1 Channel 3 global Interrupt
DMA1_Channel4_IRQn DMA1 Channel 4 global Interrupt
DMA1_Channel5_IRQn DMA1 Channel 5 global Interrupt
DMA1_Channel6_IRQn DMA1 Channel 6 global Interrupt
DMA1_Channel7_IRQn DMA1 Channel 7 global Interrupt
4、NVIC_IRQChannelCmd
Specifies whether the IRQ channel defined in NVIC_IRQChannel will be enabled or disabled.
This parameter can be set either to ENABLE or DISABLE
5、NVIC_IRQChannelPreemptionPriority
搶占優(yōu)先級(jí)
6、NVIC_IRQChannelSubPriority
子優(yōu)先級(jí)
配置應(yīng)該是
NVIC_IRQChannel = RCC_IRQn
NVIC_IRQChannelCmd= ENABLE
NVIC_IRQChannelPreemptionPriority = 1
NVIC_IRQChannelSubPriority = 0
對(duì)照著著源代碼看,沒有人恩和問題的
二、接下來看個(gè)RTC配置函數(shù)
RTC_Configuration();
1、使能外設(shè)時(shí)鐘:包括預(yù)備區(qū)域時(shí)鐘和功耗控制時(shí)鐘
RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR | RCC_APB1Periph_BKP, ENABLE);
2、使能BKP寄存器
PWR_BackupAccessCmd(ENABLE);
3、初始化BKP寄存器,并且置為默認(rèn)值
BKP_DeInit();
4、RTC使用的是低速的外部時(shí)鐘,那么首先打開低速外部時(shí)鐘
RCC_LSEConfig(RCC_LSE_ON);
5、等待外部時(shí)鐘是否起振,是否準(zhǔn)備好?
while (RCC_GetFlagStatus(RCC_FLAG_LSERDY) == RESET){}
6、外部時(shí)鐘已經(jīng)打開,那么還要將RTC的時(shí)鐘設(shè)置為外部低速時(shí)鐘
RCC_RTCCLKConfig(RCC_RTCCLKSource_LSE);
7、設(shè)置完后,需要使能外部時(shí)鐘
RCC_RTCCLKCmd(ENABLE);
8、等待RTC寄存器的時(shí)鐘同步
RTC_WaitForLastTask();
9、等待最后一個(gè)向RTC寄存器寫的指令已經(jīng)完成
RTC_WaitForLastTask();
10、接下來就是RTC中斷的配置了Enables or disables the specified RTC interrupts
RTC_ITConfig(RTC_IT_SEC, ENABLE); Second interrupt ,使能second interrupt
11、 RTC_WaitForLastTask();
12、設(shè)置RTC的與分頻系數(shù)
RTC_SetPrescaler(32767); /* RTC period = RTCCLK/RTC_PR = (32.768 KHz)/(32767+1) */
13、 RTC_WaitForLastTask();
有一點(diǎn)要注意的是當(dāng)我們做完一些對(duì)RTC的初始化之后,然后會(huì)對(duì)RTC的寄存器進(jìn)行寫,那么,為了防止
產(chǎn)生錯(cuò)誤,需要等待指令已經(jīng)我那成也就是RTC_WaitForLastTask();
至此RTC的基本配置也完成了
三、接著當(dāng)我們需要從串口輸入時(shí)鐘時(shí)間的時(shí)候,我們看一個(gè)函數(shù)Time_Adjust
1、一上來就 RTC_WaitForLastTask();
等待操作完,這個(gè)沒有壞處;
2、改變當(dāng)前的時(shí)間值
RTC_SetCounter(Time_Regulate());
3、還是一樣的RTC_WaitForLastTask();
那么仔細(xì)來看看這個(gè)Time_Regulate是如何實(shí)現(xiàn)的。
void Time_Adjust(void)
{
/* Wait until last write operation on RTC registers has finished */
RTC_WaitForLastTask();
/* Change the current time */
RTC_SetCounter(Time_Regulate());
/* Wait until last write operation on RTC registers has finished */
RTC_WaitForLastTask();
}
唯獨(dú)有個(gè)函數(shù)
RTC_SetCounter(Time_Regulate()); Sets the RTC counter value.
貌似是設(shè)置秒的函數(shù)。
四、
1、 RCC_ClearFlag(); 這是清除復(fù)位標(biāo)識(shí)的函數(shù)
2、 Time_Show();
五、重點(diǎn)來了。。。
上面為依據(jù)野火提供的示例程序,我想使用野火的板子測(cè)試肯定是沒有問題的,現(xiàn)在關(guān)鍵是我是自己做的最
小系統(tǒng),所以有很多地方?jīng)]有注意,比如在這個(gè)低速外部晶振的地方,我發(fā)發(fā)現(xiàn)一直沒有振起來,在網(wǎng)上查
了資料,說是因?yàn)閷?duì)這個(gè)負(fù)載電容要求比較高,因?yàn)閷?shí)驗(yàn)室暫時(shí)沒有這個(gè)電容,所以沒辦法,只能修改使用
的時(shí)鐘,我這里使用內(nèi)部低速晶振來給RTC提供時(shí)鐘,所以有下面代碼需要修改
就是將選擇時(shí)鐘源的地方把那三行代碼修改為
RCC_LSICmd(ENABLE);//!?。∈褂脙?nèi)部低速晶振
while (RCC_GetFlagStatus(RCC_FLAG_LSIRDY) == RESET);
RCC_RTCCLKConfig(RCC_RTCCLKSource_LSI);
對(duì)應(yīng)的分頻系數(shù)也需要修改了?。?!
這樣做測(cè)試就沒有問題了!
具體原因就在轉(zhuǎn)載的那篇博文里面。。。。