FRIENDz

FAILURE IS THE SIGN OF SUCCESS!!

PS/2 Keyboard connect with ATMEGA16

The mikroC PRO for AVR provides a library for communication with the common PS/2 keyboard.
  Important :
  • The library does not utilize interrupts for data retrieval, and requires the oscillator clock to be at least 6MHz.
  • The pins to which a PS/2 keyboard is attached should be connected to the pull-up resistors.
  • 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: Description : Example :
extern sfr sbit PS2_Data; PS/2 Data line. sbit PS2_Data at PINC0_bit;
extern sfr sbit PS2_In_Clock; PS/2 Clock line in. sbit PS2_In_Clock at PINC1_bit;
extern sfr sbit PS2_Out_Clock; PS/2 Clock line out. sbit PS2_Out_Clock at PORTC1_bit;
extern sfr sbit PS2_Data_Direction; Direction of the PS/2 Data pin. sbit PS2_Data_Direction at DDC0_bit;
extern sfr sbit PS2_Clock_Direction; Direction of the PS/2 Clock pin. sbit PS2_Clock_Direction at DDC1_bit;

Library Routines

  • Ps2_Config
  • Ps2_Key_Read

Ps2_Config

Prototype void Ps2_Config();
Returns Nothing.
Description Initializes the MCU for work with the PS/2 keyboard.
Requires Global variables :
  • PS2_Data: Data signal line
  • PS2_In_Clock: Clock signal line in
  • PS2_Out_Clock: Clock signal line out
  • 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 PS2_Data at PINC0_bit;
sbit PS2_In_Clock at PINC1_bit;
sbit PS2_Out_Clock at PORTC1_bit;
sbit PS2_Data_Direction at DDC0_bit;
sbit PS2_Clock_Direction at DDC1_bit;
...
Ps2_Config();         // Init PS/2 Keyboard

Ps2_Key_Read

Prototype unsigned short Ps2_Key_Read(unsigned short *value, unsigned short *special, unsigned short *pressed);
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 .
  • 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. 
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);

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.

unsigned short keydata = 0, special = 0, down = 0;
sbit PS2_Data at PINC0_bit;
sbit PS2_Clock_Input at PINC1_bit;
sbit PS2_Clock_Output at PORTC1_bit;
sbit PS2_Data_Direction at DDC0_bit;
sbit PS2_Clock_Direction at DDC1_bit;
void main() {
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
//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(10); // Debounce
} }
} while (1);
}

HW Connection


No comments:

Post a Comment