Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
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.
Description :
Example :
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;
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 :
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
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();
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
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.
Requires Example
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;
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
mk:@MSITStore:C:\Users\Eng.Rashed\Desktop\mikroc_PRO_PIC.chm::/ps2_library... 28/12/2012