Matrix keyboard with State Machine


Closed Thread
Results 1 to 8 of 8
  1. #1
    Join Date
    Nov 2003
    Location
    Greece
    Posts
    3,795

    Default Matrix keyboard with State Machine

    This is a Stamp Basic code by Tracey Allen to read a 4x4 matrix keyboard with Finite State Machine technique.

    I am trying to use this code in PBP but cannot understand how few commands work:

    keyn var key0.nib0 ' address the first detection as nibble array

    The above seems to alias variable keyn to key0 lowest nibble (this key0 is a word variable). Is that right?

    Then, keyn(ix)=kin ' read the row inputs into nibbles of key0

    seems to put in an array controlled by ix four values. Though the keyn was not defined as array. How does this work?


    Code:
    ' KEY_FSM.BS2
    ' Tracy Allen, http://www.emesystems.com
    ' scans a 16 key (4x4) matrix keypad
    ' using state machine for debouncing, all keys in parallel
    ' The columns are connected to BS2 outputs of port C, P8 to P11
    ' The rows are connected to BS2 inputs of port D, P12 to P15
    ' with pull-up resistors to Vdd=+5 volts.
    ' This routine has two output variables, "keys" and "keyx"
    ' Bits in "keys" go low after the corresponding key is
    ' pressed and debounced.
    ' Bits in "keyx" go high for _one iteration only_ to signal
    ' a high to low transition (button pushed & debounced)
    ' of the corresponding key
    ' In all state variables, each bit corresponds to one key.
    ' One word is used for 16 keys, in a 4x4 array.
    ' This uses a lot of variables, but in many applications it is
    ' possible to reuse most of the variables outside the keyscan loop
    ' Note that the scan can detect key combinations.
    ' The decoding step can miss keys only if they are pressed at
    ' exactly the same instant.
     
     
     
    kout var outC ' 4-bit keypad columns, state, if output
    kdir var dirC ' 4-bit keypad columns, input or output 
    kin var inD ' inputs from keypad rows connected to port D
     ' bit low for key pressed
    key0 var word ' state variable, first detection
    key1 var word ' state variable, second detection
    key2 var word ' state variable, third detection
    keyn var key0.nib0 ' address the first detection as nibble array
    keys var word ' debounced output, bit low for key pressed
    keyz var word ' state variable, previous state of keys
    keyx var word ' transition output, bit high one iteration for each press
    ix var word ' index for array addressing
     
    dirs=%0000111111111111 ' port D is input, all others output
    outs=%0000000000000000 ' start with port C columns output low
     ' pins P0 to P7 are not used in this program
    debug cls ' Clear screen
     
    scanloop:
     key2=key1 ' move the debounced states
     key1=key0
     for ix=0 to 3 ' scan 4 cols by bringing one col at a time low.
     kdir = dcd ix ' 0001,0010,0100,1000 in succession
     ' this makes one column at a time output low
     ' while the other columns are inputs
     keyn(ix)=kin ' read the row inputs into nibbles of key0
     next
     kdir = %1111 ' make all columns output (avoid floating inputs)
     keys=key0&key1&key2|keys ' key bit 0->1 when all 3 are high
     keys=key0|key1|key2&keys ' key bit 1->0 when all 3 are low
     keyx=keys^keyz & keyz ' detect change 1->0 (active low keypress)
     keyz=keys
     ' optional debug to visualize operation of the state machine
     ' slows down the operation tremendously
     ' debug home,bin16 key0,cr,bin16 key1,cr,bin16 key2,cr
     ' debug bin16 keys,32,bin16 keyx,home 
     ' optional branch to decode keypress, note keyx is transient
     if keyx>0 then decode
     ' pause 30 ' optional delay to slow the scanning
    goto scanloop
     
     
    decode:
     code1 var byte ' code1 variable
     code1 = ncd keyx-1 ' binary encode the most significant key
     lookup code1,[10,7,4,1,0,8,5,2,11,9,6,3],code1 ' translate to code
     dtmfout 7,[code1] ' send as touchtone
     lookup code1,["0123456789*#"],code1 ' --> printable character
     debug code1 ' print on screen
    goto scanloop
    Thanks,
    Ioannis
    Last edited by Ioannis; - 26th March 2022 at 21:33.

  2. #2
    Join Date
    Apr 2014
    Location
    OK
    Posts
    557


    Did you find this post helpful? Yes | No

    Default Re: Matrix keyboard with State Machine

    "dirs=00111111111111 ' port D is input, all others output"
    This looks like PICAXE code.

  3. #3
    Join Date
    May 2013
    Location
    australia
    Posts
    2,383


    Did you find this post helpful? Yes | No

    Default Re: Matrix keyboard with State Machine

    since pbp has no nibble var type i would guess that

    keyn(ix)=kin ' read the row inputs into nibbles of key0

    becomes
    keyn = keyn & ~$0f<<(ix*4);clear nibble
    keyn = keyn | kin<<(ix*4);load new value for nibble

    where kin is now a word and

    kin var inD
    becomes
    kin = portd & $f



    there is probably better pbp code for reading a keypad floating around
    Warning I'm not a teacher

  4. #4
    Join Date
    Nov 2003
    Location
    Greece
    Posts
    3,795


    Did you find this post helpful? Yes | No

    Default Re: Matrix keyboard with State Machine

    Mike, this is Stamp Basic dialect.

    Richard, thanks. This makes more sense now. Yes maybe there is better code for matrix keyboard. I was interested in this because of the debouncing and scanning all at the same time.

    Ioannis
    Last edited by Ioannis; - 27th March 2022 at 14:01.

  5. #5


    Did you find this post helpful? Yes | No

    Default Re: Matrix keyboard with State Machine

    See the code examples from Les Johnson in second post. I had this book many years ago, now out of print. You can find pdf's copies on google

    http://www.picbasic.co.uk/forum/showthread.php?t=21115

  6. #6
    Join Date
    Nov 2005
    Location
    Bombay, India
    Posts
    947


    Did you find this post helpful? Yes | No

    Default Re: Matrix keyboard with State Machine

    If you're able to understand arduino C, look at the arduino matrix keyboard example. Nice FSM implementation https://domoticx.com/arduino-library-keypad/

  7. #7
    Join Date
    Nov 2003
    Location
    Greece
    Posts
    3,795


    Did you find this post helpful? Yes | No

    Default Re: Matrix keyboard with State Machine

    Thanks to all!

    Ioannis

  8. #8


    Did you find this post helpful? Yes | No

    Default Re: Matrix keyboard with State Machine

    I did this a long time ago with a 4X4 membrane switch to play messages on an ISD chip.

    Low PORTC.7
    Pause 1
    IF PORTB.4 = 0 Then MESSAGE1
    IF PORTB.2 = 0 Then MESSAGE9
    High PORTC.7

    Low PORTC.6
    Pause 1
    IF PORTB.4 = 0 Then MESSAGE2
    IF PORTB.2 = 0 Then MESSAGE10
    High PORTC.6

    Low PORTC.2
    Pause 1
    IF PORTB.4 = 0 Then MESSAGE3
    IF PORTB.2 = 0 Then MESSAGE11
    High PORTC.2

    Low PORTC.1
    Pause 1
    IF PORTB.4 = 0 Then MESSAGE4
    IF PORTB.2 = 0 Then MESSAGE12
    High PORTC.1

    Low PORTC.0
    Pause 1
    IF PORTB.4 = 0 Then MESSAGE5
    IF PORTB.2 = 0 Then MESSAGE13
    High PORTC.0

    Low PORTA.0
    Pause 1
    IF PORTB.4 = 0 Then MESSAGE6
    IF PORTB.2 = 0 Then MESSAGE14
    High PORTA.0

    Low PORTA.1
    Pause 1
    IF PORTB.4 = 0 Then MESSAGE7
    IF PORTB.2 = 0 Then MESSAGE15
    High PORTA.1

    Low PORTC.4
    Pause 1
    IF PORTB.4 = 0 Then MESSAGE8
    IF PORTB.2 = 0 Then MESSAGE16
    High PORTC.4

Similar Threads

  1. another state machine example
    By richard in forum Code Examples
    Replies: 2
    Last Post: - 19th October 2021, 13:27
  2. Matrix keyboard for up to 32 keys
    By Pedro Pinto in forum mel PIC BASIC Pro
    Replies: 1
    Last Post: - 21st June 2009, 02:43
  3. Keyboard Matrix with a Twist
    By k3v1nP in forum mel PIC BASIC Pro
    Replies: 13
    Last Post: - 29th October 2008, 11:51
  4. State machine programming
    By tjstevens in forum mel PIC BASIC Pro
    Replies: 4
    Last Post: - 17th April 2007, 23:45
  5. Matrix keyboard problem
    By MegaADY in forum mel PIC BASIC Pro
    Replies: 8
    Last Post: - 9th May 2005, 19:50

Members who have read this thread : 1

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts