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

Back to Subject List

Old thread has been locked -- no new posts accepted in this thread
???
08/26/08 20:04
Read: times


 
#157780 - Simplify and think about debounce
Responding to: ???'s previous message
1) Avoid super-long source lines. How will you print them? How easy are they to read? How easy is it to asnwer your post?

2) Indent with spaces instead of tabs, to simplify printing and posting. Not all programs defaults to the same width of a tab, so with hard tabs, the look of your source will vary with used tool.

3) Don't skip braces when initializing multi-level arrays. How easy is it to figure out which entry is missing, if you end up with only 63 values in the list?

4) Replace the empty for loops in the delay function with a loop that looks at a timer. The compiler can throw away the empty loops, since they don't mean anything. It may further throw away the call to the function, since it is obvious that the function doesn't produce any side effect. Even if the compiler do not throw away the function call, or the for loops in the function, the time needed for the loops will greatly vary with optimization level, and with version of compiler.

5) Why so long code?
do {
    row = 0x00;
    colloc = col;
} while (colloc != 0xff); //check untill all key release

instead of:
row = 0x00;
while (col != 0xff) ; // wait until no key pressed


6) Why a dual loop?
do {
    do {
        MSDelay(20);
        colloc = col;
    } while (colloc == 0xff);
    MSDelay(5);
    colloc = col;
} while (colloc == 0xff);

instead of:
while (col == 0xff) ; // wait until at least one key pressed


7) What do you think the following code does?
while(1) {
    row = _crol_(temp,i);
    colloc = col;
    if (colloc != 0xff) {
        rowloc = i;
        i = 0;
        break;
    }
    i++;
}

You enter it with the possibility that a key is pressed. But because of bounces, the press may be the so short that you will not pick it up in this loop. But your loop is infinite, and you let i continue to increment i forever. What will happen with 'row' when i continues above 7?

Maybe keep i within the valid range with: i = (i+1) & 7;

And why not join this loop with the previous loop - the one waiting for a key? After all, they are basically doing the same thing.

Another thing: You may need a short delay after assign of 'row' until you read the column values.

And if you need to know if more than one button is pressed, then you can't break when you find one row with at least one key pressed. You have to continue and scan all 8 rows and verify that you have one-and-exactly-one row with a key. And you then have to verify that you have one-and-exactly-one column with a key.

Or write the code to support n-key roll over.

For single-key handling you can rewrite the code to:
for (;;) {
    colloc = col;
    if (colloc != 0xff) break;
}
for (i = 0,count = 0; i < 8; i++) {
    row = 1 << i;
    MSDelay(5);
    if (col != 0xff) {
        rowloc = i;
        count++;
    }
}
if (count == 1) {
    // Only one row had a pressed key. Now time to process column values.
    ...
}


8) Duplicated code may look simple, but is harder to maintain.
if (colloc == 0xfe) {
    key = key_matrix[rowloc][0];
    return key;					
} else if (colloc == 0xfd) {
    key = key_matrix[rowloc][1];
    return key;
} else if (colloc == 0xfb) {
    ...
}

can be replaced with a for loop, shifting a bit one step at a time and comparing. 0xfe == ~0x01. 0xfd == ~0x02. 0xfb == ~0x04.

And
key = key_matrix[rowloc][0];
return key;

is just
return key_matrix[rowloc][0];


9) You have called your function key_scan, so I assume that you have separated key debounce and key scanning into two functions. In that case, the key_scan function shuold not wait until no keys are pressed and then until a key is pressed. It should read the current state "now". And then let the calling function bother about deciding if there was any key pressed and if the same value has been pressed for long enough time.

If on the other hand this is your one-and-only function: Where is the debounce code?

List of 15 messages in thread
TopicAuthorDate
problem in 8x8 keypad            01/01/70 00:00      
   I see ...            01/01/70 00:00      
      this code with correction            01/01/70 00:00      
         Try the Insert Code button.            01/01/70 00:00      
            code with comment            01/01/70 00:00      
               Lots of things to look into            01/01/70 00:00      
                  reply            01/01/70 00:00      
                     One way to debounce            01/01/70 00:00      
               One Suggestion            01/01/70 00:00      
                  the easy way to do keypads            01/01/70 00:00      
   indent, indent, indent            01/01/70 00:00      
   problem in keypad            01/01/70 00:00      
      Where is the code?            01/01/70 00:00      
         code of 8x8 keypad            01/01/70 00:00      
            Simplify and think about debounce            01/01/70 00:00      

Back to Subject List