Tips and Tricks Using PICmicro MCUs 1999 Microchip

  • Slides: 17
Download presentation
Tips and Tricks Using PICmicro® MCUs © 1999 Microchip Technology Incorporated. All Rights Reserved.

Tips and Tricks Using PICmicro® MCUs © 1999 Microchip Technology Incorporated. All Rights Reserved. S 5704 A Tips & Tricks 1 1

Tips and Tricks Increments and Decrements l 16 -Bit Increment incfsz goto incf l

Tips and Tricks Increments and Decrements l 16 -Bit Increment incfsz goto incf l 16 -Bit Decrement decf incfsz goto decf l LO $+2 HI LO LO, W $+2 HI 8 -Bit Decrement to 0 x. FF NOT_AT_FF : decf incfsz goto AT_FF: © 1999 Microchip Technology Incorporated. All Rights Reserved. REG, W NOT_AT_FF S 5704 A Tips & Tricks 1 2

Tips and Tricks Comparisons l 8 -Bit Range Test Enter with value to be

Tips and Tricks Comparisons l 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 -HIVAL addlw (HIVAL-LOVAL)+1 l Compare and Swap Compare the values in registers X and Y. swap them. movf subwf bc addwf subwf X, W Y, W $+3 X Y © 1999 Microchip Technology Incorporated. All Rights Reserved. If Y < X, ; GRAB X. ; Y >= X? ; IF SO, JUMP AHEAD. ; OTHERWISE, X = X + (Y-X) = Y, ; AND Y = Y - (Y-X) = X. S 5704 A Tips & Tricks 1 3

Tips and Tricks Comparisons Minimum Enter with three values stored in registers N 1,

Tips and Tricks Comparisons Minimum Enter with three values stored in registers N 1, N 2, and N 3. Exit with min(N 1, N 2, N 3) in MIN, N 1 -3 unchanged, W scrambled. movf subwf N 1, w N 2, W movf skpc movf N 1, W movwf MIN subwf skpc addwf N 3, W N 2, W MIN © 1999 Microchip Technology Incorporated. All Rights Reserved. S 5704 A Tips & Tricks 1 4

Tips and Tricks Reverse 7 Bits Bit-Manipulation Enter with 0 ABCDEFG in W. Exits

Tips and Tricks Reverse 7 Bits Bit-Manipulation Enter with 0 ABCDEFG in W. Exits with 0 GFEDCBA in W. movwf swapf btfsc xorlw source, 3 0 x 88 ; source = 0 ABCDEFG. ; W= DEFG 0 ABC. ; If D = 1, invert D and the “ 0”. ; After this line, W = 0 EFGDABC. btfsc xorlw source, 6 0 x 05 source, 4 0 x 05 ; If A = 1, invert bits A and C. ; ; If C = 1, invert A and C again. ; After this line, W = 0 EFGDCBA. btfsc xorlw source, 2 0 x 50 source, 0 0 x 50 ; Do the same with E and G. ; ; ; After this line, W = 0 GFEDCBA. © 1999 Microchip Technology Incorporated. All Rights Reserved. S 5704 A Tips & Tricks 1 5

Tips and Tricks l Rotate in Place Bit-Manipulation Rotate without inserting an extra bit

Tips and Tricks l Rotate in Place Bit-Manipulation 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 l REG, W REG Bit-Copy 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 we’re 00000111 B copies the three LSBs. xorwf DEST © 1999 Microchip Technology Incorporated. All Rights Reserved. ; A "1" in each bit-position ; copying. . ; this example S 5704 A Tips & Tricks 1 6

Tips and Tricks Bit-Manipulation l Bit-Number [0 -7] to Bitmask [00000001 -10000000] ; Enter

Tips and Tricks Bit-Manipulation l Bit-Number [0 -7] to Bitmask [00000001 -10000000] ; Enter with bit number [0 -7] in BITNUM. mask in W. movlw btfsc movlw movwf 0 x 01 BITNUM, 1 0 x 04 temp btfsc addwf BITNUM, 0 temp btfsc swapf BITNUM, 2 temp movf temp, w © 1999 Microchip Technology Incorporated. All Rights Reserved. S 5704 A Tips & Tricks 1 Exits with bit 8

Tips and Tricks Bit-Manipulation l Bit-Setting without Affecting W or Status Set to zero:

Tips and Tricks Bit-Manipulation l Bit-Setting without Affecting W or Status Set to zero: clrf Set a left-aligned group of bits: clrf reg ; Set bits decfsz reg ; 4 -7. bcf reg, 4 ; incfsz reg ; reg Set to a power of two: clrf reg bsf reg, bit Set to 255: clrf reg decfsz reg Set a right-aligned group of bits: clrf reg ; Set bits bsf reg, 6 ; 0 -5. decfsz reg ; © 1999 Microchip Technology Incorporated. All Rights Reserved. General method, clrf bsf bsf 2 -4 bits set: reg, bit 1 reg, bit 2 reg, bit 3 reg, bit 4 General method, clrf decfsz bcf bcf 5 -7 bits set: reg reg, unbit 1 reg, unbit 2 reg, unbit 3 S 5704 A Tips & Tricks 1 9

Tips and Tricks Delays l WAIT Constant Delay 0 -1027 Cycles MACRO LOCAL CYCLES

Tips and Tricks Delays l WAIT Constant Delay 0 -1027 Cycles MACRO LOCAL CYCLES X IF ((CYCLES) > 1027) ERROR "MUST BE <1028 CYCLES!" ENDIF IF (X == 3) NOP GOTO $+1 ENDIF X = (CYCLES)/4 IF ((CYCLES) < 0) ERROR "MUST BE >= 0 CYCLES!" ENDIF X = 0 X = (CYCLES)%4 IF (X == 1) NOP ENDIF IF (X == 256) ENDIF MOVLW ADDLW SKPZ GOTO ENDIF IF (X == 2) GOTO $+1 ENDIF © 1999 Microchip Technology Incorporated. All Rights Reserved. IF (X) X -1 $-2 ENDM S 5704 A Tips & Tricks 1 10

Tips and Tricks Delays Trading Stack Space for RAM Delay 131072: Delay 114688: Delay

Tips and Tricks Delays Trading Stack Space for RAM Delay 131072: Delay 114688: Delay 98304: Delay 81920: Delay 65536: Delay 49152: Delay 32768: Delay 16384: Delay 14336: Delay 12288: Delay 10240: Delay 8192: Delay 6144: Delay 4096: Delay 2048: Delay 1792: Delay 1536: Delay 1280: Delay 1024: call call call call call © 1999 Microchip Technology Incorporated. All Rights Reserved. Delay 16384 Delay 16384 Delay 2048 Delay 2048 Delay 256 Delay 256 Delay 768: Delay 512: Delay 256: Delay 224: Delay 192: Delay 160: Delay 128: Delay 96: Delay 64: Delay 48: Delay 32: Delay 28: Delay 24: Delay 20: Delay 16: Delay 12: Delay 8: Delay 4: S 5704 A Tips & Tricks 1 call call call call call return Delay 256 Delay 32 Delay 32 Delay 4 Delay 4 12

Tips and Tricks Math l 16 -Bit Subtract with Valid Carry-Out movf SOURCE_LO, W

Tips and Tricks Math l 16 -Bit Subtract with Valid Carry-Out movf SOURCE_LO, W ; DEST = DEST - SOURCE. subwf DEST_LO ; Note that the Z flag is movf SOURCE_HI, W ; invalid after the skpc ; subtraction. incfsz SOURCE_HI, W ; subwf DEST_HI ; l 8 -Bit Add with Valid Carry-In/Out movf SOURCE, W ; DEST = DEST + SOURCE + CARRY skpnc ; Carry is valid after the incf SOURCE, W ; addition. addwf DEST ; © 1999 Microchip Technology Incorporated. All Rights Reserved. S 5704 A Tips & Tricks 1 13

Tips and Tricks Math l Tricks with Known-Zero Registers ¬ rlf addwf KZ, W

Tips and Tricks Math l Tricks with Known-Zero Registers ¬ rlf addwf KZ, W SOURCE, W DEST ; DEST = DEST + SOURCE + CARRY. ; Carry-out is only valid if SOURCE<255. ; rlf addwf KZ, W DEST ; DEST = DEST + 0 + CARRY. ; valid. ® movlw skpc addwf 255 ; DEST = DEST + 255 + CARRY. ; is valid. ; rlf addlw addwf KZ, W ; DEST = DEST + constant + CARRY, where [constant] ; 0 < constant < 255. Carry-out is DEST ; valid. ¯ DEST © 1999 Microchip Technology Incorporated. All Rights Reserved. S 5704 A Tips & Tricks 1 Carry-out is Carry-out 15

Tips and Tricks Math l Even Parity ; Enter with input in register X.

Tips and Tricks Math l Even 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 XORWF ANDLW X, W ADDWF GOTO GOTO PC ZERO ONE ZERO GOTO GOTO 00001111 B © 1999 Microchip Technology Incorporated. All Rights Reserved. ONE ZERO ONE ZERO: . . ONE: . . S 5704 A Tips & Tricks 1 17

Tips and Tricks MPASM Tips l EQU vs. #define equ 1 equ 5+3 ;

Tips and Tricks MPASM Tips l EQU vs. #define equ 1 equ 5+3 ; MPASM will replace future ; occurrences of "equ 1" with the ; value 8. #define def 1 5+3 ; MPASM will replace future ; occurrences of "def 1" with the ; string"5+3". x equ 3*equ 1 ; This line is equivalent to "x ; equ 3*8", so x = 24. y equ 3*def 1 ; THIS line, on the other hand, ; is equivalent to "y equ 3*5+3", ; so y = 18. © 1999 Microchip Technology Incorporated. All Rights Reserved. S 5704 A Tips & Tricks 1 18

Tips and Tricks Timing l 16 -Bit Pulse-Width Measurement with 5 -Cycle Resolution ;

Tips and Tricks Timing l 16 -Bit Pulse-Width Measurement with 5 -Cycle Resolution ; Enter with PULSE_WIDTH_HI: LO set to 0000. when PORT, PIN goes low. Exits ; ; CHECK_PULSE: INCFSZ DECF PULSE_WIDTH_LO PULSE_WIDTH_HI BTFSC GOTO PORT, PIN CHECK_PULSE MOVF ADDWF PULSE_WIDTH_LO, W PULSE_WIDTH_HI DONE: © 1999 Microchip Technology Incorporated. All Rights Reserved. S 5704 A Tips & Tricks 1 22

Tips and Tricks Miscellaneous l "Switch" Statement ; ENTER WITH VALUE TO BE TESTED

Tips and Tricks Miscellaneous l "Switch" Statement ; ENTER WITH VALUE TO BE TESTED IN W. XORLW BZ VAL 1 CASE 1 ; IF W = VAL 1, GOTO CASE 1. ; XORLW BZ VAL 2^VAL 1 CASE 2 ; IF W = VAL 2, GOTO CASE 2. ; XORLW BZ VAL 3^VAL 2 CASE 3 ; IF W = VAL 3, GOTO CASE 3. ; XORLW BZ VAL 4^VAL 3 CASE 4 ; IF W = VAL 4, GOTO CASE 4. ; etc. . . © 1999 Microchip Technology Incorporated. All Rights Reserved. S 5704 A Tips & Tricks 1 28

Tips and Tricks Resources l PICLIST Internet Mailing List To subscribe, send e-mail to:

Tips and Tricks Resources l PICLIST Internet Mailing List To subscribe, send e-mail to: listserv@mitvma. mit. edu with the following in the message body: subscribe piclist set piclist repro end l PICLIST Archives http: //anick. simplenet. com/piclist/ l l www. microchip. com Embedded Control Handbook © 1999 Microchip Technology Incorporated. All Rights Reserved. S 5704 A Tips & Tricks 1 29