OpenEdv-开源电子网

 找回密码
 立即注册
正点原子全套STM32/Linux/FPGA开发资料,上千讲STM32视频教程免费下载...
查看: 10143|回复: 5

STM32F051的RTC时钟设置失败

[复制链接]

5

主题

21

帖子

0

精华

初级会员

Rank: 2

积分
81
金钱
81
注册时间
2014-11-4
在线时间
4 小时
发表于 2015-1-17 17:43:55 | 显示全部楼层 |阅读模式
5金钱
我参考STM32F0固件库设置RTC时钟,每次运行到RTC_SetTime(RTC_Format_BIN, &RTC_TimeStructure) == ERROR时都是设置失败,找不到原因,请大家帮忙看看,谢谢。
主函数只调用一个函数:RTC_Init_Define();

RTC设置如下:
[mw_shl_code=c,true]#include "rtc.h" #include "usart1.h" __IO uint32_t AsynchPrediv = 0, SynchPrediv = 0; RTC_TimeTypeDef RTC_TimeStructure; RTC_InitTypeDef RTC_InitStructure; RTC_AlarmTypeDef RTC_AlarmStructure; void SET_RTC_Init(void) { RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE); PWR_BackupAccessCmd(ENABLE); RCC_LSEConfig(RCC_LSE_ON);[/mw_shl_code] [mw_shl_code=c,true] while(RCC_GetFlagStatus(RCC_FLAG_LSERDY) == RESET) { ; } RCC_RTCCLKConfig(RCC_RTCCLKSource_LSE); SynchPrediv = 0xFF; AsynchPrediv = 0x7F; RCC_RTCCLKCmd(ENABLE); RTC_WaitForSynchro(); RTC_InitStructure.RTC_AsynchPrediv = AsynchPrediv; RTC_InitStructure.RTC_SynchPrediv = SynchPrediv; RTC_InitStructure.RTC_HourFormat = RTC_HourFormat_24; if (RTC_Init(&RTC_InitStructure) == ERROR) { printf("\n\r /!\\***** RTC Prescaler Config failed ********/!\\ \n\r"); } RTC_TimeRegulate(); } void Check_RTC_SET(void) { if (RCC_GetFlagStatus(RCC_FLAG_PORRST) != RESET) { printf("\r\n Power On Reset occurred....\n\r"); } else { if (RCC_GetFlagStatus(RCC_FLAG_PINRST) != RESET)      { printf("\r\n External Reset occurred....\n\r"); } } printf("\n\r No need to configure RTC....\n\r"); RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE); PWR_BackupAccessCmd(ENABLE); RTC_WaitForSynchro(); RTC_ClearFlag(RTC_FLAG_ALRAF); RTC_TimeShow(); RTC_AlarmShow(); } void RTC_NVIC_Init(void) { NVIC_InitTypeDef NVIC_InitStructure; NVIC_InitStructure.NVIC_IRQChannel = RTC_IRQn; NVIC_InitStructure.NVIC_IRQChannelPriority = 0; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); } void RTC_Init_Define(void) { if(RTC_ReadBackupRegister(RTC_BKP_DR0) != BKP_VALUE) { SET_RTC_Init(); } else { Check_RTC_SET(); } RTC_NVIC_Init(); } void RTC_TimeShow(void) { /* Get the current Time */ RTC_GetTime(RTC_Format_BIN, &RTC_TimeStructure); printf("\n\r The current time is : %0.2d:%0.2d:%0.2d \n\r", RTC_TimeStructure.RTC_Hours, RTC_TimeStructure.RTC_Minutes, RTC_TimeStructure.RTC_Seconds); } void RTC_AlarmShow(void) { /* Get the current Alarm */ RTC_GetAlarm(RTC_Format_BIN, RTC_Alarm_A, &RTC_AlarmStructure); printf("\n\r The current alarm is : %0.2d:%0.2d:%0.2d \n\r", RTC_AlarmStructure.RTC_AlarmTime.RTC_Hours, RTC_AlarmStructure.RTC_AlarmTime.RTC_Minutes, RTC_AlarmStructure.RTC_AlarmTime.RTC_Seconds); } void RTC_TimeRegulate(void) { uint32_t tmp_hh = 0xFF, tmp_mm = 0xFF, tmp_ss = 0xFF; printf("\n\r==============Time Settings=====================================\n\r"); RTC_TimeStructure.RTC_H12 = RTC_H12_AM; printf(" Please Set Hours:\n\r"); while (tmp_hh == 0xFF) { tmp_hh = USART_Scanf(23);//最大设置值为23 RTC_TimeStructure.RTC_Hours = tmp_hh; } printf(" %0.2d\n\r", tmp_hh); printf(" Please Set Minutes:\n\r"); while (tmp_mm == 0xFF) { tmp_mm = USART_Scanf(59); RTC_TimeStructure.RTC_Minutes = tmp_mm; } printf(" %0.2d\n\r", tmp_mm); printf(" Please Set Seconds:\n\r"); while (tmp_ss == 0xFF) { tmp_ss = USART_Scanf(59); RTC_TimeStructure.RTC_Seconds = tmp_ss; } printf(" %0.2d\n\r", tmp_ss); /* 每次运行到此处都出错,设置不了 */ if(RTC_SetTime(RTC_Format_BIN, &RTC_TimeStructure) == ERROR) { printf("\n\r>> !! RTC Set Time failed. !! <<\n\r"); } else { printf("\n\r>> !! RTC Set Time success. !! <<\n\r"); RTC_TimeShow(); /* Indicator for the RTC configuration */ RTC_WriteBackupRegister(RTC_BKP_DR0, BKP_VALUE); } tmp_hh = 0xFF; tmp_mm = 0xFF; tmp_ss = 0xFF; /* Disable the Alarm A */ RTC_AlarmCmd(RTC_Alarm_A, DISABLE); printf("\n\r==============Alarm A Settings=====================================\n\r"); RTC_AlarmStructure.RTC_AlarmTime.RTC_H12 = RTC_H12_AM; printf(" Please Set Alarm Hours:\n\r"); while (tmp_hh == 0xFF) { tmp_hh = USART_Scanf(23); RTC_AlarmStructure.RTC_AlarmTime.RTC_Hours = tmp_hh; } printf(" %0.2d\n\r", tmp_hh); printf(" Please Set Alarm Minutes:\n\r"); while (tmp_mm == 0xFF) { tmp_mm = USART_Scanf(59); RTC_AlarmStructure.RTC_AlarmTime.RTC_Minutes = tmp_mm; } printf(" %0.2d\n\r", tmp_mm); printf(" Please Set Alarm Seconds:\n\r"); while (tmp_ss == 0xFF) { tmp_ss = USART_Scanf(59); RTC_AlarmStructure.RTC_AlarmTime.RTC_Seconds = tmp_ss; } printf(" %0.2d", tmp_ss); /* Set the Alarm A */ RTC_AlarmStructure.RTC_AlarmDateWeekDay = 0x31; RTC_AlarmStructure.RTC_AlarmDateWeekDaySel = RTC_AlarmDateWeekDaySel_Date; RTC_AlarmStructure.RTC_AlarmMask = RTC_AlarmMask_DateWeekDay; /* Configure the RTC Alarm A register */ RTC_SetAlarm(RTC_Format_BIN, RTC_Alarm_A, &RTC_AlarmStructure); printf("\n\r>> !! RTC Set Alarm success. !! <<\n\r"); RTC_AlarmShow(); /* Enable the RTC Alarm A Interrupt */ RTC_ITConfig(RTC_IT_ALRA, ENABLE); /* Enable the alarm A */ RTC_AlarmCmd(RTC_Alarm_A, ENABLE); }[/mw_shl_code]
中断函数
[mw_shl_code=c,true]void RTC_IRQHandler(void) { if(RTC_GetITStatus(RTC_IT_ALRA) != RESET) { RTC_ClearITPendingBit(RTC_IT_ALRA); } }[/mw_shl_code]

最佳答案

查看完整内容[请看2#楼]

回复【4楼】废墟崛起之厦: --------------------------------- 搞定了,原来是电池没有电了,谢谢了
正点原子逻辑分析仪DL16劲爆上市
回复

使用道具 举报

5

主题

21

帖子

0

精华

初级会员

Rank: 2

积分
81
金钱
81
注册时间
2014-11-4
在线时间
4 小时
 楼主| 发表于 2015-1-17 17:43:56 | 显示全部楼层
回复【4楼】废墟崛起之厦:
---------------------------------
搞定了,原来是电池没有电了,谢谢了
回复

使用道具 举报

5

主题

21

帖子

0

精华

初级会员

Rank: 2

积分
81
金钱
81
注册时间
2014-11-4
在线时间
4 小时
 楼主| 发表于 2015-1-17 17:48:14 | 显示全部楼层
主控是STM32F051
 时钟使用外部晶振32.768KHz
 使用串口输入获取数据可以正确获取,但是不能设置写入,没次都是设置失败
 设置失败在程序的117行
请大家帮下忙,谢谢
回复

使用道具 举报

530

主题

11万

帖子

34

精华

管理员

Rank: 12Rank: 12Rank: 12

积分
165540
金钱
165540
注册时间
2010-12-1
在线时间
2117 小时
发表于 2015-1-17 23:04:37 | 显示全部楼层
帮顶....
回复

使用道具 举报

57

主题

195

帖子

0

精华

中级会员

Rank: 3Rank: 3

积分
446
金钱
446
注册时间
2013-2-20
在线时间
1 小时
发表于 2015-1-18 00:14:18 | 显示全部楼层
回复【2楼】snyanglq:
---------------------------------
你这是清风的F030的程序吧,我的能设置,你可以加我QQ:792195733  交流下 程序 在公司 不好贴哦
创新超越梦想,拼搏创造奇迹....
回复

使用道具 举报

5

主题

21

帖子

0

精华

初级会员

Rank: 2

积分
81
金钱
81
注册时间
2014-11-4
在线时间
4 小时
 楼主| 发表于 2015-1-19 11:03:42 | 显示全部楼层
回复【4楼】废墟崛起之厦:
---------------------------------
谢谢你,其实我是参考固件库的例程写,听你说清风有这个程序我到网上下载参考了下,发现问题了,原来那个中断17不能去掉,我原先去掉了,要回来就可以了,至于原因具体还有看看手册,现在可以正确设置数据了。
但是又出了个新问题了,我下电之后不能保存数据,每次上电要重新设置,这是什么原因呢?我在程序126行设置正确之后有写入寄存器啊,而且不掉电的情况下是能保存的,比如我现在把程序重新下载一次,仍然能接着上次的时间累加,但是关闭电源重新打开就不行了。
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则



关闭

原子哥极力推荐上一条 /2 下一条

正点原子公众号

QQ|手机版|OpenEdv-开源电子网 ( 粤ICP备12000418号-1 )

GMT+8, 2025-8-8 18:19

Powered by OpenEdv-开源电子网

© 2001-2030 OpenEdv-开源电子网

快速回复 返回顶部 返回列表