How to set the values of time, date, dayOfWeek and status

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

cross mob
Anonymous
Not applicable

I am using PSoC 4200 RTC. The data structure is like this

   

   

typedef struct
{
uint32 time;
uint32 date;
uint32 dayOfWeek;
uint32 status;
}RTC_DATE_TIME;

   

   

My code is like this

   

   

RTC_date_time myDateAndTime, myAlarmDateAndTime;
uint32 alarmStatus = 0;

   

//Set these values. They are uint32 type values.

   

myDateAndTime.time = 
myDateAndTime.date = 
myDateAndTime.dayOfWeek = 
myDateAndTime.status = 

   

myAlarmDateAndTime.time = 
myAlarmDateAndTime.date = 
myAlarmDateAndTime.dayOfWeek = 
myAlarmDateAndTime.status = 

   

RTC_SetDateAndTime(myDateAndTime.time, myDateAndTime.date);

   

RTC_SetAlarmDateAndTime(&myAlarmDataAndTime);

   

RTC_SetAlarmMask(RTC_ALARM_SEC_MASK | RTC_ALARM_MIN_MASK | RTC_ALARM_HOUR_MASK 
        | RTC_ALARM_DAYOFWEEK_MASK | RTC_ALARM_DAYOFMONTH_MASK | RTC_ALARM_MONTH_MASK | RTC_ALARM_YEAR_MASK);

   

Read Alarm Status to check if Alarm has a match using

   

alarmStatus = RTC_GetAlarmStatus();

   

if(alarmStatus) {
    //Current time and date match alarm time and date
    RTC_ClearAlarmStatus();
}

   

   

 

   

I want to know how to set the values for

   

   

myDateAndTime.time = 
myDateAndTime.date = 
myDateAndTime.dayOfWeek = 
myDateAndTime.status = 

   

myAlarmDateAndTime.time = 
myAlarmDateAndTime.date = 
myAlarmDateAndTime.dayOfWeek = 
myAlarmDateAndTime.status = 

   

   

 

   

All are uint32 types. The datasheet tells that time is in HH:MM:SS format and date is in MM/DD/YYYY format or as set in the customize dialog.

0 Likes
8 Replies
ETRO_SSN583
Level 9
Level 9
250 likes received 100 sign-ins 5 likes given

From tech support -

   

 

   

5) "RTC_SetDateAndTime" is not clearly explained in the component datasheet. But it is provided in the component generated code as copied below:

   

 

   

*  inputTime: The time value in the HH:MM:SS format.

   

*       "HH"- The 8-bit 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 2nd 8-bit MSB denotes the minutes value, the valid entries -> 0-59.

   

*       "SS" - The 3rd 8-bit MSB denotes the seconds value, the valid entries -> 0-59.

   

*       Each byte is in the BCD format. Invalid time entries retain the

   

*       previously set values.

   

*

   

*  inputDate: The date value in the format selected in the 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.

   

 

   

So, each digit seen on a digital clock display is coded in BCD(occupying 1 nibble).

   

 

   

Regards, Dana.

0 Likes
Anonymous
Not applicable

@Dana

   

 

   

So for time I use 0x06800A0B for 06:10:11 PM ? and for date 0x12232015 for Dec 23rd 2015 using BCD values for date ?

0 Likes
ETRO_SSN583
Level 9
Level 9
250 likes received 100 sign-ins 5 likes given

Not sure about time, left justified as its 6 nibbles ?.....Maybe look at example

   

code to see how it was done.

   

 

   

Dec to BCD http://planetcalc.com/1674/

   

 

   

Regards, Dana.

0 Likes
Anonymous
Not applicable

Is this right ? 06:10:11 PM = 0x06801011

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

In the RTC.c is a BCD to converter and Hex to BCD.

   

/*******************************************************************************
* 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
Anonymous
Not applicable

How are RTC_BCD_NUMBER_SIZE and RTC_BCD_ONE_DIGIT_MASK defined?

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

There is a feature in Creator to help you discovering those #defines: Right click on the item and select "go to Definition"

   

 

   

Bob

0 Likes
Anonymous
Not applicable

I created a project with the 4200 and got it to generate the code:

   

/* Number of bits per one BCD digit. */
#define RTC_BCD_NUMBER_SIZE        (4u)
#define RTC_BCD_ONE_DIGIT_MASK     (0x0000000FuL)

0 Likes