781 lines
25 KiB
C
781 lines
25 KiB
C
/*
|
||
*********************************************************************************
|
||
头文件
|
||
*********************************************************************************
|
||
*/
|
||
#include "rtc.h"
|
||
|
||
/*
|
||
************************************************************************************
|
||
变量定义
|
||
************************************************************************************
|
||
*/
|
||
//平年的月份日期表 闰年2月份29天
|
||
const u8 mon_table[12]={31,28,31,30,31,30,31,31,30,31,30,31};
|
||
MYITStatus_Struct MYITStatus1;
|
||
u8 key_time,IS_FIRST_SAVE = 0;
|
||
/*
|
||
*********************************************************************************************************
|
||
* 函 数 名: RTC_NVIC_CONFIG()
|
||
* 功能说明: RTC中断配置
|
||
* 输 入 :无
|
||
* 输 出 :无
|
||
*********************************************************************************************************
|
||
*/
|
||
void RTC_NVIC_CONFIG(void)
|
||
{
|
||
NVIC_InitTypeDef NVIC_InitStructure;
|
||
EXTI_InitTypeDef EXTI_InitStructure;
|
||
GPIO_InitTypeDef GPIO_InitStructure;
|
||
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOC, ENABLE);
|
||
|
||
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IN;
|
||
GPIO_InitStructure.GPIO_OType=GPIO_OType_PP;
|
||
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_13;
|
||
GPIO_InitStructure.GPIO_PuPd=GPIO_PuPd_DOWN;
|
||
GPIO_Init(GPIOC,&GPIO_InitStructure);
|
||
|
||
GPIO_PinAFConfig(GPIOC,GPIO_PinSource13,GPIO_AF_WKUP);
|
||
|
||
EXTI_ClearITPendingBit(EXTI_Line20);
|
||
EXTI_InitStructure.EXTI_Line=EXTI_Line20;//RTC wakeup
|
||
EXTI_InitStructure.EXTI_LineCmd=ENABLE;
|
||
EXTI_InitStructure.EXTI_Mode=EXTI_Mode_Interrupt;
|
||
EXTI_InitStructure.EXTI_Trigger=EXTI_Trigger_Rising;
|
||
EXTI_Init(&EXTI_InitStructure);
|
||
|
||
NVIC_InitStructure.NVIC_IRQChannel=RTC_WKUP_IRQn;
|
||
NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;
|
||
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=0x00;
|
||
NVIC_InitStructure.NVIC_IRQChannelSubPriority=0x00;
|
||
NVIC_Init(&NVIC_InitStructure);
|
||
|
||
/* EXTI configuration */
|
||
EXTI_ClearITPendingBit(EXTI_Line17);
|
||
EXTI_InitStructure.EXTI_Line = EXTI_Line17;
|
||
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
|
||
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising;
|
||
EXTI_InitStructure.EXTI_LineCmd = ENABLE;
|
||
EXTI_Init(&EXTI_InitStructure);
|
||
|
||
/* Enable the RTC Alarm Interrupt */
|
||
NVIC_InitStructure.NVIC_IRQChannel = RTC_Alarm_IRQn;
|
||
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
|
||
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
|
||
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
|
||
NVIC_Init(&NVIC_InitStructure);
|
||
|
||
}
|
||
|
||
/*
|
||
*********************************************************************************************************
|
||
* 函 数 名: RTC_Time_Init()
|
||
* 功能说明: RTC初始化
|
||
* 输 入 :无
|
||
* 输 出 :无
|
||
*********************************************************************************************************
|
||
*/
|
||
void RTC_Time_Init(void)
|
||
{
|
||
|
||
RTC_InitTypeDef RTC_InitStructure;
|
||
//RTC_TimeTypeDef RTC_TimeStructure;
|
||
|
||
RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE);
|
||
|
||
/*!< Allow access to RTC */
|
||
PWR_RTCAccessCmd(ENABLE);
|
||
|
||
/*!< 使能外部低速时钟 */
|
||
RCC_LSEConfig(RCC_LSE_ON);
|
||
// RCC_LSICmd(ENABLE);
|
||
/*!< 等待外部低速时钟就绪 */
|
||
while (RCC_GetFlagStatus(RCC_FLAG_LSERDY) == RESET);
|
||
|
||
RCC_RTCCLKConfig(RCC_RTCCLKSource_LSE);//选择外部LES作为RTC时钟源
|
||
RCC_RTCCLKCmd(ENABLE);
|
||
/*!< Wait for RTC APB registers synchronisation */
|
||
RTC_WaitForSynchro();
|
||
|
||
RTC_InitStructure.RTC_AsynchPrediv=127;
|
||
RTC_InitStructure.RTC_HourFormat=RTC_HourFormat_24;
|
||
RTC_InitStructure.RTC_SynchPrediv=255;
|
||
RTC_Init(&RTC_InitStructure);//RTC的时钟1HZ
|
||
RTC_NVIC_CONFIG();
|
||
|
||
}
|
||
|
||
/*
|
||
*********************************************************************************************************
|
||
* 函 数 名: rtc_std_init()
|
||
* 功能说明: RTC初始化1
|
||
* 输 入 :无
|
||
* 输 出 :无
|
||
*********************************************************************************************************
|
||
*/
|
||
void rtc_std_init(void)
|
||
{
|
||
|
||
/* Enable the PWR clock */
|
||
RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE);
|
||
|
||
/* Allow access to RTC */
|
||
PWR_RTCAccessCmd(ENABLE);
|
||
|
||
/* Wait for RTC APB registers synchronisation */
|
||
RTC_WaitForSynchro();
|
||
RTC_NVIC_CONFIG();
|
||
|
||
}
|
||
|
||
/*
|
||
*********************************************************************************************************
|
||
* 函 数 名: SetRTC()
|
||
* 功能说明: 设置本地硬件RTC时间
|
||
* 输 入 :年 set_year,月 set_mon,日 set_day,时 set_hour,分 set_min ,秒 set_sec
|
||
* 输 出 :无
|
||
*********************************************************************************************************
|
||
*/
|
||
void SetRTC(int set_year,int set_mon,int set_day,int set_hour,int set_min ,int set_sec)
|
||
{
|
||
RTC_TimeTypeDef RTC_TimeStruct;
|
||
RTC_DateTypeDef RTC_DateStruct;
|
||
|
||
RTC_TimeStruct.RTC_Hours = set_hour;
|
||
RTC_TimeStruct.RTC_Minutes = set_min;
|
||
RTC_TimeStruct.RTC_Seconds = set_sec;
|
||
RTC_TimeStruct.RTC_H12 = 0; //程序中不使用上午/下午
|
||
RTC_SetTime(RTC_Format_BIN,&RTC_TimeStruct);
|
||
|
||
RTC_DateStruct.RTC_Year = set_year;
|
||
RTC_DateStruct.RTC_Month = set_mon;
|
||
RTC_DateStruct.RTC_Date = set_day;
|
||
RTC_DateStruct.RTC_WeekDay = 1;//程序中不使用星期
|
||
RTC_SetDate(RTC_Format_BIN,&RTC_DateStruct);
|
||
RTC_WriteBackupRegister(RTC_BKP_DR0, 0x32F2);
|
||
|
||
}
|
||
|
||
/*
|
||
*********************************************************************************************************
|
||
* 函 数 名: Set_GNSSTIME_Rtc()
|
||
* 功能说明: GPS校时
|
||
* 输 入 :无
|
||
* 输 出 :无
|
||
*********************************************************************************************************
|
||
*/
|
||
void Set_GNSSTIME_Rtc(void)
|
||
{
|
||
int set_years, set_mons, set_days, set_hours, set_mins , set_secs;
|
||
if(Save_Data.isUsefull)//定位数据有效
|
||
{
|
||
set_years=(Save_Data.UTCData[0]-'0')*10+(Save_Data.UTCData[1]-'0');//年
|
||
set_mons=(Save_Data.UTCData[2]-'0')*10+(Save_Data.UTCData[3]-'0');//月
|
||
set_days=(Save_Data.UTCData[4]-'0')*10+(Save_Data.UTCData[5]-'0');//日
|
||
|
||
set_hours=(Save_Data.UTCTime[0]-'0')*10+(Save_Data.UTCTime[1]-'0')+8;//时
|
||
set_mins=(Save_Data.UTCTime[2]-'0')*10+(Save_Data.UTCTime[3]-'0');//分
|
||
set_secs=(Save_Data.UTCTime[4]-'0')*10+(Save_Data.UTCTime[5]-'0');//秒
|
||
SetRTC(set_years,set_mons,set_days,set_hours,set_mins,set_secs);
|
||
}
|
||
|
||
}
|
||
|
||
/*
|
||
*********************************************************************************************************
|
||
* 函 数 名: GetRTC()
|
||
* 功能说明: 获取本地硬件RTC时间
|
||
* 输 入 :时分秒yymmdd 年月日hhmmss
|
||
* 输 出 :无
|
||
*********************************************************************************************************
|
||
*/
|
||
void GetRTC(char *hhmmss,char *yymmdd)
|
||
{
|
||
RTC_TimeTypeDef time;
|
||
RTC_DateTypeDef date;
|
||
RTC_GetTime(RTC_Format_BIN,&time);
|
||
RTC_GetDate(RTC_Format_BIN,&date);
|
||
|
||
yymmdd[0]=date.RTC_Year/10+0x30; //年十位
|
||
yymmdd[1]=date.RTC_Year%10+0x30; //年个位
|
||
yymmdd[2]=date.RTC_Month/10+0x30; //月十位
|
||
yymmdd[3]=date.RTC_Month%10+0x30; //月十位
|
||
yymmdd[4]=date.RTC_Date/10+0x30; //日十位
|
||
yymmdd[5]=date.RTC_Date%10+0x30; //日十位
|
||
hhmmss[0]=time.RTC_Hours/10+0x30; //小时十位
|
||
hhmmss[1]=time.RTC_Hours%10+0x30; //小时个位
|
||
hhmmss[2]=time.RTC_Minutes/10+0x30; //分钟十位
|
||
hhmmss[3]=time.RTC_Minutes%10+0x30; //分钟十位
|
||
hhmmss[4]=time.RTC_Seconds/10+0x30; //秒十位
|
||
hhmmss[5]=time.RTC_Seconds%10+0x30; //秒十位
|
||
}
|
||
void GetTime(char *hhmmss,char *yymmdd)
|
||
{
|
||
get_show_time();
|
||
yymmdd[0]=calendar.w_year/10+0x30; //年十位
|
||
yymmdd[1]=calendar.w_year%10+0x30; //年个位
|
||
yymmdd[2]=calendar.w_month/10+0x30; //月十位
|
||
yymmdd[3]=calendar.w_month%10+0x30; //月十位
|
||
yymmdd[4]=calendar.w_date/10+0x30; //日十位
|
||
yymmdd[5]=calendar.w_date%10+0x30; //日十位
|
||
hhmmss[0]=calendar.hour/10+0x30; //小时十位
|
||
hhmmss[1]=calendar.hour%10+0x30; //小时个位
|
||
hhmmss[2]=calendar.min/10+0x30; //分钟十位
|
||
hhmmss[3]=calendar.min%10+0x30; //分钟十位
|
||
hhmmss[4]=calendar.sec/10+0x30; //秒十位
|
||
hhmmss[5]=calendar.sec%10+0x30; //秒十位
|
||
}
|
||
|
||
|
||
|
||
/*
|
||
*********************************************************************************************************
|
||
* 函 数 名: RTC_GetSeconds()
|
||
* 功能说明: 获取本地硬件RTC时间S
|
||
* 输 入 :无
|
||
* 输 出 :秒secondss
|
||
*********************************************************************************************************
|
||
*/
|
||
u32 RTC_GetSeconds(void)
|
||
{
|
||
u32 secondss;
|
||
RTC_TimeTypeDef time;
|
||
RTC_GetTime(RTC_Format_BIN,&time);
|
||
secondss= time.RTC_Hours*3600 + time.RTC_Minutes*60 + time.RTC_Seconds;
|
||
return secondss;
|
||
}
|
||
|
||
/*
|
||
*********************************************************************************************************
|
||
* 函 数 名: RtcWakeUpConfig()
|
||
* 功能说明: 自动唤醒配置
|
||
* 输 入 :唤醒时间temp
|
||
* 输 出 :无
|
||
*********************************************************************************************************
|
||
*/
|
||
void RtcWakeUpConfig(u32 temp)
|
||
{
|
||
NVIC_InitTypeDef NVIC_InitStructure;
|
||
EXTI_InitTypeDef EXTI_InitStructure;
|
||
|
||
EXTI_ClearITPendingBit(EXTI_Line20);
|
||
EXTI_InitStructure.EXTI_Line=EXTI_Line20;//RTC wakeup
|
||
EXTI_InitStructure.EXTI_LineCmd=ENABLE;
|
||
EXTI_InitStructure.EXTI_Mode=EXTI_Mode_Interrupt;
|
||
EXTI_InitStructure.EXTI_Trigger=EXTI_Trigger_Rising;
|
||
EXTI_Init(&EXTI_InitStructure);
|
||
|
||
NVIC_InitStructure.NVIC_IRQChannel=RTC_WKUP_IRQn;
|
||
NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;
|
||
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=0x00;
|
||
NVIC_InitStructure.NVIC_IRQChannelSubPriority=0x00;
|
||
NVIC_Init(&NVIC_InitStructure);
|
||
|
||
RTC_WakeUpCmd(DISABLE);
|
||
RTC_ClearFlag(RTC_FLAG_WUTF);
|
||
if(temp>65535)
|
||
{
|
||
RTC_WakeUpClockConfig(RTC_WakeUpClock_CK_SPRE_17bits);
|
||
temp-=65535;
|
||
}
|
||
else RTC_WakeUpClockConfig(RTC_WakeUpClock_CK_SPRE_16bits);
|
||
RTC_SetWakeUpCounter(temp); //设置唤醒时间--S,最大65536s=18小时 2^17=131072 131072/3600s=36.4h
|
||
RTC_ClearITPendingBit(RTC_IT_WUT);
|
||
RTC_ITConfig(RTC_IT_WUT,ENABLE); //需要使能中断,不需要中断函数
|
||
RTC_WakeUpCmd(ENABLE);
|
||
}
|
||
|
||
/*
|
||
*********************************************************************************************************
|
||
* 函 数 名: RtcWakeUppin()
|
||
* 功能说明: 唤醒引脚配置
|
||
* 输 入 :无
|
||
* 输 出 :无
|
||
*********************************************************************************************************
|
||
*/
|
||
void RtcWakeUppin(void)
|
||
{
|
||
NVIC_InitTypeDef NVIC_InitStructure;
|
||
EXTI_InitTypeDef EXTI_InitStructure;
|
||
GPIO_InitTypeDef GPIO_InitStructure;
|
||
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOC, ENABLE);
|
||
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IN;
|
||
//GPIO_InitStructure.GPIO_OType=GPIO_OType_PP;
|
||
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_13;
|
||
GPIO_InitStructure.GPIO_PuPd=GPIO_PuPd_DOWN;
|
||
GPIO_Init(GPIOC,&GPIO_InitStructure);
|
||
GPIO_PinAFConfig(GPIOC,GPIO_PinSource13,GPIO_AF_WKUP);
|
||
|
||
/* Enable SYSCFG clock */
|
||
RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE);
|
||
/* Connect EXTI0 Line to PA0 pin */
|
||
SYSCFG_EXTILineConfig(EXTI_PortSourceGPIOC, EXTI_PinSource13);
|
||
EXTI_ClearITPendingBit(EXTI_Line13);
|
||
EXTI_InitStructure.EXTI_Line=EXTI_Line13;//RTC wakeup
|
||
EXTI_InitStructure.EXTI_LineCmd=ENABLE;
|
||
EXTI_InitStructure.EXTI_Mode=EXTI_Mode_Interrupt;
|
||
EXTI_InitStructure.EXTI_Trigger=EXTI_Trigger_Rising;
|
||
EXTI_Init(&EXTI_InitStructure);
|
||
|
||
NVIC_InitStructure.NVIC_IRQChannel=EXTI15_10_IRQn;
|
||
NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;
|
||
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=0x00;
|
||
NVIC_InitStructure.NVIC_IRQChannelSubPriority=0x00;
|
||
NVIC_Init(&NVIC_InitStructure);
|
||
PWR_WakeUpPinCmd(PWR_WakeUpPin_2,ENABLE);//使能唤醒引脚
|
||
}
|
||
|
||
/*
|
||
*********************************************************************************************************
|
||
* 函 数 名: Is_Leap_Year()
|
||
* 功能说明: 判断是否是闰年函数
|
||
* 输 入 :年份year
|
||
* 输 出 :该年份是不是闰年.1,是.0,不是
|
||
* 月份 1 2 3 4 5 6 7 8 9 10 11 12
|
||
* 闰年 31 29 31 30 31 30 31 31 30 31 30 31
|
||
* 非闰年 31 28 31 30 31 30 31 31 30 31 30 31
|
||
*********************************************************************************************************
|
||
*/
|
||
u8 Is_Leap_Year(u16 year)
|
||
{
|
||
year=year+2000;
|
||
// year=0x14ff&year;
|
||
if(year%4==0) //必须能被4整除
|
||
{
|
||
if(year%100==0)
|
||
{
|
||
if(year%400==0)return 1;//如果以00结尾,还要能被400整除
|
||
else return 0;
|
||
}else return 1;
|
||
}else return 0;
|
||
}
|
||
|
||
/*
|
||
*********************************************************************************************************
|
||
* 函 数 名: MYRTC_Init()
|
||
* 功能说明: RTC初始化
|
||
* 输 入 :无
|
||
* 输 出 :无
|
||
*********************************************************************************************************
|
||
*/
|
||
void MYRTC_Init(void)
|
||
{
|
||
RTC_NVIC_CONFIG();
|
||
RTC_Time_Init();
|
||
PWR_ClearFlag(PWR_FLAG_WU);//唤醒之后加上这个清除中断
|
||
}
|
||
|
||
/*
|
||
*********************************************************************************************************
|
||
* 函 数 名: RTC_AlarmAConfig()
|
||
* 功能说明: 设置远传间隔启动时间
|
||
* 输 入 :u32 seconds 单位:秒
|
||
* 输 出 :无
|
||
*********************************************************************************************************
|
||
*/
|
||
void RTC_AlarmAConfig(u32 seconds)
|
||
{
|
||
int month_day;
|
||
NVIC_InitTypeDef NVIC_InitStructure;
|
||
EXTI_InitTypeDef EXTI_InitStructure;
|
||
|
||
/* EXTI configuration */
|
||
EXTI_ClearITPendingBit(EXTI_Line17);
|
||
EXTI_InitStructure.EXTI_Line = EXTI_Line17;
|
||
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
|
||
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising;
|
||
EXTI_InitStructure.EXTI_LineCmd = ENABLE;
|
||
EXTI_Init(&EXTI_InitStructure);
|
||
|
||
/* Enable the RTC Alarm Interrupt */
|
||
NVIC_InitStructure.NVIC_IRQChannel = RTC_Alarm_IRQn;
|
||
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
|
||
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
|
||
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
|
||
NVIC_Init(&NVIC_InitStructure);
|
||
|
||
RTC_TimeTypeDef nowtimes;
|
||
RTC_DateTypeDef nowdatas;
|
||
RTC_AlarmTypeDef RTC_AlarmStructure;
|
||
RTC_ClearFlag(RTC_FLAG_ALRAF);
|
||
RTC_ClearITPendingBit(RTC_IT_ALRA);
|
||
RTC_AlarmCmd(RTC_Alarm_A, DISABLE);
|
||
RTC_GetTime(RTC_Format_BIN,&nowtimes);//获取时间
|
||
RTC_GetDate(RTC_Format_BIN,&nowdatas);//获取日期
|
||
month_day=mon_table[nowdatas.RTC_Month-1];//获取月的天数
|
||
if(nowdatas.RTC_Month==2)
|
||
{
|
||
if(!Is_Leap_Year(nowdatas.RTC_Year))//闰年
|
||
month_day+=1; //二月份天数-1
|
||
}
|
||
seconds+=nowtimes.RTC_Hours*3600+nowtimes.RTC_Minutes*60+nowtimes.RTC_Seconds+nowdatas.RTC_Date*86400;
|
||
if((seconds/86400)>month_day)//判断是否超过本月份的天数
|
||
{
|
||
month_day=(seconds/86400)-month_day;
|
||
RTC_AlarmStructure.RTC_AlarmDateWeekDay = month_day;
|
||
}else
|
||
RTC_AlarmStructure.RTC_AlarmDateWeekDay = seconds/86400; //日
|
||
/* Set the alarmA Masks */
|
||
RTC_AlarmStructure.RTC_AlarmDateWeekDaySel = RTC_AlarmDateWeekDaySel_Date;
|
||
RTC_AlarmStructure.RTC_AlarmMask = RTC_AlarmMask_None; //无屏蔽
|
||
|
||
/* Set the alarmA time */
|
||
RTC_AlarmStructure.RTC_AlarmTime.RTC_H12=RTC_H12_AM; //24H
|
||
RTC_AlarmStructure.RTC_AlarmTime.RTC_Hours=(seconds%86400)/3600;//时
|
||
RTC_AlarmStructure.RTC_AlarmTime.RTC_Minutes=(seconds%86400)%3600/60;//分
|
||
RTC_AlarmStructure.RTC_AlarmTime.RTC_Seconds=(seconds%86400)%3600%60;//秒
|
||
|
||
RTC_SetAlarm(RTC_Format_BIN, RTC_Alarm_A, &RTC_AlarmStructure);
|
||
/* Enable AlarmA interrupt */
|
||
RTC_ITConfig(RTC_IT_ALRA, ENABLE);
|
||
/* Enable the alarmA */
|
||
RTC_AlarmCmd(RTC_Alarm_A, ENABLE);
|
||
}
|
||
|
||
/*
|
||
*********************************************************************************************************
|
||
* 函 数 名: RTC_AlarmBConfig()
|
||
* 功能说明: 闹钟B配置
|
||
* 输 入 :年 set_year,月 set_mon,日 set_day,时 set_hour,分 set_min ,秒 set_sec
|
||
* 输 出 :无
|
||
*********************************************************************************************************
|
||
*/
|
||
void RTC_AlarmBConfig(int set_year,int set_mon,int set_day,int set_hour,int set_min ,int set_sec)
|
||
{
|
||
|
||
NVIC_InitTypeDef NVIC_InitStructure;
|
||
EXTI_InitTypeDef EXTI_InitStructure;
|
||
|
||
/* EXTI configuration */
|
||
EXTI_ClearITPendingBit(EXTI_Line17);
|
||
EXTI_InitStructure.EXTI_Line = EXTI_Line17;
|
||
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
|
||
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising;
|
||
EXTI_InitStructure.EXTI_LineCmd = ENABLE;
|
||
EXTI_Init(&EXTI_InitStructure);
|
||
|
||
/* Enable the RTC Alarm Interrupt */
|
||
NVIC_InitStructure.NVIC_IRQChannel = RTC_Alarm_IRQn;
|
||
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
|
||
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
|
||
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
|
||
NVIC_Init(&NVIC_InitStructure);
|
||
|
||
RTC_AlarmTypeDef RTC_AlarmStructure;
|
||
RTC_ClearFlag(RTC_FLAG_ALRBF);
|
||
RTC_ClearITPendingBit(RTC_IT_ALRB);
|
||
RTC_AlarmCmd(RTC_Alarm_B, DISABLE);
|
||
|
||
/* Set the alarmB Masks */
|
||
RTC_AlarmStructure.RTC_AlarmDateWeekDay = set_day;//日1-31
|
||
RTC_AlarmStructure.RTC_AlarmDateWeekDaySel = RTC_AlarmDateWeekDaySel_Date; //选择日期
|
||
RTC_AlarmStructure.RTC_AlarmMask = RTC_AlarmMask_None; //无屏蔽
|
||
/* Set the alarmB time */
|
||
RTC_AlarmStructure.RTC_AlarmTime.RTC_H12=RTC_H12_AM; //24H
|
||
RTC_AlarmStructure.RTC_AlarmTime.RTC_Hours=set_hour;
|
||
RTC_AlarmStructure.RTC_AlarmTime.RTC_Minutes=set_min;
|
||
RTC_AlarmStructure.RTC_AlarmTime.RTC_Seconds=set_sec;
|
||
|
||
RTC_SetAlarm(RTC_Format_BIN, RTC_Alarm_B, &RTC_AlarmStructure);
|
||
/* Set AlarmA subseconds and enable SubSec Alarm : generate 8 interripts per Second */
|
||
// RTC_AlarmSubSecondConfig(RTC_Alarm_A, 0xFF, RTC_AlarmSubSecondMask_SS14_5);
|
||
|
||
/* Enable AlarmA interrupt */
|
||
RTC_ITConfig(RTC_IT_ALRB, ENABLE);
|
||
|
||
/* Enable the alarmA */
|
||
RTC_AlarmCmd(RTC_Alarm_B, ENABLE);
|
||
|
||
}
|
||
|
||
/*
|
||
*********************************************************************************************************
|
||
* 函 数 名: Get_Wkup_MYITstatus()
|
||
* 功能说明: 检查设备唤醒源
|
||
* 输 入 :无
|
||
* 输 出 :唤醒标志 MYITStatus1
|
||
*********************************************************************************************************
|
||
*/
|
||
STB_ITStatus Get_Wkup_MYITstatus(void)
|
||
{
|
||
if(RTC_GetITStatus(RTC_IT_ALRA))
|
||
{
|
||
EXTI_ClearITPendingBit(EXTI_Line17);
|
||
RTC_ClearITPendingBit(RTC_IT_ALRA);
|
||
MYITStatus1.AlarmA_ITStatus=1;
|
||
}
|
||
|
||
if(RTC_GetITStatus(RTC_IT_ALRB))
|
||
{
|
||
EXTI_ClearITPendingBit(EXTI_Line17);
|
||
RTC_ClearITPendingBit(RTC_IT_ALRB);
|
||
MYITStatus1.AlarmB_ITStatus=1;
|
||
}
|
||
|
||
if(RTC_GetITStatus(RTC_IT_WUT))
|
||
{
|
||
EXTI_ClearITPendingBit(EXTI_Line20);
|
||
RTC_ClearITPendingBit(RTC_IT_WUT);
|
||
MYITStatus1.WUT_ITStatus=1;
|
||
}
|
||
|
||
if(GPIO_ReadInputDataBit(GPIOC,GPIO_Pin_13))
|
||
{
|
||
Check_WKUP_key=Check_WKUP();
|
||
if(Check_WKUP_key)
|
||
{
|
||
MYITStatus1.WKUP1_ITStatus=1;
|
||
}
|
||
EXTI_ClearITPendingBit(EXTI_Line13);
|
||
|
||
|
||
}
|
||
if(MYITStatus1.AlarmA_ITStatus | MYITStatus1.AlarmB_ITStatus | MYITStatus1.WUT_ITStatus | MYITStatus1.WKUP1_ITStatus)
|
||
{
|
||
if(MYITStatus1.AlarmA_ITStatus)return AlarmA_ITHP;
|
||
if(MYITStatus1.AlarmB_ITStatus)return AlarmB_ITHP;
|
||
if(MYITStatus1.WUT_ITStatus)return WUT_ITHP;
|
||
if(MYITStatus1.WKUP1_ITStatus)return WKUP1_ITHP;
|
||
}
|
||
else
|
||
{
|
||
MYITStatus1.WKUP_POWBUTTON=1;//电源上电
|
||
return Power_ON;
|
||
}
|
||
return Power_ON;
|
||
}
|
||
|
||
/*
|
||
*********************************************************************************************************
|
||
* 函 数 名: RTC_Alarm_IRQHandler()
|
||
* 功能说明: 闹钟中断
|
||
* 输 入 :无
|
||
* 输 出 :无
|
||
*********************************************************************************************************
|
||
*/
|
||
void RTC_Alarm_IRQHandler(void)
|
||
{
|
||
/* Check on the AlarmA falg and on the number of interrupts per Second (60*8) */
|
||
if(RTC_GetITStatus(RTC_IT_ALRA) != RESET)
|
||
{
|
||
/* Clear RTC AlarmA Flags */
|
||
RTC_ClearITPendingBit(RTC_IT_ALRA);
|
||
MYITStatus1.AlarmA_ITStatus=1;
|
||
RTC_AlarmCmd(RTC_Alarm_A, DISABLE);
|
||
//u5_printf("alarmA hp\r\n");
|
||
RTC_AlarmAConfig(get_pubidv());
|
||
}
|
||
|
||
/* Check on the AlarmA falg and on the number of interrupts per Second (60*8) */
|
||
if(RTC_GetITStatus(RTC_IT_ALRB) != RESET)
|
||
{
|
||
/* Clear RTC AlarmB Flags */
|
||
RTC_ClearITPendingBit(RTC_IT_ALRB);
|
||
RTC_AlarmCmd(RTC_IT_ALRB, DISABLE);
|
||
MYITStatus1.AlarmB_ITStatus=1;
|
||
IS_FIRST_SAVE = 1;
|
||
}
|
||
/* Clear the EXTIL line 17 */
|
||
EXTI_ClearITPendingBit(EXTI_Line17);
|
||
|
||
}
|
||
|
||
/*
|
||
*********************************************************************************************************
|
||
* 函 数 名: RTC_WKUP_IRQHandler()
|
||
* 功能说明: 唤醒中断
|
||
* 输 入 :无
|
||
* 输 出 :无
|
||
*********************************************************************************************************
|
||
*/
|
||
void RTC_WKUP_IRQHandler(void)
|
||
{
|
||
if(RTC_GetITStatus(RTC_IT_WUT) != RESET)
|
||
{
|
||
RTC_ClearITPendingBit(RTC_IT_WUT);
|
||
RTC_WakeUpCmd(DISABLE);
|
||
EXTI_ClearITPendingBit(EXTI_Line20);
|
||
MYITStatus1.WUT_ITStatus=1;
|
||
|
||
//u5_printf("WUT hp\r\n");
|
||
//u5_printf("wut_PROS1_RUN:%d\r\n",TASKRUNSS.PROS1_RUN);
|
||
RtcWakeUpConfig(get_meaidv() - 1);
|
||
}
|
||
}
|
||
|
||
/*
|
||
*********************************************************************************************************
|
||
* 函 数 名: Measure_Ap_S()
|
||
* 功能说明: 预约模式进行闹钟AB的处理
|
||
* 输 入 :无
|
||
* 输 出 :无
|
||
*********************************************************************************************************
|
||
*/
|
||
void Measure_Ap_S(void)
|
||
{
|
||
int year,mon,day,hour,min ,sec;
|
||
char temp[4];
|
||
RTC_AlarmCmd(RTC_Alarm_A, DISABLE);//关闭闹钟A
|
||
RTC_WakeUpCmd(DISABLE); //关闭自动唤醒
|
||
//配置闹钟B的唤醒时间
|
||
memcpy(temp,Init_Data_Equipment.appointment_time,4);
|
||
year = atoi(temp);
|
||
memset(temp,0,4);
|
||
memcpy(temp,&Init_Data_Equipment.appointment_time[4],2);
|
||
mon = atoi(temp);
|
||
memcpy(temp,&Init_Data_Equipment.appointment_time[6],2);
|
||
day = atoi(temp);
|
||
memcpy(temp,&Init_Data_Equipment.appointment_time[8],2);
|
||
hour = atoi(temp);
|
||
memcpy(temp,&Init_Data_Equipment.appointment_time[10],2);
|
||
min = atoi(temp);
|
||
memcpy(temp,&Init_Data_Equipment.appointment_time[12],2);
|
||
sec = atoi(temp);
|
||
if(sec == 0)
|
||
{
|
||
sec = 59;
|
||
if(min == 0)
|
||
{
|
||
min = 59;
|
||
hour = hour - 1;
|
||
}else min = min - 1;
|
||
}else sec = sec - 1;
|
||
|
||
RTC_AlarmBConfig(year,mon,day,hour,min,sec);
|
||
}
|
||
|
||
/*
|
||
*********************************************************************************************************
|
||
* 函 数 名: Measure_Ap_E()
|
||
* 功能说明: 预约模式设备启动后的处理
|
||
* 输 入 :无
|
||
* 输 出 :无
|
||
*********************************************************************************************************
|
||
*/
|
||
void Measure_Ap_E(void)
|
||
{
|
||
MYITStatus1.AlarmA_ITStatus = 1;
|
||
MYITStatus1.WUT_ITStatus = 1;
|
||
|
||
RTC_AlarmCmd(RTC_Alarm_B, DISABLE);//关闭闹钟B
|
||
}
|
||
|
||
/*
|
||
*********************************************************************************************************
|
||
* 函 数 名: EXTI15_10_IRQHandler()
|
||
* 功能说明: IO中断
|
||
* 输 入 :无
|
||
* 输 出 :无
|
||
*********************************************************************************************************
|
||
*/
|
||
void EXTI15_10_IRQHandler(void)
|
||
{
|
||
EXTI_ClearITPendingBit(EXTI_Line13);
|
||
//u5_printf("WAKEUP2 is HP");
|
||
}
|
||
|
||
/*
|
||
*********************************************************************************************************
|
||
* 函 数 名: Check_WKUP()
|
||
* 功能说明: 按键检查
|
||
* 输 入 :无
|
||
* 输 出 :1 按下1S 0不足1S
|
||
*********************************************************************************************************
|
||
*/
|
||
u8 Check_WKUP(void)
|
||
{
|
||
u8 t=0; //记录按下的时间
|
||
while(1)
|
||
{
|
||
if(WKUP_KD)
|
||
{
|
||
t++; //已经按下了
|
||
delay_ms(30);
|
||
if(t>=100) //按下超过1秒钟
|
||
{
|
||
return 1; //按下1s以上了
|
||
}
|
||
}else
|
||
{
|
||
return 0; //按下不足3秒
|
||
}
|
||
}
|
||
|
||
}
|
||
|
||
/*
|
||
*********************************************************************************************************
|
||
* 函 数 名: Key_Scan()
|
||
* 功能说明: 单个按键检测短按和长按事件
|
||
* 输 入 :无
|
||
* 输 出 :KEY_SHORT KEY_LONG 0
|
||
*********************************************************************************************************
|
||
*/
|
||
u8 Key_Scan(void)
|
||
{
|
||
static KEY_STATE KeyState =KEY_CHECK;
|
||
static u8 TimeCnt = 0;
|
||
u8 key_return = 0;
|
||
switch (KeyState)
|
||
{
|
||
//按键未按下状态,此时判断WKUP_KD的值
|
||
case KEY_CHECK:
|
||
if(WKUP_KD)
|
||
{
|
||
KeyState = KEY_COMFIRM; //如果按键WKUP_KD值为1,说明按键开始按下,进入下一个状态
|
||
}
|
||
else
|
||
{
|
||
KeyState = KEY_CHECK;
|
||
}
|
||
TimeCnt = 0; //计数复位
|
||
key_time=TimeCnt;
|
||
key_return=0;
|
||
break;
|
||
|
||
case KEY_COMFIRM:
|
||
if(WKUP_KD) //查看当前WKUP_KD是否还是1,再次确认是否按下
|
||
{
|
||
TimeCnt++;
|
||
key_time=TimeCnt;
|
||
if(TimeCnt>=200)
|
||
{
|
||
TimeCnt=200;
|
||
|
||
KeyState = KEY_RELEASE;
|
||
}
|
||
|
||
}
|
||
else
|
||
{
|
||
KeyState = KEY_RELEASE; // 返回上一个状态
|
||
}
|
||
break;
|
||
|
||
case KEY_RELEASE:
|
||
if(!WKUP_KD) //当前WKUP_KD值为0,说明按键已经释放,返回开始状态
|
||
{
|
||
KeyState = KEY_CHECK;
|
||
if(TimeCnt>10 )
|
||
{
|
||
key_return = KEY_SHORT;
|
||
}
|
||
|
||
if(TimeCnt>100)
|
||
{
|
||
key_return = KEY_LONG;
|
||
}
|
||
|
||
//u5_printf("TimeCnt:%d\r\n",TimeCnt);
|
||
TimeCnt=0;
|
||
}
|
||
|
||
break;
|
||
|
||
default: break;
|
||
}
|
||
return key_return;
|
||
}
|
||
|
||
/***************************** END OF FILE *********************************/
|