PSoC Lab 04: Keyboard Interface and Calculator
- Learn how to design the interface with a Matrix Keypad
- Learn how to read inputs from a matrix keypad
- Learn how to design a calculator
Required Reading Material
You will need to design a calculator that can perform addition, subtraction, multiplication, and division operations. The calculator uses a matrix keypad as input device, and display the result on the character LCD module as output device.
You have to write a C program that will determine which key has been pressed on the keypad. A schematic diagram is provided below to show connections needed to implement this lab. To do this, we will briefly introduce the fundamentals of a keypad.
Keypads are used in all types of devices, including cell phones, fax machines, microwaves, ovens, door locks, etc. There are practically everywhere. Tone of electronic devices use them for user input.
In this section we will discuss logic and interface of a matrix keypad with microcontroller to reduce the number of port pins required to read a certain number of digital input. The same logic applies to any matric keypad.
Why Matrix Keypad?
Typically, one digital input is connected to one port pin. When there are a lot of digital inputs that have to be read by microcontroller, it requires same number of port pins to read each input signal separately. It would not be feasible to allocate one pin for each of them, because these will occupy a lot of I/O pins. the main reason is that microcontrollers grow with the number of pins, and growth means more power, capabilities and most of all higher price. So in the end you can either get a cheap chip with little capabilities (that is what you need) but with few I/O pins, or a more powerful chip, which is mush more than you need.
Therefore, a new interface technique will be needed to reduce number of required pins in this kind of situation. A easiest way to do that is to arrange inputs in matrix form, which divides I/O pins into two sections: the rows and the columns. For example, a 64-key keyboard would require 64 digital input port pins. With a matrix circuit, 16 I/O pins arranged in 8 rows and 8 columns can connect 64 keys — 8 output pins to drive rows and 8 input pins to read columns.
What are the Key Matrices?
Matrix Keypad is made by arranging push button switches in rows and columns. If you want to interface a 4 by 4 (16 keys) matrix keypad with a microcontroller. In the straight forward way, you will need 16 I/O pins of a microcontroller for that, but by using a simple technique we can reduce it to 8 I/O pins. In the matrix keypad, switches are connected in a special manner a shown in the figure below.
The blue lines are the columns and the red lines the rows. There are 16 knots that the rows and columns intersect, and each knot connects one switch button. There will be no connections between columns and rows. When any of the switches are pressed, the corresponding columns and rows are connected (short circuited), which can be detected by microcontroller to identify which keys have been pressed.
How does Key Matrix Works?
We make the columns as input pins and we drive the rows making them output pins. In order for the microcontroller to determine which button is pressed, it first needs to pull each of the four rows (R1 ~ R4) either logic low or high one at a time, and then poll the states of the four columns (C1 ~ C4). Depending on the states of the columns, the microcontroller can detect which button is pressed.
Lets assume that a logic high signal is given to Row 2 (R2). If any of the key belongs to Row 2 is pressed, the high signal from Row 2 will pass to the corresponding column as high. Watch the above animation, the button '5' is pressed, then the column 2 will also have high as long as the button '5' is pressed. What this means it that. if we know which row has currently logic high signal, and we watch the columns, then we can understand which button was pressed, if we detect power on a column. For example, our program pulls all four rows low and then pulls the second row (R2) high. It then reads the input states of each column, and reads column 2 high. This means that a connection has been made between column 2 and row 2, so button '5' has been pressed.
4x4 Matrix Keypad Pinout
Matrix keypads use a combination of four rows and four columns to provide button states to the host device, typically a microcontroller. Underneath each key is a pushbutton, with one end connected to one row, and the other end connected to one column. These connections are shown in below Figure
Keypad Connection for Open Drain Output Pins
Keypad Connection with Resistors for Regular Output Pins
Required Components List
|Character LCD Module||x 1|
|4x4 Matrix KeyPad||x 1|
TopDesign and PSoC Components
Add the one "Digital Input Pin" and one "Digital Output Pin" to the TopDesign:
- Ports and Pins ➤ Digital Input Pin
- Ports and Pins ➤ Digital Output Pin
- Double click on the output pin, configure the output pin as the following diagram:
- Double click on the input pin, configure the input pin as the following diagram:
- The final TopDesign as the following diagram:
Implement the follow algorithm to "scan" the keypad in the char ReadKeypad() function.
uint8_t R, C;
- Drive one row wire high, and other rows low (Keypad_Rows_Write(R))
- Read and test the states of the column wires (C = Keypad_Cols_Read())
- If column C is high, it is shorted to row R
- if column C is low, C is not shorted to any row wire & remains pulled down
- Repeat steps 1 & 2, but with different row wire driven high (and others low)
- A key press is detected if a column line is detected in the low state
- Key position is intersection of that column and the row being driven low
- Lookup the ASCII table to find out the ASCII code of the pressed key
- If no key is pressed then the function should return a null (zero value)
There is a short time delay from the time a pattern is written to an output port to the appearance of that pattern on the external pins. After writing a pattern to an output port (to drive row lines), insert a short program delay before reading the input port and testing the keypad column lines.
write to output port; // Row lines
CyDelayUs(100); // Delay for 100 uS
read input port; // Column lines
Now, implement a 32-bit signed integral calculator with Keypad. The operator