Pull to refresh

Alternate of Packed-Binary Time Format

Reading time2 min
Views2.1K

The Real-Time Clock (RTC) Calendar Registers in STM32 microcontrollers implemented in Binary Code Decimal format (BCD) i. e., every two digits are represented by one byte (low digit in 0-3 bits and high digit 4-7 bits). At least there are 5 bytes required to store date and time data in a such format. There are cases when memory allocation for time stamp might become critical, e. g., the events log keeping in an extern non-volatile memory IC. Here the memory value for a single event (event serial number, timestamp, event parameters, and its CRC) is fixed and can compose up to 16 bytes. The maximum quantity of the events increasing, and time spent on a single operation reducing (for reserved power sources as supercapacitors and electrolytic capacitors is critical) can be achieved with less memory amount required for the event field.

The most trivial method for a useful memory amount expanding (event parameters) is BCD to binary format timestamp converting (can say Packed-Binary Time Format). For this purpose, the time value from the BCD format has to be converted to the familiar positional numeral system where one second is the minimal time unit. The next step is the countdown time choosing. In this specific case, the 1st January 2001 was accepted.

Figure 1 - Memory fields filled by using the Packed-Binary Time Format with the 16-byte event example
Figure 1 - Memory fields filled by using the Packed-Binary Time Format with the 16-byte event example

There are the following aspects that should be taken into account when converting BCD to Packed-Binary Time Format:

  • leap year (the implementation in C is shown below);

  • the number of days depending on the month (the implementation in C with the number of seconds in previous months is shown below).

if(((mark_years + 2000) % 4) != 0x00)	{														//	leap year checking
    leap_year = 0x00;
}
else	{
    if(((mark_years + 2000) % 100) != 0x00)	{
		    leap_year = 0x01;
		}
		else	{
		    if(((mark_years + 2000) % 400) != 0x00)	{
				    leap_year = 0x00;
			  }
			  else	{
			      leap_year = 0x01;
			  }
		}
}

The Packed-Binary Time Format variable takes the 4 bytes at all and is interpreted as an unsigned long type. As mentioned above the Packed-Binary Time Format variable is the number of seconds that have passed since January 1, 2001 (Monday).

for(uint8_t i = 0x01; i < mark_months; i ++)	{															//	сalculation of the number of seconds depending on the number of past months
    if(i == 0x01 || i == 0x03 || i == 0x05|| i == 0x07 || i == 0x08 || i == 0x0A)	{
		    sec_in_months += 2678400;
		}
		else if(i == 0x04 || i == 0x06 || i == 0x09 || i == 0x0B)	{
		    sec_in_months += 2592000;
		}
		else	{
		    sec_in_months += ((leap_year == 0x01) ? 2505600 : 2419200);
		}
}

To avoid recalculation need from one format of time to another every time, it`s has meaning to make this operation only once when starting the MCU. And further, the variable that stores the time date in Packed-Binary Time Format increases every second.

In cases where you only need to get the date value, the amount of memory can be reduced up to two bytes.

Advantages of using a Packed-Binary Time Format:

  • reducing amount of allocated memory;

  • increasing the processing speed when working with the Packed-Binary Time variable;

Drawbacks of using a Packed-Binary Time Format:

  • lack of readability in contrast to Binary-Code Decimal time format;

  • increasing the CPU overheads for any operations and calculations.

The source code in C is available on github

Tags:
Hubs:
If this publication inspired you and you want to support the author, do not hesitate to click on the button
Rating0
Comments0

Articles