Packages

A library to read a DS3231 Real time clock module I've only tested this with a raspberry pi a+ and a sunfounder ds3231 board.

Current section

Files

Jump to
rtc_ds3231 lib rtc_ds3231 bcd.ex
Raw

lib/rtc_ds3231/bcd.ex

defmodule RtcDs3231.Bcd do
@moduledoc false
use Bitwise
@doc """
Convert from bcd to integers
"""
def from_bcd(byte), do: high_nibble(byte) * 10 + low_nibble(byte)
@doc """
Convert number to binary coded decimal
"""
def to_bcd(number) do
high = div(number, 10)
low = rem(number, 10)
(high <<< 4) ||| low
end
def low_nibble(byte), do: byte &&& 0xF
def high_nibble(byte), do: byte >>> 4
end