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:
Conducive numbers to work with are:
The number 300 can be written as:
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:
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.
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.
[xh]RESOURCE
This software routine is available from the [i]EPE PCB Service[r] on 3.5in
disk (Disk 6, [i]PIC Tricks[r] folder), for which a nominal handling charge
applies. It is also available for free download from the [i]EPE[r]
Downloads page, accessible via the home page at
[bo]www.epemag.wimborne.co.uk[r]. It is in the [i]PIC Tricks[r] folder, as
file [bo]Dec2Bin16.txt[r].
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.
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.
(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
Check it with your calculator.
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
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.
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 addwf D3,W movwf NUMLO rlf NUMLO,F swapf D2,W ; + D2 * 16 + D2 addwf D2,W addwf NUMLO,F rlf D4,W ; + (D4 * 2 + D3) * 256 addwf D3,W 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 addwf D1,W addwf NUMLO,F skpnc incf NUMHI,F swapf D4,W ; + D4 * 16 + D0 addwf D0,W rlf NUMLO,F ; * 2 rlf NUMHI,F addwf NUMLO,F skpnc incf NUMHI,F movf D4,W ; - D4 * 256 subwf NUMHI,F swapf D4,W ; + D4 * 16 * 256 * 2 addwf NUMHI,F addwf NUMHI,F return ; Q.E.D.+
Comments:
/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 21:07, local time: 2024/11/22 01:34,
owner: JMN-EFP-786,
3.146.37.222:LOG IN
|
©2024 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? <A HREF="http://sxlist.com/techref/microchip/math/radix/bu2b-5d16b-ph.htm"> 5 digit BCD unpacked to 16bit binary in 33 inst, 33 cycles by Peter Hemsley</A> |
Did you find what you needed? |
Welcome to sxlist.com!sales, advertizing, & kind contributors just like you! Please don't rip/copy (here's why Copies of the site on CD are available at minimal cost. |
Welcome to sxlist.com! |
.