]> cloudbase.mooo.com Git - z180-stamp.git/blobdiff - avr/bcd.c
Server: Time and Date support
[z180-stamp.git] / avr / bcd.c
diff --git a/avr/bcd.c b/avr/bcd.c
new file mode 100644 (file)
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;
+}