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.

MSP430FG4619--RTC-second and millisecond

Other Parts Discussed in Thread: MSP430FG4619, MSP430F5529

Hello professors,


These days I am stuck in the FG4619 RTC part. In my project, I need to use the millisecond, but the RTC part only has second module. I made some selections that I tried to use timer module to synchronize with second part in the RTC module, but it is hard to synchronize, especially from the beginning 0s to 1s, do you have some suggestions? I'm really thankful for your time. 

Best Regards

  • you should synchronize you milisecond in second RTC interrupt,

    if new second occur  clear you milisecond counter,

    at the beginning (0s to 1s) clear you milisecond counter and start  RTC

  • Hi,

    You can try the following:

      /***************************************************************************** 
        Structure definitions required by exported routines
      *****************************************************************************/ 
      struct str_time {
        int year;
        int month;
        int day;
        int hour;
        int min;
        int sec;
        int fract;
      };
    
    /*****************************************************************************
       RTC_GetTime
       
       Description: 
          Routine is used to get the time from the units RTC
    
       Parameters
          none
    
       Return
          none
          
    *****************************************************************************/ 
    void GetTime (struct str_time *time)
    {
      long templ;
    
      /* Get values from the RTC registers */
      time->year = ((int)RTCYEARH << 8) + RTCYEARL;
      time->month = RTCMON;
      time->day = RTCDAY;
      time->hour = RTCHOUR;
      time->min = RTCMIN;
      time->sec = RTCSEC;
      time->fract = RTCPS;
      /* Convert RTCPS in fract to milliseconds */
      time->fract &= ~0x8000;
      templ = (long)time->fract * 1000l;
      time->fract = (int)(((long)time->fract * 1000l)/32768l);
    }
    
    int MatchBuffer(uint8_t *buff1, uint8_t *buff2, size_t size)
    {
      while (size > 0) {
        if (*buff1++ != *buff2++) {
          return (TRUE);
        }
        size--;
      }
      return (FALSE);
    }
    
    void RTC_GetTime(struct str_time *time)
    {
      struct str_time temp_time;
    
      do {
        GetTime(&temp_time);
        GetTime(time);
      } while (MatchBuffer((uint8_t *)time, (uint8_t *)&temp_time, sizeof(struct str_time)));
    }
    
    
    /*****************************************************************************
       RTC_SetTime
       
       Description: 
          Routine is used to set the time in the units RTC
    
       Parameters
          none
    
       Return
          none
          
    *****************************************************************************/ 
    void RTC_SetTime(struct str_time *time)
    {
      long templ;
    
      // Stop the RTC
      RTCCTL01 |= RTCHOLD;
    
      // Initialise the Calendar registers
      RTCSEC = time->sec;
      RTCMIN = time->min;
      RTCHOUR = time->hour;
      RTCDOW = GetWeekday(time->year, time->month, time->day);
      RTCDAY = time->day;
      RTCMON = time->month;
      RTCYEARH = time->year >> 8;
      RTCYEARL = time->year &0xff;
    
      // Determine Count value from millisecs
      templ = (long)time->fract;
      templ = (templ * 32768l) / 1000l;
      RTCPS = (int)templ;
    
      // Restart the RTC
      RTCCTL01 &= ~RTCHOLD;
    }
    
    

    This uses the RTCPS register to try to determine the milliseconds direct from the RTC module. 

    No warranty, but it may help with what you are doing

    Roy

  • sorry, I didnt find the RTCPS register in the msp430fg4619, do you know where is it?

    Thanks

  • thanks so your reply, the rtc only has second interrupt, how can millisecond counter in this intterupt? I mean the interrupt will be called in every one second, so I'm little confused about it.

    Thanks again

  • you should configure timer with 1ms period

    ( for example timer A with TACCR0 in up mode)

    and in this timer interrupt you should count every ms

    ( from 0 to 999 ms)

    but when you have 1 second RTC interrupt

    then you clear 1ms counter,

  • I found the RTC part can only go into the interrupt in every one minute.... it seems that no way to change it I think...have you tried that before? really thankful for your ime

  • Yuan Liao said:
    I found the RTC part can only go into the interrupt in every one minute....

    you have right,

    I didn't check datasheet,

    ok so you should count ms  during 60 seconds

    from 0 to ... 59.999 

    ms counter should be cleared in RTC interrupt routine

    whem minutes change occur,

    during reading RTC time, you should calculate ms value like that:

    (ms in second) = (ms counter value) % (seconds * 1000) ,,,

    and result of operation will be your ms value at the moment when you read RTC,

  • Hi,

    The code that I posted is for the MSP430F5xxx series. There are a number of slightly different RTC modules designated RTC-A, RTC-B. etc. The MSP430F5529 that this is for has an RTC-A which does have the RTCPS register. I have not checked the exact RTC in your processor so I suspect that it is different so thios code does not work.

    With the RTC-A the RTCPS register is a simple counter that you can use to determine the milliseconds and it is synchronised to the second interrupt so it is possible to read seconds direct from the RTC itself. 

    Using a separate timer to generate a millisecond interrupt may be made to work, but it does introduce problems with ensuring that the timer and the RTC are synchronised. The benefit of RTCPS is that the value in this register would appear to be synchronised directly to the second register in the RTC, but that does not really help if you have a different module

    Roy

**Attention** This is a public forum