At 03:22 PM 2/16/00 -0600, Scott wrote: > > ;copyright 1999 by robert a labudde, all rights reserved > >If you copy right it, you gotta make it the fastest. Right? :) Wrong! Simply the best ... Cycles don't matter for sure in a 20 ms debounce routine. I also usually have program memory 'to burn' (good pun, huh?), so a few bytes don't count either. Mostly my criteria are flexibility and clarity. I copyright everything as a matter of course. Most people don't know that the author owns the copyright even without the comment. You just have to notify the user before taking legal action. > > Debounce: > > movf DEBOUNCE_PORT,w ;get current port value > > andlw DEBOUNCE_MASK ;strip desired bits only > > xorwf DEBOUNCE_OLD,w ;find still changed bits > > andwf DEBOUNCE_BITS,f ;keep only changed ones > > xorlw h'ff' ;complement w > > andwf DEBOUNCE_OLD,w ;find unchanged bits > > iorwf DEBOUNCE_BITS,f ;merge with debounced > > return > >Where does DEBOUNCE_OLD get updated? Whoops! I wrote this routine in Nov. 1999 based on a QuickBasic test program version. I haven't used the routine yet in a PIC. I apparently dropped a line in transcription. I've attached a corrected version of the ASM, and also a copy of the QuickBasic program to show how I test logic and math programs. It works great, and it's interactive. I haven't got around to checking your modifications yet, but they look interesting. The chief advantages seem to be slightly shorter code. What's important to me, though, is that the steps mimic my thinking, so I can eventually debug or change it. If it's too tricky, it may result in errors at the boundary conditions. Thanks for the critique. '11.30.99 18.30 DEBOUNCE.BAS 'copyright 1999 by robert a labudde, all rights reserved 'debounce 8 bit port in 7 instructions (PIC) 'NOTES: iDebounced will give debounced state. This will only be updated ' if changed bits persist over 2 call cycles. If only occur on 1 ' cycle, then bits remain unchanged. ' It is important that do not call loop more often than 2x the ' required debounce time T. 'created: 11.30.99 by r.a. labudde 'changes: DEFINT I-N DECLARE FUNCTION BinToAscii$ (iBin%) DEF fnhex2$ (i) = RIGHT$("00" + HEX$(i), 2) iPort = 0 'initial state iOld = iPort 'copy iDebounced = iPort 'debounced version iMask = &H3 'only first two bits t$ = " PORT W OLD DEBOUNCED" u$ = "\ \ \ \ \ \ \ \" CLS PRINT t$ PRINT InputLoop: REM this loop would be executed every T/2 ms, where T is interval INPUT "iPort: ", iPort iW = iPort 'tranfer to working register iW = (iW AND iMask) 'only relevant bits iW = (iW XOR iOld) 'find different bits iDebounced = (iW AND iDebounced)'set different bits iOld = (iW XOR iOld) 'flip changed bits iW = (iW XOR 255) 'complement changed bits = unchanged bits iW = (iW AND iOld) 'merge with iOld iDebounced = (iW OR iDebounced) 'merge with debounced PRINT USING u$; BinToAscii$(iPort); BinToAscii(iW); BinToAscii$(iOld); BinToAscii(iDebounced) GOTO InputLoop END FUNCTION BinToAscii$ (iBin) a$ = "" iNum = iBin iDiv = 128 FOR i = 8 TO 1 STEP -1 iBit = iNum \ iDiv IF iBit = 1 THEN iNum = iNum - iBit * iDiv a$ = a$ + "1" ELSE a$ = a$ + "0" END IF iDiv = iDiv \ 2 NEXT i BinToAscii$ = a$ END FUNCTION ;02.17.00 15.40 DEBOUNCE.ASM ;copyright 1999,2000 by robert a labudde, all rights reserved ;debounce routine for pic ucontroller ;created: 11.30.99 by r.a. labudde ;changes: 02.17.00 ral, fix DEBOUNCE_OLD update ; ; ;includes: ;----------------------------- CONSTANTS ; DEBOUNCE_PORT = port to debounce ; DEBOUNCE_MASK = and mask for bits in port ;----------------------------- VARIABLES ; DEBOUNCE_OLD = register for old state ; DEBOUNCE_BITS = latest debounced bits ;-----------------------------SUBROUTINES ;............ Initialize debounce variables (1x) DebounceInit: movf DEBOUNCE_PORT,w ;get current port and andlw DEBOUNCE_MASK ;get only bits needed movwf DEBOUNCE_OLD ;save in old and movwf DEBOUNCE_BITS ;and debounced return ;............ Debounce routine @ 1/2 debounce interval. Call every T/2. Debounce: movf DEBOUNCE_PORT,w ;get current port value andlw DEBOUNCE_MASK ;strip desired bits only xorwf DEBOUNCE_OLD,w ;find still changed bits andwf DEBOUNCE_BITS,f ;keep only changed ones xorwf DEBOUNCE_OLD,f ;complement changed bits in copy xorlw h'ff' ;complement changed bits & unchanged bits andwf DEBOUNCE_OLD,w ;merge with copy iorwf DEBOUNCE_BITS,f ;merge with debounced return ================================================================ Robert A. LaBudde, PhD, PAS, Dpl. ACAFS e-mail: ral@lcfltd.com Least Cost Formulations, Ltd. URL: http://lcfltd.com/ 824 Timberlake Drive Tel: 757-467-0954 Virginia Beach, VA 23464-3239 Fax: 757-467-2947 "Vere scire est per causae scire" ================================================================