历史上的今天
今天是: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);
史海拾趣
|
FPGA的设计与高速接口技术可以帮助你满足今天的市场要求,但也提出了一些有趣的设计挑战。为了确保存储器接口的数据传输准确,在超过200兆赫兹以上,进行时序分析将发挥更突出的作用,以识别和解决系统运行的问题。在这些频率内,最重要的是创建和 ...… 查看全部问答> |
|
开发BSP上的USB function driver,已结束,mass storage disk也已能正常工作 但连接USB Serial client driver,和PC端的Activesyc连接时,设备端已显示 device connected,但PC端ActiveSyc却显示一直在连接,绿色icon一直旋转,但连接不上 debug下最后 ...… 查看全部问答> |
|
整合了MSP430G2开发板的学习资料,包括LaunchPad实验板触摸感应子卡的内容,希望对各位正在学习的坛友有帮助,同时更希望各位将自己的学习心得记录下来同大家分享,小班持续更新该整合贴内容,希望各位踊跃参与MSP430 LaunchPad实验板触摸感应子卡 ...… 查看全部问答> |
|
今天STM32做的一个工具突然罢工,测试发现电流过大(0.07A,正常时为0.03A),换了最后的一块STM32芯片就OK了。出问题的芯片重新焊在新的PCB上,外围只焊接电源,晶振,USB接口,其他IO口都悬空,发现电流就已经到了0.07A。以前由于误操作,曾经 ...… 查看全部问答> |
|
想要利用430的IO口做一个键盘,用P1的下降沿中断,可是数据手册上说PxIN寄存器复位值为随机值,且PxIN是只读存储器,写无效,那么怎么确保PxIN每位的值是1,然后按下和IO口相连的按键时能产生中断?如果不能确保是1,有的是0,按下按键不就成了上升 ...… 查看全部问答> |
|
请教msp149和mspg2553之间的用串口通讯协议需要自己写吗? 请教msp149和mspg2553之间的用串口通讯协议需要自己写吗? msp430f149串口多机通信有地址为模式,但是g2553没有,用串口通信需要自己写协议吗?有没有简单点的解决办法? 请教一下、… 查看全部问答> |
|
大神们啊 小弟要毕业了,但是要做毕业设计啊!!选的是zigbee,但是我都没有接触过啊 这两天看了一下 毫无头绪啊 哎 求大神们做过基于zigbee多点大气温湿度采集系统相关的设计可以给我发一些资料吗??小弟在此跪谢了!!! ...… 查看全部问答> |




