Keyboard interface in VHDL
This design consists of 4 modules to interface an FPGA with PS2 keyboard using DE2-115 (the same code can be used with any FPGA board).
These modules are :
PS2
Scan code
Valid Data
Key2Acii
1- Module a (PS2):
this module receives PS2 data and PS2 clock from the key board and gives out 8bit scan data and a load signal to indicate that data is completely received from key board and ready to be used.
As shown in figure the data from key board comes in ten bits start bit 8 data bits and a stop bit these data can be sampled with the falling edge of the sp2c coming from keyboard so a 10 bit shift register is used to store data coming from keyboard these bits should enter the shift register with the falling edge of ps2c but as the ps2c clock coming from keyboard might be a clock with glitches so a small glitch filter is used to make sure that the data is sampled at a true falling edge.
This small filter works by taking six samples from the received ps2c at a sampling frequency of 50 MHz if the samples equal to 111000 then its considered a falling edge otherwise its ignored.
VHDL code :
2- Module b (Scan_Code):
A keyboard consists of a matrix of keys and an embedded microcontroller that monitors (i.e., scans) the activities of the keys and sends scan code accordingly. Three types of key activities are observed:
When a key is pressed, the make code of the key is transmitted.
When a key is held down continuously, a condition known as typematic, the make code is transmitted repeatedly at a specific rate. By default, a PS2 keyboard transmits the make code about every 100 ms after a key has been held down for 0.5 second.
When a key is released, the break code of the key is transmitted
VHDL code :
For example, the make code of the A key is I C . This code can be conveyed by one packet when transmitted. The make codes of a handful of special-purpose keys, which are known as the extended keys, can have 2 to 4 bytes. A few of these keys are shown in Figure For example, the make code of the upper arrow on the right is EO 75. Multiple packets are needed for the transmission.
The break codes of the regular keys consist of FO followed by the make code of the key.
For example, the break code of the A key is FO 1C. The PS2 keyboard transmits a sequence of codes according to the key activities. For example, when we press and release the A key, the keyboard first transmits its make code and then the break code:
1C FO 1C
If we hold the key down for a while before releasing it, the make code will be transmitted multiple times:
1C 1C 1 C . . . 1C FO 1 C.
The module consists of a state machine that takes the 8 bit data from key board and check its value if it is FO then the following value will be displayed else it will continue sending the same value.
VHDL code :
3- Module c (Valid_Data):
This is only a DQ flip flop with enable its function is to take the data from the scan_code module when the module gives an enable this is to make sure that the data is valid
VHDL code :
4- Module d (Key2Acii):
The function of this module is to take the the code from the key board and convert it to ASCII code in order to display it on LCD or computer screen this module is an 8 bit input 8 bit output module that works as a multiplexer with 8 bit select that choose between constant values using the given select value.
Top Level :