Transcript
Page 1: S5704A Tips & Tricks 11 © 1999 Microchip Technology Incorporated. All Rights Reserved. S5704A Tips & Tricks 11 Tips and Tricks Tips and Tricks Using PICmicro

© 1999 Microchip Technology Incorporated. All Rights Reserved. S5704A Tips & Tricks 1S5704A Tips & Tricks 1 11

Tips and Tricks

Tips and Tricks Tips and Tricks

Using PICmicroUsing PICmicro®® MCUs MCUs

Tips and Tricks Tips and Tricks

Using PICmicroUsing PICmicro®® MCUs MCUs

Page 2: S5704A Tips & Tricks 11 © 1999 Microchip Technology Incorporated. All Rights Reserved. S5704A Tips & Tricks 11 Tips and Tricks Tips and Tricks Using PICmicro

© 1999 Microchip Technology Incorporated. All Rights Reserved. S5704A Tips & Tricks 1S5704A Tips & Tricks 1 22

Tips and Tricks

Increments and DecrementsIncrements and DecrementsIncrements and DecrementsIncrements and Decrements 16-Bit Increment

incfsz LOgoto $+2incf HI

16-Bit Decrementdecf LOincfsz LO,Wgoto $+2decf HI

8-Bit Decrement to 0xFFNOT_AT_FF :

decf REGincfsz REG,Wgoto NOT_AT_FF

AT_FF:

16-Bit Incrementincfsz LOgoto $+2incf HI

16-Bit Decrementdecf LOincfsz LO,Wgoto $+2decf HI

8-Bit Decrement to 0xFFNOT_AT_FF :

decf REGincfsz REG,Wgoto NOT_AT_FF

AT_FF:

Page 3: S5704A Tips & Tricks 11 © 1999 Microchip Technology Incorporated. All Rights Reserved. S5704A Tips & Tricks 11 Tips and Tricks Tips and Tricks Using PICmicro

© 1999 Microchip Technology Incorporated. All Rights Reserved. S5704A Tips & Tricks 1S5704A Tips & Tricks 1 33

Tips and Tricks

ComparisonsComparisonsComparisonsComparisons 8-Bit Range Test

Enter with value to be tested in W.Exits with Carry set if W is in the range [LOVAL to HIVAL], inclusive.

addlw 255-HIVALaddlw (HIVAL-LOVAL)+1

Compare and SwapCompare the values in registers X and Y. If Y < X, swap them.

movf X,W ;GRAB X.subwf Y,W ;Y >= X?bc $+3 ;IF SO, JUMP AHEAD.addwf X ;OTHERWISE, X = X + (Y-X) = Y,subwf Y ; AND Y = Y - (Y-X) = X.

8-Bit Range TestEnter with value to be tested in W.Exits with Carry set if W is in the range [LOVAL to HIVAL], inclusive.

addlw 255-HIVALaddlw (HIVAL-LOVAL)+1

Compare and SwapCompare the values in registers X and Y. If Y < X, swap them.

movf X,W ;GRAB X.subwf Y,W ;Y >= X?bc $+3 ;IF SO, JUMP AHEAD.addwf X ;OTHERWISE, X = X + (Y-X) = Y,subwf Y ; AND Y = Y - (Y-X) = X.

Page 4: S5704A Tips & Tricks 11 © 1999 Microchip Technology Incorporated. All Rights Reserved. S5704A Tips & Tricks 11 Tips and Tricks Tips and Tricks Using PICmicro

© 1999 Microchip Technology Incorporated. All Rights Reserved. S5704A Tips & Tricks 1S5704A Tips & Tricks 1 44

Tips and Tricks

ComparisonsComparisonsComparisonsComparisonsMinimum

Enter with three values stored in registers N1, N2, and N3. Exit with min(N1,N2,N3) in MIN, N1-3 unchanged, W scrambled.

movf N1,wsubwf N2,W

movf N1,Wskpcmovf N2,W

movwf MIN

subwf N3,Wskpcaddwf MIN

MinimumEnter with three values stored in registers N1, N2, and N3. Exit with min(N1,N2,N3) in MIN, N1-3 unchanged, W scrambled.

movf N1,wsubwf N2,W

movf N1,Wskpcmovf N2,W

movwf MIN

subwf N3,Wskpcaddwf MIN

Page 5: S5704A Tips & Tricks 11 © 1999 Microchip Technology Incorporated. All Rights Reserved. S5704A Tips & Tricks 11 Tips and Tricks Tips and Tricks Using PICmicro

© 1999 Microchip Technology Incorporated. All Rights Reserved. S5704A Tips & Tricks 1S5704A Tips & Tricks 1 55

Tips and Tricks

Bit-ManipulationBit-ManipulationBit-ManipulationBit-ManipulationReverse 7 Bits

Enter with 0ABCDEFG in W. Exits with 0GFEDCBA in W.

movwf source ;source = 0ABCDEFG. swapf source,w ;W= DEFG0ABC. btfsc source,3 ;If D = 1, invert D and the “0”. xorlw 0x88 ;After this line, W = 0EFGDABC.

btfsc source,6 ;If A = 1, invert bits A and C. xorlw 0x05 ; btfsc source,4 ;If C = 1, invert A and C again. xorlw 0x05 ;After this line, W = 0EFGDCBA.

btfsc source,2 ;Do the same with E and G. xorlw 0x50 ; btfsc source,0 ; xorlw 0x50 ;After this line, W = 0GFEDCBA.

Reverse 7 BitsEnter with 0ABCDEFG in W. Exits with 0GFEDCBA in W.

movwf source ;source = 0ABCDEFG. swapf source,w ;W= DEFG0ABC. btfsc source,3 ;If D = 1, invert D and the “0”. xorlw 0x88 ;After this line, W = 0EFGDABC.

btfsc source,6 ;If A = 1, invert bits A and C. xorlw 0x05 ; btfsc source,4 ;If C = 1, invert A and C again. xorlw 0x05 ;After this line, W = 0EFGDCBA.

btfsc source,2 ;Do the same with E and G. xorlw 0x50 ; btfsc source,0 ; xorlw 0x50 ;After this line, W = 0GFEDCBA.

Page 6: S5704A Tips & Tricks 11 © 1999 Microchip Technology Incorporated. All Rights Reserved. S5704A Tips & Tricks 11 Tips and Tricks Tips and Tricks Using PICmicro

© 1999 Microchip Technology Incorporated. All Rights Reserved. S5704A Tips & Tricks 1S5704A Tips & Tricks 1 66

Tips and Tricks

Bit-ManipulationBit-ManipulationBit-ManipulationBit-Manipulation Rotate in Place

Rotate without inserting an extra bit from the carry. Easily extended to multi-bit rotates. Enter with ABCDEFGH in REG. Exits with BCDEFGHA in REG, W scrambled.

rlf REG,Wrlf REG

Bit-CopyCopy bits from one register to the same position in another.

movf SOURCE,W ;The DEST bits in the positions to ;which we’re copying must not change xorwf DEST,W ;between this xorwf instruction and ;the xorwf DEST below.

andlw 00000111B ;A "1" in each bit-position we’re ;copying.. ;this example copies the three

LSBs.xorwf DEST

Rotate in PlaceRotate without inserting an extra bit from the carry. Easily extended to multi-bit rotates. Enter with ABCDEFGH in REG. Exits with BCDEFGHA in REG, W scrambled.

rlf REG,Wrlf REG

Bit-CopyCopy bits from one register to the same position in another.

movf SOURCE,W ;The DEST bits in the positions to ;which we’re copying must not change xorwf DEST,W ;between this xorwf instruction and ;the xorwf DEST below.

andlw 00000111B ;A "1" in each bit-position we’re ;copying.. ;this example copies the three

LSBs.xorwf DEST

Page 7: S5704A Tips & Tricks 11 © 1999 Microchip Technology Incorporated. All Rights Reserved. S5704A Tips & Tricks 11 Tips and Tricks Tips and Tricks Using PICmicro

© 1999 Microchip Technology Incorporated. All Rights Reserved. S5704A Tips & Tricks 1S5704A Tips & Tricks 1 88

Tips and Tricks

Bit-ManipulationBit-ManipulationBit-ManipulationBit-Manipulation Bit-Number [0-7] to Bitmask [00000001-10000000]

; Enter with bit number [0-7] in BITNUM. Exits with bit mask in W.

movlw 0x01btfsc BITNUM,1movlw 0x04movwf temp

btfsc BITNUM,0addwf temp

btfsc BITNUM,2swapf temp

movf temp,w

Bit-Number [0-7] to Bitmask [00000001-10000000] ; Enter with bit number [0-7] in BITNUM. Exits with bit mask in W.

movlw 0x01btfsc BITNUM,1movlw 0x04movwf temp

btfsc BITNUM,0addwf temp

btfsc BITNUM,2swapf temp

movf temp,w

Page 8: S5704A Tips & Tricks 11 © 1999 Microchip Technology Incorporated. All Rights Reserved. S5704A Tips & Tricks 11 Tips and Tricks Tips and Tricks Using PICmicro

© 1999 Microchip Technology Incorporated. All Rights Reserved. S5704A Tips & Tricks 1S5704A Tips & Tricks 1 99

Tips and Tricks

Bit-ManipulationBit-ManipulationBit-ManipulationBit-Manipulation Bit-Setting without Affecting W or Status Bit-Setting without Affecting W or Status

Set to zero:clrf reg

Set to a power of two:clrf regbsf reg,bit

Set to 255:clrf regdecfsz reg

Set a right-aligned group of bits:clrf reg ;Set bits bsf reg,6 ;0-5.decfsz reg ;

Set a left-aligned group of bits:clrf reg ;Set bits decfsz reg ;4-7.bcf reg,4 ;incfsz reg ;

General method, 2-4 bits set:clrf regbsf reg,bit1bsf reg,bit2bsf reg,bit3bsf reg,bit4

General method, 5-7 bits set: clrf reg decfsz reg bcf reg,unbit1 bcf reg,unbit2 bcf reg,unbit3

Page 9: S5704A Tips & Tricks 11 © 1999 Microchip Technology Incorporated. All Rights Reserved. S5704A Tips & Tricks 11 Tips and Tricks Tips and Tricks Using PICmicro

© 1999 Microchip Technology Incorporated. All Rights Reserved. S5704A Tips & Tricks 1S5704A Tips & Tricks 1 1010

Tips and Tricks

DelaysDelaysDelaysDelays Constant Delay 0-1027 Cycles Constant Delay 0-1027 Cycles

WAIT MACRO CYCLES LOCAL X

IF ((CYCLES) > 1027) ERROR "MUST BE <1028 CYCLES!" ENDIF

IF ((CYCLES) < 0) ERROR "MUST BE >= 0 CYCLES!" ENDIF

X = (CYCLES)%4

IF (X == 1) NOP ENDIF

IF (X == 2) GOTO $+1 ENDIF

IF (X == 3) NOP GOTO $+1 ENDIF

X = (CYCLES)/4

IF (X)

IF (X == 256)X = 0 ENDIF

MOVLW X ADDLW -1 SKPZ GOTO $-2 ENDIF

ENDM

Page 10: S5704A Tips & Tricks 11 © 1999 Microchip Technology Incorporated. All Rights Reserved. S5704A Tips & Tricks 11 Tips and Tricks Tips and Tricks Using PICmicro

© 1999 Microchip Technology Incorporated. All Rights Reserved. S5704A Tips & Tricks 1S5704A Tips & Tricks 1 1212

Tips and Tricks

DelaysDelaysDelaysDelaysTrading Stack Space for RAMTrading Stack Space for RAM

Delay131072: call Delay16384Delay114688: call Delay16384Delay98304: call Delay16384Delay81920: call Delay16384Delay65536: call Delay16384Delay49152: call Delay16384Delay32768: call Delay16384Delay16384: call Delay2048Delay14336: call Delay2048Delay12288: call Delay2048Delay10240: call Delay2048Delay8192: call Delay2048Delay6144: call Delay2048Delay4096: call Delay2048Delay2048: call Delay256Delay1792: call Delay256Delay1536: call Delay256Delay1280: call Delay256Delay1024: call Delay256

Delay768: call Delay256Delay512: call Delay256Delay256: call Delay32Delay224: call Delay32Delay192: call Delay32Delay160: call Delay32Delay128: call Delay32Delay96: call Delay32Delay64: call Delay32Delay48: call Delay32Delay32: call Delay4Delay28: call Delay4Delay24: call Delay4Delay20: call Delay4Delay16: call Delay4Delay12: call Delay4Delay8: call Delay4Delay4: return

Page 11: S5704A Tips & Tricks 11 © 1999 Microchip Technology Incorporated. All Rights Reserved. S5704A Tips & Tricks 11 Tips and Tricks Tips and Tricks Using PICmicro

© 1999 Microchip Technology Incorporated. All Rights Reserved. S5704A Tips & Tricks 1S5704A Tips & Tricks 1 1313

Tips and Tricks

MathMathMathMath16-Bit Subtract with Valid Carry-Out

movf SOURCE_LO,W ;DEST = DEST - SOURCE. subwf DEST_LO ;Note that the Z flag ismovf SOURCE_HI,W ;invalid after theskpc ;subtraction.incfszSOURCE_HI,W ;subwf DEST_HI;

8-Bit Add with Valid Carry-In/Out

movf SOURCE,W ;DEST = DEST + SOURCE + CARRYskpnc ;Carry is valid after theincf SOURCE,W ;addition.addwf DEST ;

Page 12: S5704A Tips & Tricks 11 © 1999 Microchip Technology Incorporated. All Rights Reserved. S5704A Tips & Tricks 11 Tips and Tricks Tips and Tricks Using PICmicro

© 1999 Microchip Technology Incorporated. All Rights Reserved. S5704A Tips & Tricks 1S5704A Tips & Tricks 1 1515

Tips and Tricks

MathMathMathMathTricks with Known-Zero Registers

rlf KZ,W ;DEST = DEST + SOURCE + CARRY.

addwf SOURCE,W ;Carry-out is only valid if SOURCE<255.addwf DEST ;

rlf KZ,W ;DEST = DEST + 0 + CARRY. Carry-out is

addwf DEST ;valid.

movlw 255 ;DEST = DEST + 255 + CARRY. Carry-out

skpc ;is valid.addwf DEST ;

rlf KZ,W ;DEST = DEST + constant + CARRY, where

addlw [constant] ;0 < constant < 255. Carry-out isaddwf DEST ;valid.

Page 13: S5704A Tips & Tricks 11 © 1999 Microchip Technology Incorporated. All Rights Reserved. S5704A Tips & Tricks 11 Tips and Tricks Tips and Tricks Using PICmicro

© 1999 Microchip Technology Incorporated. All Rights Reserved. S5704A Tips & Tricks 1S5704A Tips & Tricks 1 1717

Tips and Tricks

MathMathMathMathEven Parity;Enter with input in register X. Branches to "ZERO" or "ONE";depending on parity. Doesn’t modify register X. For odd;parity, replace "GOTO ZERO" with "GOTO ONE", and vice-versa.;19 words. 5 or 7 cycles, including branch to ZERO or ONE.

SWAPF X,WXORWF X,WANDLW 00001111B

ADDWF PCGOTO ZEROGOTO ONEGOTO ONEGOTO ZEROGOTO ONEGOTO ZEROGOTO ZERO

GOTO ONEGOTO ONEGOTO ZEROGOTO ZEROGOTO ONEGOTO ZEROGOTO ONEGOTO ONE

ZERO:....

ONE:....

Page 14: S5704A Tips & Tricks 11 © 1999 Microchip Technology Incorporated. All Rights Reserved. S5704A Tips & Tricks 11 Tips and Tricks Tips and Tricks Using PICmicro

© 1999 Microchip Technology Incorporated. All Rights Reserved. S5704A Tips & Tricks 1S5704A Tips & Tricks 1 1818

Tips and Tricks

MPASM TipsMPASM TipsMPASM TipsMPASM TipsEQU vs. #define

equ1 equ 5+3 ;MPASM will replace future ;occurrences of "equ1" with the ;value 8.

#define def1 5+3 ;MPASM will replace future ;occurrences of "def1" with the ;string"5+3".

x equ 3*equ1 ;This line is equivalent to "x ;equ 3*8", so x = 24.

y equ 3*def1 ;THIS line, on the other hand, ;is equivalent to "y equ 3*5+3", ;so y = 18.

Page 15: S5704A Tips & Tricks 11 © 1999 Microchip Technology Incorporated. All Rights Reserved. S5704A Tips & Tricks 11 Tips and Tricks Tips and Tricks Using PICmicro

© 1999 Microchip Technology Incorporated. All Rights Reserved. S5704A Tips & Tricks 1S5704A Tips & Tricks 1 2222

Tips and Tricks

TimingTimingTimingTiming16-Bit Pulse-Width Measurement with 5-Cycle

Resolution

; Enter with PULSE_WIDTH_HI:LO set to 0000. Exits ; ; when PORT,PIN goes low.

CHECK_PULSE: INCFSZ PULSE_WIDTH_LO DECF PULSE_WIDTH_HI

BTFSC PORT,PIN GOTO CHECK_PULSE

DONE: MOVF PULSE_WIDTH_LO,W ADDWF PULSE_WIDTH_HI

Page 16: S5704A Tips & Tricks 11 © 1999 Microchip Technology Incorporated. All Rights Reserved. S5704A Tips & Tricks 11 Tips and Tricks Tips and Tricks Using PICmicro

© 1999 Microchip Technology Incorporated. All Rights Reserved. S5704A Tips & Tricks 1S5704A Tips & Tricks 1 2828

Tips and Tricks

MiscellaneousMiscellaneousMiscellaneousMiscellaneous"Switch" Statement

; ENTER WITH VALUE TO BE TESTED IN W.

XORLW VAL1 ;IF W = VAL1, GOTO CASE1. BZ CASE1 ;

XORLW VAL2^VAL1 ;IF W = VAL2, GOTO CASE2. BZ CASE2 ;

XORLW VAL3^VAL2 ;IF W = VAL3, GOTO CASE3. BZ CASE3 ;

XORLW VAL4^VAL3 ;IF W = VAL4, GOTO CASE4. BZ CASE4 ;

etc...

Page 17: S5704A Tips & Tricks 11 © 1999 Microchip Technology Incorporated. All Rights Reserved. S5704A Tips & Tricks 11 Tips and Tricks Tips and Tricks Using PICmicro

© 1999 Microchip Technology Incorporated. All Rights Reserved. S5704A Tips & Tricks 1S5704A Tips & Tricks 1 2929

Tips and Tricks

ResourcesResourcesResourcesResources PICLIST Internet Mailing List

To subscribe, send e-mail to:[email protected]

with the following in the message body:subscribe piclistset piclist reproend

PICLIST Archiveshttp://anick.simplenet.com/piclist/

www.microchip.com Embedded Control Handbook

PICLIST Internet Mailing ListTo subscribe, send e-mail to:

[email protected] the following in the message body:

subscribe piclistset piclist reproend

PICLIST Archiveshttp://anick.simplenet.com/piclist/

www.microchip.com Embedded Control Handbook


Recommended