0% found this document useful (0 votes)
188 views3 pages

PS2 Interfacing With ATMega16.

The document discusses a PS/2 library for communicating with a PS/2 keyboard. It describes initializing the library, reading keys pressed, and provides an example of reading keys and sending them via UART. Special keys are also mapped. Hardware connection of a PS/2 keyboard is shown.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
188 views3 pages

PS2 Interfacing With ATMega16.

The document discusses a PS/2 library for communicating with a PS/2 keyboard. It describes initializing the library, reading keys pressed, and provides an example of reading keys and sending them via UART. Special keys are also mapped. Hardware connection of a PS/2 keyboard is shown.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

PS/2 Library

Page 1 of 3

PS/2 Library
mikroCPROforAVRLibraries>HardwareLibraries>

PS/2 Library
ThemikroCPROforAVRprovidesalibraryforcommunicationwiththecommonPS/2keyboard. Important :

Thelibrarydoesnotutilizeinterruptsfordataretrieval,andrequirestheoscillatorclocktobeatleast6MHz. ThepinstowhichaPS/2keyboardisattachedshouldbeconnectedtothepull-upresistors. AlthoughPS/2isatwo-waycommunicationbus,thislibrarydoesnotprovideMCU-to-keyboardcommunication;e.g.pressingthe CapsLock keywillnotturnontheCapsLockLED.

External dependencies of PS/2 Library


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

Description : PS/2Dataline. PS/2Clocklinein. PS/2Clocklineout. DirectionofthePS/2Datapin. DirectionofthePS/2Clockpin.

Example :
sbitPS2_DataatPINC0_bit; sbitPS2_In_Clockat PINC1_bit; sbitPS2_Out_Clockat PORTC1_bit; sbitPS2_Data_Directionat DDC0_bit; sbitPS2_Clock_Directionat DDC1_bit;

extern sfr sbitPS2_Out_Clock;

extern sfr sbitPS2_Data_Direction;

extern sfr sbitPS2_Clock_Direction;

Library Routines

Ps2_Config Ps2_Key_Read

Ps2_Config
Prototype Returns Description Requires voidPs2_Config(); Nothing. InitializestheMCUforworkwiththePS/2keyboard. Globalvariables:

PS2_Data:Datasignalline PS2_In_Clock:Clocksignallinein PS2_Out_Clock:Clocksignallineout PS2_Data_Direction:DirectionoftheDatapin PS2_Clock_Direction:DirectionoftheClockpin

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

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

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

Special Function Keys


Key F1 F2 F3 F4 F5 F6 F7 F8 F9 F10 F11 F12 Enter PageUp PageDown Backspace Insert Delete Windows Ctrl Shift Alt PrintScreen Pause CapsLock End Home ScrollLock NumLock LeftArrow RightArrow UpArrow DownArrow Escape Tab Value returned 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35

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!

Want more examples and libraries? Find them on

mk:@MSITStore:C:\Users\Eng.Rashed\Desktop\MikroC%20Courses\mikroC_PRO_...

28/12/2012

You might also like