RTC的使用很简单,对寄存器进行初始化,然后读取,最后显示就可以了。只是硬件还有问题,这点花了我一天时间!RTC需要单独供电的,一开始我以为他们直接接到电源上了呢,后来也怀疑过,但是当时可能是太粗心,看错引脚了。后来才找到原来VDDRTC是需要接2.5V电源的。在调试的过程中,软件和硬件都要考虑,都应该检查!
以下是部分源程序:
void Rtc_Init(void)
{
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=0x00;
}
读取数据
void Read_Rtc(void)
{
rRTCCON = 0x01; // R/W enable, 1/32768, Normal(merge), No reset
while(1)
{
if (rBCDYEAR == 0x99)
{
year = 0x1999;
}
else
{
year = 0x2000 + rBCDYEAR;
}
month = rBCDMON;
day = rBCDDAY;
weekday = rBCDDATE;
hour = rBCDHOUR;
min = rBCDMIN;
sec = rBCDSEC;
if (sec!=0)
break;
}
rRTCCON = 0x00; // R/W disable(for power consumption), 1/32768, Normal(merge), No reset
}
显示函数
void DispYear(int num)
{
char str[5];
char *ch2;
str[0] = num/4096+0x30;
str[1] = (num%4096)/256+0x30;
str[2] = (num%4096)%256/16+0x30;
str[3] = (num%4096)%256%16+0x30;
str[4] = '/0';
ch2=str;
for(;*ch2;ch2++)
Uart0Send(*ch2);
}
void DispNum(int num)
{
char str[3];
char *ch2;
str[0]=num/16+0x30;
str[1]=num%16+0x30;
str[2]='/0';
ch2=str;
for(;*ch2;ch2++)
Uart0Send(*ch2);
}