Follow along with the video below to see how to install our site as a web app on your home screen.
Note: This feature may not be available in some browsers.
unsigned char hex2bcd (unsigned char x)
{
unsigned char y;
y = (x / 10) << 4;
y = y | (x % 10);
return (y);
}
From fmayr@electronic.tu-graz.ac.at Thu Feb 6 10:24:30 1997
Date: Wed, 05 Feb 1997 15:36:00 +0100
From: Friedrich Mayr <fmayr@electronic.tu-graz.ac.at>
To: 8051code@keil.com
Subject: Fast LONG to BCD conversion
This function was written within the Keil C51 environment
by Dr. Roehrer (Inst. f. Elektronik, TU-Graz) and me
to be called from a C - Program as: "void bin2bcd(void)"
The following global variable (must be located in the
internal RAM of the processor, "data" or "idata" ) is
used both to pass the argument (a "long" value) to the
function, as well as to receive the result (6 digits):
union
{
unsigned char buf[6];
long in;
} idata U_b;
The purpose of the code is to convert a signed long value
to BCD - code with leading sign if needed. Values exceeding
the range of 6(-:5) Digits should all be displayed by '-'.
The usage of memory was critical because no external RAM is
present in the system, so only the Accu, B, R1, R2, R3, R4
are used aside of the digit- (and argument-) buffer "U_b".
The trick applied in the function is to use a
"small" (nibble by nibble) division and the extensive
use of the '51 - opcodes XCHD and SWAP.
This results in very fast execution.
The function can easily be modified for example
to time-calculations or extended to the full "long"
range .