Download presentation
Presentation is loading. Please wait.
Published byGerald Taylor Modified over 9 years ago
1
S5704A Tips & Tricks 11 © 1999 Microchip Technology Incorporated. All Rights Reserved. S5704A Tips & Tricks 11 Tips and Tricks Tips and Tricks Using PICmicro ® MCUs
2
S5704A Tips & Tricks 12 © 1999 Microchip Technology Incorporated. All Rights Reserved. S5704A Tips & Tricks 12 Tips and Tricks Increments and Decrements 16-Bit Increment incfszLO goto$+2 incfHI 16-Bit Decrement decfLO incfszLO,W goto$+2 decfHI l 8-Bit Decrement to 0xFF NOT_AT_FF : decfREG incfszREG,W gotoNOT_AT_FF AT_FF: 16-Bit Increment incfszLO goto$+2 incfHI 16-Bit Decrement decfLO incfszLO,W goto$+2 decfHI l 8-Bit Decrement to 0xFF NOT_AT_FF : decfREG incfszREG,W gotoNOT_AT_FF AT_FF:
3
S5704A Tips & Tricks 13 © 1999 Microchip Technology Incorporated. All Rights Reserved. S5704A Tips & Tricks 13 Tips and Tricks ComparisonsComparisons 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. addlw255-HIVAL addlw(HIVAL-LOVAL)+1 Compare and Swap Compare the values in registers X and Y. If Y = X? bc$+3;IF SO, JUMP AHEAD. addwfX;OTHERWISE, X = X + (Y-X) = Y, subwfY; AND Y = Y - (Y-X) = X. 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. addlw255-HIVAL addlw(HIVAL-LOVAL)+1 Compare and Swap Compare the values in registers X and Y. If Y = X? bc$+3;IF SO, JUMP AHEAD. addwfX;OTHERWISE, X = X + (Y-X) = Y, subwfY; AND Y = Y - (Y-X) = X.
4
S5704A Tips & Tricks 14 © 1999 Microchip Technology Incorporated. All Rights Reserved. S5704A Tips & Tricks 14 Tips and Tricks ComparisonsComparisons Minimum Enter with three values stored in registers N1, N2, and N3. Exit with min(N1,N2,N3) in MIN, N1-3 unchanged, W scrambled. movfN1,w subwfN2,W movfN1,W skpc movfN2,W movwfMIN subwfN3,W skpc addwfMIN Minimum Enter with three values stored in registers N1, N2, and N3. Exit with min(N1,N2,N3) in MIN, N1-3 unchanged, W scrambled. movfN1,w subwfN2,W movfN1,W skpc movfN2,W movwfMIN subwfN3,W skpc addwfMIN
5
S5704A Tips & Tricks 15 © 1999 Microchip Technology Incorporated. All Rights Reserved. S5704A Tips & Tricks 15 Tips and Tricks Bit-ManipulationBit-Manipulation Reverse 7 Bits Enter with 0ABCDEFG in W. Exits with 0GFEDCBA in W. movwfsource ;source = 0ABCDEFG. swapfsource,w ;W= DEFG0ABC. btfscsource,3 ;If D = 1, invert D and the “0”. xorlw0x88 ;After this line, W = 0EFGDABC. btfscsource,6 ;If A = 1, invert bits A and C. xorlw0x05 ; btfscsource,4 ;If C = 1, invert A and C again. xorlw0x05 ;After this line, W = 0EFGDCBA. btfscsource,2 ;Do the same with E and G. xorlw0x50 ; btfscsource,0 ; xorlw0x50 ;After this line, W = 0GFEDCBA. Reverse 7 Bits Enter with 0ABCDEFG in W. Exits with 0GFEDCBA in W. movwfsource ;source = 0ABCDEFG. swapfsource,w ;W= DEFG0ABC. btfscsource,3 ;If D = 1, invert D and the “0”. xorlw0x88 ;After this line, W = 0EFGDABC. btfscsource,6 ;If A = 1, invert bits A and C. xorlw0x05 ; btfscsource,4 ;If C = 1, invert A and C again. xorlw0x05 ;After this line, W = 0EFGDCBA. btfscsource,2 ;Do the same with E and G. xorlw0x50 ; btfscsource,0 ; xorlw0x50 ;After this line, W = 0GFEDCBA.
6
S5704A Tips & Tricks 16 © 1999 Microchip Technology Incorporated. All Rights Reserved. S5704A Tips & Tricks 16 Tips and Tricks Bit-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. rlfREG,W rlfREG Bit-Copy Copy bits from one register to the same position in another. movfSOURCE,W;The DEST bits in the positions to ;which we’re copying must not change xorwfDEST,W;between this xorwf instruction and ;the xorwf DEST below. andlw00000111B;A "1" in each bit-position we’re ;copying.. ;this example copies the three LSBs. xorwfDEST 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. rlfREG,W rlfREG Bit-Copy Copy bits from one register to the same position in another. movfSOURCE,W;The DEST bits in the positions to ;which we’re copying must not change xorwfDEST,W;between this xorwf instruction and ;the xorwf DEST below. andlw00000111B;A "1" in each bit-position we’re ;copying.. ;this example copies the three LSBs. xorwfDEST
7
S5704A Tips & Tricks 17 © 1999 Microchip Technology Incorporated. All Rights Reserved. S5704A Tips & Tricks 17 Tips and Tricks Bit-ManipulationBit-Manipulation 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. rrfREG,W andlw0x44 subwfREG movfREG,W andlw0x33 addwfREG rrfREG andlw0x11 addwfREG rrfREG swapfREG,W addwfREG,W andlw0x0F
8
S5704A Tips & Tricks 18 © 1999 Microchip Technology Incorporated. All Rights Reserved. S5704A Tips & Tricks 18 Tips and Tricks Bit-ManipulationBit-Manipulation Bit-Number [0-7] to Bitmask [00000001-10000000] ; Enter with bit number [0-7] in BITNUM. Exits with bit mask in W. movlw0x01 btfscBITNUM,1 movlw0x04 movwftemp btfscBITNUM,0 addwftemp btfscBITNUM,2 swapftemp movftemp,w Bit-Number [0-7] to Bitmask [00000001-10000000] ; Enter with bit number [0-7] in BITNUM. Exits with bit mask in W. movlw0x01 btfscBITNUM,1 movlw0x04 movwftemp btfscBITNUM,0 addwftemp btfscBITNUM,2 swapftemp movftemp,w
9
S5704A Tips & Tricks 19 © 1999 Microchip Technology Incorporated. All Rights Reserved. S5704A Tips & Tricks 19 Tips and Tricks Bit-ManipulationBit-Manipulation l Bit-Setting without Affecting W or Status Set to zero: clrfreg Set to a power of two: clrfreg bsfreg,bit Set to 255: clrfreg decfszreg Set a right-aligned group of bits: clrfreg;Set bits bsfreg,6;0-5. decfszreg; Set a left-aligned group of bits: clrfreg;Set bits decfszreg;4-7. bcfreg,4; incfszreg; General method, 2-4 bits set: clrf reg bsf reg,bit1 bsf reg,bit2 bsf reg,bit3 bsf reg,bit4 General method, 5-7 bits set: clrf reg decfsz reg bcf reg,unbit1 bcf reg,unbit2 bcf reg,unbit3
10
S5704A Tips & Tricks 110 © 1999 Microchip Technology Incorporated. All Rights Reserved. S5704A Tips & Tricks 110 Tips and Tricks DelaysDelays l 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
11
S5704A Tips & Tricks 111 © 1999 Microchip Technology Incorporated. All Rights Reserved. S5704A Tips & Tricks 111 Tips and Tricks DelaysDelays l Non-Constant Delay 20-271 Cycles SKIP: RRF COUNTER MOVLW 4 SUBWF COUNTER BCF COUNTER,6 BCF COUNTER,7 LOOP: NOP DECFSZ COUNTER GOTO LOOP RETURN MOVLW [NUMBER OF CYCLES] CALL DELAY.... ; DELAY 20-271 CYCLES. ENTER WITH ; NUMBER OF CYCLES TO DELAY IN W. ; ; DELAY IS INCLUSIVE OF "MOVLW", ; “CALL”, AND “RETURN” OVERHEAD. DELAY: MOVWF COUNTER BTFSC COUNTER, 0 GOTO $+1 BTFSS COUNTER, 1 GOTO SKIP NOP GOTO $+1
12
S5704A Tips & Tricks 112 © 1999 Microchip Technology Incorporated. All Rights Reserved. S5704A Tips & Tricks 112 Tips and Tricks DelaysDelays Trading Stack Space for RAM Delay131072: call Delay16384 Delay114688: call Delay16384 Delay98304: call Delay16384 Delay81920: call Delay16384 Delay65536: call Delay16384 Delay49152: call Delay16384 Delay32768: call Delay16384 Delay16384: call Delay2048 Delay14336: call Delay2048 Delay12288: call Delay2048 Delay10240: call Delay2048 Delay8192: call Delay2048 Delay6144: call Delay2048 Delay4096: call Delay2048 Delay2048: call Delay256 Delay1792: call Delay256 Delay1536: call Delay256 Delay1280: call Delay256 Delay1024: call Delay256 Delay768: call Delay256 Delay512: call Delay256 Delay256: call Delay32 Delay224: call Delay32 Delay192: call Delay32 Delay160: call Delay32 Delay128: call Delay32 Delay96: call Delay32 Delay64: call Delay32 Delay48: call Delay32 Delay32: call Delay4 Delay28: call Delay4 Delay24: call Delay4 Delay20: call Delay4 Delay16: call Delay4 Delay12: call Delay4 Delay8: call Delay4 Delay4: return
13
S5704A Tips & Tricks 113 © 1999 Microchip Technology Incorporated. All Rights Reserved. S5704A Tips & Tricks 113 Tips and Tricks MathMath l 16-Bit Subtract with Valid Carry-Out movfSOURCE_LO,W;DEST = DEST - SOURCE. subwfDEST_LO;Note that the Z flag is movfSOURCE_HI,W;invalid after the skpc;subtraction. incfszSOURCE_HI,W; subwfDEST_HI ; l 8-Bit Add with Valid Carry-In/Out movfSOURCE,W;DEST = DEST + SOURCE + CARRY skpnc;Carry is valid after the incfSOURCE,W;addition. addwfDEST;
14
S5704A Tips & Tricks 114 © 1999 Microchip Technology Incorporated. All Rights Reserved. S5704A Tips & Tricks 114 Tips and Tricks MathMath l 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: addwfSUMLO;SUM = SUM + SAMPLE. movfSUMHI,W;(AND WHILE WE'RE HERE, PUT THE OLD skpnc;AVERAGE IN W). incfSUMHI; subwfSUMLO;ADJUST THE SUM BY SUBTRACTING THE OLD skpc;AVERAGE FROM THE NEW SUM. decfSUMHI; movfSUMHI,W;W=THE NEW AVERAGE (ADJUSTED SUM/256) return;RETURN.
15
S5704A Tips & Tricks 115 © 1999 Microchip Technology Incorporated. All Rights Reserved. S5704A Tips & Tricks 115 Tips and Tricks MathMath l Tricks with Known-Zero Registers rlfKZ,W;DEST = DEST + SOURCE + CARRY. addwfSOURCE,W;Carry-out is only valid if SOURCE<255. addwfDEST; rlfKZ,W;DEST = DEST + 0 + CARRY. Carry-out is addwfDEST;valid. movlw255;DEST = DEST + 255 + CARRY. Carry-out skpc;is valid. addwfDEST; rlfKZ,W;DEST = DEST + constant + CARRY, where addlw[constant];0 < constant < 255. Carry-out is addwfDEST;valid.
16
S5704A Tips & Tricks 116 © 1999 Microchip Technology Incorporated. All Rights Reserved. S5704A Tips & Tricks 116 Tips and Tricks MathMath l Even 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. swapfX,W xorwfX rrfX,W xorwfX rrfX,W rlfX xorwfX
17
S5704A Tips & Tricks 117 © 1999 Microchip Technology Incorporated. All Rights Reserved. S5704A Tips & Tricks 117 Tips and Tricks MathMath 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 X,W XORWF X,W ANDLW 00001111B ADDWF PC GOTO ZERO GOTO ONE GOTO ZERO GOTO ONE GOTO ZERO GOTO ONE GOTO ZERO GOTO ONE GOTO ZERO GOTO ONE ZERO:.... ONE:....
18
S5704A Tips & Tricks 118 © 1999 Microchip Technology Incorporated. All Rights Reserved. S5704A Tips & Tricks 118 Tips and Tricks MPASM Tips EQU vs. #define equ1equ5+3;MPASM will replace future ;occurrences of "equ1" with the ;value 8. #define def15+3;MPASM will replace future ;occurrences of "def1" with the ;string"5+3". xequ3*equ1;This line is equivalent to "x ;equ 3*8", so x = 24. yequ3*def1;THIS line, on the other hand, ;is equivalent to "y equ 3*5+3", ;so y = 18.
19
S5704A Tips & Tricks 119 © 1999 Microchip Technology Incorporated. All Rights Reserved. S5704A Tips & Tricks 119 Tips and Tricks MPASM Tips Accessing Registers Above 0x7F Method #1: REGEQUxxx - 128;Generates no warnings, but also....;doesn’t allow REG to be viewed MOVWFREG;in MPLAB Watch Windows. Method #2: MOVWFREG;Generates an MPASM warning. Method #3: MOVWFREG & 0x7F;Generates no warning, even when ;accidentally used with a ;page-0 register. Method #4: MOVWFREG ^ 0x80;Generates no warning when used ;correctly, but does generate a ;warning if accidentally used ;with a page-0 register.
20
S5704A Tips & Tricks 120 © 1999 Microchip Technology Incorporated. All Rights Reserved. S5704A Tips & Tricks 120 Tips and Tricks MPASM Tips l HIGH-and-LOW vs. Shift-and-AND BIGNUMEQU0x123456 LSBEQULOW(BIGNUM);’LSB’ will always be set to ;0x56. MSBEQUHIGH (BIGNUM);Some versions of MPASM will ;set "MSB" to 0x12; others ;will set it to 0x34. SAFEMSBEQU(BIGNUM>>16) & 0xFF ;This method will always set ;"SAFEMSB" to 0x12.
21
S5704A Tips & Tricks 121 © 1999 Microchip Technology Incorporated. All Rights Reserved. S5704A Tips & Tricks 121 Tips and Tricks TimingTiming l Exact Sync to the TMR0 Prescaler ; Here, TMR0:PRE must be 01:3 or 02:0 BTFSS TMR0,BIT1 GOTO $+1 ; Here, TMR0:PRE must be 02: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 there’s 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 RELOAD+4 MOVWF TMR0 ; Enter this section with TMR0 ; between 128 and 255, inclusive, ; and the prescaler divide-by- ; ratio set to divide-by-4. WAITFOR0: BTFSC TMR0,BIT7 GOTO WAITFOR0 ; Here, TMRO:PRESCALER can be ; 00:2 or 00:3 or 01:0. BTFSS TMR0,BIT0 GOTO $+1 ; Here, TMR0:PRE must be 01:1 ; or 01:2. GOTO $+1
22
S5704A Tips & Tricks 122 © 1999 Microchip Technology Incorporated. All Rights Reserved. S5704A Tips & Tricks 122 Tips and Tricks TimingTiming 16-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
23
S5704A Tips & Tricks 123 © 1999 Microchip Technology Incorporated. All Rights Reserved. S5704A Tips & Tricks 123 Tips and Tricks Hardware Interfacing l Zero 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.
24
S5704A Tips & Tricks 124 © 1999 Microchip Technology Incorporated. All Rights Reserved. S5704A Tips & Tricks 124 Tips and Tricks l Setting ADCON0: Clock Select bits l 4 A/D clock (Tad) selections : l Fosc/2, Fosc/8, Fosc/32 and Frc(internal RC) l Tad Min. = 1.6 uS for V REF => 3.0V or 3.0uS for V REF < 3.0V For fastest A/D response, calculate the Tad which is closest to the Minimum value for Tad Example: V REF = 5.0V, Fosc = 4.0 MHz l 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. l For fastest response, select Fosc = 5.0 MHz (Tad = 1.6uS) instead of 4.0Mhz (Tad = 2.0 uS). Hardware Interfacing
25
S5704A Tips & Tricks 125 © 1999 Microchip Technology Incorporated. All Rights Reserved. S5704A Tips & Tricks 125 Tips and Tricks l Setting ADCON0: GO/DONE l To 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 l On 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”. btfscADCON0,DONE goto$-1 Time for check = 3Tcycles l Checking 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 Hardware Interfacing
26
S5704A Tips & Tricks 126 © 1999 Microchip Technology Incorporated. All Rights Reserved. S5704A Tips & Tricks 126 Tips and Tricks l A/D Conversion in Sleep l Reduces digital noise during A/D conversion. l Enable Frc mode for Tad l Enable or disable A/D Interrupt l Start A/D conversion and goto sleep l On A/D Completion: l If interrupt is enabled then CPU wakes up from sleep l If interrupt is disabled then CPU does not wake up from sleep, but A/D circuit is turned OFF (ADON is still set). l In Crystal mode conversion in sleep is slow since most crystals take 2 mS or more to wake-up l For fast conversion in sleep, use RC oscillator Hardware Interfacing
27
S5704A Tips & Tricks 127 © 1999 Microchip Technology Incorporated. All Rights Reserved. S5704A Tips & Tricks 127 Tips and Tricks MiscellaneousMiscellaneous l Direct Modification of PC (or PCL) For example, to calculate 2^W, where W is in the range [0-7]: TBL 1,2,4,8,16,32,64,128 assembles to: ADDWF PCL ADDLW 0 ADDLW -1 ADDLW -3 ADDLW -7 ADDLW -15 ADDLW -31 ADDLW -63 ADDLW 121 TBL:MACRO A,B,C,D,E,F,G,H LOCAL X ADDWF PCL X:ADDLW HIGH (A)+1 - HIGH (B) ADDLW HIGH (B)+1 - HIGH (C) ADDLW HIGH (C)+1 - HIGH (D) ADDLW HIGH (D)+1 - HIGH (E) ADDLW HIGH (E)+1 - HIGH (F) ADDLW HIGH (F)+1 - HIGH (G) ADDLW HIGH (G)+1 - HIGH (H) ADDLW HIGH (H)-7 IF ((HIGH $) != (HIGH X)) ERROR "TABLE CROSSES PAGE” ERROR “BOUNDARIES! DO” ERROR “SOMETHING!” ENDIF ENDM
28
S5704A Tips & Tricks 128 © 1999 Microchip Technology Incorporated. All Rights Reserved. S5704A Tips & Tricks 128 Tips and Tricks MiscellaneousMiscellaneous "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...
29
S5704A Tips & Tricks 129 © 1999 Microchip Technology Incorporated. All Rights Reserved. S5704A Tips & Tricks 129 Tips and Tricks ResourcesResources 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 www.microchip.com l Embedded Control Handbook 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 www.microchip.com l Embedded Control Handbook
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.