PS/2 Library

The mikroPascal PRO for ARM provides a library for communication with the common PS/2 keyboard.

  Important :

External dependencies of PS/2 Library

Stellaris

The following variables must be defined in all projects using PS/2 Library: Description : Example :
var PS2_Data : sbit; sfr; external; PS/2 Data line. var PS2_Data : sbit at GPIO_PORTD_DATA0_bit;
var PS2_Clock : sbit; sfr; external; PS/2 Clock line. var PS2_Clock : sbit at GPIO_PORTD_DATA1_bit;
var PS2_Data_Direction : sbit; sfr; external; Direction of the PS/2 Data pin. var PS2_Data_Direction : sbit at GPIO_PORTD_DIR0_bit;
var PS2_Clock_Direction : sbit; sfr; external; Direction of the PS/2 Clock pin. var PS2_Clock_Direction : sbit at GPIO_PORTD_DIR1_bit;

MSP432

The following variables must be defined in all projects using PS/2 Library: Description : Example :
var PS2_Data : sbit; sfr; external; PS/2 Data line. var PS2_Data : sbit at DIO_P6OUT.B0;
var PS2_Clock_Input : sbit; sfr; external; PS/2 Clock Input line. var PS2_Clock_Input : sbit at DIO_P6IN.B1;
var PS2_Clock_Output : sbit; sfr; external; PS/2 Clock Output line. var PS2_Clock_Output : sbit at DIO_P6OUT.B1;
var PS2_Data_Direction : sbit; sfr; external; Direction of the PS/2 Data pin. var PS2_Data_Direction : sbit at DIO_P6DIR.B0;
var PS2_Clock_Direction : sbit; sfr; external; Direction of the PS/2 Clock pin. var PS2_Clock_Direction : sbit at DIO_P6DIR.B1;

STM32

The following variables must be defined in all projects using PS/2 Library: Description : Example :
var PS2_Data_Input : sbit; sfr; external; PS/2 Data Input line. var PS2_Data_Input : sbit at GPIOD_IDR.B0;
var PS2_Clock_Input : sbit; sfr; external; PS/2 Clock Input line. var PS2_Clock_Input : sbit at GPIOD_IDR.B1;
var PS2_Clock_Output : sbit; sfr; external; PS/2 Clock Output line. var PS2_Clock_Output : sbit at GPIOD_ODR.B1;

CEC1x02

The following variables must be defined in all projects using PS/2 Library: Description : Example :
var PS2_Data_Input : sbit; sfr; external; PS/2 Data Input line. var PS2_Data_Input : sbit at GPIO_INPUT_PIN_001_bit;
var PS2_Clock_Input : sbit; sfr; external; PS/2 Clock Input line. var PS2_Clock_Input : sbit at GPIO_INPUT_PIN_002_bit;
var PS2_Clock_Output : sbit; sfr; external; PS/2 Clock Output line. var PS2_Clock_Output : sbit at GPIO_OUTPUT_PIN_002_bit;

Library Routines

Ps2_Config

Prototype

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 
var PS2_Data            : sbit at GPIO_PORTD_DATA0_bit;
    PS2_Clock           : sbit at GPIO_PORTD_DATA1_bit;
    PS2_Data_Direction  : sbit at GPIO_PORTD_DIR0_bit;
    PS2_Clock_Direction : sbit at GPIO_PORTD_DIR1_bit;
// End of PS2 pinout definition// Init PS/2 Keyboard

MSP432

// PS2 pinout definition 
var PS2_Data     : sbit at DIO_P6OUT.B0;
    PS2_Clock_Input     : sbit at DIO_P6IN.B1;
    PS2_Clock_Output    : sbit at DIO_P6OUT.B1;
    PS2_Data_Direction  : sbit at DIO_P6DIR.B0;
    PS2_Clock_Direction : sbit at DIO_P6DIR.B1;
// End of PS2 pinout definition// Init PS/2 Keyboard

STM32

var PS2_Data_Input       : sbit at GPIOD_IDR.B0;
    PS2_Clock_Input      : sbit at GPIOD_IDR.B1;
    PS2_Clock_Output     : sbit at GPIOD_ODR.B1;
...
Ps2_Config();         // Init PS/2 Keyboard

CEC1x02

var PS2_Data_Input       : sbit at GPIO_INPUT_PIN_001_bit;
    PS2_Clock_Input      : sbit at GPIO_INPUT_PIN_001_bit;
    PS2_Clock_Output     : sbit at GPIO_OUTPUT_PIN_002_bit;
...
Ps2_Config();         // Init PS/2 Keyboard
Notes

None.

Ps2_Key_Read

Prototype

function Ps2_Key_Read(var value : byte; var special : byte; var pressed : byte) : word;

Description

The function retrieves information on key pressed.

Parameters
  • value: holds the value of the key pressed. For characters, numerals, punctuation marks, and space value will store the appropriate ASCII code. Routine “recognizes” the function of Shift and Caps Lock, and behaves appropriately. For special function keys see Special Function Keys Table.
  • special: is a flag for special function keys (F1, Enter, Esc, etc). If key pressed is one of these, special will be set to 1, otherwise 0.
  • pressed: is set to 1 if the key is pressed, and 0 if it is released.
Returns

  • 1 if reading of a key from the keyboard was successful
  • 0 if no key was pressed

Requires

PS/2 keyboard needs to be initialized. See Ps2_Config routine.

Example
var value, special, pressed : word;
...
// Press Enter to continue:
repeat {
  if (Ps2_Key_Read(value, special, pressed)) then 
    if ((value = 13) and (special = 1)) then break;
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