unsigned char mode;\r
unsigned char alarm[3];\r
unsigned char lastcdn[2];\r
-unsigned char lastyear[2];\r
+unsigned char lastyear[5];\r
unsigned char digit;\r
unsigned char changed;\r
unsigned char run;\r
}\r
}\r
P3OUT|=0x01; //chip select up\r
+ for(i=0;i<255;++i); //looks like the RTC might not like another SPI transmission exactly before the previous one, sometimes.\r
}\r
\r
#pragma vector=PORT2_VECTOR\r
time[7]=inbuf[7];//year\r
leap=inbuf[6]&0x20;\r
\r
- spi(RTC_READ,0x0f,inbuf,outbuf,1); // <- because of RTC bug (?)\r
+ spi(RTC_READ,0x0f,inbuf,outbuf,1); // <- because of RTC bug (probably not needed anymore?)\r
if(!((time[2]^alarm[1])|(time[3]^alarm[2])) && alarm[0])\r
{\r
mode=MODE_ALARM;\r
//P2SEL&= ~0x40;\r
}\r
\r
+ lastyear[4]=(time[7]==0x00 && lastyear[0]==0x99)?__bcd_add_short(lastyear[1],0x01):lastyear[1];\r
if (time[7]!=lastyear[0])\r
{\r
- if(time[7]<lastyear[0])\r
- lastyear[1]=__bcd_add_short(lastyear[1],0x01);\r
- \r
- lastyear[0]=time[7];\r
+ if(time[7]==lastyear[2])\r
+ ++(lastyear[3]);\r
+ else\r
+ lastyear[3]=0;\r
\r
- outbuf[0]=ALARMVALUE;\r
- outbuf[1]=lastyear[0];\r
- outbuf[2]=lastyear[1];\r
+ lastyear[2]=time[7];\r
\r
- spi(RTC_WRITE,0x26,inbuf,outbuf,3);\r
+ if(lastyear[3]>55)\r
+ {\r
+ lastyear[3]=0;\r
+ \r
+ if(time[7]==0x00 && lastyear[0]==0x99)\r
+ lastyear[1]=__bcd_add_short(lastyear[1],0x01);\r
+ \r
+ lastyear[0]=time[7];\r
+ \r
+ outbuf[0]=ALARMVALUE;\r
+ outbuf[1]=lastyear[0];\r
+ outbuf[2]=lastyear[1];\r
+ spi(RTC_WRITE,0x26,inbuf,outbuf,3);\r
+ } \r
}\r
\r
+ \r
if(mode==MODE_DEBUG && changed)\r
spi(RTC_READ,settime[1],settime,outbuf,1);\r
}\r
{\r
dispvalue[0]=symbol[time[7]&0x0f];\r
dispvalue[1]=symbol[(time[7]&0xf0)>>4];\r
- dispvalue[2]=symbol[lastyear[1]&0x0f];\r
- dispvalue[3]=symbol[(lastyear[1]&0xf0)>>4]&(alarm[0]?SYMB_DOT:SYMB_NUL);\r
+ dispvalue[2]=symbol[lastyear[4]&0x0f];\r
+ dispvalue[3]=symbol[(lastyear[4]&0xf0)>>4]&(alarm[0]?SYMB_DOT:SYMB_NUL);\r
}\r
else\r
{\r
else\r
{\r
dispvalue[0]=SYMB_NUL;\r
- dispvalue[1]=SYMB_3&SYMB_DOT;\r
+ dispvalue[1]=SYMB_4&SYMB_DOT;\r
dispvalue[2]=SYMB_1;\r
dispvalue[3]=SYMB_NUL&(alarm[0]?SYMB_DOT:SYMB_NUL);\r
}\r
ME2|=USPIE0;\r
U0CTL &= ~SWRST;\r
\r
+ \r
//init RTC\r
\r
+ //Looks like sometimes the first SPI reads rom the RTC may be incorrect.\r
+ //To be sure, let's do a few redundant reads before starting the real work\r
+ \r
+ for (delay = 0xFF; delay > 0; --delay)\r
+ spi(RTC_READ,0x00,inbuf,outbuf,1);\r
+ \r
//set 24 hour mode\r
spi(RTC_READ,0x03,inbuf,outbuf,1);\r
if(inbuf[0]&0x40)\r
alarm[1]=inbuf[1];\r
alarm[2]=inbuf[2];\r
\r
- spi(RTC_READ,0x0f,inbuf,outbuf,1); // <- because of RTC bug (?)\r
+ spi(RTC_READ,0x2f,inbuf,outbuf,1); // <- because of RTC bug (?)\r
\r
//get last countdown value from RTC SRAM\r
spi(RTC_READ,0x23,inbuf,outbuf,3);\r
lastcdn[1]=0;\r
}\r
\r
- spi(RTC_READ,0x0f,inbuf,outbuf,1); // <- because of RTC bug (?)\r
+ spi(RTC_READ,0x2f,inbuf,outbuf,1); // <- because of RTC bug (?)\r
\r
//get last year value from RTC SRAM\r
spi(RTC_READ,0x26,inbuf,outbuf,3);\r
lastyear[0]=0x00;\r
lastyear[1]=0x20;\r
}\r
+ lastyear[3]=0;\r
\r
//start oscillator\r
spi(RTC_READ,0x01,inbuf,outbuf,1);\r