pic32 rtccTime structure

Thread Starter

adrenalina

Joined Jan 4, 2011
78
Hello everybody. I am working on a project using the rtcc module from a pic32 and I have a question about the module.
what would happen with the code below, will tm.hour = 0x10 or will it be 0x0A? If it equals 0x0A is there some way to do bcd math with c?

Rich (BB code):
rtccTime tm;

tm.hour = 0x09;
tm.hour += 1;
 

ErnieM

Joined Apr 24, 2011
8,377
Let's start by looking at the rtcc structure. It is defined in rtcc.h, and you can read about it in the "C32 Peripherial Lib Guide" which you should find as the very last item in your MPLAB Help | Topics area.

Rich (BB code):
// union/structure for read/write of time into the RTCC device
typedef union
{
    struct
    {
        unsigned char    rsvd;        // reserved for future use. should be 0
        unsigned char    sec;         // BCD codification for seconds, 00-59
        unsigned char    min;         // BCD codification for minutes, 00-59
        unsigned char    hour;        // BCD codification for hours, 00-24
    };                                // field access
    unsigned char        b[4];        // byte access
    unsigned short       w[2];        // 16 bits access
    unsigned long        l;           // 32 bits access
}rtccTime;
From this we can see the hours member is defined as an unsigned char and holds a packed BCD number.

Thus when you perform your code:
Rich (BB code):
rtccTime tm;

tm.hour = 0x09;
tm.hour += 1;
the tm.hour quantity advances from 0x09 to 0x0A as any other unsigned char would. That is not so good for you as you wish the answer to be 0x10 to properly represent the packed BCD number.

The solution is to write some routine to do the increment and check the answer. This also applies to the min and sec members, meaning incrementing sec may mean you also increment min and hour.

Aside: while slightly more cryptic for the beginner, the preferred way to increment a variable is with the increment operator:
Rich (BB code):
rtccTime tm;

tm.hour = 0x09;
tm.hour++;   // use the increment operator to do this
Depending on the compiler this may result in a tiny bit smaller code, while in this context it still gives the same "incorrect" answer and needs further coding to correct to a valid packed BCD number.
 

BMorse

Joined Sep 26, 2009
2,675
I used the following code as a way to make sure my hours are right in the time structure when incrementing the Hours via user input buttons....

Rich (BB code):
int tSH,nREP;
tSH=tSH+0x01;			//increment hour variable
nREP=nREP+0x01;                //Increment counter
if(nREP>0x09){tSH=tSH+6;nREP=0;}//If counter has reached 10 add 6 to Hour variable
if(tSH>0x23){tSH=0x00;nREP=0;}//If Hour Variable has reached 24, reset to 0
ttm.hour=tSH;    //Set Time structure with Hour Variable
 

ErnieM

Joined Apr 24, 2011
8,377
Well if you're going to be that way...

Inline increment:
Rich (BB code):
  if( (++hours & 0x0F) > 0x09) hours += 0x06;
  if ( (hours) > 0x23 ) hours = 0;
Subroutine increment:
Rich (BB code):
void IncHour(unsigned char* digit)
{
  if( (++*digit & 0x0F) > 0x09) *digit += 0x06;
  if ( *digit > 0x23 ) *digit = 0;
}
Function increment:
Rich (BB code):
unsigned char IncHour(unsigned char digit)
 {
   if( (++*digit & 0x0F) > 0x09) *digit += 0x06;
   if ( *digit > 0x23 ) *digit = 0;
   return digit;
 }
(No wonder they call "C" a "write-only" language.)
 
Last edited:
Top