X-Git-Url: http://cloudbase.mooo.com/gitweb/z180-stamp.git/blobdiff_plain/ad9bc17c8ecab1f0bbd26f2270d4d396f4bc5e52..845cbdbdd011f875219542dc5916f390cd952514:/avr/bcd.c 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. + * + * 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; +}