cancel
Showing results for 
Search instead for 
Did you mean: 

4x3 keypad improved with autorepeat and debounce

spiovan9
Associate II
Posted on December 24, 2010 at 22:20

Hi all,

Here's an improved code for the 4x3 keypad with debounce and autorepeat.

With STM8S 2MHz clock it performs pretty close to the PC keyboard.

The action() function is out from the keypad management state machine for clarity.

Suggestions and improvements are welcome.

Hope it helps.

Merry Xmas and happy holidays

Steven

u8 key = 255;

u8 oldkey = 255;

u8 AutoRptMax = 64;

u8 AutoRptCount = 0;

while(1)

    {

        key = GetKeyPressed(); //Scan the keypad to getting the code of the key pressed

    

        // < Is it a valid key pressed ? >

        if (key < 12) // YES

        {

            // < Is key still the old one ? >

            if (key == oldkey) // YES

            {

                // (Start counting for autorepeat to occour)

                DelayMS(50);

                AutoRptCount++;

                

                // < Is time to execute the key action? >

                if (AutoRptCount >= AutoRptMax) // YES

                {

                    Action(key); // perform the action according to the key code

                    AutoRptCount = 0; // reset the autorepeat counter to get ready for the next autorepeat event

                    AutoRptMax = 16; // make the autorepeat limit short for speeding up the next repetition when keeps keypressing

                }

            }

            else // NO, the key is new brand

            {

                Action(key); // perform the action according to the key code

                DelayMS(50); // and also make a little debouncing

            }

            

        } // END if (key < 12)

        

        else // NO, the key is not among the 12 defined keys

        {

            // < Was the key pad released ? >

            if (oldkey < 255 ) // YES

            {

                AutoRptCount = 0; // reset the autorepeat counter

                AutoRptMax = 128; // and restart using the first typing delay long

            }            

        }

        

        oldkey = key;

    }

void Action(u8 key)

{

    switch(key)

    {

        case 9: // backspace (mode entry is ''right after writing'' so we need to cursor left twice)

            LCD_DATA(0x10,0);

            LCD_CHR(32);

            LCD_DATA(0x10,0);

        break;

        case 11:

            LCD_DATA(0x14,0); // cursor right

        break;

       

        default:

            LCD_CHR(key + 48 + 1); // display keycode

        break;

    }

}

void DelayMS(int delay)

{

    int nCount = 0;

    while (delay != 0)

    {

        nCount = 100;

        while (nCount != 0)

        {

            nCount--;

        }

        delay--;

    }

}

#stm8s #debounce #autorepeat #4x3-keypad
2 REPLIES 2
spiovan9
Associate II
Posted on January 02, 2011 at 20:21

ok,

the original thread was updated.

Please remove this one if you would.

Thanks

SP

Andrew Neil
Chief II
Posted on December 27, 2010 at 14:50

''Here's an improved code for the 4x3 keypad''

 

 

I guess your ''improvement'' relates to this:

[DEAD LINK /public/STe2ecommunities/mcu/Lists/STM8SDiscovery/Flat.aspx?RootFolder=/public/STe2ecommunities/mcu/Lists/STM8SDiscovery/Discovery with LCD and 4x3 keypad example&FolderCTID=0x01200200770978C69A1141439FE559EB459D758000CCDF802E596CAF44ADED5E61F5CA8B1B]https://my.st.com/public/STe2ecommunities/mcu/Lists/STM8SDiscovery/Flat.aspx?RootFolder=%2fpublic%2fSTe2ecommunities%2fmcu%2fLists%2fSTM8SDiscovery%2fDiscovery%20with%20LCD%20and%204x3%20keypad%20example&FolderCTID=0x01200200770978C69A1141439FE559EB459D758000CCDF802E596CAF44ADED5E61F5CA8B1B

You should also update that thread with a link to this ''improvement''...