8 Replies Latest reply on May 13, 2016 8:55 AM by Chad.Wagner_1573706

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

    jayanth.devarayanadurga

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

         

      [code]

         

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

         

      [/code]

         

      My code is like this

         

      [code]

         

      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();
      }

         

      [/code]

         

       

         

      I want to know how to set the values for

         

      [code]

         

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

         

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

         

      [/code]

         

       

         

      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.

        • 1. Re: How to set the values of time, date, dayOfWeek and status
          user_14586677

          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.

          • 2. Re: How to set the values of time, date, dayOfWeek and status
            jayanth.devarayanadurga

            @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 ?

            • 3. Re: How to set the values of time, date, dayOfWeek and status
              user_14586677

              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.

              • 4. Re: How to set the values of time, date, dayOfWeek and status
                jayanth.devarayanadurga

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

                • 5. Re: How to set the values of time, date, dayOfWeek and status
                  user_242978793

                  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);
                  }

                  • 6. Re: How to set the values of time, date, dayOfWeek and status
                    Chad.Wagner_1573706

                    How are RTC_BCD_NUMBER_SIZE and RTC_BCD_ONE_DIGIT_MASK defined?

                    • 7. Re: How to set the values of time, date, dayOfWeek and status
                      user_1377889

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

                         

                       

                         

                      Bob

                      • 8. Re: How to set the values of time, date, dayOfWeek and status
                        Chad.Wagner_1573706

                        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)