📄 rtc.c
字号:
/*******************************************************************************************
* File: rtc.c
* Author: Embest ShengWang He
* desc: Test the RTC function in microprocessor
* History:
*******************************************************************************************/
#include <stdio.h>
#include <signal.h>
#include <unistd.h>
//define CONFIGURATION REGISTER and RTC REGISTER address
#define rRTCCON (*(volatile unsigned char *)0x1d70040)
#define rRTCRST (*(volatile unsigned char *)0x1d7006c)
#define rBCDSEC (*(volatile unsigned char *)0x1d70070)
#define rBCDMIN (*(volatile unsigned char *)0x1d70074)
#define rBCDHOUR (*(volatile unsigned char *)0x1d70078)
#define rBCDDAY (*(volatile unsigned char *)0x1d7007c)
#define rBCDDATE (*(volatile unsigned char *)0x1d70080)
#define rBCDMON (*(volatile unsigned char *)0x1d70084)
#define rBCDYEAR (*(volatile unsigned char *)0x1d70088)
//define the default data for RTC
#define TESTYEAR (0x02)
#define TESTMONTH (0x12)
#define TESTDAY (0x31)
#define TESTDATE (0x03) // SUN:1 MON:2 TUE:3 WED:4 THU:5 FRI:6 SAT:7
#define TESTHOUR (0x23)
#define TESTMIN (0x59)
#define TESTSEC (0x59)
void Read_Rtc(void);
void USE_RTC(void);
int Test_Rtc(void);
int nYear;
int nMonth,nDay,nWeekday,nHour,nMin,nSec;
char *szdate[8] = {"","SUN","MON","TUE","WED","THU","FRI","SAT"};
int main(void)
{
int i = 0, a , b;
char cYn;
//read current time
Read_Rtc();
printf(" Current Time is %02x-%02x-%02x %s", nYear, nMonth, nDay, szdate[nWeekday]);
printf(" %02x:%02x:%02x\n", nHour, nMin, nSec);
//check the RTC
printf("\n check the RTC now(Y/N)? ");
cYn = getchar();
getchar();
if((cYn == 0x0d)|(cYn == 0x59)|(cYn == 0x79)) //want to check RTC?
{
if( Test_Rtc() ) USE_RTC();
else printf("\n RTC does not work. "); ;
}
else USE_RTC();
//display Current time
while(i<5)
{
Read_Rtc();
printf(" Current Time is %02x-%02x-%02x %s", nYear, nMonth, nDay, szdate[nWeekday]);
printf(" %02x:%02x:%02x\n", nHour, nMin, nSec);
++i;
for( a = 0; a<10000; a++ )
for( b=0; b<1800; b++ ); //wait
}
return 0;
}
/**********************************************************************************************
* Name: Read_Rtc
* Function: Read current time from RTC Register
* Paramater: None
* Return: None
* Modify:
* Comment:
**********************************************************************************************/
void Read_Rtc(void)
{
//Uart_Printf("This test should be excuted once RTC test(Alarm) for RTC initialization\n");
rRTCCON = 0x01; // R/W enable, 1/32768, Normal(merge), No reset
while(1)
{
if(rBCDYEAR == 0x99)
nYear = 0x1999;
else
nYear = 0x2000 + rBCDYEAR;
nMonth = rBCDMON;
nDay = rBCDDAY;
nWeekday = rBCDDATE;
nHour = rBCDHOUR;
nMin = rBCDMIN;
nSec = rBCDSEC;
if(nSec != 0)
break;
}
rRTCCON = 0x0; // R/W disable(for power consumption), 1/32768, Normal(merge), No reset
}
/***********************************************************************************************
* Name: Test_Rtc
* Function: Check the RTC function working or not
* Paramater: None
* Return: isRtcok ----0
----1
* Modify:
* Comment:
*************************************************************************************************/
int Test_Rtc(void)
{
int a,b,nIsRtcok;
//write the rtc Register
rRTCCON = 0x01; // R/W enable, 1/32768, Normal(merge), No reset
rBCDYEAR = TESTYEAR;
rBCDMON = TESTMONTH;
rBCDDAY = TESTDAY; // SUN:1 MON:2 TUE:3 WED:4 THU:5 FRI:6 SAT:7
rBCDDATE = TESTDATE;
rBCDHOUR = TESTHOUR;
rBCDMIN = TESTMIN;
rBCDSEC = TESTSEC;
rRTCCON = 0x0; // R/W disable(for power consumption), 1/32768, Normal(merge), No reset
printf("\n Set Default Time at 20%02x-%02x-%02x %s", TESTYEAR, TESTMONTH, TESTDAY, szdate[TESTDATE]);
printf(" %02x:%02x:%02x\n\r", TESTHOUR, TESTMIN, TESTSEC);
//check rtc work or not
nIsRtcok = 0;
for( a=0; a<10000; a++ )
for( b=0; b<5000; b++ ); // wait
Read_Rtc();
if((nYear != TESTYEAR) |(nMonth != TESTMONTH)|(nDay != TESTDAY)|(nHour != TESTHOUR)|(nMin != TESTMIN)|(nSec != TESTSEC))
nIsRtcok = 1;
printf(" RTC is OK now");
return nIsRtcok;
}
/****************************************************************************************************
* Name: USE_RTC
* Function: To set time for RTC
* Paramater: None
* Return: None
* Modify:
* Comment:
****************************************************************************************************/
void USE_RTC(void)
{
char cYn,cTmp,i,cN09=1;
char cNum0 = 0x30; //"0";
char cNum9 = 0x39; //"9";
char szchar[] = {0,'-',' ',':'};
char szDATE[12]; //xxxx-xx-xx x
char szTIME[8]; //xx:xx:xx
int a=0,b=0;
//set time
printf("\n RTC Working now. To set time(Y/N)? ");
cYn = getchar();
getchar();
if( (cYn == 0x0d) | (cYn == 0x59) | (cYn == 0x79) ) //want to set time?
{
///////////////////////////////////////////////////////////////////////////////////
// set the data about year,month,day and date
do{
printf("\nCurrent day is (%04x,%02x,%02x, %s). To set day(yy-mm-dd w):"
,nYear,nMonth,nDay,szdate[nWeekday]); //display currrent time
while((szDATE[a] = getchar())!= '\n') ++a; //get data from keybroad
//check the format of the data
if(szDATE[0] == 0x32)
{
if((szDATE[4] == szchar[1]) & (szDATE[7] == szchar[1]) & (szDATE[10] == szchar[2]) )
{
if( (szDATE[11]>0x30) & (szDATE[11]<0x38) )
{
i = 0; cN09 = 0;
while(i<12)
{
if((i != 4)&(i != 7)&(i != 10))
{
if( (szDATE[i]<cNum0) | (szDATE[i]>cNum9) )
{
cN09 = 1;
break;
}
}
i++;
}
if(cN09 == 0)
break; //all right
} // if date 1 - 7
} // if "-" or " "
} // if 32 (21th century)
cN09 = 1;
printf("\n Wrong value!! To set again(Y/N)? ");
cYn = getchar(); //want to set DATE again?
getchar();
//if((yn == 0x4E)|(yn == 0x6E)|(yn == 0x59)|(yn == 0x79)) Uart_SendByte(yn);
}while((cYn == 0x0d)|(cYn == 0x59)|(cYn == 0x79));
//write the data into RTC Register
if(cN09 == 0)
{
rRTCCON = 0x01; // R/W enable, 1/32768, Normal(merge), No reset
rBCDYEAR = ((szDATE[2]<<4)|0x0f) & (szDATE[3]|0xf0); //->szyear;
rBCDMON = ((szDATE[5]<<4)|0x0f) & (szDATE[6]|0xf0); //->szmonth;
rBCDDAY = ((szDATE[8]<<4)|0x0f) & (szDATE[9]|0xf0); //->szday;
cTmp = ( (szDATE[11]&0x0f) + 1 );
if(cTmp == 8) rBCDDATE = 1;// SUN:1 MON:2 TUE:3 WED:4 THU:5 FRI:6 SAT:7
else rBCDDATE = cTmp;
rRTCCON = 0x00; // R/W disable
}
else printf("\n\n Use Current DATE Settings.\n");
///////////////////////////////////////////////////////////////////////////////////
// set the data about hour, minute,sec
do{
printf("\nCurrent time is (%02x:%02x:%02x). To set time(hh:mm:ss): "
,nHour,nMin,nSec); //display currrent time
while((szTIME[b] = getchar()) != '\n')++b; //get data from keybroad
//check the format of the data
if((szTIME[2] == szchar[3]) & (szTIME[5] == szchar[3]))
{
i=0; cN09 = 0;
while(i<8)
{
if((i != 2)|(i != 5))
{
if( (szTIME[i]<cNum0) & (szTIME[i]>cNum9) )
{
cN09 = 1;
break;
}
}
i++;
}
if(cN09 == 0)
{
cTmp = ((szTIME[0]<<4)|0x0f) & (szTIME[1]|0xf0);
if((cTmp >0) & (cTmp<0x24))
{
szTIME[2] = cTmp; //->shour;
cTmp = ((szTIME[3]<<4)|0x0f) & (szTIME[4]|0xf0);
if(cTmp <= 0x59)
{
szTIME[5] = cTmp; //->smin;
cTmp = ((szTIME[6]<<4)|0x0f) & (szTIME[7]|0xf0);
if(cTmp <= 0x59)
break; //all right
} //if min < 59
} //if 0 < hour < 24
} //if num 0-9
}
cN09 = 1;
printf("\n Wrong value!! To set again(Y/N)? ");
cYn = getchar(); //want to set Time again?
getchar();
//if((yn == 0x4E)|(yn == 0x6E)|(yn == 0x59)|(yn == 0x79)) Uart_SendByte(yn);
}while((cYn == 0x0d)|(cYn == 0x59)|(cYn == 0x79));
//write the data into RTC Register
if(cN09 == 0)
{
rRTCCON = 0x01; // R/W enable, 1/32768, Normal(merge), No reset
rBCDHOUR = szTIME[2]; //->szhour;
rBCDMIN = szTIME[5]; //->szmin;
rBCDSEC = ((szTIME[6]<<4)|0x0f) & (szTIME[7]|0xf0); //->szsec;
rRTCCON = 0x00; // R/W disable
}
else printf("\n\n Use Current TIME Settings.\n");
}
else printf("\n Use Current Settings...\n");
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -