(STM32F4) Real-time Clock
老實說Real-time Clok這項功能,我也只有在PC和手機上有見過,其他的應用產品上我也很少見到。
言歸正傳在STM32F4 RTC這項功能在IC內部就有內建,在早期的8051是如果要做RCT是需要外接IC的。
而RTC只需要透過register setting就可以達成12 or 24 format也有提供2個 alrm interrupt, wake up。

由上面的Function diagram可以得知幾個重要的地方。
1. 紫色的框框是我們主要必須配置的Block
2. Alarm 有 A & B 可以使用且有 ALRAF ALRBF,2個flag可以知道是否有發生Event
3. 反黑的2個 block 是我們取得RTC時間的Register
4. 在block最上方有一個backup register和RTC tamper control register做了一下功課,理解是因為RTC與SRAM有關因為必須存放資料。而STM32F4有一塊區域叫做backup domain這塊區域是由power switch做控制分成Standby/VBAT/low power(LP)。所以必須要enable power control register backup access。

RTC配置
void RTC_Config(void)
{
RTC_InitTypeDef RTC_InitStructure;
RTC_TimeTypeDef RTC_TimeStructure;
RTC_DateTypeDef RTC_DateStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR,ENABLE);
PWR_BackupAccessCmd(ENABLE);
RCC_LSICmd(ENABLE);
while(RCC_GetFlagStatus(RCC_FLAG_LSIRDY) == RESET);
RCC_RTCCLKConfig(RCC_RTCCLKSource_LSI);
RCC_RTCCLKCmd(ENABLE);
RTC_WaitForSynchro(); if(RTC_ReadBackupRegister(RTC_BKP_DR0) != 0x1234)
{
RTC_WriteProtectionCmd(DISABLE);
RTC_EnterInitMode(); RTC_InitStructure.RTC_HourFormat = RTC_HourFormat_24;
RTC_InitStructure.RTC_AsynchPrediv = 0x7D-;
RTC_InitStructure.RTC_SynchPrediv = 0xFF-;
RTC_Init(&RTC_InitStructure); RTC_TimeStructure.RTC_Seconds = 0x00;
RTC_TimeStructure.RTC_Minutes = ;
RTC_TimeStructure.RTC_Hours = 0x01;
RTC_TimeStructure.RTC_H12 = RTC_H12_AM;
RTC_SetTime(RTC_Format_BIN,&RTC_TimeStructure); RTC_DateStructure.RTC_Date = ;
RTC_DateStructure.RTC_Month = ;
RTC_DateStructure.RTC_WeekDay= RTC_Weekday_Thursday;
RTC_DateStructure.RTC_Year = ;
RTC_SetDate(RTC_Format_BIN,&RTC_DateStructure); RTC_ExitInitMode();
RTC_WriteBackupRegister(RTC_BKP_DR0,0x1234);
RTC_WriteProtectionCmd(ENABLE);
}
PWR_BackupAccessCmd(DISABLE);
}
RTC有防讀寫保護所以在設定時必須要進入/出init mode
RTC_EnterInitMode();
RTC_ExitInitMode();
再網上我最有疑問的設定是下列這句
if(RTC_ReadBackupRegister(RTC_BKP_DR0) != 0x1234)
這句設定是指說DR0必須要判斷是否為第一次寫入設定如果不是則進入if繼續做初始化。否則就不做初始化設定。
因為我的板子是使用F4系列的Discovery所以我的RTC沒有LSE可以用,所以我的設定是LSI (internal和External Clock)。
int main(void)
{
LED_Config();
Delay_Init(); /* initialize delay function */
USART_Config();
RTC_Config();
printf("\n--------------------------------------------------\n");
printf("\n STM32F4 RTC Function test \n");
printf("\n--------------------------------------------------\n"); while()
{
RTC_GetTime(RTC_Format_BIN, &data_time);
RTC_GetDate(RTC_Format_BIN, &data_date);
printf("%d-%02d-%02d ,", data_date.RTC_Year + , data_date.RTC_Month, data_date.RTC_Date);
printf("%02d:%02d:%02d\n", data_time.RTC_Hours, data_time.RTC_Minutes, data_time.RTC_Seconds);
DelayMs();
}
}
(STM32F4) Real-time Clock的更多相关文章
- STM32F4 Timer External Clock TI2 Both Edges Demo
#define CLK_FREQ ( 10000 ) #define CORE_FREQ ( 168000000 ) static void TIM_GPIO_Config( void ) { GPI ...
- SPI中的极性CPOL和相位CPHA
详解SPI中的极性CPOL和相位CPHA SPI由于接口相对简单(只需要4根线),用途算是比较广泛,主要应用在 EEPROM,FLASH, 实时时钟,AD转换器,还有数字信号处理器和数字信号解码器之间 ...
- STM32F4时钟配置分析
//学习STM32F4的过程中关于时钟上面讲的比较好 特地转发与大家分享 STM32F4时钟设置分析 原文博客链接:http://blog.csdn.net/jdh99,作者:jdh,转载请注明. 环 ...
- STM32F4 SPI with DMA
STM32F4 SPI with DMA A few people have requested code, so I thought I’d post the code showing how I’ ...
- 78-WS2812-Library (STM32F4)
78-WS2812-Library (STM32F4) //-------------------------------------------------------------- // File ...
- VGA Output from STM32F4 Discovery board
VGA Output from STM32F4 Discovery board I love the web! There are so many cool projects out there, a ...
- STM32F4 External interrupts
STM32F4 External interrupts Each STM32F4 device has 23 external interrupt or event sources. They are ...
- STM32F4: Generating parallel signals with the FSMC
STM32F4: Generating parallel signals with the FSMC The goal: The memory controller can be used to ge ...
- How to properly set clock speed for STM32F4xx devices
http://stm32f4-discovery.com/2015/01/properly-set-clock-speed-stm32f4xx-devices/ I see that you have ...
随机推荐
- 最小的VIM操作指南
最小VIM操作指南 vim的操作命令非常多,为了能开始工作,必须学会一个最小的vim操作集合,这里做个总结. 1.插入.追加.插入新行 i:在当前光标所在字符的前面插入,当前字符及其后面的字符后撤 a ...
- AWT和布局管理器
AWT(Abstract Window Toolkit)抽象窗口开发包 component:可以显示出来的与用户进行交互的图形元素 container:容纳其他component元素的容器:conti ...
- DLL卸载
[DLL卸载] 1.扫描Module.通过CreateToohelp32Snapshot.Module32First.Module32Next来完成. 2.通过FreeLibrary来卸载.通过在ke ...
- FZU2282 Wand
题意 n个数字,要求至少k个数字位置不变,其余进行错排的方案数 分析 错排公式: D(n)=(n-1)[D(n-2)+D(n-1)] 如果n个数字,i个数字位置不变,其余进行错排的的方案数是C(n, ...
- Entity Framework 6.0 Tutorials(11):Download Sample Project
Download Sample Project: Download a sample project for Entity Framework 6 Database-First model below ...
- Appium移动端自动化测试之元素定位(三)
1.name定位 driver.find_element_by_id(') driver.find_element_by_id(') driver.find_element_by_name('登录') ...
- 编写高质量代码改善C#程序的157个建议——建议41:实现标准的事件模型
建议41:实现标准的事件模型 上一建议中,我们实现了一个带事件通知的文件传输类FileUploader.虽然已经满足需求,但却不符合C#的编码规范,查看EventHandler的原型声明: publi ...
- 编写高质量代码改善C#程序的157个建议——建议36:使用FCL中的委托声明
建议36:使用FCL中的委托声明 FCL中存在3类这样的委托声明,它们分别是:Action.Func.Predicate.尤其是在它们的泛型版本出来以后,已经能够满足我们在实际编码过程中的大部分需求. ...
- 加载 页面 中js 方法
js 文件中 var mingchen= mingchen|| { init: function (){ } }; 文件中 mingchen.init(); 注意问题: 在新加载 页面中 ...
- ubuntu 下python安装及hello world
//@desn:ubuntu 下python安装及hello world //@desn:码字不宜,转载请注明出处 //@author:张慧源 <turing_zhy@163.com> ...