Bladeren bron

bcd: Add files copied from esp8266-clock

Helmut Pozimski 1 jaar geleden
bovenliggende
commit
18d86d8af8
2 gewijzigde bestanden met toevoegingen van 39 en 0 verwijderingen
  1. 26 0
      main/bcd.c
  2. 13 0
      main/bcd.h

+ 26 - 0
main/bcd.c

@@ -0,0 +1,26 @@
+/*
+ * SPDX-FileCopyrightText: 2022 Helmut Pozimski <helmut@pozimski.eu>
+ *
+ * SPDX-License-Identifier: GPL-2.0-only
+ */
+
+#include <stdint.h>
+
+uint8_t bcd_first_digit_value_bin(uint8_t bcd_value) {
+    return (((bcd_value & 0xF0) >> 4)) * 10;
+}
+
+uint8_t bcd_last_digit_value_bin(uint8_t bcd_value) {
+    return bcd_value & 0x0F;
+}
+
+uint8_t bcd2bin(uint8_t bcd_value) {
+    return bcd_last_digit_value_bin(bcd_value) + bcd_first_digit_value_bin(bcd_value);
+}
+
+uint8_t bin2bcd(uint8_t bin_value) {
+    uint8_t second_digit = (bin_value / 10) << 4;
+    uint8_t first_digit = bin_value % 10;
+    return first_digit + second_digit;
+} 
+

+ 13 - 0
main/bcd.h

@@ -0,0 +1,13 @@
+/*
+ * SPDX-FileCopyrightText: 2022 Helmut Pozimski <helmut@pozimski.eu>
+ *
+ * SPDX-License-Identifier: GPL-2.0-only
+ */
+
+uint8_t bcd_first_digit_value_bin(uint8_t bcd_value);
+
+uint8_t bcd_last_digit_value_bin(uint8_t bcd_value);
+
+uint8_t bcd2bin(uint8_t bcd_value);
+
+uint8_t bin2bcd(uint8_t bin_value);