目录
1.rtc.c
#include "rtc.h"
__IO uint32_t TimeDisplay = 0;
void 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 = 1;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
void RTC_Configuration(void)
{
/* Enable PWR and BKP clocks */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR | RCC_APB1Periph_BKP, ENABLE);
/* Allow access to BKP Domain */
PWR_BackupAccessCmd(ENABLE);
/* Reset Backup Domain */
BKP_DeInit();
/* Enable the LSI OSC */
RCC_LSICmd(ENABLE);
/* Wait till LSI is ready */
while (RCC_GetFlagStatus(RCC_FLAG_LSIRDY) == RESET)
{}
/* Select the RTC Clock Source */
RCC_RTCCLKConfig(RCC_RTCCLKSource_LSI);
/* Enable RTC Clock */
RCC_RTCCLKCmd(ENABLE);
/* Wait for RTC registers synchronization */
RTC_WaitForSynchro();
/* Wait until last write operation on RTC registers has finished */
RTC_WaitForLastTask();
/* Enable the RTC Second */
RTC_ITConfig(RTC_IT_SEC, ENABLE);
/* Wait until last write operation on RTC registers has finished */
RTC_WaitForLastTask();
/* Set RTC prescaler: set RTC period to 1sec */
RTC_SetPrescaler(40000);
/* Wait until last write operation on RTC registers has finished */
RTC_WaitForLastTask();
/* To output second signal on Tamper pin, the tamper functionality
must be disabled (by default this functionality is disabled) */
BKP_TamperPinCmd(DISABLE);
/* Enable the RTC Second Output on Tamper Pin */
// BKP_RTCOutputConfig(BKP_RTCOutputSource_None);
NVIC_Configuration();
}
void Time_Adjust(int Tmp_HH,int Tmp_MM, int Tmp_SS)
{
/* Wait until last write operation on RTC registers has finished */
RTC_WaitForLastTask();
/* Change the current time */
RTC_SetCounter((Tmp_HH*3600 + Tmp_MM*60 + Tmp_SS));
/* Wait until last write operation on RTC registers has finished */
RTC_WaitForLastTask();
}
uint32_t THH = 0, TMM = 0, TSS = 0;
void Time_Display(uint32_t TimeVar)
{
/* Reset RTC Counter when Time is 23:59:59 */
if (RTC_GetCounter() == 0x0001517F)
{
RTC_SetCounter(0x00);
/* Wait until last write operation on RTC registers has finished */
RTC_WaitForLastTask();
}
/* Compute hours */
THH = TimeVar / 3600;
/* Compute minutes */
TMM = (TimeVar % 3600) / 60;
/* Compute seconds */
TSS = (TimeVar % 3600) % 60;
// printf("Time: %0.2d:%0.2d:%0.2d\r", THH, TMM, TSS);
}
void RTC_IRQHandler(void)
{
if (RTC_GetITStatus(RTC_IT_SEC) != RESET)
{
/* Clear the RTC Second interrupt */
RTC_ClearITPendingBit(RTC_IT_SEC);
TimeDisplay = 1;
/* Wait until last write operation on RTC registers has finished */
RTC_WaitForLastTask();
}
}
2.rtc.h
#ifndef __RTC_H
#define __RTC_H
#include "stm32f10x.h"
void RTC_Configuration(void);
void Time_Adjust(int Tmp_HH,int Tmp_MM, int Tmp_SS);
void Time_Display(uint32_t TimeVar);
extern uint32_t THH, TMM, TSS;
extern __IO uint32_t TimeDisplay;
#endif /* __RTC_H */
3.实时刷新
if (TimeDisplay == 1)
{
Time_Display(RTC_GetCounter());
TimeDisplay = 0;
sprintf((char*)DisBuff,"Time: %0.2d:%0.2d:%0.2d", THH, TMM, TSS);
LCD_DisplayStringLine(Line3,DisBuff);
}