Module bcd

Module bcd 

Source
Expand description

§BCD (Binary Coded Decimal) conversion utilities for RTC operations

This module provides conversion functions between BCD and decimal formats commonly used in Real-Time Clock (RTC) chips like the DS1307.

§BCD Format Overview

Binary Coded Decimal (BCD) represents decimal digits using 4-bit binary patterns:

  • Each decimal digit (0-9) is encoded in 4 bits
  • One byte can hold two decimal digits (00-99)
  • High nibble = tens digit, low nibble = ones digit

§Format Examples

DecimalBCD BinaryBCD Hex
000000 00000x00
010000 00010x01
090000 10010x09
100001 00000x10
230010 00110x23
590101 10010x59
991001 10010x99

Functions§

from_decimal
Convert decimal byte to BCD encoding
to_decimal
Convert a BCD encoded byte to decimal First 4 bits are tens of the number Last 4 bits are ones of the number