This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

CCS/MSP430F6721: Problem calculating seconds since epoch from calendar date and time

Part Number: MSP430F6721

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();
    }



  • Just in case someone might be interested, the problem lies where hour * 3600. If hour <= 18 therefore the result < 65536, then the final resulted seconds value is correct. If hour >18 then somehow resulted value of seconds is 65536 smaller than correct value. I believe this is something related to implementation dependent conversion of int to long.

    As for now, I use following line to get around it.

    unsigned long const secondsOfHour = 3600;
    seconds = second + minute * 60 + hour * secondsOfHour+ dayOfYear * 86400 + (year - 70) * 31536000 + ((year - 69) / 4) * 86400 - ((year - 1) / 100) * 86400 + ((year + 299 ) / 400) * 86400;

  • Hi Zhiyong,

    This is definitely an int to long conversion issue, thank you for further debugging this and coming up with a solution.

    Regards,
    Ryan
  • Hi Ryan,

    Thanks for your reply. Could you point me to some documentation on this int to long conversion? I have been under the assumption that when converting int to long, all bytes will be copied to lower ( 2 or 4) bytes of long from int. Since I am mostly self-taught, I feel I might be missing something.

    Regards,

    Zhiyong

  • I am not aware on any formal documentation on the subject, this issue relates more to C data type conversion then something generated by TI/MSP430.

    Regards,
    Ryan

**Attention** This is a public forum