You are on page 1of 3

If you connect CUNET to CSG and execute the above program, the CSG module will

NOTE 2. Keypad Input


Application note 2 will cover a 4 by 4 Keypad by taking its input and outputting the results to a 4 digit 7 segment module (CSG module)
CB280
P8 P9
0 4 5 6 7 8 9
10

show numbers that will count up. The key matrix can be read easily through the command KEYPAD. pressed. If you look

carefully at the keypad, you will see that scancode does not match the actual key In order to read the correct key, we will use a KEYTABLE before outputting the value to the CSG.

12 13 14 15

P0 P1 P2 P3
1 2 3

<Filename: keypadnum.cul>
Const Device = CB280 Set I2c 9,8 Dim I As Integer Dim K As Integer
1 4 7
ESC

11

P4 P5 P6 P7

2 5 8 0

3 6 9
CLR

ST OP

Const Byte KEYTABLE = (1,4,7,10,2,5,8,0,3,6,9,11,12,13,14,15)


L R
GO

Do I=Keypad(0) If I < 16 Then I = KEYTABLE(I) Csgdec 0,I

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

End If

And now, we will make a simple program that receives input.

When a number key The We then use the

input is received, it is displayed to the CSG module as a 4 digit number. number is stored into the variable K, which is in BCD code. function BCD2BIN to convert the BCD value back into binary.

<Filename: num4in.cul> <Filename: csgprint.cul>


Const Device = CB280 Set I2c 9,8 Dim I As Byte Do Csgdec 0,I I = I + 1 Loop 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 ' ' ' Do While Keypad(0) < 255 Loop M = Bcd2bin(K) Debug Dec M,CR End If Loop WAIT UNTIL KEY DEPRESS

Ekeypad
Variable = EKEYPAD( portblockIn, portblockOut) Variable : Variable to store results (Returns Byte) PortblockIn : Port Block to receive input (Input) PortblockOut : Port Block to output (Output) This command EKEYPAD extends KEYPAD to read up to 64 key inputs. Blocks can be used to read up to 64 key inputs. Block must be selected separately. For ports not used within the input Port Block, a resistor must be connected to 5V. This pin may not be used for other purpose when using this command. For ports not used within the output Port Block, they can be left in OPEN state. This Two Port

Input Port Block and output Port

When there is no input, the returned scancode is 255.

By using Do While keypad(0) This Otherwise, the

pin also may not be used for other purposes.

The following is an example of using

< 255, we will wait until a key is unpressed which will return a scancode of 255. is to let the processor stop reading input while a key is pressed. fast.

Port Block 0 as input and Port Block 1 as output.

processor might receive multiple key inputs since execution time of CUBLOC is very
0 1

By using _D(0) = M, you can pass the scancode value to relay D0 of LADDER LOGIC. If you need to use a keypad in LADDER, you can modify this code a little bit to get your results quick.

2 3 4 5 6 7

8 9 10 11 12 13 14 15

If no keys are pressed, 255 will be returned. code will be returned.

Otherwise, the pressed keys scan

Keypad
Variable = KEYPAD( PortBlock) Variable : Variable to store results (Returns Byte) PortBlock : Port Block Use this command Keypad to read input from keypad. read a 4 by 4 keypad input. A Port Block can be used to

Keypad input can be connected to the lower 4 bits of the

Port Block and keypad output can be connected to higher 4 bits of the Port Block. Please refer to the below diagram.

4 5 6 7

8 9
10

12 13 14 15

0 1 2 3
1 2 3

11

4 5 6 7

A = KEYPAD(0) Read the status of keypad connected to Port Block 0

If no keys are pressed, 255 will be returned. code will be returned.

Otherwise, the pressed keys scan