Sei sulla pagina 1di 4

PS/2 Library

Page 1 of 4

PS/2 Library
The mikroC PRO for PIC provides a library for communication with the common PS/2 keyboard. Note: The library does not utilize interrupts for data retrieval, and requires the oscillator clock to be at least 6MHz. Note: The pins to which a PS/2 keyboard is attached should be connected to the pull-up resistors. Note: 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


The following variables must be defined in all projects using PS/2 Library:
extern sfr sbit PS2_Data;

Description :

Example :

PS/2 Data line.

sbit PS2_Data at RC0_bit; sbit PS2_Clock at RC1_bit; sbit PS2_Data_Direction at TRISC0_bit; sbit PS2_Clock_Direction at TRISC1_bit;

extern sfr sbit PS2_Clock;

PS/2 Clock line.

extern sfr sbit PS2_Data_Direction;

Direction of the PS/2 Data pin.

extern sfr sbit PS2_Clock_Direction;

Direction of the PS/2 Clock pin.

Library Routines

Ps2_Config Ps2_Key_Read

Ps2_Config
void Ps2_Config(); Nothing. Initializes the MCU for work with the PS/2 keyboard. Global variables :

Prototype Returns Description Requires

PS2_Data: Data signal line PS2_Clock: Clock signal line in PS2_Data_Direction: Direction of the Data pin PS2_Clock_Direction: Direction of the Clock pin

must be defined before using this function. Example


sbit sbit sbit sbit PS2_Data at RC0_bit; PS2_Clock at RC1_bit; PS2_Data_Direction at TRISC0_bit; PS2_Clock_Direction at TRISC1_bit;

mk:@MSITStore:C:\Users\Eng.Rashed\Desktop\mikroc_PRO_PIC.chm::/ps2_library... 28/12/2012

PS/2 Library

Page 2 of 4

... Ps2_Config();

// Init PS/2 Keyboard

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

Prototype

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 Example

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


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);

Special Function Keys

Key F1 F2 F3 F4 F5 F6 F7 F8 F9 F10 F11 F12 Enter

Value returned 1 2 3 4 5 6 7 8 9 10 11 12 13

mk:@MSITStore:C:\Users\Eng.Rashed\Desktop\mikroc_PRO_PIC.chm::/ps2_library... 28/12/2012

PS/2 Library

Page 3 of 4

Page Up Page Down Backspace Insert Delete Windows Ctrl Shift Alt Print Screen Pause Caps Lock End Home Scroll Lock Num Lock Left Arrow Right Arrow Up Arrow Down Arrow Escape Tab

14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35

Library Example
This simple example reads values of the pressed keys on the PS/2 keyboard and sends them via UART.

unsigned short keydata = 0, special = 0, down = 0; sbit sbit sbit sbit PS2_Data PS2_Clock PS2_Data_Direction PS2_Clock_Direction at at at at RC0_bit; RC1_bit; TRISC0_bit; TRISC1_bit;

void main() { ANSEL = 0; ANSELH = 0; // Configure AN pins as digital I/O

UART1_Init(19200); // Initialize UART module at 19200 bps Ps2_Config(); // Init PS/2 Keyboard Delay_ms(100); // Wait for keyboard to finish UART1_Write_Text("Ready"); 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

mk:@MSITStore:C:\Users\Eng.Rashed\Desktop\mikroc_PRO_PIC.chm::/ps2_library... 28/12/2012

PS/2 Library

Page 4 of 4

//Usart_Write('n');

// uncomment this line if usart terminal also expects line feed // for new line transition

} else if (down && !special && keydata) { UART1_Write(keydata); } } Delay_ms(1); } while (1); } // debounce

HW Connection

Example of PS2 keyboard connection

mk:@MSITStore:C:\Users\Eng.Rashed\Desktop\mikroc_PRO_PIC.chm::/ps2_library... 28/12/2012

Potrebbero piacerti anche