📄 rtc.c
字号:
/******************** (C) COPYRIGHT 2003 STMicroelectronics ********************
* File Name : rtc.c
* Author : MCD Application Team
* Date First Issued : 30/09/2003
* Description : This file provides all the RTC software functions
********************************************************************************
* History:
* 20/05/03 : First Version.
*******************************************************************************/
#include "rtc.h"
// Global interrupt
#define RTC_GI_Mask 0x0008
#define RTC_GI_Index 3
// OverFlow interrupt
#define RTC_OWI_Mask 0x0004
#define RTC_OWI_Index 2
// Alarm interrupt
#define RTC_AI_Mask 0x0002
#define RTC_AI_Index 1
// Second interrupt
#define RTC_SI_Mask 0x0001
// Configuration Flag Mask
#define RTC_CNF_Mask 0x0010
// Operation OFF flag
#define RTC_RTOFF_Mask 0x0020
/*******************************************************************************
* Function Name : RTC_Delay
* Description : This routine is used to insert a delay
* Input : None
* Return : None
*******************************************************************************/
void RTC_Delay( void )
{
u16 _Tmp;
for (_Tmp = 0x0; _Tmp < 0x7F; _Tmp ++);
}
/*******************************************************************************
* Function Name : RTC_ClearCounter
* Description : This routine is used to clear the RTC counter value
* Input : None
* Return : None
*******************************************************************************/
void RTC_ClearCounter (void)
{
// Wait For Last Task Completion
RTC_WaitForLastTask();
// Enter In Configuration Mode
RTC_EnterCfgMode();
// Wait For Last Task Completion
RTC_WaitForLastTask();
// Clears RTC counter
RTC->CNTH =0x0000;
// Wait For Last Task Completion
RTC_WaitForLastTask();
RTC->CNTL =0x0000;
// Exit From Configuration Mode
RTC_ExitCfgMode ();
// Wait For Last Task Completion
RTC_WaitForLastTask();
// Wait For Last Task Completion
RTC_WaitForLastTask ();
}
/*******************************************************************************
* Function Name : RTC_PrescalerConfig
* Description : This routine is used to set the Prescaler Value
* Input : The New prescaler Value
* Return : None
*******************************************************************************/
void RTC_PrescalerConfig (u32 Xprescaler)
{
if ( RTC_PrescalerValue () != Xprescaler )
{
// Wait For Last Task Completion
RTC_WaitForLastTask ();
// Enter In Configuration Mode
RTC_EnterCfgMode ();
// Wait For Last Task Completion
RTC_WaitForLastTask();
// Set the prescaler MSB part
RTC->PRLH = (Xprescaler & 0x000F0000) >> 16;
// Wait For Last Task Completion
RTC_WaitForLastTask();
// Set the prescaler LSB part
RTC->PRLL = (Xprescaler & 0x0000FFFF);
// Wait For Last Task Completion
RTC_WaitForLastTask();
// Exit From Configuration Mode
RTC_ExitCfgMode ();
// Wait For Last Task Completion
RTC_WaitForLastTask ();
}
}
/*******************************************************************************
* Function Name : RTC_AlarmConfig
* Description : This routine is used to set the RTC alarm Value
* Input : an u32 value that holds the Real Time clock alarm time.
* Return : None
*******************************************************************************/
void RTC_AlarmConfig (u32 Xalarm)
{
// Wait For Last Task Completion
RTC_WaitForLastTask ();
// Enter In Configuration Mode
RTC_EnterCfgMode ();
// Wait For Last Task Completion
RTC_WaitForLastTask();
// Set The MSB part of the Alarm Time
RTC->ALRH = (Xalarm & 0xFFFF0000) >> 16;
// Wait For Last Task Completion
RTC_WaitForLastTask();
// Set The LSB part of the Alarm Time
RTC->ALRL = (Xalarm & 0x0000FFFF);
// Wait For Last Task Completion
RTC_WaitForLastTask();
// Exit From Configuration Mode
RTC_ExitCfgMode ();
// Wait For Last Task Completion
RTC_WaitForLastTask ();
}
/*******************************************************************************
* Function Name : RTC_FlagClear
* Description : This routine is used to clear the RTC flags
* Input : an RTC flag
* Return : None
*******************************************************************************/
void RTC_FlagClear (RTC_FLAGS Xflag)
{
// Wait For Last Task Completion
RTC_WaitForLastTask();
// Enter In Configuration Mode
RTC_EnterCfgMode();
// Wait For Last Task Completion
RTC_WaitForLastTask();
// Clear an RTC flag
RTC->CRL &= ~Xflag;
// Wait For Last Task Completion
RTC_WaitForLastTask();
// Exit From Configuration Mode
RTC_ExitCfgMode ();
// Wait For Last Task Completion
RTC_WaitForLastTask();
}
/*******************************************************************************
* Function Name : RTC_ITClear
* Description : This routine is used to clear the RTC interrupts
* Input : an RTC interrupt
* Return : None
*******************************************************************************/
void RTC_ITClear (RTC_IT Xrtcit)
{
// Wait For Last Task Completion
RTC_WaitForLastTask();
// Enter In Configuration Mode
RTC_EnterCfgMode();
// Clears an RTC interrupt
RTC->CRL &= ~Xrtcit;
// Exit From Configuration Mode
RTC_ExitCfgMode ();
// Wait For Last Task Completion
RTC_WaitForLastTask ();
}
/*******************************************************************************
* Function Name : RTC_EnterCfgMode
* Description : This routine is used to enter in the Configuration Mode
* Input : None
* Return : None
*******************************************************************************/
void RTC_EnterCfgMode(void)
{
// Set the CNF flag to enter in the Configuration Mode
RTC->CRL |= RTC_CNF_Mask;
// Wait For Last Task Completion
RTC_WaitForLastTask ();
}
/*******************************************************************************
* Function Name : RTC_ExitCfgMode
* Description : This routine is used to exit from the Configuration Mode
* Input : None
* Return : None
*******************************************************************************/
void RTC_ExitCfgMode(void)
{
// Reset the CNF flag to exit from the Configuration Mode
RTC->CRL &= ~RTC_CNF_Mask;
// Wait For Last Task Completion
RTC_WaitForLastTask ();
}
/*******************************************************************************
* Function Name : RTC_WaitForLastTask
* Description : This routine waits for the last task completion
* Input : None
* Return : None
*******************************************************************************/
void RTC_WaitForLastTask(void)
{
// Loop until the Last operation Completion
while (!(RTC->CRL & RTC_RTOFF_Mask));
}
/*******************************************************************************
* Function Name : RTC_SetTime
* Description : This routine sets the RTC Time
* Input : None
* Return : None
*******************************************************************************/
void RTC_SetTime(u8 TmpH, u8 TmpM, u8 TmpS )
{
u32 Tmp = TmpH * 3600 + TmpM * 60 + TmpS;
// Wait For Last Task Completion
RTC_WaitForLastTask();
// Enter In Configuration Mode
RTC_EnterCfgMode();
// Wait For Last Task Completion
RTC_WaitForLastTask();
// Wait For Last Task Completion
RTC->CNTL = Tmp & 0x0000FFFF;
// Wait For Last Task Completion
RTC_WaitForLastTask();
RTC->CNTH = ( Tmp & 0xFFFF0000 ) >> 16;
// Wait For Last Task Completion
RTC_WaitForLastTask();
// Exit From Configuration Mode
RTC_ExitCfgMode ();
}
/*******************************************************************************
* Function Name : RTC_SetAlarmTime
* Description : This routine sets the Alarm Time
* Input : None
* Return : None
*******************************************************************************/
void RTC_SetAlarmTime(u8 TmpH, u8 TmpM, u8 TmpS )
{
u32 Tmp = TmpH * 3600 + TmpM * 60 + TmpS;
// Wait For Last Task Completion
RTC_WaitForLastTask();
// Enter In Configuration Mode
RTC_EnterCfgMode();
// Wait For Last Task Completion
RTC_WaitForLastTask();
// Wait For Last Task Completion
RTC->ALRL = Tmp & 0x0000FFFF;
// Wait For Last Task Completion
RTC_WaitForLastTask();
RTC->ALRH = ( Tmp & 0xFFFF0000 ) >> 16;
// Wait For Last Task Completion
RTC_WaitForLastTask();
// Exit From Configuration Mode
RTC_ExitCfgMode ();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -