PS2 Keyboard Library

PS2 Keyboard Library

Overview

Libary for mikroPascal to use a PS2 Keyboard with a PIC MCU.
This project is designed to work with PIC24F96GA010 but with minor adjustments, it should work with any other PIC MCU.

Library functions

1. Function PS2_Key_Pressed : byte;
   - This function will return TRUE (255) if a key was pressed
     otherwise FALSE (0)
   - PS2 Keyboard must be init

2. Procedure PS2_Key_Read(var DATA, DC, POS : byte);
   - This procedure returns all the data of the key that was
     pressed.

   - DATA represents the ASCII value of the key or a number that can be
     looked up in the table below if the key is a data key
   - DC is 1 for a command key  (eg: F1,ENTER, ESC, ...)
           0 for a ASCII key (eg: a, b, c, 1, 2, ...)
   - POS is 1 if the key was pressed and
     0 if released
     otherwise FALSE (0)

   - PS2 Keyboard must be init
   - PS2_Key_Pressed MUST be called first

3. PS2_Init(var port : byte);
   - Init PS2_Keyboard on the given PORT
     ___DATA on bit 0
     ___CLOCK on bit 1
REMEMBER PULL UP RESISTORS FORM ___DATA to VDD and ___CLOCK to VDD

4. Other Procedures for internal use only
   - Procedure PS2_Wait_High;
   - Procedure PS2_Wait_Low;
   - Procedure PS2_Enable;
   - Procedure PS2_Disable;
   - Procedure PS2_Get_Hex(var data : byte);
   - Function PS2_Get_Bit : byte;

1.  For all ASCII keys (eg: a, b, c, 1, 2, 3, ...) including space, the the DATA that the procedure
    PS2_Key_Read, returns will corrospond to those of the characters ASCII value.

2.  If SHIFT is pressed and then an ASCII key, the shifted characters ASCII value will be returned.
    Still a small bug that doesn't always return right value if SHIFT is pressed (ONLY P16)
    For example SHIFT + a will return A.            SHIFT + , will return <
  SHIFT + 2 will return @.            SHIFT + 7 will return &

Just like a PC:
3.  If Caps Lock is on and SHIFT is not pressed the upper-case ASCII will be send
4.  If Caps Lock is on and SHIST is presstd the lower-case ASCII will be send

Please note for above DC will be 0 which represents a character

5.  For all command keys (eg: ENTER, F1, Num-Lock, Tab, ...), DC will be 1 which represents a
    command key.

6.  If a command key is pressed a value will be send through DATA which can be looked up in
    the table below. (Eg: 13 represenst ENTER)
7.  If more than one command key is pressed a series of DATA will be send in the order they are
    pressed. (eg:  SHIFT + F1 will send  21 and then 01)

8.  If Num Lock is off the commands will be send
    For example: If you press the 7 on the keypad, 27(HOME) will be send with DC = 1
                 If you press the 0 on the keypad, 17(INS) will be send with DC = 1
9.  If Num Lock is on the ASCII value of the number pressed will be send with DC = 0

POS will always be 1 if the key was pressed and
                       0 if the key was released.
  

 program PS2_Keyboard;

Uses PS2_P16;                                  // Uses PS2 Libary for P16
Var KeyData,
    DC,
    POS  : byte;
Begin
     CMCON  := 0x07;                           // Disable ADC
     INTCON :=  0;                             // Disable all interupts
     LCD_Init(PortB);                          // Init LCD on Port B
     LCD_CMD(LCD_CURSOR_OFF);
     PS2_Init(PortA);                          // Init PS2 Keyboard on PortA
     Delay_ms(100);                            // Wait for keyboard to finish
     Repeat
     Begin
          If PS2_Key_Pressed then              // Returns TRUE if key was pressed
          Begin
               PS2_Key_Read(keydata,DC,POS);   // Get Data of the key
               If POS = 1 then if DC = 0 then  // Display key if it was a ASCII
               Begin                           // key and it was pressed
                    LCD_CHR_CP(keydata);       // at current position
               End;
               If POS = 1 then if DC = 1 then  // If Command key was pressed
               Begin
                    If keydata = 13 then       // Was it ENTER
                       LCD_Cmd(LCD_Second_Row);// Go to second row
               End;
          End;
     End;
     Until 1 = 0;                              // Repeat forever
end.
 

 
Shopping Cart
0 items
 
Riecktron Electronics © 2012