Low Pin Count Demo Board User’s Guide
DS51556A-page 24 © 2005 Microchip Technology Inc.
One of the simplest ways to switch debounce is to sample the switch until the signal is
stable or continue to sample the signal until no more bounces are detected. How long
to continue sampling requires some investigation. However, 5 mS is usually plenty
long, while still reacting fast enough that the user won’t notice it.
Lesson 6 shows how to sample the line at a 1 mS rate waiting for a number of
sequential state changes, which is a simple matter of counting to 5, then resetting the
counter every time it’s still in the original unchanged state.
The Switch on the LPC Demo Board doesn’t bounce much, but it is good practice to
debounce all switches in the system.
FIGURE 3-6: SIMPLE SWITCH DEBOUNCE PROGRAM FLOW
Increment Counter
No
No
Reset Counter
Change State
Reset Counter
Is Counter = 5?
Switch has
changed states?
Yes
Yes
Delay 1 mS
LPC Demo Board Lessons
© 2005 Microchip Technology Inc. DS51556A-page 25
3.2.7 Lesson 7: Reversible Variable Speed Rotate
Lesson 7 combines Lessons 5 and 6 using the button to reverse the direction of rotation
when the button is pressed and adjusting the potentiometer to control the speed of
rotation.
The program needs to keep track of rotation direction and new code needs to be added
to rotate in the other direction.
Lesson 5 rotates right and checks for a ‘1’ in the Carry bit to determine when to restart
the sequence. In Lesson 7, we’ll also need to rotate left and check for a ‘1’ in bit 4 of
the display. When the1’ shows up in bit 4 of the display, re-insert it into the bit 0
position.
EXAMPLE 3-6: REVERSIBLE VARIABLE SPEED ROTATE EXAMPLE
Original Version:
Rotate
RRF Display,f
BTFSC STATUS,C ;Did the bit rotate into the carry?
BSF Display,3 ;yes, put it into bit 3.
Bidirectional Version:
Rotate
BCF STATUS,C ;ensure the carry bit is clear
BTFSS Direction,0
GOTO RotateLeft
RotateRight
RRF Display,f
BTFSC STATUS,C ;Did the bit rotate into the carry?
BSF Display,3 ;yes, put it into bit 3.
GOTO MainLoop
RotateLeft
RLF Display,f
BTFSC Display,4 ;did it rotate out of the display
BSF Display,0 ;yes, put it into bit 0
GOTO MainLoop
Low Pin Count Demo Board User’s Guide
DS51556A-page 26 © 2005 Microchip Technology Inc.
3.2.8 Lesson 8: Function Calls
Lesson 8 shows the reversible LEDs but with the Delay Loop rewritten as a function.
New Instructions
CALL Invokes functions or subroutines
RETURN Terminates functions or subroutines
RETLW Terminates functions or subroutines
Functions or Subroutines are invoked with the CALL instruction and terminated with a
RETURN or RETLW instruction. RETURN jumps back to the original program at the
location following the CALL. RETLW also returns to the calling program, but loads Wreg
with a constant.
The mid-range PICmicro MCU device’s CALL stack can hold up to 8 return addresses.
If a ninth CALL is made, it will overwrite the first one and then the program will not be
able to RETURN all the way back.
Passing Arguments
Arguments to the subroutine may be passed in a number of ways. Wreg is a convenient
place to pass one byte and the FSR may be used to pass another byte, if not otherwise
used. If more data must be passed, a buffer must be allocated.
When the Delay function is pulled out to a subroutine, the ADC result is moved into
Wreg, then the CALL transfers control to the Delay subroutine. The RETURN transfers
control to the MOVLW following the CALL.
EXAMPLE 3-7: FUNCTION CALL EXAMPLE
MOVF ADRESH,w
CALL Delay ;call delay function
;returns here when done
...
GOTO xxx
; Delay function.
; Delay time is Wreg value * 771 uS
Delay
MOVWF Delay2
DelayLoop
DECFSZ Delay1,f
GOTO DelayLoop
DECFSZ Delay2,f
GOTO DelayLoop
RETURN

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