📄 rtc.c
字号:
#include "..\inc\rtc.h"
#include "..\ucos_ii\ucos_ii.h"
char *date[7] = {"SUN","MON","TUE","WED","THU","FRI","SAT"};
char *month[12]={ "January ","February ","March ","April ", "May ","June ", "July ", "August ", "September ","October ", "November ", "December "};
INT16U Get_Year;
INT8U Get_Month,Get_Day,Get_Date,Get_Hour,Get_Minutes,Get_Second;
OS_EVENT *Rtc_Alarm_Sem;
OS_EVENT *Rtc_Tick_Sem;
OS_EVENT *Rtc_Rw_Sem;
/*****************************RTC INT*********************************/
void Rtc_Init(void)
{
Rtc_Alarm_Sem = OSSemCreate(0);
Rtc_Tick_Sem = OSSemCreate(0);
Rtc_Rw_Sem = OSSemCreate(1);
}
/*
*********************************************************************************************************
* SET DATE ONLY
*
* Description : Set the date of the time-of-day clock
* Arguments : Month is the desired month (1..12)
* Day is the desired day (1..31)
* Year is the desired year (0..99)
* Week is the desired week (0..7)
*********************************************************************************************************
*/
void Set_Rtc_Date(int Year,int Month,int Day,int Week)
{
INT8U err;
OSSemPend(Rtc_Rw_Sem,0,&err);
rRTCCON = 0x01;
rBCDYEAR = Year-2000;
rBCDMON = Month;
rBCDDAY = Day;
rBCDDATE = Week; // SUN:1 MON:2 TUE:3 WED:4 THU:5 FRI:6 SAT:7
rRTCCON = 0x00;
OSSemPost(Rtc_Rw_Sem);
}
/*
*********************************************************************************************************
* SET TIME ONLY
*
* Description : Set the time-of-day clock
* Arguments : Hour is the desired hour (0..23)
* Minutes is the desired minutes (0..59)
* Second is the desired seconds (0..59)
*********************************************************************************************************
*/
void Set_Rtc_Time(int Hour,int Minutes,int Second)
{
INT8U err;
OSSemPend(Rtc_Rw_Sem,0,&err);
rRTCCON = 0x01;
rBCDHOUR = Hour;
rBCDMIN = Minutes;
rBCDSEC = Second;
rRTCCON = 0x00;
OSSemPost(Rtc_Rw_Sem);
}
/*****************************GET RTC TIME*****************************/
void Get_Rtc_Time(void)
{
INT8U err;
OSSemPend(Rtc_Rw_Sem,0,&err);
rRTCCON = 0x01;
Get_Year=2000+rBCDYEAR;
Get_Month=rBCDMON;
Get_Day=rBCDDAY;
Get_Date=rBCDDATE;
Get_Hour=rBCDHOUR;
Get_Minutes=rBCDMIN;
Get_Second=rBCDSEC;
rRTCCON = 0x00;
OSSemPost(Rtc_Rw_Sem);
}
/****************************SET ALARM********************************/
void Set_Alarm_Date_Time(int Ayear,int Amonth,int Aday,int Ahour,int Aminute,int Asecond)
{
INT8U err;
OSSemPend(Rtc_Rw_Sem,0,&err);
rRTCCON = 0x01;
rALMYEAR=Ayear-2000;
rALMMON =Amonth;
rALMDAY =Aday ;
rALMHOUR=Ahour ;
rALMMIN =Aminute;
rALMSEC =Asecond;
rRTCCON = 0x00; // R/W disable(for power consumption), 1/32768, Normal(merge), No reset
OSSemPost(Rtc_Rw_Sem);
}
void Alarm_Start(void)
{
rRTCALM =0x7f;
rINTMSK=~(BIT_GLOBAL|BIT_RTC);
}
void Alarm_Stop(void)
{ rRTCALM =0x00;
rINTMSK=BIT_GLOBAL|BIT_RTC;
}
void Rtc_Alarm(void)
{
rI_ISPC=BIT_RTC;
OSSemPost(Rtc_Alarm_Sem);
}
/***********************************SET RTC TICK***********************/
void Set_Rtc_Tick(void)
{
rINTMSK = BIT_GLOBAL | BIT_TICK;
rRTCCON = 0x01; //R/W enable(for power consumption), 1/32768, Normal(merge), No reset
rTICINT = ((1<<7)+127); //START
rRTCCON = 0x00; //END
rINTMSK=~(BIT_GLOBAL|BIT_TICK);
}
void Rtc_Tick(void)
{
rI_ISPC=BIT_TICK;
OSSemPost(Rtc_Tick_Sem);
}
/***********************************DISPLAY RTC************************/
void Display_Rtc(void)
{
Get_Rtc_Time();
Uart_Printf("%4x,%s,%2x,%s,%2x:%2x:%2x\n",Get_Year,month[Get_Month],Get_Day,date[Get_Date],Get_Hour,Get_Minutes,Get_Second);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -