summaryrefslogtreecommitdiff
path: root/avr/bcd.c
diff options
context:
space:
mode:
Diffstat (limited to 'avr/bcd.c')
-rw-r--r--avr/bcd.c21
1 files changed, 21 insertions, 0 deletions
diff --git a/avr/bcd.c b/avr/bcd.c
new file mode 100644
index 0000000..5154852
--- /dev/null
+++ b/avr/bcd.c
@@ -0,0 +1,21 @@
+/*
+ * (C) Copyright 2014 Leo C. <erbl259-lmu@yahoo.de>
+ *
+ * SPDX-License-Identifier: GPL-2.0+
+ */
+
+#include "stdlib.h"
+#include "stdint.h"
+#include "bcd.h"
+
+uint_fast8_t bcd2bin(uint8_t val)
+{
+ return (val >> 4) * 10 + (val & 0x0f);
+}
+
+uint8_t bin2bcd (uint_fast8_t val)
+{
+ div_t d = div(val, 10);
+
+ return (d.quot << 4) | d.rem;
+}