Professional Documents
Culture Documents
Binary To BCD Conversion
Binary To BCD Conversion
Binary To BCD Conversion
AN2112
Summary
This Application Note describes a method to convert 8 bit, 16 bit and 24-bit binary numbers to
BCD (binary coded decimal). Following the algorithm any word length can be converted to BCD.
Introduction
Binary to BCD conversion can be carried out
easily if the processor has a Decimal Adjust
instruction. When there is no Decimal Adjust
available, this is not possible.
The Algorithm
2/12/2003
Revision A
-1-
AN2112
bBCD_2:
checkDigits:
This subroutine checks each nibble of the output.
[binary0]
[tBcd0]
[tBcd1]
[bitCount]
bBCD8_2
bBCD8_2:
mov [byteCount],2
call checkDigits
jmp bBCD8_1
checkDigits:
mov A,tBcd0-1
add A,[byteCount]
mov X,A
mov A,[X+0]
mov [temp],A
add [X+0],03h
tst [X+0],8h
jnz chkDigit2
mov A,[temp]
mov [X+0],A
chkDigit2:
mov
mov
add
tst
jnz
mov
mov
A,[X+0]
[temp],A
[X+0],30h
[X+0],80h
retCheckDigits
A,[temp]
[X+0],A
2.
3.
4.
5.
6.
7.
8.
retCheckDigits:
dec [byteCount]
jnz checkDigits
ret
1.
Program Analysis
2.
3.
4.
2/12/2003
1.
Revision A
-2-
AN2112
Comments:
1.
2.
3.
4.
5.
Input
[binary0]
[binary1] [binary0]
[binary2] [binary1] [binary0]
Output
[tBCD1] [tBCD0]
[tBCD2] [tBCD1] [tBCD0]
[tBCD3] [tBCD2] [tBCD1] [tBCD0]
Description
8-Bit Binary to BCD Conversion
16-Bit Binary to BCD Conversion
24-Bit Binary to BCD Conversion
Code Size
70 bytes
77 bytes
84 bytes
Execution Time
Minimum
Maximum
2045 CPU Cycles
2429 CPU Cycles
5819 CPU Cycles
6971 CPU Cycles
11353 CPU Cycles
13657 CPU Cycles
Contact:
2/12/2003
M. Ganesh Raaja
R&D Engineer and owner of
Omega Electronics & Consultants
M. Ganesh does freelancing R&D
work for various manufacturing
industries. He obtained his diploma
in Electronics and Communications
Engg. in 1992. He has his own
consultant firm called Omega
Electronics & Consultants where
they develop products for
instrumentation and process
control. He also develops and
transfers technologies for
manufacturing companies.
856, 9th Cross, Hebbal 2nd Stage,
Mysore - 570 017, India.
Nature of Work: Development of
products for instrumentation and
process control.
Skill Set: Microcontrollers
(assembly language programming),
analog circuits, PCB designing
using PROTEL.
Revision A
-3-
AN2112
2/12/2003
Revision A
-4-