基於stm32f103zet6的RTC學習

來源:互聯網
上載者:User

RTC配置
一、秒中斷的配置,RTC就是一個定時器而已,沒什麼大不了的!
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(主優先順序)subpriority(副優先順序);
2、NVIC_InitTypeDef NVIC_InitStructure;
一個這樣的結構體,找到手冊
資料成員
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
搶佔優先順序
6、NVIC_IRQChannelSubPriority
子優先順序
配置應該是
NVIC_IRQChannel = RCC_IRQn
NVIC_IRQChannelCmd= ENABLE
NVIC_IRQChannelPreemptionPriority = 1
NVIC_IRQChannelSubPriority = 0
對照著著原始碼看,沒有人恩和問題的
二、接下來看個RTC配置函數
    RTC_Configuration();
1、使能外設時鐘:包括預備地區時鐘和功耗控制時鐘
  RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR | RCC_APB1Periph_BKP, ENABLE);
2、使能BKP寄存器
  PWR_BackupAccessCmd(ENABLE);
3、初始化BKP寄存器,並且置為預設值
  BKP_DeInit();
4、RTC使用的是低速的外部時鐘,那麼首先開啟低速外部時鐘
  RCC_LSEConfig(RCC_LSE_ON);
5、等待外部時鐘是否起振,是否準備好?
  while (RCC_GetFlagStatus(RCC_FLAG_LSERDY) == RESET){}
6、外部時鐘已經開啟,那麼還要將RTC的時鐘設定為外部低速時鐘
  RCC_RTCCLKConfig(RCC_RTCCLKSource_LSE);
7、設定完後,需要使能外部時鐘
  RCC_RTCCLKCmd(ENABLE);
8、等待RTC寄存器的時鐘同步
  RTC_WaitForLastTask();
9、等待最後一個向RTC寄存器寫的指令已經完成
  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、設定RTC的與分頻係數
  RTC_SetPrescaler(32767); /* RTC period = RTCCLK/RTC_PR = (32.768 KHz)/(32767+1) */
13、  RTC_WaitForLastTask();
有一點要注意的是當我們做完一些對RTC的初始化之後,然後會對RTC的寄存器進行寫,那麼,為了防止
產生錯誤,需要等待指令已經我那成也就是RTC_WaitForLastTask();
至此RTC的基本配置也完成了
三、接著當我們需要從串口輸入時鐘時間的時候,我們看一個函數Time_Adjust
1、一上來就  RTC_WaitForLastTask();
等待操作完,這個沒有壞處;
2、改變當前的時間值
  RTC_SetCounter(Time_Regulate());
3、還是一樣的RTC_WaitForLastTask();
那麼仔細來看看這個Time_Regulate是如何?的。
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();
}
唯專屬個函數
  RTC_SetCounter(Time_Regulate()); Sets the RTC counter value. 
貌似是設定秒的函數。
四、
1、  RCC_ClearFlag(); 這是清除複位標識的函數
2、  Time_Show();
五、重點來了。。。
上面為依據野火提供的樣本程式,我想使用野火的板子測試肯定是沒有問題的,現在關鍵是我是自己做的最
小系統,所以有很多地方沒有注意,比如在這個低速外部晶振的地方,我發發現一直沒有振起來,在網上查
了資料,說是因為對這個負載電容要求比較高,因為實驗室暫時沒有這個電容,所以沒辦法,只能修改使用
的時鐘,我這裡使用內部低速晶振來給RTC提供時鐘,所以有下面代碼需要修改
就是將選擇時鐘源的地方把那三行代碼修改為
RCC_LSICmd(ENABLE);
//!!!使用內部低速晶振
while (RCC_GetFlagStatus(RCC_FLAG_LSIRDY) == RESET);
RCC_RTCCLKConfig(RCC_RTCCLKSource_LSI);  
對應的分頻係數也需要修改了!!!
這樣做測試就沒有問題了!
具體原因就在轉載的那篇博文裡面。。。。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.