From Mike McLaren, K8LH, (Westland, MI, USA)
While collaborating on a little 10F200 project recently (256 words of program memory and 16 bytes RAM) I came up with the following little Bin2Bcd routine. The additional code (PrintIt, PutDigit, etc.) simply prints the "0".."255" number as right justified with leading zero suppression;
;****************************************************************** ; * ; 8 bit to 3 digit half-packed BCD, Mike McLaren, K8LH (Jan-09) * ; * ; input: WREG, 0x00..0xFF, 0..255 * ; output: tens, 0x00..0x25, packed bcd * ; ones, 0x00..0x09 * ; * ; 12 words, 2 variables ; tens equ 0x1A ; packed BCD 'hundreds' & 'tens' ones equ 0x1B ; single BCD 'ones' mask equ 0x1C ; Bin2Bcd clrf tens ; decf tens,F ; preset 'tens' to -1 div10 movwf ones ; incf tens,F ; bump 'tens', 0x00..0x25 movlw 6 ; using "packed bcd" format addwf tens,W ; bcd "digit carry"? skpndc ; no, skip, else movwf tens ; fix 'tens' movlw 10 ; ones = ones - 10 subwf ones,W ; borrow? bc div10 ; no, branch, else PrintIt movlw " " ; prep leading zero suppression movwf mask ; mask = 0x20 = " " (space char) swapf tens,W ; get hundreds, 0..2 call PutDigit ; print " " or "1".."2" movf tens,W ; get tens, 0..9 call PutDigit ; print " " or "0".."9" movf ones,W ; get ones, 0..9 goto PutNumber ; always print "0".."9" PutDigit andlw 0x0F ; skpz ; PutNumber bsf mask,4 ; mask = 0x30 = "0" iorwf mask,W ; wreg = " " or "0".."9" goto Put232 ; Put232 or PutLCD
file: /Techref/microchip/math/radix/b2bhp-8p3d-mm.htm, 5KB, , updated: 2010/10/4 04:41, local time: 2024/11/24 16:47,
3.144.235.141: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://ecomorder.com/techref/microchip/math/radix/b2bhp-8p3d-mm.htm"> PIC Microcontoller Radix Math Method Binary to BCD half-packed 8 bit to 3 digit - Mike McLaren</A> |
Did you find what you needed? |
Welcome to ecomorder.com! |
Welcome to ecomorder.com! |
.