From: Scott Dattalo
rrf bcd, W
andlw 01111000b ;W = tens*8
movwf temp
clrc
rrf temp, F ;temp = tens*4
rrf temp, F ;temp = tens*2
subwf bcd, W ;W = tens*16 + ones - tens*8
;W = tens*8 + ones
addwf temp, W ;W = tens*10 + ones
Code:
; ==========================================================
; From: Scott Dattalo
;
; This version saves one cycle.
;
swapf bcd, W
andlw 0x0F ; W=tens
movwf temp
addwf temp, W ; W=2*tens
addwf temp, F ; temp=3*tens (note carry is cleared)
rlf temp, W ; W=6*tens
subwf bcd, W ; W = 16*tens+ones - 6*tens
; ==========================================================
; From: Silvan Minghetti
;
; Does not require an additional temp register, but trashes
; the content of the bcd register.
;
; Expects bcd value in 'bcd', returns binary value in WREG.
; To return the binary value in 'bcd', change the last line
; to subwf bcd, F
;
swapf bcd, F
rlf bcd, W
andlw b'00011110' ; WREG = tens * 2
swapf bcd, F
subwf bcd, F ; bcd = (tens * 16) - (tens * 2) + ones
subwf bcd, F ; bcd = (tens * 14) - (tens * 2) + ones
subwf bcd, W ; WREG = (tens * 12) - (tens * 2) + ones
; ==========================================================
; From: Scott Dattalo
;
; The upper method adapted for the PIC18 Family, saves one
; more cycle.
;
swapf bcd, W
andlw 0x0F ; W= tens
rlncf WREG, W ; W= 2*tens
subwf bcd, F ; 16*tens + ones - 2*tens
subwf bcd, F ; 14*tens + ones - 2*tens
subwf bcd, W ; 12*tens + ones - 2*tens
+Comments:
RRCF BCD, W ; BCD/2 in WREG+
RRCF WREG, W ; BCD/2/2 in WREG
ANDLW 0x3C ; mask upper nibble as 4 * tens
SUBWF BCD, F ; BCD = (16 * tens) - (4 * tens) = 12 * tens + ones
RRNCF WREG, W ; BCD/2/2/2 in WREG = (2 * tens)
SUBWF BCD, F ; BCD = (12 * tens) - (2 * tens) = 10 * tens + ones
See also:
| file: /Techref/microchip/math/radix/bp2b-2d8b.htm, 3KB, , updated: 2006/12/10 17:48, local time: 2012/2/10 00:10,
38.107.179.230:LOG IN |
| ©2012 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://www.piclist.com/techref/microchip/math/radix/bp2b-2d8b.htm"> PIC Microcontoller Radix Math Method BCD packed to binary 2 digit to 8 bit</A> |
| Did you find what you needed? |
SX MASTERS: Eric Smith and Richard Ottosen's SERVID is an intellegent RS232 video terminal (4x20 character display) in one chip. See the write up here.. |
.