📄 calendar.c
字号:
/******************** (C) COPYRIGHT 2007 STMicroelectronics ********************
* File Name : calendar.c
* Author : MCD Application Team
* Version : V1.1
* Date : 11/26/2007
* Description : This file includes the calendar driver for the
* STM3210B-EVAL demonstration.
********************************************************************************
* THE PRESENT SOFTWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
* WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE TIME.
* AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY DIRECT,
* INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE
* CONTENT OF SUCH SOFTWARE AND/OR THE USE MADE BY CUSTOMERS OF THE CODING
* INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
*******************************************************************************/
/* Includes ------------------------------------------------------------------*/
#include "main.h"
/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
/* Time Structure definition */
struct time_t
{
u8 sec_l;
u8 sec_h;
u8 min_l;
u8 min_h;
u8 hour_l;
u8 hour_h;
};
struct time_t time_struct;
/* Alarm Structure definition */
struct alarm_t
{
u8 sec_l;
u8 sec_h;
u8 min_l;
u8 min_h;
u8 hour_l;
u8 hour_h;
};
struct alarm_t alarm_struct;
/* Date Structure definition */
struct date_t
{
u8 month;
u8 day;
u16 year;
};
struct date_t date_s;
static u32 wn = 0, dn = 0, dc = 0;
static u16 daycolumn = 0, dayline = 0;
u8 MonLen[12]= {32, 29, 32, 31, 32, 31, 32, 32, 31, 32, 31, 32};
u8 MonthNames[12][20] =
{" JANUARY ", " FEBRUARY ", " MARCH ",
" APRIL ", " MAY ", " JUNE ",
" JULY ", " AUGUST ", " SEPTEMBER ",
" OCTOBER ", " NOVEMBER ", " DECEMBER "};
/* Private function prototypes -----------------------------------------------*/
static u8 IsLeapYear(u16 nYear);
static void WeekDayNum(u32 nyear, u8 nmonth, u8 nday);
static void RTC_Configuration(void);
static void RegulateYear(void);
static void RegulateMonth(void);
static void RegulateDay(void);
static void Time_PreAdjust(void);
static void Date_PreAdjust(void);
/* Private functions ---------------------------------------------------------*/
/*******************************************************************************
* Function Name : ReadDigit
* Description : Reads digit entered by user, using menu navigation keys.
* Input : None
* Output : None
* Return : Digit value
*******************************************************************************/
u8 ReadDigit(u8 ColBegin, u8 CountBegin, u8 ValueMax, u8 ValueMin)
{
u32 MyKey = 0, tmpValue = 0;
/* Set the Back Color */
LCD_SetBackColor(Red);
/* Set the Text Color */
LCD_SetTextColor(White);
/* Initialize tmpValue */
tmpValue = CountBegin;
/* Display */
LCD_DisplayChar(Line8, ColBegin, (tmpValue + 0x30));
/* 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 == ValueMax)
{
tmpValue = (ValueMin - 1);
}
/* Display new value */
LCD_DisplayChar(Line8, ColBegin,((++tmpValue) + 0x30));
}
/* If "DOWN" pushbutton is pressed */
if(MyKey == DOWN)
{
/* Decrease the value of the digit */
if(tmpValue == ValueMin)
{
tmpValue = (ValueMax + 1);
}
/* Display new value */
LCD_DisplayChar(Line8, ColBegin,((--tmpValue) + 0x30));
}
/* If "SEL" pushbutton is pressed */
if(MyKey == SEL)
{
/* Set the Back Color */
LCD_SetBackColor(White);
/* Set the Text Color */
LCD_SetTextColor(Red);
/* Display new value */
LCD_DisplayChar(Line8, ColBegin, (tmpValue + 0x30));
/* Return the digit value and exit */
return tmpValue;
}
}
}
/*******************************************************************************
* Function Name : Calendar_Init
* Description : Initializes calendar application.
* Input : None
* Output : None
* Return : None
*******************************************************************************/
void Calendar_Init(void)
{
u32 i = 0, tmp = 0, pressedkey = 0;
/* Initialize Date structure */
date_s.month = 01;
date_s.day = 01;
date_s.year = 2007;
if(BKP_ReadBackupRegister(BKP_DR1) != 0xA5A5)
{
LCD_SetBackColor(Blue);
LCD_SetTextColor(White);
LCD_DisplayStringLine(Line7, "Time and Date Config");
LCD_DisplayStringLine(Line8, "Select: Press SEL ");
LCD_DisplayStringLine(Line9, "Abort: Press Any Key");
while(1)
{
pressedkey = ReadKey();
if(pressedkey == SEL)
{
/* Adjust Time */
Time_PreAdjust();
return;
}
else if (pressedkey != NOKEY)
{
return;
}
}
}
else
{
/* PWR and BKP clocks selection ------------------------------------------*/
RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR | RCC_APB1Periph_BKP, ENABLE);
/* Allow access to BKP Domain */
PWR_BackupAccessCmd(ENABLE);
/* 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();
/* Initialize Date structure */
date_s.month = (BKP_ReadBackupRegister(BKP_DR3) & 0xFF00) >> 8;
date_s.day = (BKP_ReadBackupRegister(BKP_DR3) & 0x00FF);
date_s.year = BKP_ReadBackupRegister(BKP_DR2);
daycolumn = BKP_ReadBackupRegister(BKP_DR4);
dayline = BKP_ReadBackupRegister(BKP_DR5);
if(RTC_GetCounter() / 86399 != 0)
{
for(i = 0; i < (RTC_GetCounter() / 86399); i++)
{
Date_Update();
}
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);
LCD_DisplayStringLine(Line8, " Days elapsed ");
tmp = i/100;
LCD_DisplayChar(Line8, 276,(tmp + 0x30));
tmp = ((i%100)/10);
LCD_DisplayChar(Line8, 260,(tmp + 0x30));
tmp = ((i%100)%10);
LCD_DisplayChar(Line8, 244,(tmp + 0x30));
LCD_DisplayStringLine(Line9, " Press SEL ");
while(ReadKey() != SEL)
{
}
LCD_ClearLine(Line8);
LCD_ClearLine(Line9);
LCD_DisplayStringLine(Line9, "Push SEL to Continue");
Date_Display(date_s.year, date_s.month, date_s.day);
while(ReadKey() != SEL)
{
}
BKP_WriteBackupRegister(BKP_DR4, daycolumn);
BKP_WriteBackupRegister(BKP_DR5, dayline);
}
}
}
/*******************************************************************************
* Function Name : Time_Regulate
* Description : Returns the time entered by user, using menu vavigation keys.
* Input : None
* Output : None
* Return : Current time RTC counter value
*******************************************************************************/
u32 Time_Regulate(void)
{
u32 Tmp_HH = 0, Tmp_MM = 0, Tmp_SS = 0;
/* Read time hours */
Tmp_HH = ReadDigit(244, time_struct.hour_h, 0x2, 0x0);
if(Tmp_HH == 2)
{
if(time_struct.hour_l > 3)
{
time_struct.hour_l = 0;
}
Tmp_HH = Tmp_HH*10 + ReadDigit(228, time_struct.hour_l, 0x3, 0x0);
}
else
{
Tmp_HH = Tmp_HH*10 + ReadDigit(228, time_struct.hour_l, 0x9, 0x0);
}
/* Read time minutes */
Tmp_MM = ReadDigit(196, time_struct.min_h,5, 0x0);
Tmp_MM = Tmp_MM*10 + ReadDigit(182, time_struct.min_l, 0x9, 0x0);
/* Read time seconds */
Tmp_SS = ReadDigit(150, time_struct.sec_h,5, 0x0);
Tmp_SS = Tmp_SS*10 + ReadDigit(134, time_struct.sec_l, 0x9, 0x0);
/* Return the value to store in RTC counter register */
return((Tmp_HH*3600 + Tmp_MM*60 + Tmp_SS));
}
/*******************************************************************************
* Function Name : Time_PreAdjust
* Description : Returns the time entered by user, using demoboard keys.
* Input : None
* Output : None
* Return : None
*******************************************************************************/
static void Time_PreAdjust(void)
{
LCD_Clear(White);
LCD_SetDisplayWindow(160, 223, 128, 128);
LCD_DrawBMP(0x0063FB00);
/* Disable LCD Window mode */
LCD_WindowModeDisable();
if(BKP_ReadBackupRegister(BKP_DR1) != 0xA5A5)
{
RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR | RCC_APB1Periph_BKP, ENABLE);
/* Allow access to BKP Domain */
PWR_BackupAccessCmd(ENABLE);
LCD_DisplayStringLine(Line7, "RTC Config PLZ Wait.");
/* RTC Configuration */
RTC_Configuration();
LCD_DisplayStringLine(Line7, " TIME ");
/* Clear Line8 */
LCD_ClearLine(Line8);
/* Display time separators ":" on Line4 */
LCD_DisplayChar(Line8, 212, ':');
LCD_DisplayChar(Line8, 166, ':');
/* Display the current time */
Time_Display(RTC_GetCounter());
/* Wait until last write operation on RTC registers has finished */
RTC_WaitForLastTask();
/* Change the current time */
RTC_SetCounter(Time_Regulate());
BKP_WriteBackupRegister(BKP_DR1, 0xA5A5);
/* Clear Line7 */
LCD_ClearLine(Line7);
/* Clear Line8 */
LCD_ClearLine(Line8);
/* Adjust Date */
Date_PreAdjust();
}
else
{
/* Clear Line8 */
LCD_ClearLine(Line8);
/* Display time separators ":" on Line4 */
LCD_DisplayChar(Line8, 212, ':');
LCD_DisplayChar(Line8, 166, ':');
/* Display the current time */
Time_Display(RTC_GetCounter());
/* Wait until last write operation on RTC registers has finished */
RTC_WaitForLastTask();
/* Change the current time */
RTC_SetCounter(Time_Regulate());
}
}
/*******************************************************************************
* Function Name : Time_Adjust
* Description : Returns the time entered by user, using demoboard keys.
* Input : None
* Output : None
* Return : None
*******************************************************************************/
void Time_Adjust(void)
{
/* Disable the JoyStick interrupts */
IntExtOnOffConfig(DISABLE);
/* PreAdjust Time */
Time_PreAdjust();
/* Clear the LCD */
LCD_Clear(White);
/* Display the menu */
DisplayMenu();
/* Enable the JoyStick interrupts */
IntExtOnOffConfig(ENABLE);
}
/*******************************************************************************
* Function Name : Time_Display
* Description : Displays the current time.
* Input : None
* Output : None
* Return : None
*******************************************************************************/
void Time_Display(u32 TimeVar)
{
/* Display time hours */
time_struct.hour_h=(u8)( TimeVar / 3600)/10;
LCD_DisplayChar(Line8, 244,(time_struct.hour_h + 0x30));
time_struct.hour_l=(u8)(((TimeVar)/3600)%10);
LCD_DisplayChar(Line8, 228,(time_struct.hour_l + 0x30));
/* Display time minutes */
time_struct.min_h=(u8)(((TimeVar)%3600)/60)/10;
LCD_DisplayChar(Line8, 196,(time_struct.min_h + 0x30));
time_struct.min_l=(u8)(((TimeVar)%3600)/60)%10;
LCD_DisplayChar(Line8, 182,(time_struct.min_l + 0x30));
/* Display time seconds */
time_struct.sec_h=(u8)(((TimeVar)%3600)%60)/10;
LCD_DisplayChar(Line8, 150,(time_struct.sec_h + 0x30));
time_struct.sec_l=(u8)(((TimeVar)%3600)%60)%10;
LCD_DisplayChar(Line8, 134,(time_struct.sec_l + 0x30));
}
/*******************************************************************************
* Function Name : Time_Show
* Description : Shows the current time (HH/MM/SS) on LCD.
* Input : None
* Output : None
* Return : None
******************************************************************************/
void Time_Show(void)
{
u32 testvalue = 0, pressedkey = 0;
/* Set the Back Color */
LCD_SetBackColor(White);
/* Set the Text Color */
LCD_SetTextColor(Blue);
/* Disable the JoyStick interrupts */
IntExtOnOffConfig(DISABLE);
LCD_Clear(White);
LCD_SetDisplayWindow(160, 223, 128, 128);
LCD_DrawBMP(0x0063FB00);
/* Disable LCD Window mode */
LCD_WindowModeDisable();
if(BKP_ReadBackupRegister(BKP_DR1) != 0xA5A5)
{
LCD_DisplayStringLine(Line7, "Time and Date Config");
LCD_DisplayStringLine(Line8, "Select: Press SEL ");
LCD_DisplayStringLine(Line9, "Abort: Press Any Key");
while(testvalue == 0)
{
pressedkey = ReadKey();
if(pressedkey == SEL)
{
/* Adjust Time */
Time_PreAdjust();
/* Clear the LCD */
LCD_Clear(White);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -