On Sun, 2006-01-01 at 23:44 -0500, Josh Koffman wrote: > Hi Scott, > > What do you think the best way to combine your debounce routine with > my matrix scanning routine would be? I'm thinking I would setup my > routine to scan and then dump the output in 8 bytes (64 switches). > Then I could call your routine to debounce those input bytes. I will > have to do a bit of work to the variables so that the various passes > don't stomp on each other. Perhaps I should use some indirect > addressing. Thoughts? Hi Josh, My routine requires 3-bits per switch, or 4-bits if you wish to track when a switch changes states (which is something you requested in your original post). You have 64 switches, so 64*4 = 256 bits = 32 bytes are required. I don't think you need to bother saving the raw, scanned values. The de-bounce routine is so short that you can just call it whenever a scanned value is obtained. I think the easiest approach is to turn the routine into a macro and in-line it with your scan code. Unroll the scan loop to simplify things even more. I don't know the details of your code, but you probably can even write a macro to scan your matrix. mMatrixScan 0 mDeBounce PORTB, clock_A0, clock_B0, cva_0 movwf changes0 mMatrixScan 1 mDeBounce PORTB, clock_A1, clock_B1, cva_1 movwf changes1 .... mMatrixScan 7 mDeBounce PORTB, clock_A7, clock_B7, cva_7 movwf changes7 The mMatrixScan macro scans one group of 8 switches. Here it's assumed that PORTB contains the un-filtered switch state. The mDeBounce macro filters one group of 8 switches. Its inputs are the variables needed for the algorithm. At the end of the macro W will contain the change of state. And this is stored in the 'changesX' variable. Here's the routine in macro form mDeBounce macro _csa, _count_A, _count_B, _cva ;Increment the vertical counter MOVF _count_B,W XORWF _count_A,F COMF _count_B,F ;See if any changes occurred MOVF _csa,W XORWF _cva,W ;Reset the counter if no change has occurred ANDWF _count_B,F ANDWF _count_A,F ;If there is a pending change and the count has ;rolled over to 0, then the change has been filtered XORLW 0xff ;Invert the changes IORWF _count_A,W ;If count is 0, both A and B IORWF _count_B,W ;bits are 0 ;Any bit in W that is clear at this point means that the input ;has changed and the count has rolled over. XORLW 0xff XORWF _cva,F endm If you're using an 18F device, then it would be reasonable to use indirect addressing. For the mid-range family, this routine will get a little unwieldy with indirect addressing and double in size. Scott -- http://www.piclist.com PIC/SX FAQ & list archive View/change your membership options at http://mailman.mit.edu/mailman/listinfo/piclist