10 bit Binary to BCD Conversion
Posted on the Chat Zone by Ed Grens on September 22, 2004 at 03 08 38
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. ;====================================================== list p=16f628 radix dec #include __config 0x3D30 ;Set Up RAM Variables blo equ 0x20 ;lsbyte of 10b binary bhi equ 0x21 ;msb d4 equ 0x23 ;msdigit d3 equ 0x24 d2 equ 0x25 d1 equ 0x26 ;lsd ;Program Code org 0x00 ;Effective Reset Vector ; nop goto Start ;Jump by interrupt ; Start movlw 0x07 movwf CMCON ;Digital I/O movlw 0x03 ;Test input movwf bhi movlw 0xFF movwf blo movlw 0x04 call Bin2decg Hold goto Hold ;Finished Bin2decg ;10b binaey to BCD clrf d1 clrf d2 clrf d3 clrf d4 clrc rrf bhi,f ;N = 4*Q + R rrf blo,f ;blo = Q rrf d1,f ;d1 = R as temp, R25 goto B2d2 ;repeat B2d3 addwf blo,f ;get remainder ; after /100, C = 0 rlf d1,f ;*4 and add R back in rlf blo,f rlf d1,f rlf blo,f ;4*blo + R = N  ; 1000*d4  100*d3 movlw 10 B2d4 subwf blo,f ;blo = N  1000*d4  ; 100*d3  10*d2 skpc goto B2d5 ;blo10 goto B2d4 B2d5 addwf blo,f ;put last 10 back, C=0 movf blo,w movwf d1 return end ********************** 12bit code Posted by Peter Hemsley on September 18, 2004 at 17 40 56 Message ; Bonus 12Bit version ; Leaner and Meaner, 36 instructions ; Ideal for displaying A/D values Bin2DecFast movf NUMHI,w iorlw 0xF0 ;w=H216 movwf D1 ;D1=H216 addwf D1,f ;D1=H2*232 addwf D1,f ;D1=H2*348 movwf D2 ;D2=H216 addlw D'5' ;w=H221 addwf D2,f ;D2=H2*237 Done! addlw D'41' ;w=H2+20 movwf D0 ;D0=H2+20 swapf NUMLO,w iorlw 0xF0 ;w=H116 addwf D1,f ;D1=H2*3+H164 addwf D0,f ;D0=H2+H1+4, C=1 rlf D0,f ;D0=(H2+H1)*2+9, C=0 comf D0,f ;D0=(H2+H1)*210 rlf D0,f ;D0=(H2+H1)*420 movf NUMLO,w andlw 0x0F ;w=H0 addwf D0,f ;D0=H0(H2+H1)*420 Done! rlf D1,f ;C=0, D1=H2*6+H1*2128 Done! movlw D'5' movwf D3 movlw D'10' mod0 addwf D0,f ;D(X)=D(X)mod10 decf D1,f ;D(X+1)=D(X+1)+D(X)div10 skpc goto mod0 mod1 addwf D1,f decf D2,f skpc goto mod1 mod2 addwf D2,f decf D3,f skpc goto mod2 return+
See also:
Comments:
Michael Lee Van Dyke Says:
On the 10 bit code, the 9th instruction after the Bin2decg label is is "goto B2d2". There is no B2d2 label in the code, it will not work as it is written here.+
Questions:
file: /Techref/microchip/math/radix/b2bu10b4deag.htm, 8KB, , updated: 2018/6/29 12:06, local time: 2019/10/14 23:07,
owner: JMNEFP786,

©2019 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://massmind.org/techref/microchip/math/radix/b2bu10b4deag.htm"> 10bit to BCD unpacked 4 digit in 38 inst ~88 cycles by E. A. Grens</A> 
Did you find what you needed? 
Welcome to massmind.org! 
Welcome to massmind.org! 
.