## How to debounce in xmega?

4 posts / 0 new
Author
Message
```void row1_col1()
{

PORTC.OUTSET = 0x20;
PORTC.OUTCLR = 0x0F;
PORTC_OUTCLR = 0xD0;

if(((PORTC.IN & 0x01)==0b00000001)&&((PORTC.IN & 0x2F)==0b00100000)) //1
{

}

}
```

This is a part of the matrix keypad code for xmega a3bu, I'm not understanding how to write a debounce logic for the same.

Nitin Shenoy

Just read at a regular interval, in the range of a few milliseconds. When you get some number in a row with the same value, then it is not bouncing.

There are other ways, of course. But I always have some kind of "clock" ticking so that works just fine for me.

Jim

Jim Wagner Oregon Research Electronics, Consulting Div. Tangent, OR, USA http://www.orelectronics.net

As Jim says the technique for debounce is pretty much the same whatever the micro (which is why Jack Ganssle was able to write the renowned papers that show the common solutions). The essence of it is that you need "temporal sampling". That is something that is going to read the input states at fixed time intervals and compare the "history" of the input over a few samples to see if it's been seen in a stable state for a while. To do that implies the use of a timer. So folks typically setup a timer interrupt that occurs every few milliseconds and each time it checks the input pins and records their history. If several recent samples are all the same state the pin can be considered to have settled into that state.

nitinjs wrote:

This is a part of the matrix keypad code for xmega a3bu, I'm not understanding how to write a debounce logic for the same.

How I do it is to split the problem into two parts...

2) Debounce the result

What I do, for a 4x4 keypad, is read the keypad and put the result into a 16-bit variable where each bit corresponds to a specific key. I then debounce that value using 'vertical counters'.

#1 This forum helps those that help themselves

#2 All grounds are not created equal

#3 How have you proved that your chip is running at xxMHz?

#4 "If you think you need floating point to solve the problem then you don't understand the problem. If you really do need floating point then you have a problem you do not understand." - Heater's ex-boss