Author Topic: == SPI C Source Code for Keypad functions ==  (Read 6476 times)

modtro2

  • Administrator
  • Hero Member
  • *****
  • Posts: 564
    • View Profile
== SPI C Source Code for Keypad functions ==
« on: November 27, 2006, 06:36:36 PM »
The following C Source code uses the spiPutGetByte() function defined in the following post:
http://forum.modtronix.com/index.php?topic=399.0
For it to work, you will have to add the spiPutGetByte() function to your code too!

The Keypad on the LCD2S shares port pins with user inputs and outputs. As a result, not all user inputs and outputs and the full 16 key (4 rows by 4 columns) keypad encoder can be used at the same time. For details, see the LCD2S documentation. The LCD2S has a 16 key buffer, and can store 16 keys before losing data! Following are some examples how to configure and use the keypad encoder on the LCD2S family of boards.


Configure for 4x4 keypad, and read keys
The following example shows how to configure the LCD2S board to have a 16 key (4 rows by 4 columns) keypad encoder. When configuring it for a 16 key keypad, the OUT1, GPIO1, GPIO2 and GPIO3 user inputs and outputs are not available. Additionally jumper J1 on the back of the LCD2S board has to be removed! To configure the keypad we have to send a "Configure Keypad and I/O" command to the LCD2S. This command must always be preceded by a "Remember" command. The code for configuring the LCD2S for a 16 key keypad encoder is:

Code: [Select]
    //Send "Remember Command" - "Configure Keypad and I/O" command must be preceded by a remember command
    spiPutGetByte(0xF5);     //Sync byte - each SPI command has to start with this byte!
    spiPutGetByte(0x8D);     //"Remember" command

    //Configure keypad for 4 rows by 4 columns
    spiPutGetByte(0xF5);     //Sync byte - each SPI command has to start with this byte!
    spiPutGetByte(0xE0);     //"Configure Keypad and I/O" command
    spiPutGetByte(0x00);     //Parameter for "Configure Keypad and I/O" command - 0x00 = 4x4 keypad

The following code can then be used to poll the LCD2S at regular intervals to see if there is any pending keypad data. Don't poll too often, else the receive buffer might overflow. A typical time to poll the board would be every 50ms to 300ms.

Code: [Select]
    char c;
    spiPutGetByte(0xF5);     //Sync byte - each SPI command has to start with this byte!
    spiPutGetByte(0xD1);     //"Read Keypad Data" command
    spiPutGetByte(0x00);     //A dummy byte has to be sent before reading a byte!
    delay_us(10);            //Add an extra delay of 10us. This is required if SPI communication in
                             //spiPutGetByte() function is very fast.
    c = spiPutGetByte(0x00); //Read keypad data byte, will be 0 if there is no keypad data

    //If a key byte was read, c will have a value from 'a' to 'p'
    if (c != 0) {
        //The read key is in c, do something with it!
    }


Configure for 4x1 keypad, and read keys
The following example shows how to configure the LCD2S board to have a 4 key (4 rows by 1 column) keypad encoder. When configuring it for a 4 key keypad, the OUT1, GPIO1 and GPIO2 user inputs and outputs are available, but the GPIO3 is not available. Additionally jumper J1 on the back of the LCD2S board has to be inserted! To configure the keypad we have to send a "Configure Keypad and I/O" command to the LCD2S. This command must always be preceded by a "Remember" command. The code for configuring the LCD2S for a 4 key keypad encoder is:

Code: [Select]
    //Send "Remember Command" - "Configure Keypad and I/O" command must be preceded by a remember command
    spiPutGetByte(0xF5);     //Sync byte - each SPI command has to start with this byte!
    spiPutGetByte(0x8D);     //"Remember" command

    //Configure keypad for 4 rows by 1 column
    spiPutGetByte(0xF5);     //Sync byte - each SPI command has to start with this byte!
    spiPutGetByte(0xE0);     //"Configure Keypad and I/O" command
    spiPutGetByte(0x07);     //Parameter for "Configure Keypad and I/O" command - 0x07 = 4x1 keypad

The same code as above (in the 4x4 keypad example) can be used to poll the LCD2S at regular intervals to see if there is any pending keypad data.
« Last Edit: November 27, 2006, 07:10:39 PM by modtro2 »