RTC API Methods

Tip / Sign in to post questions, reply, level up, and achieve exciting badges. Know more

cross mob
Anonymous
Not applicable

Hello

   

I am trying to use the RTC in order to keep track of time but I am having some difficulties with a few methods regarding the extraction of the values.

   

RTC_SetDateAndTime(0x00000000, 0x14000101); Trying to start the values for the time to be 00;00;00 (HH;MM;SS) and the year as 2000;01;01 (YYYY;MM;DD).

   

and for extraction and control I simply use the GattDatabase + CySmart (windows).

   

RTC_GetDateAndTime(&dateTime);
    time = dateTime.time;
    date = dateTime.date;
  
    dataTimeDate[4] = RTC_GetSecond(time);
    dataTimeDate[5] = RTC_GetMinutes(time);
    dataTimeDate[6] = RTC_GetHours(time);
    dataTimeDate[7] = RTC_GetDay(date);
    dataTimeDate[8] = RTC_GetMonth(date);

   

    //dataTimeDate[9] = (((RTC_GetYear(date)<<0)&0xff));
    //dataTimeDate[10] = (((RTC_GetYear(date)<<8)&0xff));

   

The two rows above is an attempt to extract the two first digits and the last two digits of the YYYY

   

The values I succesfully write to the database and read with CySmart are 00;00;00 followed by 01;01;B2;00.

   

From this I conclude: Time Values are correct (the value for "seconds" of course start to move) but as for the date value, only day and month are correct. The Year always write B2, regardless of the value I set it to start with.

   


Best reguards

   

Kristoffer

0 Likes
1 Solution
rola_264706
Level 8
Level 8
50 likes received 25 likes received 10 likes received

One other item the API for the RTC has BCD to Hex and BCD to binary conversion routines so you just need to make single call of the routine. 

   

*******************************************************************************
* Function Name: RTC_ConvertBCDToDec
********************************************************************************
*
* Summary:
*  Converts a 4-byte BCD number into a 4-byte hexadecimal number. Each byte is
*  converted individually and returned as an individual byte in the 32-bit
*  variable.
*
* Parameters:
*  bcdNum: A 4-byte BCD number. Each byte represents BCD.
*          0x11223344 -> 4 bytes 0x11, 0x22, 0x33 and 0x44 the in BCD format.
*
* Return:
*  decNum: A 4-byte hexadecimal equivalent number of the BCD number.
*          BCD number 0x11223344 -> returned hexadecimal number 0x0B16212C.
*
*******************************************************************************/
uint32 RTC_ConvertBCDToDec(uint32 bcdNum)
{
    uint32 i;
    uint32 mult;
    uint32 retVal;

   

    mult   = 1u;
    retVal = 0u;

   

    for(i = 0u; i < 16u; i++)
    {
        retVal += (bcdNum & RTC_BCD_ONE_DIGIT_MASK) * mult;
        bcdNum >>= RTC_BCD_NUMBER_SIZE;
        mult *= 10u;
    }

   

    return(retVal);
}

   


/*******************************************************************************
* Function Name: RTC_ConvertDecToBCD
********************************************************************************
*
* Summary:
*  Converts a 4-byte hexadecimal number into a 4-byte BCD number. Each byte
*  is converted individually and returned as an individual byte in the 32-bit
*  variable.
*
* Parameters:
*  decNum: A 4-byte hexadecimal number. Each byte is represented in hex.
*          0x11223344 -> 4 bytes 0x11, 0x22, 0x33 and 0x44 in the hex format.
*
* Return:
*  bcdNum: A 4-byte BCD equivalent of the passed hexadecimal number. Hexadecimal
*  number 0x11223344 -> returned BCD number 0x17345168.
*
*******************************************************************************/
uint32 RTC_ConvertDecToBCD(uint32 decNum)
{
    uint32 shift;
    uint32 tmpVal;
    uint32 retVal;

   

    shift  = 0u;
    retVal = 0u;
    tmpVal = decNum;

   

    do
    {
        retVal |= ((tmpVal % 10u) << shift);
        tmpVal /= 10u;
        shift  += RTC_BCD_NUMBER_SIZE;
    }
    while(tmpVal >= 10u);

   

    retVal |= (tmpVal << shift);

   

    return(retVal);
}

View solution in original post

0 Likes
8 Replies
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

Welcome in the forum, Kristoffer!

   

From Datasheet: "YYYY" - The 16-bit LSB denotes a year in BCD, the valid entries -> 1900-2200. Each byte is in the BCD format. Invalid date entries retain the previously set values.

   

BCD is Binary coded decimal, so year 2016 must be 0x02000106, year 2000 is 0x02000000.

   

 

   

Bob

0 Likes
lock attach
Attachments are accessible only for community members.
rola_264706
Level 8
Level 8
50 likes received 25 likes received 10 likes received

Kristoffer Here is a program that shows how to set the time and read the time out on a terminal and an LCD. It uses the system tick so it is not too accurate.  Since you are using a PSOC Ble then you can use the 32.768 Crystal that is on the board and it will be very accurate. You'll need to change the device to the Psoc ble that you are using.

0 Likes
Anonymous
Not applicable

Hi Bob. Thank you for your reply.
I am new to the PSoC scene so bare with me here.

   

Using 0x02000106 resulted in 06;01;B2;00.

   

First of; When you say 0x02000106; Do you mean to write 2016 in binary (Hex should work fine aswell I suppose, only fewer symbols) because the inparameter should be a uint32 and using all 8 hex symbols for the year does not leave room for day/month.

   

To be able to write the Year, month and day using 32bit, => (binary) 16for Year, 8 forMonth, 8for Day or  (Hex) 4 for Year, 2 for Month, 2 for day. but perhaps I am missing something else...

   

Also thank you bobgoar, I will look into the code example a bit closer but I would like to understand why not simply inputting the time/date value works.

0 Likes
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

Sorry, I put too many zeroes into the format.

   

MM/DD/YYYY these are 8 digits, so January first 2000 would be 0x01012000

   

and today (May 14th 2026) 0x05142016

   

 

   

Bob

0 Likes
rola_264706
Level 8
Level 8
50 likes received 25 likes received 10 likes received

  The component RTC stores the data in BCD format. I sent you the  code as it is easier to study it than explain it in text.  When you query the RTC component it returns the data in BCD format also.  That is why your data looks so strange.

   

void RTC_SetDateAndTime(uint32 time, uint32 date)
Description:
Sets the time and date values as current time and date.
Parameters:
time: Time value in "HH:MM:SS" format:
"HH"- The 2nd MSB that denotes the hour value. 0-23 for the 24-hour format and 1-12 for the 12-hour format. The MSB bit of the value denotes AM/PM for the 12-hour format (0-AM and 1-PM).
"MM" - The 3nd 8-bit MSB denotes the minutes value, the valid entries -> 0-59.
"SS" - The LSB denotes the seconds value, the valid entries -> 0-59. Each byte is in the BCD format. Invalid time entries retain the previously set values.
Date: Date value in format selected in customizer. For the MM/DD/YYYY format:
"MM" - The 8-bit MSB denotes the month value in BCD, the valid entries -> 1-12
"DD" - The 2nd 8-bit MSB denotes a day of the month value in BCD, the valid entries -> 1-31.
"YYYY" - The 16-bit LSB denotes a year in BCD, the valid entries -> 1900-2200. Each byte is in the BCD format. Invalid date entries retain the previously set values.

0 Likes
Anonymous
Not applicable

Hello this is Kristoffer (from a different account).

   

Bob Marlowe:

   

I have tried inputting 0x20160514 and it results 0E:05:E0:00 (in my gattDatabase) aswell as other attempts:

   

0x20160514 =>0E:05:E0:00

   

0x20160422 =>16:04:E0:00

   

0x20140428 =>1C:04:DE:00

   

0x20131109=>09:0B:DD:00

   

Well it does seem to work (atleast the month and the day). I am still a bit confused how I can mark the nbr as a hex nbr and still write the nbr I want in decimal (even if it is written  in bcd). To me it feels like I should write 2000 using four hex digits as 07D0 (2000 in hex would be worth something else?). But it does work to write it in decimal and I can settle for now; Im sure im just confusing myself and my mind will work it out. But the year nbrs are still not quite right. Maybe its my method of splitting the bytes?

   

    timeHourMinSec[2] = (((RTC_GetYear(date)<<0)&0xff));
    timeHourMinSec[3] = (((RTC_GetYear(date)<<8)&0xff));

   

Bobgoar:

   

The SysStick Example uses alot of code for defining the date but it yields the same results for the year value in the database. The Example does however use different printing methods where it is not necessery to split the year value.

   

        sprintf(timeBuffer, "%02lu:%02lu:%02lu", RTC_GetHours(time), RTC_GetMinutes(time), RTC_GetSecond(time));
        sprintf(dateBuffer, "%02lu/%02lu/%02lu", RTC_GetMonth(date), RTC_GetDay(date), RTC_GetYear(date));

   

Best regards Kristoffer

0 Likes
Anonymous
Not applicable

Hi guys.

   

Thanks for all the help.

   

We feel abit silly now.

   

dataTimeDate[9] = (((RTC_GetYear(date)<<0)&0xff));
dataTimeDate[10] = (((RTC_GetYear(date)<<8)&0xff));

   

Its left Shifted when we really needed to right shift ... Duh!

   

 

   

Best reguards Jonas V (Kristoffer Vs brother)

0 Likes
rola_264706
Level 8
Level 8
50 likes received 25 likes received 10 likes received

One other item the API for the RTC has BCD to Hex and BCD to binary conversion routines so you just need to make single call of the routine. 

   

*******************************************************************************
* Function Name: RTC_ConvertBCDToDec
********************************************************************************
*
* Summary:
*  Converts a 4-byte BCD number into a 4-byte hexadecimal number. Each byte is
*  converted individually and returned as an individual byte in the 32-bit
*  variable.
*
* Parameters:
*  bcdNum: A 4-byte BCD number. Each byte represents BCD.
*          0x11223344 -> 4 bytes 0x11, 0x22, 0x33 and 0x44 the in BCD format.
*
* Return:
*  decNum: A 4-byte hexadecimal equivalent number of the BCD number.
*          BCD number 0x11223344 -> returned hexadecimal number 0x0B16212C.
*
*******************************************************************************/
uint32 RTC_ConvertBCDToDec(uint32 bcdNum)
{
    uint32 i;
    uint32 mult;
    uint32 retVal;

   

    mult   = 1u;
    retVal = 0u;

   

    for(i = 0u; i < 16u; i++)
    {
        retVal += (bcdNum & RTC_BCD_ONE_DIGIT_MASK) * mult;
        bcdNum >>= RTC_BCD_NUMBER_SIZE;
        mult *= 10u;
    }

   

    return(retVal);
}

   


/*******************************************************************************
* Function Name: RTC_ConvertDecToBCD
********************************************************************************
*
* Summary:
*  Converts a 4-byte hexadecimal number into a 4-byte BCD number. Each byte
*  is converted individually and returned as an individual byte in the 32-bit
*  variable.
*
* Parameters:
*  decNum: A 4-byte hexadecimal number. Each byte is represented in hex.
*          0x11223344 -> 4 bytes 0x11, 0x22, 0x33 and 0x44 in the hex format.
*
* Return:
*  bcdNum: A 4-byte BCD equivalent of the passed hexadecimal number. Hexadecimal
*  number 0x11223344 -> returned BCD number 0x17345168.
*
*******************************************************************************/
uint32 RTC_ConvertDecToBCD(uint32 decNum)
{
    uint32 shift;
    uint32 tmpVal;
    uint32 retVal;

   

    shift  = 0u;
    retVal = 0u;
    tmpVal = decNum;

   

    do
    {
        retVal |= ((tmpVal % 10u) << shift);
        tmpVal /= 10u;
        shift  += RTC_BCD_NUMBER_SIZE;
    }
    while(tmpVal >= 10u);

   

    retVal |= (tmpVal << shift);

   

    return(retVal);
}

0 Likes