Low Pin Count Demo Board User’s Guide
DS51556A-page 30 © 2005 Microchip Technology Inc.
Restore Context
Once the peripheral is serviced, it needs to restore the context and resume the main
program. Restoring the context is a little harder than it might seem at first. The obvious
method doesn’t work because the MOVF W_Temp,w may affect the Z flag, which was
restored in the previous instruction. Instead, a pair of SWAPF instructions can restore
Wreg without affecting the flags in the Status register. SWAPF exchanges the high and
low nibbles. The first SWAPF switches the nibbles in the file register and the second one
switches them back and puts the result in Wreg.
EXAMPLE 3-9: CONTEXT RESTORE
Finally, RETFIE transfers control back to the original program and sets the GIE bit,
re-enabling interrupts.
FIGURE 3-10: SWAPF INSTRUCTION
;incorrect context restore
MOVF STATUS_Temp,w
MOVWF STATUS
MOVF W_Temp ;this may change the Z bit
;in the Status register
;good context restore
MOVF STATUS_Temp,w
MOVWF STATUS
SWAPF W_Temp,f ;swap in place
SWAPF W_Temp,w ;swap with Wreg destination
Before
After
1 0 1 0 0 0 1 1
1 0 1 00 0 1 1
LPC Demo Board Lessons
© 2005 Microchip Technology Inc. DS51556A-page 31
3.2.11 Lesson 11: Indirect Data Addressing
The FSR (File Select Register) allows the specifying of a file register address. A
subsequent read or write to the INDF (Indirect File register) refers to the file register
addressed by the FSR.
This may be used to implement a moving average filter. The moving average keeps a
list of the last n values and averages them together. The Filter needs two parts: A
circular queue and a function to calculate the average.
FIGURE 3-11: MOVING AVERAGES
Calculating averages in a mid-range PICmicro is best accomplished by using the FSR
to keep track of where the next value will be inserted. This ensures the oldest value is
always overwritten with the newest and doesn’t waste time moving values within the
memory.
EXAMPLE 3-10: FILE SELECT REGISTER EXAMPLE
Time
Average
n 105 102 101 104 99 103 105 107 103
n + 1 106 105 102 101 104 99 103 105 103
n + 2 110 106 105 102 101 104 99 103 104
Newest value inserted here
The rest move down one
Conceptual View
Implementation View
Time
Average
n 107 105 101 104 99 101 102 105 103
n + 1 106 105 102 101 99 101 102 105 103
n + 2 106 110 103 99 99 101 102 105 104
Pointer to oldest value
Older value overwritten, pointer advanced
Pointer advanced
;insert new value into a queue, enter with new value in
;Wreg
MOVF temp ;save the latest value
MOVF QueuePointer,w
MOVWF FSR ;load FSR with the queue pointer
MOVF temp,w
MOVWF INDF ;Write the latest value to the queue
Low Pin Count Demo Board User’s Guide
DS51556A-page 32 © 2005 Microchip Technology Inc.
Lesson 11 adds a Moving Average Filter to the Analog-to-Digital code in Lesson 4.
Twisting the potentiometer changes the value read by the Analog-to-Digital. The filtered
value is then sent to the LED display. The filter only runs every 0.2 seconds to slow
down the display changes and make it visible. The display appears to count from the
old potentiometer position to the new position.
The filter averages the last 8 readings. Choosing a power of two for the number of
samples allows division by simple rotates instead of longhand.
Rather than summing the array every time, it’s faster to keep a running sum, then
subtract out the oldest value in the queue and adding in the new value.

DM164120-1

Mfr. #:
Manufacturer:
Microchip Technology
Description:
Development Boards & Kits - PIC / DSPIC PICkit 2 Low Pin Cnt Demo Board
Lifecycle:
New from this manufacturer.
Delivery:
DHL FedEx Ups TNT EMS
Payment:
T/T Paypal Visa MoneyGram Western Union

Products related to this Datasheet