PS/2 Library
The mikroBasic PRO for ARM provides a library for communication with the common PS/2 keyboard.
Important :
- The library does not utilize interrupts for data retrieval, and requires the oscillator clock to be at least 6MHz.
- The pins to which a PS/2 keyboard is attached should be connected to the pull-up resistors.
- Although PS/2 is a two-way communication bus, this library does not provide MCU-to-keyboard communication; e.g. pressing the Caps Lock key will not turn on the Caps Lock LED.
External dependencies of PS/2 Library
Stellaris
| The following variables must be defined in all projects using PS/2 Library: | Description : | Example : |
|---|---|---|
dim PS2_Data as sbit sfr external |
PS/2 Data line. | dim PS2_Data as sbit at GPIO_PORTD_DATA0_bit |
dim PS2_Clock as sbit sfr external |
PS/2 Clock line. | dim PS2_Clock as sbit at GPIO_PORTD_DATA1_bit |
dim PS2_Data_Direction as sbit sfr external |
Direction of the PS/2 Data pin. | dim PS2_Data_Direction as sbit at GPIO_PORTD_DIR0_bit |
dim PS2_Clock_Direction as sbit sfr external |
Direction of the PS/2 Clock pin. | dim PS2_Clock_Direction as sbit at GPIO_PORTD_DIR1_bit |
STM32
| The following variables must be defined in all projects using PS/2 Library: | Description : | Example : |
|---|---|---|
dim PS2_Data_Input as sbit sfr external |
PS/2 Data Input line. | dim PS2_Data_Input as sbit at GPIOD_IDR.B0 |
dim PS2_Clock_Input as sbit sfr external |
PS/2 Clock Input line. | dim PS2_Clock_Input as sbit at GPIOD_IDR.B1 |
dim PS2_Clock_Output as sbit sfr external |
PS/2 Clock Output line. | dim PS2_Clock_Output as sbit at GPIOD_ODR.B1 |
Library Routines
Ps2_Config
| Prototype |
sub procedure Ps2_Config() |
|---|---|
| Description |
Initializes the MCU for work with the PS/2 keyboard. |
| Parameters |
None. |
| Returns |
Nothing. |
| Requires |
External dependencies of the library from the top of the page must be defined before using this function. |
| Example |
Stellaris
' PS2 pinout definition
dim PS2_Data as sbit at GPIO_PORTD_DATA0_bit
PS2_Clock as sbit at GPIO_PORTD_DATA1_bit
PS2_Data_Direction as sbit at GPIO_PORTD_DIR0_bit
PS2_Clock_Direction as sbit at GPIO_PORTD_DIR1_bit
' End of PS2 pinout definition
...
Ps2_Config() ' Init PS/2 Keyboard
STM32
dim PS2_Data_Input as sbit at GPIOD_IDR.B0
PS2_Clock_Input as sbit at GPIOD_IDR.B1
PS2_Clock_Output as sbit at GPIOD_ODR.B1
...
Ps2_Config() ' Init PS/2 Keyboard
|
| Notes |
None. |
Ps2_Key_Read
| Prototype |
sub function Ps2_Key_Read(dim byref value as byte, dim byref special as byte, dim byref pressed as byte) as word |
|---|---|
| Description |
The function retrieves information on key pressed. |
| Parameters |
|
| Returns |
|
| Requires |
PS/2 keyboard needs to be initialized. See Ps2_Config routine. |
| Example |
dim value, special, pressed as word
...
' Press Enter to continue:
do {
if (Ps2_Key_Read(value, special, pressed)) then
if ((value = 13) and (special = 1)) then
break
end if
end if
loop until (0=1)
|
| Notes |
None. |
Special Function Keys
| Key | Value returned |
|---|---|
| F1 | 1 |
| F2 | 2 |
| F3 | 3 |
| F4 | 4 |
| F5 | 5 |
| F6 | 6 |
| F7 | 7 |
| F8 | 8 |
| F9 | 9 |
| F10 | 10 |
| F11 | 11 |
| F12 | 12 |
| Enter | 13 |
| Page Up | 14 |
| Page Down | 15 |
| Backspace | 16 |
| Insert | 17 |
| Delete | 18 |
| Windows | 19 |
| Ctrl | 20 |
| Shift | 21 |
| Alt | 22 |
| Print Screen | 23 |
| Pause | 24 |
| Caps Lock | 25 |
| End | 26 |
| Home | 27 |
| Scroll Lock | 28 |
| Num Lock | 29 |
| Left Arrow | 30 |
| Right Arrow | 31 |
| Up Arrow | 32 |
| Down Arrow | 33 |
| Escape | 34 |
| Tab | 35 |
Library Example
This simple example reads values of the pressed keys on the PS/2 keyboard and sends them via UART.
Stellaris
program PS2_Example
dim keydata, special, down as byte
dim PS2_Data as sbit at GPIO_PORTD_DATA0_bit
PS2_Clock as sbit at GPIO_PORTD_DATA1_bit
PS2_Data_Direction as sbit at GPIO_PORTD_DIR0_bit
PS2_Clock_Direction as sbit at GPIO_PORTD_DIR1_bit
main:
UART0_Init(56000) ' Initialize UART module at 56000 bps
Ps2_Config() ' Init PS/2 Keyboard
Delay_ms(100) ' Wait for keyboard to finish
UART0_Write_Text("Ready") ' Ready
UART0_Write(13) ' Line Feed
UART0_Write(10) ' Carriage return
while TRUE ' Endless loop
if Ps2_Key_Read(keydata, special, down) then ' If data was read from PS/2
if (down <> 0) and (keydata = 16) then ' Backspace read
UART0_Write(0x08) ' Send Backspace to usart terminal
else
if (down <> 0) and (keydata = 13) then ' Enter read
UART0_Write(10) ' Send carriage return to usart terminal
UART0_Write(13) ' Uncomment this line if usart terminal also expects line feed
' for new line transition
else
if (down <> 0) and (special = 0) and (keydata <> 0) then ' Common key read
UART0_Write(keydata) ' Send key to usart terminal
end if
end if
end if
end if
Delay_ms(1) ' Debounce period
wend
end.
STM32
program PS2_Example
dim keydata, special, down as byte
dim PS2_Data_Input as sbit at GPIOD_IDR.B0
PS2_Clock_Input as sbit at GPIOD_IDR.B1
PS2_Clock_Output as sbit at GPIOD_ODR.B1
main:
UART1_Init(56000) ' Initialize UART module at 56000 bps
Ps2_Config() ' Init PS/2 Keyboard
Delay_ms(100) ' Wait for keyboard to finish
UART1_Write_Text("Ready") ' Ready
UART1_Write(13) ' Line Feed
UART1_Write(10) ' Carriage return
while TRUE ' Endless loop
if Ps2_Key_Read(keydata, special, down) then ' If data was read from PS/2
if (down <> 0) and (keydata = 16) then ' Backspace read
UART1_Write(0x08) ' Send Backspace to usart terminal
else
if (down <> 0) and (keydata = 13) then ' Enter read
UART1_Write(10) ' Send carriage return to usart terminal
UART1_Write(13) ' Uncomment this line if usart terminal also expects line feed
' for new line transition
else
if (down <> 0) and (special = 0) and (keydata <> 0) then ' Common key read
UART1_Write(keydata) ' Send key to usart terminal
end if
end if
end if
end if
Delay_ms(1) ' Debounce period
wend
end.
Copyright (c) 2002-2012 mikroElektronika. All rights reserved.
What do you think about this topic ? Send us feedback!
What do you think about this topic ? Send us feedback!




