微波EDA网,见证研发工程师的成长!
首页 > 硬件设计 > 嵌入式设计 > stm32的RTC操作

stm32的RTC操作

时间:12-03 来源:互联网 点击:
分为两个文件:

rtc.c

/*******************************************************************************
* 文件名 :rtc.c
* 描述 :wit_yuan
* 论坛 :
**********************************************************************************/
#include "rtc.h"
#include "stdio.h"
#include "misc.h"
uint8_t const *WEEK_STR[] = {"日", "一", "二", "三", "四", "五", "六"};
uint8_t const *zodiac_sign[] = {"猪", "鼠", "牛", "虎", "兔", "龙", "蛇", "马", "羊", "猴", "鸡", "狗"};

/* 秒中断标志,进入秒中断时置1,当时间被刷新之后清0 */
__IO uint32_t TimeDisplay;
extern struct rtc_time systemtime;
/*
* 函数名:NVIC_Configuration
* 描述 :配置RTC秒中断的主中断优先级为1,次优先级为0
* 输入 :无
* 输出 :无
* 调用 :外部调用
*/
void RTC_NVIC_Configuration(void)
{
NVIC_InitTypeDef NVIC_InitStructure;

/* Configure one bit for preemption priority */
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);

/* Enable the RTC Interrupt */
NVIC_InitStructure.NVIC_IRQChannel = RTC_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}

/*
* 函数名:RTC_Configuration
* 描述 :配置RTC
* 输入 :无
* 输出 :无
* 调用 :外部调用
*/
void RTC_Configuration(void)
{
RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR | RCC_APB1Periph_BKP, ENABLE);
PWR_BackupAccessCmd(ENABLE);
BKP_DeInit();
RCC_LSEConfig(RCC_LSE_ON);
while (RCC_GetFlagStatus(RCC_FLAG_LSERDY) == RESET)
{}
RCC_RTCCLKConfig(RCC_RTCCLKSource_LSE);
RCC_RTCCLKCmd(ENABLE);
RTC_WaitForSynchro();
RTC_WaitForLastTask();
RTC_ITConfig(RTC_IT_SEC, ENABLE);
RTC_WaitForLastTask();
RTC_SetPrescaler(32767);
RTC_WaitForLastTask();
}

/*
* 函数名 :Time_Regulate
* 描述 :返回用户在超级终端中输入的时间值,并将值储存在
* RTC 计数寄存器中。
* 输出 :用户在超级终端中输入的时间值,单位为 s
* 调用 :内部调用
* editted : 2014-07-05
* author : wit_yuan
*/
extern struct rtc_time systemtime;

uint32_t Time_Regulate(struct rtc_time *tm)
{
tm->tm_year = systemtime.tm_year + 2000;
tm->tm_mon = systemtime.tm_mon;
tm->tm_mday = systemtime.tm_mday;
tm->tm_hour = systemtime.tm_hour;
tm->tm_min = systemtime.tm_min;
tm->tm_sec = systemtime.tm_sec;
/* Return the value to store in RTC counter register */
return(((systemtime.tm_hour) * 3600 + (systemtime.tm_min) * 60 + systemtime.tm_sec));
}

/*
* 函数名:Time_Adjust
* 描述 :时间调节
* 输入 :无
* 输出 :无
* 调用 :外部调用
*/
void Time_Adjust(struct rtc_time *tm)
{
/* Wait until last write operation on RTC registers has finished */
RTC_WaitForLastTask();
/* Get time entred by the user on the hyperterminal */
Time_Regulate(tm);

/* Get wday */
GregorianDay(tm);
/* Change the current time */
RTC_SetCounter(mktimev(tm));
/* Wait until last write operation on RTC registers has finished */
RTC_WaitForLastTask();
}

/*
* 函数名:Time_Display
* 描述 :显示当前时间值
* 输入 :-TimeVar RTC计数值,单位为 s
* 输出 :无
* 调用 :内部调用
*/
void Time_Display(uint32_t TimeVar,struct rtc_time *tm)
{
volatile uint32_t THH = 0, TMM = 0, TSS = 0;
uint32_t BJ_TimeVar;
/*把标准时间转换为北京时间*/
BJ_TimeVar =TimeVar + 8*60*60;

to_tm(BJ_TimeVar, tm);/*把定时器的值转换为北京时间*/
}
/*
* 函数名:Time_Show
* 描述 :在超级终端中显示当前时间值
* 输入 :无
* 输出 :无
* 调用 :外部调用
*/
void Time_Show(void)
{

}

uint8_t USART_Scanf(uint32_t value)
{
uint32_t index = 0;
uint32_t tmp[2] = {0, 0};

while (index < 2)
{
while (USART_GetFlagStatus(USART1, USART_FLAG_RXNE) == RESET)
{}
tmp[index++] = (USART_ReceiveData(USART1));
if ((tmp[index - 1] < 0x30) || (tmp[index - 1] > 0x39))
{
index--;
}
}
index = (tmp[1] - 0x30) + ((tmp[0] - 0x30) * 10);
if (index > value)
{
return 0xFF;
}
return index;
}

void Time_Config(struct rtc_time *tm)
{
if (BKP_ReadBackupRegister(BKP_DR1) != 0xA5A5)
{
RTC_Configuration();
Time_Adjust(tm);
BKP_WriteBackupRegister(BKP_DR1, 0xA5A5);
}
else
{
if (RCC_GetFlagStatus(RCC_FLAG_PORRST) != RESET)
{
}
else if (RCC_GetFlagStatus(RCC_FLAG_PINRST) != RESET)
{
}
RTC_WaitForSynchro();

RTC_ITConfig(RTC_IT_SEC, ENABLE);

RTC_WaitForLastTask();
}
#ifdef RTCClockOutput_Enable
RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR | RCC_APB1Periph_BKP, ENABLE);
PWR_BackupAccessCmd(ENABLE);
BKP_TamperPinCmd(DISABLE);
BKP_RTCOutputConfig(BKP_RTCOutputSource_CalibClock);
#endif
RCC_ClearFlag();
//Time_Show();
}

/*********************************************************
* funciton : drv_set_time
* description : set the rtc time
* time : 2015-08-22 pm
* author : wit_yuan
***********************************************************/
void drv_set_time(struct rtc_time *tm)
{
RTC_Configuration();
Time_Adjust(tm);
BKP_WriteBackupRegister(BKP_DR1, 0xA5A5);
RCC_ClearFlag();
}

/*********************************************************
* funciton : wit_get_time
* description : get the rtc time
* time : 2015-07-07 am
* author : wit_yuan
***********************************************************/

void drv_get_time(struct rtc_time *tm)
{
uint32_t TimeVar;
//struct rtc_time tm;
uint32_t BJ_TimeVar;
TimeVar = RTC_GetCounter();
*tm = systemtime;
BJ_TimeVar =TimeVar + 8 * 60 * 60;
to_tm(BJ_TimeVar, tm);/*把定时器的值转换为北京时间*/
// printf("%d年%d月%d日 %d时%d分%d秒 \n",tm.tm_year,tm.tm_mon,tm.tm_mday,tm.tm_hour,tm.tm_min,tm.tm_sec);
// return tm;
}

void RTC_IRQHandler(void)
{
if (RTC_GetITStatus(RTC_IT_SEC) != RESET)
{
RTC_ClearITPendingBit(RTC_IT_SEC);
TimeDisplay = 1;
if (TimeDisplay == 1)
{
TimeDisplay = 0;
}
RTC_WaitForLastTask();
}
}

void init_rtc(void )
{
RTC_NVIC_Configuration();
Time_Config(&systemtime);
}

/*****************************END OF FILE****2015-08-23 guide by wit_yuan**************************/

Copyright © 2017-2020 微波EDA网 版权所有

网站地图

Top