Tool/software: Code Composer Studio
Hi,
I am trying to implement a simple function to calculate seconds since epoch(1970-01-01 00:00:00) since MSP430F67xx MCU don't have counter mode in RTC modules. The following is the formula I used:
tm_sec + tm_min*60 + tm_hour*3600 + tm_yday*86400 + (tm_year-70)*31536000 + ((tm_year-69)/4)*86400 - ((tm_year-1)/100)*86400 + ((tm_year+299)/400)*86400
As exampled in code attached below, with hard coded year/month/day/hour/minute/second, the calculated seconds as output from MSP430 seems always smaller than the number I manually calculated in calculator by 65536 or 0x00010000.
If hard coded calendar as 2016-06-25 20:59:12, the seconds calculated by MSP430 is 1466736416/0x576C9F20 while the correct value should be 1466801952/0x576D9F20
What I am missing here? Any comment or feedback will be appreciated.
//WDTCTL = WDTPW | WDTHOLD; // Stop watchdog timer sysInit();
WDTCTL = WDTPW | WDTSSEL__ACLK | WDTIS_3; // Enable watchdog timer: ACLK, 4min and 16 seconds
initSystem();
unsigned long seconds = 0;
char output[5], i;
int year, month, day, hour, minute, second, dayOfYear;
while (1) {
year = 116;
month = 6;
day = 25;
hour = 20;
minute = 59;
second = 12;
//dayOfYear = getDayOfYear(month, day, RTCYEAR);
dayOfYear = 175;
seconds = second + minute * 60 + hour * 3600 + dayOfYear * 86400 + (year - 70) * 31536000 + ((unsigned int)((year - 69) / 4)) * 86400 - ((unsigned int)((year - 1) / 100)) * 86400 + ((year + 299 ) / 400) * 86400;
//seconds = calenda2Seconds(month, day, RTCYEAR);
output[0] = RTCYEAR >> 8;
output[1] = RTCYEAR;
output[2] = month;
output[3] = day;
output[4] = hour;
output[5] = minute;
output[6] = second;
output[7] = dayOfYear >> 8;
output[8] = dayOfYear;
output[9] = seconds >> 24;
output[10] = seconds >> 16;
output[11] = seconds >> 8;
output[12] = seconds;
for(i = 0; i < 13; i++) {
while (!(UCA1IFG&UCTXIFG)); // USCI_A0 TX buffer ready?
UCA1TXBUF = output[i];
}
__no_operation();
}