📄 stm32rtc.txt
字号:
while(1)
{
/* Check which key is pressed */
MyKey = ReadKey();
/* If "UP" pushbutton is pressed */
if(MyKey == UP)
{ ++tmpValue;
/* Increase the value of the digit */
if(tmpValue == 60)
{
tmpValue = 0;
}
Writetime(tmpValue,50,194);
}
/* If "DOWN" pushbutton is pressed */
if(MyKey == DOWN)
{
/* Decrease the value of the digit */
if(tmpValue == 0)
{
tmpValue = 60;
}
/* Display new value */
Writetime(--tmpValue,50,194);
}
/* If "SEL" pushbutton is pressed */
if(MyKey == SEL)
{
/* Display new value */
Writetime(tmpValue,50,194);
/* Return the digit value and exit */
RTMinute = tmpValue;
return;
}
}
}
/*******************************************************************************
* Function Name : Time_Show
* Description : Shows the current time (HH:MM:SS) on the Hyperterminal.
* Input : None
* Output : None
* Return : None
******************************************************************************/
void Time_Show(void)
{ u32 i=0, tmp = 0;
/* If 1s has paased */
/* If 1s has paased */
if(TimeDisplay == 1)
{
if(RTC_GetCounter() / 86399 != 0)
{
for(i = 0; i < (RTC_GetCounter() / 86399); i++)
{
CaculateTime();
}
RTC_SetCounter(RTC_GetCounter() % 86399);
BKP_WriteBackupRegister(BKP_DR2, date_s.year);
tmp = date_s.month << 8;
tmp |= date_s.day;
BKP_WriteBackupRegister(BKP_DR3, tmp) ;
}
Time_Display(RTC_GetCounter());
TimeDisplay = 0;
}
}
/*******************************************************************************
* Function Name : Date_Regulate
* Description : Sets the date entered by user, using menu navigation keys.
* Input : None
* Output : None
* Return : None
*******************************************************************************/
void Date_Regulate(void)
{ u32 tmp = 0;
/* Check which key is pressed */
/* Regulate year */
line_h(75,4,34,h_blue);
RegulateYear();
line_h(75,4,34,black);
line_h(75,44,74,h_blue);
/* Regulate the month */
RegulateMonth();
line_h(75,44,74,black);
line_h(75,94,124,h_blue);
/* Regulate day */
RegulateDay();
line_h(75,94,124,black);
BKP_WriteBackupRegister(BKP_DR2, date_s.year);
tmp = date_s.month << 8;
tmp |= date_s.day;
BKP_WriteBackupRegister(BKP_DR3, tmp);
Time_PreAdjust();
}
/*******************************************************************************
* Function Name : RegulateYear
* Description : Regulates the year.
* Input : None
* Output : None
* Return : None
*******************************************************************************/
static void RegulateYear(void)
{
u8 tmpValue = 0;
u8 MyKey = 0;
/* Initialize tmpValue */
tmpValue = date_s.year;
/* Endless loop */
while(1)
{
/* Check which key is pressed */
MyKey = ReadKey();
/* If "UP" pushbutton is pressed */
if(MyKey == UP)
{
++tmpValue;
/* Increase the value of the digit */
if(tmpValue == 100)
{
tmpValue = 0;
}
Writetime(tmpValue,50,4);
}
/* If "DOWN" pushbutton is pressed */
if(MyKey == DOWN)
{
/* Decrease the value of the digit */
if(tmpValue == 0)
{
tmpValue =100;
}
/* Display new value */
Writetime(--tmpValue,50,4);
}
/* If "SEL" pushbutton is pressed */
if(MyKey == SEL)
{
/* Display new value */
Writetime(tmpValue,50,4);
/* Return the digit value and exit */
date_s.year = tmpValue;
return;
}
}
}
/*******************************************************************************
* Function Name : RegulateMonth
* Description : Regulates month.
* Input : None
* Output : None
* Return : None
*******************************************************************************/
static void RegulateMonth(void)
{
u8 tmpValue = 0;
u8 MyKey = 0;
/* Initialize tmpValue */
tmpValue =date_s.month;
/* Endless loop */
while(1)
{
/* Check which key is pressed */
MyKey = ReadKey();
/* If "UP" pushbutton is pressed */
if(MyKey == UP)
{
/* Increase the value of the digit */
if(tmpValue == 12)
{
tmpValue = 0;
}
Writetime(++tmpValue,50,44);
}
/* If "DOWN" pushbutton is pressed */
if(MyKey == DOWN)
{
/* Decrease the value of the digit */
if(tmpValue == 1)
{
tmpValue = 13;
}
/* Display new value */
Writetime(--tmpValue,50,44);
}
/* If "SEL" pushbutton is pressed */
if(MyKey == SEL)
{
/* Display new value */
Writetime(tmpValue,50,44);
/* Return the digit value and exit */
date_s.month = tmpValue;
return;
}
}
}
/*******************************************************************************
* Function Name : RegulateDay
* Description : Regulates day.
* Input : None
* Output : None
* Return : None
*******************************************************************************/
static void RegulateDay(void)
{
u8 tmpValue = 0;
u8 MyKey = 0, ValueMax = 0;
if(date_s.month == 2)
{
if(IsLeapYear(date_s.year))
ValueMax = 29;
else
ValueMax = 28 ;
}
else
{
ValueMax = MaxDayArray[date_s.month-1];
}
/* Initialize tmpValue */
tmpValue = date_s.day;
/* Endless loop */
while(1)
{
/* Check which key is pressed */
MyKey = ReadKey();
/* If "UP" pushbutton is pressed */
if(MyKey == UP)
{
if(tmpValue == ValueMax)
{
tmpValue = 0;
}
Writetime(++tmpValue,50,94);
}
/* If "DOWN" pushbutton is pressed */
if(MyKey == DOWN)
{
if(tmpValue == 1)
{
tmpValue = ValueMax+1;
}
Writetime(--tmpValue,50,94);
}
/* If "SEL" pushbutton is pressed */
if(MyKey == SEL)
{ Writetime(tmpValue,50,94);
/* Return the digit value and exit */
date_s.day = tmpValue;
return;
}
}
}
//////////////////////////////////////////////////
//计算年月日:
/*CaculateTime :Caculate new Time*/
void CaculateTime(void)
{
if(date_s.month==2) { //
date_s.day++;
if(IsLeapYear(date_s.year)) { //leap year
if ((date_s.day>MaxDayArray[date_s.month-1]+1)) {
//Day overflow
date_s.day=1;
date_s.month++;
}
} else { //no leap year
if ((date_s.day>MaxDayArray[date_s.month-1])) {
date_s.day=1;
date_s.month++;
}
}
} else { //
date_s.day++;
if ((date_s.day>MaxDayArray[date_s.month-1])) {
date_s.day=1;
date_s.month++;
if(date_s.month>12) {
//Month overflow
date_s.month=1;
date_s.year++;
if (date_s.year>99) {
//Year overflow
date_s.year=0;
}
}
}
}
}
/*******************************************************************************
* Function Name : IsLeapYear
* Description : Check whether the passed year is Leap or not.
* Input : None
* Output : None
* Return : 1: leap year
* 0: not leap year
*******************************************************************************/
static u8 IsLeapYear(u16 nYear)
{
if(nYear % 4 != 0) return 0;
if(nYear % 100 != 0) return 1;
return (u8)(nYear % 400 == 0);
}
/*******************************************************
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -