PS/2 Library

The mikroC 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 :
extern sfr sbit PS2_Data; PS/2 Data line. sbit PS2_Data at GPIO_PORTD_DATA0_bit;
extern sfr sbit PS2_Clock; PS/2 Clock line. sbit PS2_Clock at GPIO_PORTD_DATA1_bit;
extern sfr sbit PS2_Data_Direction; Direction of the PS/2 Data pin. sbit PS2_Data_Direction at GPIO_PORTD_DIR0_bit;
extern sfr sbit PS2_Clock_Direction; Direction of the PS/2 Clock pin. sbit PS2_Clock_Direction at GPIO_PORTD_DIR1_bit;

STM32

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

Library Routines

Ps2_Config

Prototype

void 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

sbit PS2_Data            at GPIO_PORTD_DATA0_bit;
sbit PS2_Clock           at GPIO_PORTD_DATA1_bit;
sbit PS2_Data_Direction  at GPIO_PORTD_DIR0_bit;
sbit PS2_Clock_Direction at GPIO_PORTD_DIR1_bit;
...
Ps2_Config();         // Init PS/2 Keyboard

STM32

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

None.

Ps2_Key_Read

Prototype

unsigned int Ps2_Key_Read(unsigned short *value, unsigned short *special, unsigned short *pressed);

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
unsigned short keydata = 0, special = 0, down = 0;
...
// Press Enter to continue:
do {
  if (Ps2_Key_Read(&keydata, &special, &down)) {
    if (down && (keydata == 16)) break;
  }
} while (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

unsigned short keydata, special, down;
 
sbit PS2_Data            at GPIO_PORTD_DATA0_bit;
sbit PS2_Clock           at GPIO_PORTD_DATA1_bit;
sbit PS2_Data_Direction  at GPIO_PORTD_DIR0_bit;
sbit PS2_Clock_Direction at GPIO_PORTD_DIR1_bit;

void 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");
  UART0_Write(10);                                    // Line Feed
  UART0_Write(13);                                    // Carriage return

  do {
    if (Ps2_Key_Read(&keydata, &special, &down)) {
      if (down && (keydata == 16)) {                  // Backspace
         UART0_Write(0x08);
      }
      else if (down && (keydata == 13)) {             // Enter
        UART0_Write('r');                            // send carriage return to usart terminal
        //UART0_Write('n');                          // uncomment this line if usart terminal also expects line feed
                                                      // for new line transition
      }
      else if (down && !special && keydata) {
        UART0_Write(keydata);                         // Send key to usart terminal
      }
    }
    Delay_ms(1);                                      // Debounce period
  } while (1);
}

STM32

unsigned short keydata, special, down;
 
sbit PS2_Data_Input            at GPIOD_IDR.B0;
sbit PS2_Clock_Input           at GPIOD_IDR.B1;
sbit PS2_Clock_Output          at GPIOD_ODR.B1;


void main() {
  UART1_Init(56000);                                  // Initialize UART module at 56000 bps
  GPIO_Config(&GPIOE_BASE,0xff00,_GPIO_CFG_DIGITAL_OUTPUT);
  Ps2_Config();                                       // Init PS/2 Keyboard
  Delay_ms(100);                                      // Wait for keyboard to finish
  UART1_Write_Text("Ready");
  UART1_Write(10);                                    // Line Feed
  UART1_Write(13);                                    // Carriage return

  do {
    if (Ps2_Key_Read(&keydata, &special, &down)) {
      if (down && (keydata == 16)) {                  // Backspace
         UART1_Write(0x08);
      }
      else if (down && (keydata == 13)) {             // Enter
        UART1_Write('r');                            // send carriage return to usart terminal
        //UART0_Write('n');                          // uncomment this line if usart terminal also expects line feed
                                                      // for new line transition
      }
      else if (down && !special && keydata) {
        UART1_Write(keydata);                         // Send key to usart terminal
      }
    }
    Delay_ms(1);                                      // Debounce period
  } while (1);
}
Copyright (c) 2002-2012 mikroElektronika. All rights reserved.
What do you think about this topic ? Send us feedback!
Want more examples and libraries? 
Find them on LibStock - A place for the code