Does anyone else find the RTCC module on PIC24s really awkward to use?

Thread Starter

Robin66

Joined Jan 5, 2016
275
I feel that I must be missing some tricks. I discovered the RTCC module and started using it rather than incrementing global "seconds", "minutes", "hours", "dayOfMonth", "month", and "year" myself on an timer based interrupt event. However, now if I want to extract a timestamp from the module I end up re-constituting the no. of seconds from 2 vars which are contained within the same 16bit field, so even though the C looks a bit messy, the assembly is much worse because it needs to extract the fields which xc16 has shielded me from.

eg. previously to format the time into a string I would run this
Code:
sprintf(str,"%02hu:%02hu:%02hu",hours,minutes,seconds);
However now I need to run this.

Code:
sprintf(str,"%01hu%01hu:%01hu%01hu:%01hu%01hu",TIMEHbits.HRTEN,TIMEHbits.HRONE,TIMEHbits.MINTEN,TIMEHbits.MINONE,TIMELbits.SECTEN,TIMELbits.SECONE);
It doesn't seem like progress to me. I feel that there should be an easier way to extract the no. of seconds from the RTCC module than the below
Code:
194:                       second  = TIMELbits.SECTEN*10 + TIMELbits.SECONE;
00DF6A  800EE2     MOV TIMEL, W2
00DF6C  DE114C     LSR W2, #12, W2
00DF6E  800EE4     MOV TIMEL, W4
00DF70  DE2248     LSR W4, #8, W4
00DF72  610167     AND W2, #0x7, W2
00DF74  B9116A     MUL.SU W2, #10, W2
00DF76  62026F     AND W4, #0xF, W4
00DF78  420102     ADD W4, W2, W2
Sure I'm getting the calendar with leap-years for free, but the code to increment on a second interrupt was straightforward and very efficient. I'm now losing a lot more instructions formating the current time than the cost of executing the below every second.
Code:
    static const short nDay[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
  
    /* Real Time Clock */
    if (++rtcMilli >= 1000) {
        rtcMilli = 0;
        if (++rtcSec >= 60) {
            rtcSec = 0;
            if (++rtcMin >= 60) {
                rtcMin = 0;
                if (++rtcHour >= 24) {
                    rtcHour = 0;
                    short n = nDay[rtcMon - 1];
                    if ((n == 28) && !(rtcYear & 3)) n++;
                    if (++rtcMday > n) {
                        rtcMday = 1;
                        if (++rtcMon > 12) {
                            rtcMon = 1;
                            rtcYear++;
                        }
                    }
                }
            }
        }
    }
 
Top