From: b Date: Thu, 10 Sep 2015 07:36:58 +0000 (+0200) Subject: keep year digits, workaround RTC bug X-Git-Tag: v1.4~1 X-Git-Url: http://bicyclesonthemoon.info/git-projects/?a=commitdiff_plain;h=ce0faae9ffe73348e987c3350095698469db752c;p=staf%2Fstaf keep year digits, workaround RTC bug --- diff --git a/definitions.h b/definitions.h old mode 100644 new mode 100755 diff --git a/main.c b/main.c old mode 100644 new mode 100755 index 86fb16f..c2e9f14 --- a/main.c +++ b/main.c @@ -10,7 +10,7 @@ unsigned char settime[3]; unsigned char mode; unsigned char alarm[3]; unsigned char lastcdn[2]; -unsigned char lastyear[2]; +unsigned char lastyear[5]; unsigned char digit; unsigned char changed; unsigned char run; @@ -138,6 +138,7 @@ void spi( } } P3OUT|=0x01; //chip select up + for(i=0;i<255;++i); //looks like the RTC might not like another SPI transmission exactly before the previous one, sometimes. } #pragma vector=PORT2_VECTOR @@ -158,7 +159,7 @@ __interrupt void P2_ISR() time[7]=inbuf[7];//year leap=inbuf[6]&0x20; - spi(RTC_READ,0x0f,inbuf,outbuf,1); // <- because of RTC bug (?) + spi(RTC_READ,0x0f,inbuf,outbuf,1); // <- because of RTC bug (probably not needed anymore?) if(!((time[2]^alarm[1])|(time[3]^alarm[2])) && alarm[0]) { mode=MODE_ALARM; @@ -200,20 +201,33 @@ __interrupt void P2_ISR() //P2SEL&= ~0x40; } + lastyear[4]=(time[7]==0x00 && lastyear[0]==0x99)?__bcd_add_short(lastyear[1],0x01):lastyear[1]; if (time[7]!=lastyear[0]) { - if(time[7]55) + { + lastyear[3]=0; + + if(time[7]==0x00 && lastyear[0]==0x99) + lastyear[1]=__bcd_add_short(lastyear[1],0x01); + + lastyear[0]=time[7]; + + outbuf[0]=ALARMVALUE; + outbuf[1]=lastyear[0]; + outbuf[2]=lastyear[1]; + spi(RTC_WRITE,0x26,inbuf,outbuf,3); + } } + if(mode==MODE_DEBUG && changed) spi(RTC_READ,settime[1],settime,outbuf,1); } @@ -601,8 +615,8 @@ __interrupt void P2_ISR() { dispvalue[0]=symbol[time[7]&0x0f]; dispvalue[1]=symbol[(time[7]&0xf0)>>4]; - dispvalue[2]=symbol[lastyear[1]&0x0f]; - dispvalue[3]=symbol[(lastyear[1]&0xf0)>>4]&(alarm[0]?SYMB_DOT:SYMB_NUL); + dispvalue[2]=symbol[lastyear[4]&0x0f]; + dispvalue[3]=symbol[(lastyear[4]&0xf0)>>4]&(alarm[0]?SYMB_DOT:SYMB_NUL); } else { @@ -642,7 +656,7 @@ __interrupt void P2_ISR() else { dispvalue[0]=SYMB_NUL; - dispvalue[1]=SYMB_3&SYMB_DOT; + dispvalue[1]=SYMB_4&SYMB_DOT; dispvalue[2]=SYMB_1; dispvalue[3]=SYMB_NUL&(alarm[0]?SYMB_DOT:SYMB_NUL); } @@ -739,8 +753,15 @@ int main( void ) ME2|=USPIE0; U0CTL &= ~SWRST; + //init RTC + //Looks like sometimes the first SPI reads rom the RTC may be incorrect. + //To be sure, let's do a few redundant reads before starting the real work + + for (delay = 0xFF; delay > 0; --delay) + spi(RTC_READ,0x00,inbuf,outbuf,1); + //set 24 hour mode spi(RTC_READ,0x03,inbuf,outbuf,1); if(inbuf[0]&0x40) @@ -772,7 +793,7 @@ int main( void ) alarm[1]=inbuf[1]; alarm[2]=inbuf[2]; - spi(RTC_READ,0x0f,inbuf,outbuf,1); // <- because of RTC bug (?) + spi(RTC_READ,0x2f,inbuf,outbuf,1); // <- because of RTC bug (?) //get last countdown value from RTC SRAM spi(RTC_READ,0x23,inbuf,outbuf,3); @@ -787,7 +808,7 @@ int main( void ) lastcdn[1]=0; } - spi(RTC_READ,0x0f,inbuf,outbuf,1); // <- because of RTC bug (?) + spi(RTC_READ,0x2f,inbuf,outbuf,1); // <- because of RTC bug (?) //get last year value from RTC SRAM spi(RTC_READ,0x26,inbuf,outbuf,3); @@ -801,6 +822,7 @@ int main( void ) lastyear[0]=0x00; lastyear[1]=0x20; } + lastyear[3]=0; //start oscillator spi(RTC_READ,0x01,inbuf,outbuf,1);