PS2 Interfacing With ATMega16.
PS2 Interfacing With ATMega16.
Page 1 of 3
PS/2 Library
mikroCPROforAVRLibraries>HardwareLibraries>
PS/2 Library
ThemikroCPROforAVRprovidesalibraryforcommunicationwiththecommonPS/2keyboard. Important :
Example :
sbitPS2_DataatPINC0_bit; sbitPS2_In_Clockat PINC1_bit; sbitPS2_Out_Clockat PORTC1_bit; sbitPS2_Data_Directionat DDC0_bit; sbitPS2_Clock_Directionat DDC1_bit;
Library Routines
Ps2_Config Ps2_Key_Read
Ps2_Config
Prototype Returns Description Requires voidPs2_Config(); Nothing. InitializestheMCUforworkwiththePS/2keyboard. Globalvariables:
mustbedefinedbeforeusingthisfunction. Example
sbitPS2_DataatPINC0_bit; sbitPS2_In_ClockatPINC1_bit; sbitPS2_Out_ClockatPORTC1_bit; sbitPS2_Data_DirectionatDDC0_bit; sbitPS2_Clock_DirectionatDDC1_bit; ... Ps2_Config();// Init PS/2 Keyboard
Ps2_Key_Read
Prototype Returns
1ifreadingofakeyfromthekeyboardwassuccessful 0ifnokeywaspressed
Description
Thefunctionretrievesinformationonkeypressed. Parameters:
value:holdsthevalueofthekeypressed.Forcharacters,numerals,punctuationmarks,andspacevaluewillstore
mk:@MSITStore:C:\Users\Eng.Rashed\Desktop\MikroC%20Courses\mikroC_PRO_...
28/12/2012
PS/2 Library
Page 2 of 3
theappropriateASCIIcode.Routinerecognizesthefunctionof Shift and CapsLock ,andbehavesappropriately.For specialfunctionkeysseeSpecialFunctionKeysTable. special:isaflagforspecialfunctionkeys( F1 , Enter , Esc ,etc).Ifkeypressedisoneofthese,special willbeset to1,otherwise0. pressed:issetto1ifthekeyispressed,and0ifitisreleased.
Requires Example
PS/2keyboardneedstobeinitialized.SeePs2_Configroutine.
unsigned shortkeydata=0,special=0,down=0; ... // Press Enter to continue: do{ if(Ps2_Key_Read(&keydata,&special,&down)){ if(down&&(keydata==16))break; } }while(1);
Library Example
ThissimpleexamplereadsvaluesofthepressedkeysonthePS/2keyboardandsendsthemviaUART.
CopyCodeToClipboard
unsigned shortkeydata=0,special=0,down=0; sbitPS2_DataatPINC0_bit; sbitPS2_Clock_InputatPINC1_bit; sbitPS2_Clock_OutputatPORTC1_bit; sbitPS2_Data_DirectionatDDC0_bit; sbitPS2_Clock_DirectionatDDC1_bit; voidmain(){ UART1_Init(19200);// Initialize UART module at 19200 bps Ps2_Config();// Init PS/2 Keyboard
mk:@MSITStore:C:\Users\Eng.Rashed\Desktop\MikroC%20Courses\mikroC_PRO_...
28/12/2012
PS/2 Library
Page 3 of 3
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
ExampleofPS2keyboardconnection
Copyright (c) 2002-2012 mikroElektronika. All rights reserved. What do you think about this topic ? Send us feedback!
mk:@MSITStore:C:\Users\Eng.Rashed\Desktop\MikroC%20Courses\mikroC_PRO_...
28/12/2012