User Tools

Site Tools

한국어

cubloc:keypad_input:index

Keypad Input

This application demonstrates interfacing to a 4×4 keypad and displaying the results on a 4 digit seven segment module (CSG module)

The CSG module is a 4 digit seven segment LED module that can be connected via CUNET or the I2C interface to display numbers and custom characters.

Const Device = CB280
Set I2c 9,8
Dim i As Byte
Do
  CsgDec 0,i
  i = i + 1
Loop

If you connect the CSG to the CuNet connector and execute the program above, the CSG module will show incrementing numbers. The key matrix can be easily read using the KeyPad command. If you look carefully at the keypad, you will see that the scan code does not match the actual key pressed. In order to read the correct key, we will use a Byte array (KEYTABLE) to map the scan codes to the appropriate key.

Const Device = CB280
Set I2c 9,8
Dim i As Integer
Dim K As Integer
Const Byte KEYTABLE = (1,4,7,10,2,5,8,0,3,6,9,11,12,13,14,15)
Do
  i = Keypad(0)
  If i < 16 Then
    i = KEYTABLE(i)
    Csgdec 0,i
  End If
Loop

And now we will make a simple program that receives input. When a number key input is received, it is displayed to the CSG module as a 4 digit number. The number is stored int the variable K, which is in BCD format. We then use the BCD2Bin command to convert the BCD value back into binary

Const Device = CB280
Set I2c 9,8
Dim i As Integer
Dim K As Integer
Dim M As Integer
K = 0
Const Byte KEYTABLE = (1,4,7,10,2,5,8,0,3,6,9,11,12,13,14,15)
Do
  i = Keypad(0)
  If i < 16 Then
    i = KEYTABLE(i)
    If i < 10 Then
      K = K << 4
      K = K + i
      Csghex 0,K
    End If
    '
    ' WAIT UNTIL KEY DEPRESS
    '
    Do While KeyPad(0) < 255
    Loop
    M = BCD2Bin(K)
    Debug Dec M,CR
  End If
Loop

When there is no input, the returned scan code is 255. Using the statment Do While keypad(0) < 255, we wait until a key is released which will return a scan code of 255. This is to allow the processor to stop reading input while a key is being pressed. Otherwise, the processor might receive multiple key inputs since the Cubloc's execution speed is very fast.

Using _D(0) = M, you can pass the scan code to Ladder Logic's D0 register. If you need to use a keypad in Ladder Logic, you can make minor modifications to this code to quickly get your results.

cubloc/keypad_input/index.txt · Last modified: 2016/04/14 11:07 (external edit)