>-----Original Message----- >From: Mike Mansheim [mailto:Michael_J_Mansheim@GRACO.COM] >Sent: Thursday, May 16, 2002 12:12 PM >To: PICLIST@MITVMA.MIT.EDU >Subject: Re: [PIC]: S/W deboucing > > > - happens every 1 ms via a timer based interrupt (but I don't like > stuff like this in the actual isr) > - variables: BUTTON_STATE, DEBOUNCE_COUNTER > - use BUTTON_STATE, not PORTB.0 for any button based decisions in > the rest of your code > >if (PORTB.0 not equal to BUTTON_STATE): increment DEBOUNCE_COUNTER >else: DEBOUNCE_COUNTER =3D 0 > >if (DEBOUNCE_COUNTER equal 50): toggle BUTTON_STATE DEBOUNCE_COUNTER =3D 0 For another approach... perhaps the timing of the input is important, and is being=20 delayed by the debounce routine (most do). The following routine responds immediately, but then refuses to respond again until after a preset time. This also makes a dandy "auto-repeat" function.. It is NOT a one-shot. (Holding the input ON longer than dblaps will CREATE a repeating input) I didn't bother with "real" timing or interrupts, I'm just counting laps through the main loop. This may or may not be good for your application. It should be easy enough to change it to interrupt-driven. Alas, this only tells that the button is pressed, it does not detect the release of the input. ****Warning, incomplete code, designed only to illustrate a point**** Variable: debounce ;Our "lap counter" variable #define dblaps 200 ;Preset value? dbinput ;call here once every loop=09 MOVF debounce,0 ;get debounce counter BTFSC STATUS,Z ;test for zero GOTO checkin ;Is zero, check input DECFSZ debounce ;decrement counter RETURN ;not ready for new input checkin ;counter is zero, check input BTFSS PORT,BIT ;Read Input RETURN ;Bit not set, return MOVLW dblaps ;get debounce preset MOVWF debounce ;Store it away ;;*************************************************** ;;***** Add code to respond to valid input here ;;*************************************************** RETURN ;Exit, stage Left=09 I'm sure there are many ways to improve this example. If you'll take a set value of 255 for the dblaps, you could replace the MOVLW and MOVWF with a DECF debounce,1 I am still pretty new at this. As I push the "Send" button, I brace myself for the onslaught of corrections, revisions, and theological interpretations of my few lines of code. (Notice I didn't reveal how the variable was allocated!) I'm learning.. Feel free to point out my errors. Lyle Hazelwood -- http://www.piclist.com hint: To leave the PICList mailto:piclist-unsubscribe-request@mitvma.mit.edu