Email: Password: Remember Me | Create Account (Free)

Back to Subject List

Old thread has been locked -- no new posts accepted in this thread
???
06/24/06 15:16
Read: times


 
Msg Score: +3
 +3 Informative
#119013 - Multiple switch debouncing
Arvind said:
The program written below should work as follows : ... switches

are connected to PORT 1 and LED;s are connected to PORT 2 ...

on hitting any switch the corresponding LED should bre ON

ie ... if we press switch 0 LED 0 should ON ... if agin we

press switch 0 then the LED 0 should become OFF ..


How to debounce 8 switches at the same time?

If you have a look, what the micro reads at Port1 when the switches are pressed and the port lines are sampled every 10msec, then you will see the following (a bit idealized):

..1111111110101000000000000000000000011010111111111111111111..
           ^                         ^
        pressed                   released

This assumes, that the Port1 lines are pulled high by the internal or an external pull-up and are pulled-low by the switches.

This plot means: As long as the switch isn't pressed, the micro reads "1". When the switch is pressed, then it will read a "0", which is indicated by the first "^". Afterwards you see some tooglings, which result from contact bouncing. After the debouncing time, which varies between a few milliseconds and about 50msec, depending on quality and age of switch contact, the micro reads many "0" up to the moment, when the switch is released, which is indicated by the second "^". Then, the micro reads "1" again. But due to contact bouncing during switch release again some low to high and high to low togglings can be seen. After the debouncing time the micro reads "1" all the time again.

The idea of the following debouncing scheme is now, to detect the moment, when the micro reads "0" for the first time after reading some "1" in a row. If the number of needed "1" readings to accept a switching is larger than the maximum debouncing time, then an effective debouncing can be achieved. Here five "1" readings in a row followed by one "0" is needed to detect the pressing of switch. So, the switch must have been released for at least 50msec, as every 10msec a sample is taken.

But how to know about the last samplings up to 50msec ago?
We must store them of course in the RAM of micro. This is done here in the registers r2-r7: When a new sample is taken, the older ones are shifted by one step, r6 to r7, r5 to r6, r4 to r5, r3 to r4, r2 to r3 and the new sample is stored into r2. The sample in r7 is lost, of course.

As this shiftings must be done every 10msec, they must be a part of the debouncing code snippet. The according code lines are:

mov r7,r6       ;save r6
mov r6,r5       ;save r5
mov r5,r4       ;save r4
mov r4,r3       ;save r3
mov r3,r2       ;save r2

mov a,p1        ;take a new sample
mov r2,a        ;store it in r2

Remember, this is only a simple example, not ready code! You can store the samples elsewhere, of course, if you want. And if you want to expand the number of samples, then you can use indexed addressing and a loop incrementing the index.

The whole code uses one register set r0-7, so the original values from the main program must be saved and/or bank switching must be used. That you must manage, this is only a code snippet explaining the details of debouncing scheme.

The debouncing scheme needs to fabricate the AND function of r7, r6, r5, r4, r3 and the complement of actual sample which is in r2. Only when the bits in r3-7 are "1" and the bit in r2 is "0", the pressing of switch is detected and the result of these many ANDs is "1". This result is stored into r0, because we will need it later again. So, the code snippet looks like that, up to now:

mov r7,r6       ;save r6
mov r6,r5       ;save r5
mov r5,r4       ;save r4
mov r4,r3       ;save r3
mov r3,r2       ;save r2

mov a,p1        ;take a new sample
mov r2,a        ;store it in r2

cpl a           ;complement the last sample
anl a,r7        ;AND sample in r7 to it
anl a,r6        ;AND sample in r6 to it
anl a,r5        ;AND sample in r5 to it
anl a,r4        ;AND sample in r4 to it
anl a,r3        ;AND sample in r3 to it
mov r0,a        ;save the result in r0

So, whenever a switch is pressed, fullfilling the condition that the switch was released for at least 50msec immediately before, then the according bit of the result of expanded AND operation stored in r0 is "1".

Now we need code, which makes the according bit in P2 to toggle, whenever the switch was "successfully" pressed. For this we fabricate a little table: When the bit in r0 is "0", means when the switch wasn't pressed, then the content of P2 need no change. But when the bit of r0 is "1", then the according bit in P2 must be complemented:

r0  old P2   new P2
-------------------
0      0        0
0      1        1
1      0        1
1      1        0

This function looks like EXOR, which the 80C52 cannot perform directly. But we can do it by using an OR, a NAND and an AND operation. So, the code snippet becomes:

mov r7,r6       ;save r6
mov r6,r5       ;save r5
mov r5,r4       ;save r4
mov r4,r3       ;save r3
mov r3,r2       ;save r2

mov a,p1        ;take a new sample
mov r2,a        ;store it in r2

cpl a           ;complement the last sample
anl a,r7        ;AND sample in r7 to it
anl a,r6        ;AND sample in r6 to it
anl a,r5        ;AND sample in r5 to it
anl a,r4        ;AND sample in r4 to it
anl a,r3        ;AND sample in r3 to it
mov r0,a        ;save the result in r0

orl a,p2        ;r0 OR P2
mov r1,a        ;save the result in r1, we need it later
mov a,r0        ;we need r0 again
anl a,p2        ;r0 AND P2
cpl a           ;complement the result to perform NAND
anl a,r1        ;(NAND result) AND (OR result) to perform EXOR

mov p2,a        ;actualize P2

This code snippet must be executed every 10msec, invoked by a timer interrupt, for instance. If your main program has nothing to do, you can also poll the timer.

Kai

List of 27 messages in thread
TopicAuthorDate
Multiple switch debouncing            01/01/70 00:00      
   Otherways            01/01/70 00:00      
   Vertical counters            01/01/70 00:00      
      vertical conters in C            01/01/70 00:00      
      and here            01/01/70 00:00      
   vertical counter with press detection            01/01/70 00:00      
   debounce and denoise needed            01/01/70 00:00      
      You are right of course, but...            01/01/70 00:00      
      Reminds me... (off topic)            01/01/70 00:00      
   XRL P2,A            01/01/70 00:00      
      some hair-splitting            01/01/70 00:00      
         exchange            01/01/70 00:00      
   thanx Kai ...            01/01/70 00:00      
      my way            01/01/70 00:00      
         exactly!            01/01/70 00:00      
            Spot on.            01/01/70 00:00      
            Two samples enough?            01/01/70 00:00      
               varies with design            01/01/70 00:00      
      Conditional executing versus branching            01/01/70 00:00      
   KEY RELEASE SUBROUTINE ...            01/01/70 00:00      
      Not really...            01/01/70 00:00      
      a TOTAL misconception or -very annoying            01/01/70 00:00      
         He does not            01/01/70 00:00      
   Beer o'clock            01/01/70 00:00      
      Yes, good idea            01/01/70 00:00      
      never did, never will            01/01/70 00:00      
         Agree            01/01/70 00:00      

Back to Subject List