SnipPIC |
|
Index | Case statement
Converting a 3-bit number in a 8-bit mask Divide 16-bit by 8-bit Continious delay Divide by 3 Compute parity BCD to binary BCD (packed) to binary BCD (packed) addition BCD (packed) subtraction Binary to BCD (packed) Multiple byte increment and decrement Conversion of byte to percentage Convert byte to HEX Complement carry flag Moving data block Rotating register |
CASE statement
From: Alexandr Redchuk |
Wswitch macro
wsw_last set 0 ;setup variable only endm case macro val, addr
.... Wswitch
|
Converting a 3-bit
number in a 8-bit mask
From: Mike Keitz |
There is a trick method that works for 4 bits. It replaces a
2-bit
number in a variable with a 4-bit mask: incf
BitP, W ;W = 0001 0010 0011 0100
It could be the core of an 8-bit routine thus: ; Convert 3-bit number (0-7) in INDEX to a 8-bit mask (00000001
...
|
Divide 16-bit by 8-bit
From: Bob Fehrenbach |
;Divide 16 bits by 8 bits, 8 bit result.
;Conventional shift and subtract ;Result MUST fit in 8 bits, otherwise erroneous result. ;110 clock cycles. ;Source: BF ; dvdHI : dvdLO / dvsrLO -> dvdLO, rem in dvdHI ;Uses count div8:
|
Continious delay
From: Andy Warren |
; This routine delays X cycles. Enter with X (in the range
; [20-271]) in the W register. ; ; Note that the delay is inclusive of the "MOVLW X", "CALL ; DELAY", and "RETURN" overhead, so a sequence like: ; ; MOVLW 100 ; CALL DELAY ; MOVLW 200 ; CALL DELAY ; ; will delay EXACTLY 300 cycles. DELAY:
RETURN
|
Divide by 3
From: Scott Dattalo |
; Divide by 3
; Input in W , Output W/3 ; ADDLW 1 SKPNC RETLW 0x55 MOVWF quo_L
RLF
quo_L, F
ADDWF quo_L,
F
;At this point, we have dividend * 5. We actually need
SWAPF quo_H,
W ;quo_H * 0x10
SWAPF quo_L,
W ;quo_L * 0x10
SKPNDC
;Note the 'DC'
ADDWF quo_H,
W ;quo_H += quo_L*0x10 (plus stuff)
|
Compute parity
From: John Payson |
;This routine will leave the parity of X in X.0
;while blenderizing most of the rest of X swapf X,
W
|
BCD to binary
From: Mike Keitz |
;You have to have the 2 BCD digits in the low 4 bits of
;bcdh and bcdl, with the high 4 bits all zero. ;The result is place in bin. ;Note that it could be modified to just reuse one of the ;bcd locations to store the binary result. movfw bcdh
|
BCD (packed) to binary
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 |
BCD (packed) addition
From: Scott Dattalo |
;******************************************
;bcd_add ; ; Computes z = x + y ; where x,y,z are all 8-bit packed BCD numbers ; Exits with C=1 if x+y > 0x99 and with z=1 if ; x+y = 0x100. ; Note that z can be aliased to x or y so that ;it's possible to calculate x = x+y or y = x+y ;This routine forms the BCD two's complement of ;y and then uses the bcd_subtract routine to ;find the sum. e.g. z = x - (-y) = x + y ; ; 10 cycles bcd_add
RETURN
|
BCD (packed) subtraction
From: Scott Dattalo |
;******************************************
;bcd_subtract ; ; Computes z = x - y ; where x,y,z are all 8-bit packed BCD numbers ; Exits with C=1 (and DC=1 too) if x>=y ; and with z=1 if x==y. ; Note that z can be aliased to x or y so that ;it's possible to calculate x = x-y or y = x-y ; 9 cycles (+ return) bcd_subtract MOVF
y, W ;W = y
RETURN
|
Binary to BCD (packed)
From: Scott Dattalo |
;********************************
;binary_to_bcd - 8-bits ; ;Input ; bin - 8-bit binary number ;Outputs ; hundreds - the hundreds digit of the BCD conversion ; tens_and_ones - the tens and ones digits of the BCD conversion binary_to_bcd: CLRF
hundreds
BTFSC bin,4
BTFSC bin,
5
BTFSC bin,
6
BTFSC bin,
7
ADDLW 0x60
MOVWF tens_and_ones
|
Multiple byte increment and
decrement
From: Dmitry Kiryashov |
; Multi byte increment (expandable)
movlw 1
; Multi byte decrement (expandable) movlw 1
|
Conversion of byte to percentage
From: Scott Dattalo |
;*******************************************************************
;scale_hex2dec ; The purpose of this routine is to scale a hexadecimal byte to a ;decimal byte. In other words, if 'h' is a hexadecimal byte then ;the scaled decimal equivalent 'd' is: ; d = h * 100/256. ;Note that this can be simplified: ; d = h * 25 / 64 = h * 0x19 / 0x40 ;Multiplication and division can be expressed in terms of shift lefts ;and shift rights: ; d = [ (h<<4) + (h<<3) + h ] >> 6 ;The program divides the shifting as follows so that carries are automatically ;taken care of: ; d = (h + (h + (h>>3)) >> 1) >> 2 ; ;Inputs: W - should contain 'h', the hexadecimal value to be scaled ;Outputs: W - The scaled hexadecimal value is returned in W ;Memory: temp ;Calls: none scale_hex2dec MOVWF temp
;Hex value is in W.
RETURN
|
Convert byte to HEX
From: Marco Di Leo |
swapf value,
W ; Get the value nibble-swapped
andlw 0x0f ; Isolate the low nibble addlw -0x0a ; Check for digit value btfsc STATUS, C ; If 0-9 skip next addlw 0x07 ; Add offset between digits and ; letters (use 0x27 for lowercase) addlw 0x3a ; Add back the value we subtracted ; for test and the offset between ; 0 and the char '0' (here W holds the high nibble char) movf
value, W ; Get the value
in W
(here W holds the low
nibble char)
|
Complement carry flag
From: Chip Weller |
bcf
STATUS, 1 ; Clear Digit Carry
incfsz STATUS, F ; Carry complemented, DC has old carry |
Moving data block
From: Dmitry Kiryashov |
; for ( i = num_of_elements; i != 0; ) { i--; b[i] = a[i]; }
movlw num_of_elements
movlw (b-a)
decfsz i, F
|
Rotating register
From: Dale Wescombe |
RLF REG, W
; Where reg is the register who's contents
; you want to rotate RLF REG, F ; This does leave the W register scrambled ; but you can enter with REG having "abcdefgh" ; and leave with it having "bcdefgha" |
Please note: I have not tested the routines in this page so use them at your own risk.