sioc 實驗 2 : real-time clock(rtc)

49
WU-YANG Technology Co., Ltd. SIOC 實實 2 Real-Time Clock(RTC) 實實實 / 實實實 MIAT 實實實 實實實實實實實實 實實

Upload: reina

Post on 24-Feb-2016

224 views

Category:

Documents


0 download

DESCRIPTION

SIOC 實驗 2 : Real-Time Clock(RTC) . 李怡臻 / 陳慶瀚 MIAT 實驗室 中央大學資訊工程 學系. 實驗目的. 了解 Real Time Clock(RTC) 硬體控制原理 了解 STM32 的 RTC 控制器 driver 設計及應用. RTC 原理. RTC 是一種提供日期 / 時間的週邊硬體元件; 常用於嵌入式系統中顯示時間資訊和紀錄事件發生的時間; STM32 的 RTC 硬體位於 backup 區,一旦系統主電源斷電, RTC 仍可藉由輔助電力 ( 例如鈕扣電池 ) 維持運作,因而可實現不中斷的即時時鐘功能。. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: SIOC  實驗 2 : Real-Time Clock(RTC)

WU-YANG Technology Co., Ltd.

SIOC 實驗 2 : Real-Time Clock(RTC)

李怡臻 / 陳慶瀚MIAT 實驗室中央大學資訊工程 學系

Page 2: SIOC  實驗 2 : Real-Time Clock(RTC)

實驗目的

2

了解 Real Time Clock(RTC) 硬體控制原理了解 STM32 的 RTC 控制器 driver 設計及應用

Page 3: SIOC  實驗 2 : Real-Time Clock(RTC)

RTC原理

3

RTC 是一種提供日期 / 時間的週邊硬體元件; 常用於嵌入式系統中顯示時間資訊和紀錄事件發生的時間; STM32 的 RTC 硬體位於 backup 區,一旦系統主電源斷電, RTC 仍可藉由輔助電力 ( 例如鈕扣電池 ) 維持運作,因而可實現不中斷的即時時鐘功能。

Page 4: SIOC  實驗 2 : Real-Time Clock(RTC)

RTC 硬體架構

4

Page 5: SIOC  實驗 2 : Real-Time Clock(RTC)

RTC 核心● 兩個模組: programmable prescaler + programmable counter● prescaler division factor up to 220

● 32-bit programmable counter : 如果 TR_CLK 周期為 1 秒, 32-bit counter 最多可計時至 136 年/* Set RTC prescaler: set RTC period to 1sec */ RTC_SetPrescaler(32767); /* RTC period = RTCCLK/RTC_PR = (32.768 KHz)/(32767+1) */

● 兩個獨立輸入的 clocks: PCLK1 和 RTC

● 三個 maskable interrupt:– Alarm interrupt, 產生 alarm 中斷– Seconds interrupt, 產生最常 1 秒的週期中斷訊號– Overflow interrupt, 偵測 counter 是否 overflow.

5

Page 6: SIOC  實驗 2 : Real-Time Clock(RTC)

RTC Registers

6

Page 7: SIOC  實驗 2 : Real-Time Clock(RTC)

Memory Mapping Table

7

Page 8: SIOC  實驗 2 : Real-Time Clock(RTC)

RTC Control Register high

8

Page 9: SIOC  實驗 2 : Real-Time Clock(RTC)

RTC Control Register low

9

RTC operation OFFConfiguration flag

Registers synchronized flag

Overflow flagAlarm flag

Second flag

Page 10: SIOC  實驗 2 : Real-Time Clock(RTC)

RTC Counter Register

RTC counter register high (RTC_CNTH)

10

Address offset: 0x18Reset value: 0x0000

紀錄計數的秒值

Page 11: SIOC  實驗 2 : Real-Time Clock(RTC)

RTC Alarm Register

11

RTC_ALR暫存器存放秒數值,如果 RTC_CNT的值與RTC_ALR的值相等,鬧鐘發出中斷訊號(RTC_alarmIT)

Page 12: SIOC  實驗 2 : Real-Time Clock(RTC)

RTC Register Mapping

12

Page 13: SIOC  實驗 2 : Real-Time Clock(RTC)

WU-YANG Technology Co., Ltd.

RTC Standard Driver Library

13

Page 14: SIOC  實驗 2 : Real-Time Clock(RTC)

Declaration of RTC peripheral

14

RTC registers

RTC declaration

Page 15: SIOC  實驗 2 : Real-Time Clock(RTC)

RTC firmware library function

15

Page 16: SIOC  實驗 2 : Real-Time Clock(RTC)

RTC Interrupt Configure Function

16

Page 17: SIOC  實驗 2 : Real-Time Clock(RTC)

RTC_IT in RTC Interrupt Configure Function

Example:/* Wait until last write operation on RTC registers is terminated */RTC_WaitForLastTask();/* Alarm interrupt enabled */RTC_ITConfig(RTC_IT_ALR, ENABLE);

17

Page 18: SIOC  實驗 2 : Real-Time Clock(RTC)

RTC EnterConfigure Mode Function

Example:/* Enable the configuration mode */RTC_EnterConfigMode();

18

Page 19: SIOC  實驗 2 : Real-Time Clock(RTC)

RTC_ExitConfigureMode Function

Example:/* Exit the configuration mode */RTC_ExitConfigMode();

19

Page 20: SIOC  實驗 2 : Real-Time Clock(RTC)

RTC_GetCounter Function

Example:/* Gets the counter value */u32 RTCCounterValue;RTCCounterValue = RTC_GetCounter();

20

Page 21: SIOC  實驗 2 : Real-Time Clock(RTC)

RTC_SetCounter Function

Example:/* Wait until last write operation on RTC registers is terminated */RTC_WaitForLastTask();RTC_SetCounter(0xFFFF5555);

21

Page 22: SIOC  實驗 2 : Real-Time Clock(RTC)

RTC_SetPrescaler function

Example:/* Wait until last write operation on RTC registers is terminated */RTC_WaitForLastTask();/* Sets Prescaler value to 0x7A12 */RTC_SetPrescaler(0x7A12);

22

Page 23: SIOC  實驗 2 : Real-Time Clock(RTC)

RTC_SetAlarm Function

Example:/* Wait until last write operation on RTC registers is terminated */RTC_WaitForLastTask();RTC_SetAlarm(0xFFFFFFFA);

23

Page 24: SIOC  實驗 2 : Real-Time Clock(RTC)

RTC_GetFlagStatus Function

24

Example:/* Gets the RTC overflow interrupt status */FlagStatus OverrunFlagStatus;OverrunFlagStatus = RTC_GetFlagStatus(RTC_Flag_OW);

Page 25: SIOC  實驗 2 : Real-Time Clock(RTC)

RTC Clear Flag Function

Example:RTC_WaitForLastTask(); /* Wait until last write operation on RTC registers is terminated */RTC_ClearFlag(RTC_FLAG_OW); /* Clears the RTC overflow flag */

25

Page 26: SIOC  實驗 2 : Real-Time Clock(RTC)

RTC Get Interrupt Status Function

/* Gets the RTC Second interrupt status */ITStatus SecondITStatus;SecondITStatus = RTC_GetITStatus(RTC_IT_SEC);

26

Page 27: SIOC  實驗 2 : Real-Time Clock(RTC)

RTC Clear Interrupt Pending Bit Function

Example:RTC_WaitForLastTask();/* Clears the RTC Second interrupt */RTC_ClearITPendingBit(RTC_IT_SEC);

27

Page 28: SIOC  實驗 2 : Real-Time Clock(RTC)

實驗

28

Page 29: SIOC  實驗 2 : Real-Time Clock(RTC)

29

Package description路徑  C:\Keil\ARM\Examples\ST\STM32F10xFWLib

Page 30: SIOC  實驗 2 : Real-Time Clock(RTC)

30

實驗 1– 超級終端機顯示時間 使用 RTC second interrupt 來產生時間,並用 Virtual COM Port 在超級終端機上顯示時間。

Page 31: SIOC  實驗 2 : Real-Time Clock(RTC)

嵌入式軟體架構

31

Connect the EVB and the IOB

Programming

Embedded Software Side

Use the “Dubond thread”

BootupSTM32F10x

RCC Configure

NVIC Configure

GPIO Configure

UART Configure

Waiting to Receive String

HyperTermainal Software

Interrupt RTC Interrupt Handler

1. Get Counter Value2. Change value to time

Download Program to the Flash of the STM32F103ZC

RTC Configure

Page 32: SIOC  實驗 2 : Real-Time Clock(RTC)

Development Flow  cont.

32

Main()

NVIC_Configuration(void)

RTC_Configuration(void)

Time_Show()

If(TimeDisplay = =1 ) Time_Display(u32 TimeVar)

Time_Adjust();Time_Regulate()

void RTC_IRQHandler(void){ TimeDisplay = 1;}

Second Interrupt!!

Page 33: SIOC  實驗 2 : Real-Time Clock(RTC)

RTC Grafcet

33

Page 34: SIOC  實驗 2 : Real-Time Clock(RTC)

NVIC Configure

34

/* NVIC Configure */void NVIC_Configuration(void){ NVIC_InitTypeDef NVIC_InitStructure;#ifdef VECT_TAB_RAM /* Set the Vector Table base location at 0x20000000 */ NVIC_SetVectorTable(NVIC_VectTab_RAM, 0x0); #else /* VECT_TAB_FLASH */ /* Set the Vector Table base location at 0x08000000 */ //NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x0); NVIC_SetVectorTable(0x08003000, 0x0); #endif /* Configure one bit for preemption priority */ NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1); /* Enable the RTC Interrupt */ NVIC_InitStructure.NVIC_IRQChannel = RTC_IRQChannel; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure);}

Page 35: SIOC  實驗 2 : Real-Time Clock(RTC)

RTC Configure

35

/* RTC Configure */void RTC_Configuration(void){ /* Enable PWR and BKP clocks */ RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR | RCC_APB1Periph_BKP, ENABLE); PWR_BackupAccessCmd(ENABLE); /* Allow access to BKP Domain */ BKP_DeInit(); /* Reset Backup Domain */ RCC_LSEConfig(RCC_LSE_ON); /* Enable LSE */ while (RCC_GetFlagStatus(RCC_FLAG_LSERDY) == RESET); /* Wait till LSE is ready */ RCC_RTCCLKConfig(RCC_RTCCLKSource_LSE); /* Select LSE as RTC Clock Source */ RCC_RTCCLKCmd(ENABLE); /* Enable RTC Clock */ RTC_WaitForLastTask(); RTC_WaitForSynchro(); /* Wait for RTC registers synchronization */ RTC_WaitForLastTask(); /* Wait until last write operation on RTC registers has finished */ RTC_ITConfig(RTC_IT_SEC, ENABLE); /* Enable the RTC Second */ RTC_WaitForLastTask(); /* Wait until last write operation on RTC registers is terminated */ /* Set RTC prescaler: set RTC period to 1sec */ RTC_SetPrescaler(32767); /* RTC period = RTCCLK/RTC_PR = (32.768 KHz)/(32767+1) */ RTC_WaitForLastTask(); /* Wait until last write operation on RTC registers has finished */}

Page 36: SIOC  實驗 2 : Real-Time Clock(RTC)

Main program

36

/* 超級終端機顯示 RTC時間 */int main(void){#ifdef DEBUG debug();#endif NVIC_Configuration(); /* NVIC configuration */ USART_Configuration(); /* Configure the USART1 */

if (BKP_ReadBackupRegister(BKP_DR1) != 0xA5A5) { /* Backup data register value is not correct or not yet programmed (when the first time the program is executed) */ printf("\r\n\n RTC not yet configured...."); RTC_Configuration(); /* RTC Configuration */ printf("\r\n RTC configured...."); Time_Adjust(); /* Adjust time by values entred by the user on the hyperterminal */ BKP_WriteBackupRegister(BKP_DR1, 0xA5A5); }

Page 37: SIOC  實驗 2 : Real-Time Clock(RTC)

37

/* 續上頁 */else { /* Check if the Power On Reset flag is set */ if(RCC_GetFlagStatus(RCC_FLAG_PORRST) != RESET) printf("\r\n\n Power On Reset occurred...."); else if(RCC_GetFlagStatus(RCC_FLAG_PINRST) != RESET) printf("\r\n\n External Reset occurred...."); printf("\r\n No need to configure RTC...."); RTC_WaitForSynchro(); /* Wait for RTC registers synchronization */ RTC_ITConfig(RTC_IT_SEC, ENABLE); /* Enable the RTC Second */ /* Wait until last write operation on RTC registers has finished */ RTC_WaitForLastTask(); } RCC_ClearFlag(); /* Clear reset flags */ while(1) Time_Show();}

Page 38: SIOC  實驗 2 : Real-Time Clock(RTC)

Time_Regulate( ) & Time_Adjust( ) functionu32 Time_Regulate(void){ u32 Tmp_HH = 0xFF, Tmp_MM = 0xFF, Tmp_SS = 0xFF; Tmp_HH =0; Tmp_MM = 0;  Tmp_SS = 0; * /* Return the value to store in RTC counter register */ return((Tmp_HH*3600 + Tmp_MM*60 + Tmp_SS));}void Time_Adjust(void){ RTC_WaitForLastTask(); RTC_SetCounter(Time_Regulate()); /* Change the current time */ RTC_WaitForLastTask(); }

38

Page 39: SIOC  實驗 2 : Real-Time Clock(RTC)

Time_Display( ) & Time_Show( ) functionvoid Time_Display(u32 TimeVar){ u32 THH = 0, TMM = 0, TSS = 0; THH = TimeVar / 3600; /* Compute hours */ TMM = (TimeVar % 3600) / 60; /* Compute minutes */ TSS = (TimeVar % 3600) % 60; /* Compute seconds */ printf("Time: %0.2d:%0.2d:%0.2d\r", THH, TMM, TSS);}void Time_Show(void){ printf("\n\r"); while (1) /* Infinite loop */ { if (TimeDisplay == 1) /* If 1s has paased */ { Time_Display(RTC_GetCounter()); /* Display current time */ TimeDisplay = 0; } }}

39

Page 40: SIOC  實驗 2 : Real-Time Clock(RTC)

RTC Interrupt Handler

40

/* Interrupt Handler*/<stm32f10x_it.c>/* Interrupt Handler*/void RTC_IRQHandler(void){ if (RTC_GetITStatus(RTC_IT_SEC) != RESET) { RTC_ClearITPendingBit(RTC_IT_SEC); /* Clear the RTC Second interrupt */ TimeDisplay = 1; /* Enable time update */

/* Wait until last write operation on RTC registers has finished */ RTC_WaitForLastTask(); /* Reset RTC Counter when Time is 23:59:59 */ if (RTC_GetCounter() == 0x00015180) { RTC_SetCounter(0x0); /* Wait until last write operation on RTC registers has finished */ RTC_WaitForLastTask(); } }}

Page 41: SIOC  實驗 2 : Real-Time Clock(RTC)

LAB1 DEMO

41

HyperTermainal Output

Print timeHour: Minuets: Second

Page 42: SIOC  實驗 2 : Real-Time Clock(RTC)

42

實驗 2 – RTC時間調整 利用 RTC second interrupt 來產生時間,並用 Virtual COM Port 在超級終端機上顯示時間。 請修改 u32 Time_Regulate(void) ,讓使用者自行輸入現在時間, 使用

printf() 與 scanf() 。

Page 43: SIOC  實驗 2 : Real-Time Clock(RTC)

Sample code

u32 Time_Regulate(void){ u32 Tmp_HH = 0xFF, Tmp_MM = 0xFF, Tmp_SS = 0xFF;/*   modify your code Tmp_HH =0; Tmp_MM = 0;   Tmp_SS = 0;*/ * /* Return the value to store in RTC counter register */ return((Tmp_HH*3600 + Tmp_MM*60 + Tmp_SS));}void Time_Adjust(void){ RTC_WaitForLastTask(); /* Wait until last write operation has finished */ RTC_SetCounter(Time_Regulate()); /* Change the current time */ RTC_WaitForLastTask(); /* Wait until last write operationhas finished */}

43

Page 44: SIOC  實驗 2 : Real-Time Clock(RTC)

LAB2 DEMO

44

Page 45: SIOC  實驗 2 : Real-Time Clock(RTC)

45

實驗 3 – RTC Alarm Interrupt

利用 RTC alarm interrupt 製作鬧鐘,請使用者先輸入現在時間,再輸入鬧鐘時間。 請參考 RTC second interrupt 的使用方式,修改

void RTC_Configuration(void) void Time_Adjust(void) void RTC_IRQHandler(void)

Page 46: SIOC  實驗 2 : Real-Time Clock(RTC)

RTC Configure

46

/* RTC Configure *//* Enable the RTC Second */ RTC_ITConfig(RTC_IT_ALR, ENABLE );/* Wait until last write operation on RTC registers is finished */ RTC_WaitForLastTask();

Page 47: SIOC  實驗 2 : Real-Time Clock(RTC)

Time Adjust

47

void Time_Adjust(void){ RTC_WaitForLastTask(); /* Wait until last write operation on RTC registers has finished */ RTC_SetCounter(Time_Regulate()); /* Change the current time */ RTC_WaitForLastTask(); /* Wait until last write operation on RTC registers has finished */

/*   modify your code   RTC_SetAlarm(0x10);  RTC_WaitForLastTask();

*/}

Page 48: SIOC  實驗 2 : Real-Time Clock(RTC)

RTC Interrupt Handler

48

/* Interrupt Handler */void RTC_IRQHandler(void){ if (RTC_GetITStatus(RTC_IT_SEC) != RESET) { RTC_ClearITPendingBit(RTC_IT_SEC); /* Clear RTC Second interrupt */ TimeDisplay = 1; /* Enable time update */ /* Wait until last write operation on RTC registers has finished */ RTC_WaitForLastTask(); /* Reset RTC Counter when Time is 23:59:59 */ if (RTC_GetCounter() == 0x00015180) { RTC_SetCounter(0x0); /* Wait until last write operation on RTC registers has finished */ RTC_WaitForLastTask(); } }/*   modify your code

if(RTC_GetITStatus(RTC_IT_ALR) != RESET){ …………} */

}

Page 49: SIOC  實驗 2 : Real-Time Clock(RTC)

LAB3 DEMO

49