## 5 digit BCD unpacked to 16bit binary in 33 inst, 33 cycles by Peter Hemsley

Published in EPE Dec '03 issue. By PETER HEMSLEY

Speed up your PIC's data conversion and compression.

MATHS is all about playing with numbers, so let's play and create a super-fast decimal to 16-bit binary routine for PIC microcontrollers. It is based on expressing powers of ten in terms of powers of two.

Here are the first five powers of ten:

```1     = 1
10    = 8 + 2
100   = 64 + 32 + 4
1000  = 512 + 256 + 128 + 64 + 32 + 8
10000 = 8192 + 1024 + 512 + 256 + 16
```
If X represents any decimal digit between 0 and 9 then:
```X     = (X * 1)
X0    = (X * 8) + (X * 2)
X00   = (X * 64) + (X * 32) + (X * 4)
X000  = (X * 512) + (X * 256) + (X * 128) + (X * 64) + (X * 32) + (X * 8)
X0000 = (X * 8192) + (X * 1024) + (X * 512) + (X * 256) + (X * 16)
```
These five expressions are the basis on which we can write a routine to convert a string of decimal digits into binary. The routine is to be written in assembler so any expression must be conducive to available processor instructions, namely NIBBLE SWAP, SHIFT LEFT and ADD.

Conducive numbers to work with are:

```2   (SHIFT LEFT to multiply by 2)
16  (NIBBLE SWAP to multiply by 16)
256 (The result goes into the high byte of the binary)
```
If your mind is somewhat blank at this point let's try a simple example to get the grey matter working.

The number 300 can be written as:

```(3 * 64) + (3 * 32) + (3 * 4)
```
Re-writing it in terms of conducive numbers we get:
```3 * 16 * 2 * 2 + 3 * 16 * 2 + 3 * 2 * 2
```
Now reduce and re-arrange the expression to:
```((3 * 16 + 3) * 2 + 3 * 16) * 2 = 300
```

This expression can be calculated easily using NIBBLE SWAP, SHIFT LEFT and ADD.

Ok that was easy enough, so now for the tricky part.

The 1000's and 10000's expressions contain six and five terms respectively. If we also use SUBTRACT, the number of terms can be reduced to three and four:

```X     = (X * 1)
X0    = (X * 8) + (X * 2)
X00   = (X * 64) + (X * 32) + (X * 4)
X000  = (X * 1024) - (X * 32) + (X * 8)
X0000 = (X * 8192) + (X * 2048) - (X * 256) + (X * 16)
```
These five expressions can now be written in terms of conducive numbers and combined to give:
```N = (((D1 + D3 + D4 * 256) * 2 + D2 * 16 + D2 + D3 * 256) * 2 - D3 * 16 +
D2 * 16 + D1 + D4 * 16 * 256) * 2 + D4 * 16 + D0 - D4 * 256
```
Where D0 = ones, D1 = tens, D2 = hundreds, D3 = thousands, D4 = ten thousands

To save a lot of typing, and you a big headache, the details of how this expression was arrived at have been omitted. It is simple enough though a little lengthy.

There is a problem however, it is the -D4 * 256 at the end of the expression. If the input is greater than 63231 the running total will exceed the allotted 16 bits. So, again, re-arrange the expression.

```N = (((D1 + D3 + D4 * 256) * 2 + D2 * 16 + D2 + D3 * 256) * 2 - D3 * 16 +
D2 * 16 + D1) * 2 + D4 * 16 + D0 - D4 * 256 + D4 * 16 * 256 * 2
```
Now there is an addition of a large number at the end of the expression, therefore overflow will not occur.

The PIC routine in Listing 1 is an almost literal translation of this expression into assembler, with just a few tweaks to make the code more efficient. The variables may be allocated (equated) to any registers of your choice.

Incidentally, the routine will work with numbers up to 99999, the 17th bit (or bit 16) being returned in the carry.

Finally, if your numerical value is expressed in ASCII characters, each character may be converted to a BCD (binary-coded-decimal) format by subtracting 48, which makes it easy to then check if it is a valid decimal digit.

+

Code:

• ```LISTING 1

; 5 digit decimal to 16 (17) bit binary. By Peter Hemsley, March 2003.
; Input decimal digits in D0 (LSD) to D4 (MSD)
; Output 16 bit binary in NUMHI and NUMLO
; No temporary variables required
; Code size: 33 instructions
; Execution time: 33 cycles (excluding Call and Return)
; Returns carry set if > 65535 (and NUMHI-LO MOD 65536)

dec2bin16
movf  D1,W        ; (D1 + D3) * 2
movwf NUMLO
rlf   NUMLO,F

swapf D2,W        ; + D2 * 16 + D2

rlf   D4,W        ; + (D4 * 2 + D3) * 256
movwf NUMHI

rlf   NUMLO,F     ; * 2
rlf   NUMHI,F

swapf D3,W        ; - D3 * 16
subwf NUMLO,F
skpc
decf  NUMHI,F

swapf D2,W        ; + D2 * 16 + D1
skpnc
incf  NUMHI,F

swapf D4,W        ; + D4 * 16 + D0

rlf   NUMLO,F     ; * 2
rlf   NUMHI,F

skpnc
incf  NUMHI,F

movf  D4,W        ; - D4 * 256
subwf NUMHI,F

swapf D4,W        ; + D4 * 16 * 256 * 2

return            ; Q.E.D.
```
+

• spamroberthedan at STOPspamxavierkamial.com
/techref/microchip/math/radix/bu2b-5d16b-ph.htm This was what I was looking for, an efficient BCD to binary conversion. I download data from a PC that was manipulated by EXCEL onto a PIC for storage on an external EEPROM. This conversion was required to generate proper address bytes for the memory EEPROMs.

I do have one request, is it possible to expand this logic to support the 17-bit addressing of the 128KB AT24C1024 Serial EEPROM?

I'm not quite capable of expanding the code myself. I can follow your explanation, but my math skills end about there. An expanded version capable of supporting a number of 131,072 would be greatly appreciated.

Thank you very much for the current version!

Robert Hedan
:)
+

Questions:

There seem to be an error in the code. The lines "D2*16+D1 +D4*16+D0 *2" should be "D2*16+D1 *2 +D4*16+D0"
This way the program goes OK in my simulator.
Am I correct?
+

 file: /Techref/microchip/math/radix/bu2b-5d16b-ph.htm, 7KB, , updated: 2007/7/7 22:07, local time: 2022/8/8 02:49, owner: JMN-EFP-786, TOP NEW HELP FIND:  3.235.140.84:LOG IN

 ©2022 These pages are served without commercial sponsorship. (No popup ads, etc...).Bandwidth abuse increases hosting cost forcing sponsorship or shutdown. This server aggressively defends against automated copying for any reason including offline viewing, duplication, etc... Please respect this requirement and DO NOT RIP THIS SITE. Questions?Please DO link to this page! Digg it! / MAKE! 5 digit BCD unpacked to 16bit binary in 33 inst, 33 cycles by Peter Hemsley

After you find an appropriate page, you are invited to your to this massmind site! (posts will be visible only to you before review) Just type a nice message (short messages are blocked as spam) in the box and press the Post button. (HTML welcomed, but not the <A tag: Instead, use the link box to link to another page. A tutorial is available Members can login to post directly, become page editors, and be credited for their posts.

Attn spammers: All posts are reviewed before being made visible to anyone other than the poster.
 Did you find what you needed? "No. I'm looking for: " "No. Take me to the search page." "No. Take me to the top so I can drill down by catagory" "No. I'm willing to pay for help, please refer me to a qualified consultant"

### Welcome to sxlist.com!

Site supported by
& kind contributors
just like you!

(here's why

Copies of the site on CD
are available at minimal cost.

.