历史上的今天
返回首页

历史上的今天

今天是:2024年08月29日(星期四)

正在发生

2019年08月29日 | STM32F429 >> 19. RTC_实时时钟(Code)

2019-08-29 来源:eefocus

配置及读取日期和时间

此工程没有读取亚秒值。


若想让时钟断电后持续计时,则不要使能RTC_Config() 函数中的后备域访问。


bsp_rtc.h

/**

  ******************************************************************************

  * @file    bsp_rtc.h

  * @author  Waao

  * @version V1.0.0

  * @date    17-Feb-2019

  * @brief   This file contains some board support package's definition for the RTC.

  *            

  ******************************************************************************

  * @attention

  *

  * None

*

  ******************************************************************************

  */

#ifndef __BSP_RTC_H_

#define __BSP_RTC_H_


#include "stm32f4xx_rtc.h"

#include

#include



//============== Date values =================

#define YEAR                 19

#define MONTH                2 // RTC_Month_February

#define DATE                 19

#define WEEKDAY              2 // RTC_Weekday_Tuesday

//============================================


//============== Time values =================

#define HOURS                0x00

#define MINUTES              0x00

#define SECONDS              0x00

//============================================


void RTC_Config(void);

void RTC_DateConfig(void);

void RTC_TimeConfig(void);

#endif



bsp_rtc.c

/**

  ******************************************************************************

  * @file    bsp_rtc.c

  * @author  Waao

  * @version V1.0.0

  * @date    17-Feb-2019

  * @brief   This file contains some board support package's functions for the RTC.

  *            

  ******************************************************************************

  * @attention

  *

  * None

*

  ******************************************************************************

  */


#include "bsp_rtc.h"


/**

  * @brief  Initialize the RTC.

  * @param  None  

  * @retval None

  */

void RTC_Config(void)

{

RTC_InitTypeDef RTC_InitStructure;

//RCC_APB1PeriphClockLPModeCmd(RCC_APB1Periph_PWR, ENABLE);

/* If we ENABLE the Backup Access, we can write the RTC, RCC_BDCR, PWR_CSR register. 

* So that we can modify the time values again. But we cannot modify the time values 

* if we disable that, the time values will keep count forever even through we power 

* down the device.

* If you want to make it counter run forever, you can DISABLE that.

*/

PWR_BackupAccessCmd(ENABLE);

RCC_LSEConfig(RCC_LSE_ON);

while(RCC_GetFlagStatus(RCC_FLAG_LSERDY) == RESET);

RCC_RTCCLKConfig(RCC_RTCCLKSource_LSE);

RCC_RTCCLKCmd(ENABLE);

RTC_InitStructure.RTC_HourFormat = RTC_HourFormat_24;

RTC_InitStructure.RTC_AsynchPrediv = 127;

RTC_InitStructure.RTC_SynchPrediv = 255;

RTC_Init(&RTC_InitStructure);

}



/**

  * @brief  Configure the now date.

  * @param  None  

  * @retval None

  */

void RTC_DateConfig(void)

{

RTC_DateTypeDef SetDate_InitStructure; 


// SetDate_InitStructure

SetDate_InitStructure.RTC_Year = YEAR;

SetDate_InitStructure.RTC_Month =MONTH;

SetDate_InitStructure.RTC_WeekDay = WEEKDAY;

SetDate_InitStructure.RTC_Date = DATE;


RTC_SetDate(RTC_Format_BIN, &SetDate_InitStructure);

}



/**

  * @brief  Configure the now time.

  * @param  None  

  * @retval None

  */

void RTC_TimeConfig(void)

{

RTC_TimeTypeDef SetTime_InitStructure;

// SetTime_InitStructure

SetTime_InitStructure.RTC_H12 = RTC_H12_PM;

SetTime_InitStructure.RTC_Hours = HOURS;

SetTime_InitStructure.RTC_Minutes = MINUTES;

SetTime_InitStructure.RTC_Seconds = SECONDS;


RTC_SetTime(RTC_Format_BIN, &SetTime_InitStructure);

}


main.c

/**

  ******************************************************************************

  * @file    main.c

  * @author  Waao

  * @version V1.0.0

  * @date    19-Feb-2019

  * @brief   RTC operation.

  *            

  ******************************************************************************

  * @attention

  *

  * None

*

  ******************************************************************************

  */


#include

#include

#include



/**

 1. @brief  Main

 2. @param  None  

 3. @retval None

  */

int main(void)

{

RTC_DateTypeDef GetDate_InitStructure;

RTC_TimeTypeDef GetTime_InitStructure;


// We can use the print function

USART_GPIO_Config();

USART1_Config();

// We can use the Delay function

SysTick_Init();

RTC_Config();

RTC_DateConfig();

RTC_TimeConfig();

while(1)

{

RTC_GetDate(RTC_Format_BIN, &GetDate_InitStructure);

printf("nToday is 20%d-%d-%d, and the WeekDay is %d", GetDate_InitStructure.RTC_Year, GetDate_InitStructure.RTC_Month, GetDate_InitStructure.RTC_Date, GetDate_InitStructure.RTC_WeekDay);

RTC_GetTime(RTC_Format_BIN, &GetTime_InitStructure);

printf("nThe real time is %0.2d:%0.2d:%0.2d", GetTime_InitStructure.RTC_Hours, GetTime_InitStructure.RTC_Minutes, GetTime_InitStructure.RTC_Seconds);

printf("n");

Delay(100000);

}

}


闹钟

闹钟编程指南:


配置好基本的日期和时间;

开启闹钟中断(NVIC, EXTI);

配置闹钟中断结构体并使能中断;

编写闹钟中断函数执行代码;

bsp_rtc.h

//================= Alarm ====================

#define RTC_Alarm_Channel    RTC_Alarm_IRQn

#define RTC_Alarm_EXTI_LINE  EXTI_Line17

#define Specified_RTC_Alarm  RTC_Alarm_A

#define Alarm_HOURS          0

#define Alarm_MINUTES        0

#define Alarm_SECONDS        1

//============================================


bsp_rtc.c

/**

  * @brief  Configure the Alarm.

  * @param  None  

  * @retval None

  */

void RTC_AlarmConfig(void)

{

RTC_AlarmTypeDef Alarm_InitStructure;

NVIC_Config();

EXTI_Config();

RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE);

RTC_AlarmCmd(Specified_RTC_Alarm, DISABLE);

// Alarm_InitStructure

Alarm_InitStructure.RTC_AlarmTime.RTC_H12 = RTC_H12_PM;

Alarm_InitStructure.RTC_AlarmTime.RTC_Hours = Alarm_HOURS;

Alarm_InitStructure.RTC_AlarmTime.RTC_Minutes = Alarm_MINUTES;

Alarm_InitStructure.RTC_AlarmTime.RTC_Seconds = Alarm_SECONDS;

/* You can dicide which field is invalid.

* For example, if you configure the RTC_AlarmMask_DateWeekDay is invalid, the alarm will 

* trigger each day.

*/

Alarm_InitStructure.RTC_AlarmMask = RTC_AlarmMask_DateWeekDay;

Alarm_InitStructure.RTC_AlarmDateWeekDaySel = RTC_AlarmDateWeekDaySel_WeekDay;

Alarm_InitStructure.RTC_AlarmDateWeekDay = 19;

RTC_SetAlarm(RTC_Format_BIN, Specified_RTC_Alarm, &Alarm_InitStructure);

RTC_ITConfig(RTC_IT_ALRA, ENABLE);

RTC_AlarmCmd(Specified_RTC_Alarm, ENABLE);

  RTC_ClearFlag(RTC_FLAG_ALRAF);

  EXTI_ClearITPendingBit(EXTI_Line17);

}


bsp_exti.c

/**

  * @brief  Configure the NVIC

  * @param  None

  * @retval None

  */

void NVIC_Config(void)

{

NVIC_InitTypeDef NVIC_InitStructure;

//Configure the NVIC to prioritygroup1

NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);

//Configure the preemption priority to 1

NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;

//Configure the subpriority to 1

NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;

//Enable the interrupt channel

NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;

NVIC_InitStructure.NVIC_IRQChannel = RTC_Alarm_Channel;

NVIC_Init(&NVIC_InitStructure);

}



/**

  * @brief  Configure the EXTI

  * @param  None

  * @retval None

  */

void EXTI_Config(void)

{

EXTI_InitTypeDef EXTI_InitStructure;

EXTI_InitStructure.EXTI_Line = RTC_Alarm_EXTI_LINE;

EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;

EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising;

EXTI_InitStructure.EXTI_LineCmd = ENABLE;

EXTI_Init(&EXTI_InitStructure);

}



stm32f4xx_it.c

/**

  * @brief  This function handles RTC Alarm.

  * @param  None

  * @retval None

  */

void RTC_Alarm_IRQHandler(void)

{

if(RTC_GetITStatus(RTC_IT_ALRA) == SET)

{

LED1_ON;

printf("nAlarm");

}

  RTC_ClearITPendingBit(RTC_IT_ALRA);

  EXTI_ClearITPendingBit(EXTI_Line17);

}


main.c

/**

  ******************************************************************************

  * @file    main.c

  * @author  Waao

  * @version V1.0.0

  * @date    19-Feb-2019

  * @brief   RTC operation.

  *            

  ******************************************************************************

  * @attention

  *

  * None

*

  ******************************************************************************

  */

#include

#include

#include

#include

#include

#include



/**

  * @brief  Main

  * @param  None  

  * @retval None

  */

int main(void)

{

RTC_DateTypeDef GetDate_InitStructure;

RTC_TimeTypeDef GetTime_InitStructure;


// We can use the print function

USART_GPIO_Config();

USART1_Config();

// We can use the Delay function

SysTick_Init();

KEY_GPIO_Config();

LED_GPIO_Config();

RTC_Config();

RTC_DateConfig();

RTC_TimeConfig();

RTC_AlarmConfig();

while(1)

{

RTC_GetDate(RTC_Format_BIN, &GetDate_InitStructure);

printf("nToday is 20%d-%d-%d, and the WeekDay is %d", GetDate_InitStructure.RTC_Year, GetDate_InitStructure.RTC_Month, GetDate_InitStructure.RTC_Date, GetDate_InitStructure.RTC_WeekDay);

RTC_GetTime(RTC_Format_BIN, &GetTime_InitStructure);

printf("nThe real time is %0.2d:%0.2d:%0.2d", GetTime_InitStructure.RTC_Hours, GetTime_InitStructure.RTC_Minutes, GetTime_InitStructure.RTC_Seconds);

推荐阅读

史海拾趣

申风(everanalog)公司的发展小趣事

在申风(everanalog)公司的发展过程中,技术突破和产品创新一直是其核心竞争力。公司不断投入研发资源,致力于开发出具有自主知识产权的集成电路产品。其中,多通道整合型电源管理芯片的成功研发,不仅填补了国内市场的空白,也赢得了客户的广泛认可。这些技术突破和产品创新为公司的快速发展奠定了坚实的基础。

C-MEDIA公司的发展小趣事

近年来,网络游戏市场呈现出蓬勃发展的态势,C-MEDIA公司也看到了其中的商机。于是,公司通过收购C&C Media,进一步深入日本网络游戏市场。这次收购不仅帮助C-MEDIA公司把握了该市场的成长机遇,也有效地拓展了其海外运营实力。C&C Media旗下的网络游戏门户网站“MK-STYLE”为个人用户提供了丰富的网络游戏服务,进一步巩固了C-MEDIA在网络游戏领域的市场地位。

GWM Associates公司的发展小趣事
在需要高灵敏度信号检测的实验中,如物理实验、生物信号检测等。
Echelon公司的发展小趣事

进入21世纪后,Echelon在智能楼宇领域取得了重大突破。公司凭借其先进的LonWorks技术和能源管理服务方案,成功为多个大型楼宇提供了智能化的能源管理解决方案。这些解决方案不仅提高了楼宇的能源利用效率,还降低了运行成本,赢得了客户的广泛赞誉。Echelon在智能楼宇领域的成功,进一步巩固了其在能源管理市场的领先地位。

ARCOL公司的发展小趣事

为了进一步扩大市场份额,ARCOL公司积极寻求与国内外知名企业的合作机会。通过与合作伙伴建立战略合作关系,ARCOL成功打入国际市场,产品销量大幅提升。同时,公司还积极参加各类国际电子展会和交流活动,与全球客户建立了紧密的合作关系,为公司的全球化发展打下了坚实的基础。

Applied Engineering Products (AEP)公司的发展小趣事

在电子行业的早期,AEP公司以其卓越的技术团队和对市场需求的敏锐洞察力脱颖而出。公司研发出了一款高效能、低成本的电源管理芯片,这一创新产品迅速在市场上获得了广泛的认可。随着这款产品的热销,AEP公司的知名度逐渐提升,其产品线也逐渐扩展到其他电子元器件领域。公司不断投入研发,推动技术创新,逐渐在电子行业站稳了脚跟。

问答坊 | AI 解惑

电子密码锁设计

本任务要求设计者设计制作一个较成熟的电子密码锁系统,具备以下功能: 1. 限时报警,当密码输入时间操作限制,进行声光报警 2.连续输入密码错误次数超过3次,进行声光报警,同时自动延时下次密码输入时间,该时间可由用户设定。 3.密码可由用 ...…

查看全部问答>

成功解决FPGA设计时序问题的三大要点

FPGA的设计与高速接口技术可以帮助你满足今天的市场要求,但也提出了一些有趣的设计挑战。为了确保存储器接口的数据传输准确,在超过200兆赫兹以上,进行时序分析将发挥更突出的作用,以识别和解决系统运行的问题。在这些频率内,最重要的是创建和 ...…

查看全部问答>

ActiveSyc连接不上

开发BSP上的USB function driver,已结束,mass storage disk也已能正常工作 但连接USB Serial client driver,和PC端的Activesyc连接时,设备端已显示 device connected,但PC端ActiveSyc却显示一直在连接,绿色icon一直旋转,但连接不上 debug下最后 ...…

查看全部问答>

北京有没有好的嵌入式方面的单位

小弟现在想了解下。。已经做了一年多的这方面工作。。现在6k 向1w挺进。。。用c c++ 在linux下开发多…

查看全部问答>

【规整贴】MSP430G2开发板学习精华帖

整合了MSP430G2开发板的学习资料,包括LaunchPad实验板触摸感应子卡的内容,希望对各位正在学习的坛友有帮助,同时更希望各位将自己的学习心得记录下来同大家分享,小班持续更新该整合贴内容,希望各位踊跃参与MSP430 LaunchPad实验板触摸感应子卡 ...…

查看全部问答>

了解一下关于STM32电流过大的问题

今天STM32做的一个工具突然罢工,测试发现电流过大(0.07A,正常时为0.03A),换了最后的一块STM32芯片就OK了。出问题的芯片重新焊在新的PCB上,外围只焊接电源,晶振,USB接口,其他IO口都悬空,发现电流就已经到了0.07A。以前由于误操作,曾经 ...…

查看全部问答>

IO口输入状态的初始化问题

想要利用430的IO口做一个键盘,用P1的下降沿中断,可是数据手册上说PxIN寄存器复位值为随机值,且PxIN是只读存储器,写无效,那么怎么确保PxIN每位的值是1,然后按下和IO口相连的按键时能产生中断?如果不能确保是1,有的是0,按下按键不就成了上升 ...…

查看全部问答>

请教msp149和mspg2553之间的用串口通讯协议需要自己写吗?

请教msp149和mspg2553之间的用串口通讯协议需要自己写吗?     msp430f149串口多机通信有地址为模式,但是g2553没有,用串口通信需要自己写协议吗?有没有简单点的解决办法?   请教一下、…

查看全部问答>

基于zigbee多点大气温湿度采集系统

大神们啊  小弟要毕业了,但是要做毕业设计啊!!选的是zigbee,但是我都没有接触过啊  这两天看了一下 毫无头绪啊 哎 求大神们做过基于zigbee多点大气温湿度采集系统相关的设计可以给我发一些资料吗??小弟在此跪谢了!!! ...…

查看全部问答>

ADI处理器丛书---高速设计技术

这本书只能用好的不得了形容了,反正我看了是受益匪浅,高清完整,欢迎喜欢ADI技术的,喜欢模拟技术的下载学习。…

查看全部问答>