主时钟20MHz,外接副时钟接32。768KHz做实时时钟频率,在程序运行中,发现每一分钟,时间要慢2秒,请问是怎么回事?下面是RE做RTC的部分程序,请帮忙解决
#pragma interrupt INT_timer_re();
void Clock_Init (void)
{
uCHAR detectTime=10;
prcr=0x01;
cm13=1;
cm15=1;
cm05=0;
while(0!=detectTime)
{
if(ocd3) twdDelay(50);
detectTime--;
}
if(ocd3)
{
fra00=1;
twdDelay(50);
fra1=0;
twdDelay(50);
fra01=1;
ocd=1;
}
else
ocd=0;
cm16=0;
cm17=0;
cm06=0;
prcr=0;
}
void Sub_Clock_Init (void)
{
uCHAR detectTime=10;
pd4_3 = 0; //P4_3(XCIN), P4_4(XCOUT): Set input port
pd4_4 = 0; //P4_3(XCIN), P4_4(XCOUT): Set input port
prc0=1; //System control register protect disabled
cm04=1; //XCIN-XCOUT oscillation function
prc0=0; //System control register protect
while(0!=detectTime)
{
twdDelay(50);
detectTime--;
}
}
void timer_re_init(void)
{
uCHAR detectTime=10;
tstart_trecr1 = 0; /* Stop Timer RE operation */
while(0!=detectTime)
{
if(tcstf_trecr1 == 0)
break;
twdDelay(50);
detectTime--;
}
treic = 0x00; /* Disable Timer RE Interrupt */
trerst_trecr1 = 1; /* Timer RE Register and Control Circuit Reset */
trerst_trecr1 = 0;
rcs0_trecsr = 0; /*Set to 0 in real-time clock mode */
rcs1_trecsr = 0; //Set to 0 in real-time clock mode
rcs2_trecsr = 0; //Set to 0 in real-time clock mode
rcs3_trecsr = 1; /* Set to "1" real-time clock mode */
tresec = 0x00; //Second data, 00 second set
tremin = 0x00; //Minute data, 00 minute set
trehr = 0x00; //Hour data, 00 hour set
trewk = 0x00; //Day of week data, Sunday set
toena_trecr1 = 0; //Disable clock output
h12_h24_trecr1 = 1; //Operating mode, 24-hour mode set
pm_trecr1 = 0; //A.M./P.M. bit, a.m. set
int_trecr1 = 1; //Set to 1 in real-time clock mode
seie_trecr2 = 1; //Period of the second interrupt enabled
treic = 0x07; /* Timer RE interrupt enabled (level 2) */
tstart_trecr1 = 1; /* Start Timer RE operation */
detectTime=10;
while(0!=detectTime)
{
if(tcstf_trecr1)
break;
twdDelay(50);
detectTime--;
}
}
void hdwinit( void )
{
_DisbleInt;
/* Clock generator initiate */
Clock_Init();
/* Port initiate */
Port_Init();
/* INT initiate */
INT_Init();
/* WDT initiate */
//WDT_Init();
/* AD initiate */
AD_Init();
/* TM00 initiate */
timer_ra_init();
/* TM01 initiate */
timer_rc_init();
//real time clock initate
Sub_Clock_Init();
timer_re_init();
/*IIC initate*/
Iic_init();
_EnableInt; //开中断
}
void main(void)
{
hdwinit();
while(1)
{
}
}
void INT_timer_re(void)
{
TimeData[0] = tresec&0x7f; //Second data backed up
TimeData[1] = tremin&0x7f; //Minute data backed up
TimeData[2] = trehr&0x3f; //Hour data backed up
TimeData[3] = trewk&0x07; //Week data backed up
TimeData[4] = trecr1; //A.M./P.M. bit backed up
}