You are on page 1of 29

Tips and Tricks

Tips and Tricks Using PICmicro MCUs

1999 Microchip Technology Incorporated. All Rights Reserved.

S5704A Tips & Tricks 1

Tips and Tricks

Increments and Decrements


q

16-Bit Increment
incfsz goto incf LO $+2 HI LO LO,W $+2 HI

16-Bit Decrement
decf incfsz goto decf

8-Bit Decrement to 0xFF


NOT_AT_FF : decf incfsz goto AT_FF: REG REG,W NOT_AT_FF
S5704A Tips & Tricks 1 2

1999 Microchip Technology Incorporated. All Rights Reserved.

Tips and Tricks

8-Bit Range Test

Comparisons

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

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 If Y < X,

;GRAB X. ;Y >= X? ;IF SO, JUMP AHEAD. ;OTHERWISE, X = X + (Y-X) = Y, ; AND Y = Y - (Y-X) = X.
S5704A Tips & Tricks 1 3

1999 Microchip Technology Incorporated. All Rights Reserved.

Tips and Tricks

Minimum

Comparisons

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,w subwf N2,W movf skpc movf N1,W N2,W

movwf MIN subwf N3,W skpc addwf MIN


1999 Microchip Technology Incorporated. All Rights Reserved.

S5704A Tips & Tricks 1

Tips and Tricks

Reverse 7 Bits
movwf swapf btfsc xorlw btfsc xorlw btfsc xorlw btfsc xorlw btfsc xorlw

Bit-Manipulation
Exits with 0GFEDCBA in W. ;source = 0ABCDEFG. ;W= DEFG0ABC. ;If D = 1, invert D and the 0. ;After this line, W = 0EFGDABC. ;If A = 1, invert ; ;If C = 1, invert ;After this line, bits A and C. A W and C again. = 0EFGDCBA.

Enter with 0ABCDEFG in W. source source,w source,3 0x88 source,6 0x05 source,4 0x05 source,2 0x50 source,0 0x50

;Do the same with E and G. ; ; ;After this line, W = 0GFEDCBA.


S5704A Tips & Tricks 1 5

1999 Microchip Technology Incorporated. All Rights Reserved.

Tips and Tricks

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 rlf
q

REG,W REG

Bit-Copy
Copy bits from one register to the same position in another. movf SOURCE,W ;The DEST bits in the positions to ;which were copying must not change ;between this xorwf instruction and ;the xorwf DEST below. ;A "1" in each bit-position were ;copying.. ;this example copies the three LSBs.

xorwf DEST,W

andlw 00000111B xorwf DEST


1999 Microchip Technology Incorporated. All Rights Reserved.

S5704A Tips & Tricks 1

Tips and Tricks

Bit-Manipulation
q

Bit Counter
Count the number of "1" bits in a register. On exit, W contains the number of "1" bits in REG, and REG is scrambled. rrf andlw subwf movf andlw addwf rrf andlw addwf rrf swapf addwf andlw REG,W 0x44 REG REG,W 0x33 REG REG 0x11 REG REG REG,W REG,W 0x0F
S5704A Tips & Tricks 1 7

1999 Microchip Technology Incorporated. All Rights Reserved.

Tips and Tricks

Bit-Manipulation
q

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


; Enter with bit number [0-7] in BITNUM. mask in W. movlw btfsc movlw movwf 0x01 BITNUM,1 0x04 temp Exits with bit

btfsc BITNUM,0 addwf temp btfsc BITNUM,2 swapf temp movf temp,w

1999 Microchip Technology Incorporated. All Rights Reserved.

S5704A Tips & Tricks 1

Tips and Tricks

Bit-Setting without Affecting W or Status


reg

Bit-Manipulation
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 reg bsf reg,bit1 bsf reg,bit2 bsf reg,bit3 bsf reg,bit4 General method, clrf decfsz bcf bcf bcf 5-7 bits set: reg reg reg,unbit1 reg,unbit2 reg,unbit3
9

Set to zero: clrf

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.

S5704A Tips & Tricks 1

Tips and Tricks

q
WAIT

Constant Delay 0-1027 Cycles


MACRO LOCAL CYCLES X

Delays
IF (X == 3) NOP GOTO $+1 ENDIF X = (CYCLES)/4 IF (X) IF (X == 256) X = 0 ENDIF MOVLW ADDLW SKPZ GOTO ENDIF ENDM
S5704A Tips & Tricks 1 10

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
1999 Microchip Technology Incorporated. All Rights Reserved.

X -1 $-2

Tips and Tricks

Non-Constant Delay 20-271 Cycles


MOVLW CALL .... [NUMBER OF CYCLES] DELAY SKIP: RRF RRF MOVLW SUBWF BCF BCF LOOP: NOP DECFSZ GOTO RETURN $+1
S5704A Tips & Tricks 1 11

Delays

COUNTER COUNTER 4 COUNTER COUNTER,6 COUNTER,7

; ; ; ; ;

DELAY 20-271 CYCLES. ENTER WITH NUMBER OF CYCLES TO DELAY IN W. DELAY IS INCLUSIVE OF "MOVLW", CALL, AND RETURN OVERHEAD.

DELAY: MOVWF BTFSC GOTO BTFSS GOTO NOP GOTO COUNTER COUNTER, 0 $+1 COUNTER, 1 SKIP

COUNTER LOOP

1999 Microchip Technology Incorporated. All Rights Reserved.

Tips and Tricks

Delays
Trading Stack Space for RAM
Delay131072: Delay114688: Delay98304: Delay81920: Delay65536: Delay49152: Delay32768: Delay16384: Delay14336: Delay12288: Delay10240: Delay8192: Delay6144: Delay4096: Delay2048: Delay1792: Delay1536: Delay1280: Delay1024: call call call call call call call call call call call call call call call call call call call Delay16384 Delay16384 Delay16384 Delay16384 Delay16384 Delay16384 Delay16384 Delay2048 Delay2048 Delay2048 Delay2048 Delay2048 Delay2048 Delay2048 Delay256 Delay256 Delay256 Delay256 Delay256 Delay768: Delay512: Delay256: Delay224: Delay192: Delay160: Delay128: Delay96: Delay64: Delay48: Delay32: Delay28: Delay24: Delay20: Delay16: Delay12: Delay8: Delay4: call call call call call call call call call call call call call call call call call return Delay256 Delay256 Delay32 Delay32 Delay32 Delay32 Delay32 Delay32 Delay32 Delay32 Delay4 Delay4 Delay4 Delay4 Delay4 Delay4 Delay4

1999 Microchip Technology Incorporated. All Rights Reserved.

S5704A Tips & Tricks 1

12

Tips and Tricks

Math
q16-Bit

Subtract with Valid Carry-Out


movf SOURCE_LO,W subwf DEST_LO movf SOURCE_HI,W skpc incfsz SOURCE_HI,W subwf DEST_HI ; ;DEST = DEST - SOURCE. ;Note that the Z flag is ;invalid after the ;subtraction. ;

q8-Bit

Add with Valid Carry-In/Out


movf SOURCE,W skpnc incf SOURCE,W addwf DEST ;DEST = DEST + SOURCE + CARRY ;Carry is valid after the ;addition. ;

1999 Microchip Technology Incorporated. All Rights Reserved.

S5704A Tips & Tricks 1

13

Tips and Tricks

Math
q

RAM-Efficient Moving-Window Average

;CALCULATE NEW_AVERAGE = (255 * OLD_AVERAGE + NEW_SAMPLE)/256. ;ENTER WITH THE NEW SAMPLE IN W. EXITS WITH THE NEW AVERAGE IN W. AVERAGE: addwf movf skpnc incf subwf skpc decf movf return

SUMLO SUMHI,W SUMHI SUMLO SUMHI SUMHI,W

;SUM = SUM + SAMPLE. ;(AND WHILE WE'RE HERE, PUT THE OLD ;AVERAGE IN W). ; ;ADJUST THE SUM BY SUBTRACTING THE OLD ;AVERAGE FROM THE NEW SUM. ; ;W=THE NEW AVERAGE (ADJUSTED SUM/256) ;RETURN.
S5704A Tips & Tricks 1 14

1999 Microchip Technology Incorporated. All Rights Reserved.

Tips and Tricks

Math
qTricks

with Known-Zero Registers


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

x CARRY. addwf addwf

;Carry-out is only valid if SOURCE<255. ; KZ,W ;valid. 255 ;DEST = DEST + 255 + CARRY. ;DEST = DEST + 0 + CARRY.

y rlf Carry-out is addwf DEST z Carry-out skpc addwf movlw

DEST

;is valid. ;

1999 Microchip Technology Incorporated. All Rights Reserved.

{ rlf KZ,W ;DEST = DEST + constant + CARRY, where 15 addlw [constant] ;0 S5704A Tips & Tricks 1 < 255. Carry-out is < constant

Tips and Tricks

Math
qEven

Parity

; Enter with input in register "X". Exits with ; even parity in bits 1, 2, 3, 4, and 5 of ;register "X". ; ; 7 words, 7 cycles. swapf xorwf rrf xorwf rrf rlf xorwf
1999 Microchip Technology Incorporated. All Rights Reserved.

X,W X X,W X X,W X X


S5704A Tips & Tricks 1 16

Tips and Tricks

Math
qEven

Parity

;Enter with input in register X. Branches to "ZERO" or "ONE" ;depending on parity. Doesnt 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 ADDWF GOTO GOTO GOTO GOTO GOTO GOTO GOTO X,W X,W
00001111B

PC ZERO ONE ONE ZERO ONE ZERO ZERO

GOTO GOTO GOTO GOTO GOTO GOTO GOTO GOTO ZERO: .... ONE: ....
S5704A Tips & Tricks 1

ONE ONE ZERO ZERO ONE ZERO ONE ONE

1999 Microchip Technology Incorporated. All Rights Reserved.

17

Tips and Tricks

MPASM Tips
qEQU

vs. #define
equ 5+3 ;MPASM will replace future ;occurrences of "equ1" with the ;value 8. ;MPASM will replace future ;occurrences of "def1" with the ;string"5+3".

equ1

#define def1 5+3

equ

3*equ1 ;This line is equivalent to "x ;equ 3*8", so x = 24. 3*def1 ;THIS line, on the other hand, ;is equivalent to "y equ 3*5+3", ;so y = 18.
S5704A Tips & Tricks 1 18

equ

1999 Microchip Technology Incorporated. All Rights Reserved.

Tips and Tricks

MPASM Tips
qAccessing

Registers Above 0x7F


;Generates no warnings, but also ;doesnt allow REG to be viewed ;in MPLAB Watch Windows.

Method #1: REG EQU xxx - 128 .... MOVWF REG Method #2: MOVWF REG

;Generates an MPASM warning.

Method #3: MOVWF REG & 0x7F ;Generates no warning, even when ;accidentally used with a ;page-0 register. Method #4: MOVWF REG ^ 0x80 ;Generates no warning when used ;correctly, but does generate a ;warning if accidentally used ;with a page-0 register.
1999 Microchip Technology Incorporated. All Rights Reserved.

S5704A Tips & Tricks 1

19

Tips and Tricks

MPASM Tips
qHIGH-and-LOW

vs. Shift-and-AND

BIGNUM

EQU 0x123456 ;LSB will always be set to ;0x56. ;Some versions of MPASM will ;set "MSB" to 0x12; others ;will set it to 0x34.

LSB EQU LOW (BIGNUM)

MSB EQU HIGH (BIGNUM)

SAFEMSB EQU (BIGNUM>>16) & 0xFF ;This method will always set ;"SAFEMSB" to 0x12.

1999 Microchip Technology Incorporated. All Rights Reserved.

S5704A Tips & Tricks 1

20

Tips and Tricks

Timing
qExact
; ; ; ;

Sync to the TMR0 Prescaler


; Here, TMR0:PRE must be 01:3 or 02:0 BTFSS GOTO TMR0,BIT1 $+1

Enter this section with TMR0 between 128 and 255, inclusive, and the prescaler divide-byratio set to divide-by-4.

WAITFOR0: BTFSC GOTO TMR0,BIT7 WAITFOR0

; Here, TMR0:PRE must be 02:2 GOTO $+1

; Here, TMRO:PRESCALER can be ; 00:2 or 00:3 or 01:0. BTFSS GOTO TMR0,BIT0 $+1

; Here, TMR0:PRE must be 01:1 ; or 01:2. GOTO $+1

; TMR0 always increments from 02 to ; 03 right here. When we reload TMR0, ;we need to add 4 to the reload value ;because the MOVLW/MOVWF takes 2 ;cycles and theres an additional 2;cycle synchronization delay. TMR0 ;will resume incrementing at EXACTLY ;the moment when it would have rolled ;over from 03 to 04. MOVLW S5704A Tips & Tricks 1 MOVWF RELOAD+4 TMR0
21

1999 Microchip Technology Incorporated. All Rights Reserved.

Tips and Tricks

Timing
q16-Bit

Pulse-Width Measurement with 5-Cycle Resolution


Exits ; ;

; Enter with PULSE_WIDTH_HI:LO set to 0000. when PORT,PIN goes low. CHECK_PULSE: INCFSZ DECF BTFSC GOTO DONE: MOVF ADDWF PULSE_WIDTH_LO,W PULSE_WIDTH_HI PULSE_WIDTH_LO PULSE_WIDTH_HI PORT,PIN CHECK_PULSE

1999 Microchip Technology Incorporated. All Rights Reserved.

S5704A Tips & Tricks 1

22

Tips and Tricks

Hardware Interfacing
qZero

I/O-Pin PICmicro-to- PICmicro Communication

Your Master PICmicro controls the Slave PICmicro's MCLR line. The Master normally keeps the Slave's MCLR line high (out of reset). When the Master wants to send a message, it pulls the Slave's MCLR line low, then pulls it high for a short time before pulling it low, then high again. The "message" is the time between MCLR-low pulses.

Message error-proofing methods:


Redundancy: Send the message two or more times and require that the Slave see consecutive, identical messages before acting on them. Parity: Send your message, then follow it with a "parity message that brings the total time-between-MCLR-low-pulses time to some constant value. Framing: Send a unique-length "start of message" pulse, then your message pulse, then a unique-length "end of message pulse. The Slave doesn't accept the message unless it sees the "start" and "end" pulses around it.
1999 Microchip Technology Incorporated. All Rights Reserved.

S5704A Tips & Tricks 1

23

Tips and Tricks

qSetting
q4

ADCON0: Clock Select bits

Hardware Interfacing

A/D clock (Tad) selections :


q

Fosc/2, Fosc/8, Fosc/32 and Frc(internal RC) qTad Min. = 1.6 uS for VREF => 3.0V or 3.0uS for VREF < 3.0V For fastest A/D response, calculate the Tad which is closest to the Minimum value for Tad

Example: VREF = 5.0V, Fosc = 4.0 MHz


For fastest A/D response, select Fosc/8 (2.0 uS). If Frc is selected, instead of Fosc/8, then Min. time would be 2.0us, but the Max. time would be 6.0us I.e. 3 times slower than the best A/D response time. qFor fastest response, select Fosc = 5.0 MHz (Tad = 1.6uS) instead of 4.0Mhz (Tad = 2.0 uS).
q
1999 Microchip Technology Incorporated. All Rights Reserved.

S5704A Tips & Tricks 1

24

Tips and Tricks

Hardware Interfacing
qSetting
qTo

ADCON0: GO/DONE

start an A/D conversion the GO bit should be set to 1. Note: ADON and GO bit should not be set in the same instruction qOn conversion complete, GO/DONE = 0 & ADIF = 1. Fastest check of A/D conversion completion: Bit test ADIF or the GO/DONE bit in tight loop . btfsc goto ADCON0,DONE $-1

Time for check = 3Tcycles


qChecking

A/D completion using Interrupts takes: 3T cycles for interrupt latency 10T cycles for Context Saving + 1T cycle for ADIF check Total = 14 T cycles
S5704A Tips & Tricks 1 25

1999 Microchip Technology Incorporated. All Rights Reserved.

Tips and Tricks

Hardware Interfacing
qA/D

Conversion in Sleep
digital noise during A/D conversion. qEnable Frc mode for Tad qEnable or disable A/D Interrupt qStart A/D conversion and goto sleep

qReduces

qOn

A/D Completion: qIf interrupt is enabled then CPU wakes up from sleep qIf interrupt is disabled then CPU does not wake up from sleep, but A/D circuit is turned OFF (ADON is still set).

Crystal mode conversion in sleep is slow since most crystals take 2 mS or more to wake-up
qFor

qIn

fast conversion in sleep, use RC oscillator


S5704A Tips & Tricks 1 26

1999 Microchip Technology Incorporated. All Rights Reserved.

Tips and Tricks

Miscellaneous
qDirect
TBL: MACRO LOCAL ADDWF ADDLW ADDLW ADDLW ADDLW ADDLW ADDLW ADDLW ADDLW

Modification of PC (or PCL)


A,B,C,D,E,F,G,H X PCL HIGH HIGH HIGH HIGH HIGH HIGH HIGH HIGH For example, to calculate 2^W, where W is in the range [0-7]: TBL 1,2,4,8,16,32,64,128 (A)+1 (B)+1 (C)+1 (D)+1 (E)+1 (F)+1 (G)+1 (H)-7 HIGH HIGH HIGH HIGH HIGH HIGH HIGH (B) (C) (D) (E) (F) (G) (H) assembles to: ADDWF ADDLW ADDLW ADDLW ADDLW ADDLW ADDLW ADDLW ADDLW PCL 0 -1 -3 -7 -15 -31 -63 121

X:

IF ((HIGH $) != (HIGH X)) ERROR "TABLE CROSSES PAGE ERROR BOUNDARIES! DO ERROR SOMETHING! ENDIF ENDM
1999 Microchip Technology Incorporated. All Rights Reserved.

S5704A Tips & Tricks 1

27

Tips and Tricks

q"Switch"

Statement
VAL1 CASE1

Miscellaneous

; ENTER WITH VALUE TO BE TESTED IN W.

XORLW BZ XORLW BZ XORLW BZ XORLW BZ etc...

;IF W = VAL1, GOTO CASE1. ; ;IF W = VAL2, GOTO CASE2. ; ;IF W = VAL3, GOTO CASE3. ; ;IF W = VAL4, GOTO CASE4. ;

VAL2^VAL1 CASE2 VAL3^VAL2 CASE3 VAL4^VAL3 CASE4

1999 Microchip Technology Incorporated. All Rights Reserved.

S5704A Tips & Tricks 1

28

Tips and Tricks

Resources
q

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

q q q

PICLIST Archives

http://anick.simplenet.com/piclist/

www.microchip.com Embedded Control Handbook


S5704A Tips & Tricks 1 29

1999 Microchip Technology Incorporated. All Rights Reserved.

You might also like