PS/2 Library

The mikroBasic PRO for 8051 provides a library for communication with the common PS/2 keyboard.

Notes:

External dependencies of PS/2 Library

The following variables must be defined in all projects using PS/2 Library: Description : Example :
dim PS2_DATA as sbit bdata sfr external PS/2 Data line. dim PS2_DATA as sbit at P0_0_bit
dim PS2_CLOCK <as sbit bdata sfr external PS/2 Clock line. dim PS2_CLOCK as sbit at P0_1_bit

Library Routines

Ps2_Config

Prototype

sub procedure Ps2_Config()

Returns

Nothing.

Description

Initializes the MCU for work with the PS/2 keyboard.

Requires

Global variables :

  • PS2_DATA: Data signal pin
  • PS2_CLOCK: Clock signal pin
must be defined before using this function.

Example
' PS2 pinout definition
dim PS2_DATA as sbit at P0_0_bit
    PS2_CLOCK as sbit at P0_1_bit
...
Ps2_Config()         ' Init PS/2 Keyboard

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 byte

Returns

  • 1 if reading of a key from the keyboard was successful
  • 0 if no key was 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.

Requires

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

Example
dim value, special, pressed as byte
...
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)

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.

program PS2_Example

dim keydata, special, down as byte
 
' PS2 module connections
dim PS2_DATA  as sbit at P0_0_bit
    PS2_CLOCK as sbit at P0_1_bit
' End PS2 module connections

main:
  UART1_Init(4800)                                    ' Initialize UART module at 4800 bps
  Ps2_Config()                                        ' Initialize PS/2 Keyboard
  Delay_ms(100)                                       ' Wait for keyboard to finish

  keydata = 0
  special = 0
  down = 0

  UART1_Write_Text("Ready")
  UART1_Write(10)                                     ' Line Feed
  UART1_Write(13)                                     ' 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(10)                                      ' Debounce period
  wend
end.

HW Connection

Example PS2 keyboard connection

Example of PS2 keyboard connection

Copyright (c) 2002-2013 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