In SX Microcontrollers, SX/B Compiler and SX-Key Tool, James Newton wrote: [url=http://www.sxlist.com/techref/ubicom/lib/math/radix/index_sx.htm]http://www.sxlist.com/techref/ubicom/lib/math/radix/index_sx.htm [/url] has several 8 bit to 12 bit bcd (aka half packed or packed bcd) routines... why 9 bits? 8 bits is easy, but: 2^8th is 256 which only uses about 1/3 of the capasity of a 3 digit display 2^9th is 512 so that does fit in 3 digits... gives about half the possible output values. 2^10th is 1024 which is a better fit for 3... humm.. ok, there is a 10 bit to 4 digit(?) routine (why bother with the 25 values past 999?) at [url=http://www.sxlist.com/techref/microchip/math/radix/b2bu-10b4d-eag.htm]http://www.sxlist.com/techref/microchip/math/radix/b2bu-10b4d-eag.htm [/url] but it hasn't been converted to parallax / SASM notation yet... that requires a few seconds at: [url=http://www.sxlist.com/cgi-bin/mpasm2sasm2.exe]http://www.sxlist.com/cgi-bin/mpasm2sasm2.exe [/url] which then gives us: [code] ;======= Bin2Dec10G.asm ============= Sep 04 ========= ; Test of 10b binary to 4 digit conversion as ; based on division by powers of 10. ; ; E. A., Grens ; ; For most PICs - here for 16F628 with 4 MHz ; internal oscillator. ; ; Average processor cycles for execution of ; subroutine over the input range 0x000 to 0x3FF ; is 88. No variables required except the ; input blh, blo and the out BCD digits. ; Subroutine length 38. ;====================================================== ;-------Set Up RAM Variables--------------------------- blo equ $20 ;lsbyte of 10b binary bhi equ $21 ;msb d4 equ $23 ;msdigit d3 equ $24 d2 equ $25 d1 equ $26 ;lsd ;-------Program Code-------------------------------- org $00 ;Effective Reset Vector ; nop jmp Start ;Jump by interrupt ; Start mov W, #$07 mov CMCON, W ;Digital I/O mov W, #$03 ;Test input mov bhi, W mov W, #$FF mov blo, W mov W, #$04 call Bin2decg Hold jmp Hold ;Finished Bin2decg ;10b binaey to BCD clr d1 clr d2 clr d3 clr d4 clc rr bhi ;N = 4*Q + R rr blo ;blo = Q rr d1 ;d1 = R as temp, R25 jmp B2d2 ;repeat B2d3 add blo, W ;get remainder ; after /100, C = 0 rl d1 ;*4 and add R back in rl blo rl d1 rl blo ;4*blo + R = N - ; 1000*d4 - 100*d3 mov W, #10 B2d4 sub blo, W ;blo = N - 1000*d4 - ; 100*d3 - 10*d2 sc jmp B2d5 ;blo10 jmp B2d4 B2d5 add blo, W ;put last 10 back, C=0 mov W, blo mov d1, W ret end [/code] Now, that obviously needs some polish, but it should save you some time. Note that this routine is NOT complete, it only figures out D1 by dividing blo/bhi by 10 and saving the remander, but D2 is just the same thing over again with the value left in blo/bhi after D1 is extracted. So you can stop at D3 and have your 3 digit version. Please consider publishing what you come up with both here and at sxlist.com? ---------- End of Message ---------- You can view the post on-line at: http://forums.parallax.com/forums/default.aspx?f=7&p=1&m=84028#m84033 Need assistance? Send an email to the Forum Administrator at forumadmin@parallax.com The Parallax Forums are powered by dotNetBB Forums, copyright 2002-2005 (http://www.dotNetBB.com)