PS2 Keyboard Interface with AVR MCU

A PC keyboard is an old and trusted human machine interface. Most peoples are familiar with it. When a text entry is required it is the best method. If we can interface the PC keyboard with an AVR MCU we can create a whole lot of interesting applications! This tutorial will focus on our easy to use PS2 keyboard library for AVR MCU.

keyboard

Fig. 1 – A PC Keyboard(PS2 Type).

 

The PS2 Keyboard Library for AVR

The PS2 Keyboard library for AVR has only two functions one for initializing the library and one for reading a ASCII character from the queue. The keyboard library automatically translates the scan codes to ASCII characters and buffers them in a FIFO queue. That means even if the CPU is busy doing something else and a character arrives from the keyboard, it will be automatically buffered in a queue. After that the CPU can read the characters anytime when it is free.

void InitPS2()

Function to initialize the PS2 keyboard library. Internally it initialize the ps2 system and sets up the INT0 isr for handling PS data traffic.

Arguments:

NONE

Return Value:

NONE

 

char ReadFromKbdQ(uint8_t wait)

Function to read a character from the keyboard buffer. The wait parameter can be 0 or non zero. When wait is non zero the the function will wait if the queue is empty until any character is available in the queue. If wait is 0 then the function returns immediately if the queue is empty returning a 0. If their are some characters pending the the buffer it they will be returned one by one (for each call) in a FIFO(first in first our basis) basis.

Argument:

0: If you want to wait till any key is pressed.

non-zero : if you do not want to wait if the buffer is empty.

Return Value:

The ASCII character read from buffer. 0 if the buffer is empty and wait parameter is 0.

Example Usage

The following example shows you how to display string entered using keyboard to LCD display module. The example uses our popular LCD library for AVRs to control the LCD Module. Please see the related tutorial for more detail on LCD interfacing.


/******************************************************************************
Program to display a string entered from a PS2 keyboard connected to AVR MCU.

Software:

   IDE: Atmel Studio 6
   Compiler: avr-gcc
   
   LCD Library : eXtreme LCD Lib (open source)
   Keyboard Library: eXtreme Keyboard Lib (non open source)

Hardware:
   Board: xBoard MINI
   
   Kbd Data: PC0
   Kbd Clock: INT0 (PD2)
   
Copyright (C) 2008 - 2012 eXtreme Electronics, India.

WARNING !!!

NO PART OF THIS WORK CAN BE USED IN ANY PRINTED OR ELECTRONIC MEDIA
WITHOUT A WRITTEN PERMISSION FOR THE ORIGINAL AUTHORS.

WE STRICTLY PROHIHIT THE USE OF THIS SOURCE CODE IN ANY COMMERCIAL
APPLICATION.
   
******************************************************************************/
#include <avr/io.h>
#include <avr/interrupt.h>
#include <util/delay.h>

#include "lib/kb/ps2.h"
#include "lib/lcd/lcd.h"


void Wait();

int main(void)
{
      //Initialize Keyboard library
      InitPS2();

      //Enable Interrupts
      sei();

      //Initially LCD library
      LCDInit(LS_NONE);

      //Write an Intro message on LCD
      LCDWriteString("KBD TEST :)");

      //Goto next line on LCD
      LCDGotoXY(0,1);

      while(1)
      {
         //Wait for a char from keyboard
         char ch= ReadFromKbdQ(1);  //Parameter 1 indicates wait till a key press is detected.

         LCDData(ch);

         Wait();

      }
}

void Wait()
{
   uint8_t i;
   for(i=0;i<2;i++)
   {
      _delay_loop_2(0);
   }
}

Note on compiling the code.

You need Atmel Studio 6 with the built in avr-gcc compiler to build the project.

  • Create a new GCC C Executable Project.
  • Select Device as ATmega8.
  • Hit Alt + F7 to bring the project option dialog.
  • Under AVR GNU C Compiler/Symbols, add a symbol F_CPU = 16000000
  • Under AVR GNU Linker / Libraries add a library kbm816m
  • Create a folder named lib in your project using the solution explorer (right pane in AS6).
  • Create a sub folder named lcd and kb under the folder lib using the same procedure.
  • Using windows explorer (file manager) copy/paste lcd library files inside the lib/lcd folder.
  • Add the lcd library files to AS6 project using the solution explorer. Use command Right Click->Add->Existing Item.
  • Using windows explorer (file manager) copy/paste keyboard library files inside the lib/kb folder.
  • No need to add them to project as you did for lcd lib since it is precompiled.
  • Hit Alt + F7 to bring the project option dialog.
  • Under AVR GNU Linker / Libraries -> Library search path add the lib/kb folder to the search path list.
  • Using solution explorer open the file KeyboardTest.c (the main file of project) and copy/paste the above code.
  • Hit F7 to build the project.
  • A hex file will be generated inside Debug folder inside the project folder.
  • Burn this HEX file the ATmega8 MCU.
  • Set LOW Fuse as 0xFF and HIGH Fuse as 0xC9.
  • Connect the keyboard and LCD as per the schematic.

Schematic for ATmega8 and PC Keyboard Interface.

avr pc keyboard interface schematic

Fig. 2 – Keyboard Interface Schematic.

 

ps2 keyboard connector

Fig. 3 – A PS2 Connector.

 

ps2 connector

Fig. 4 – PS2 Connector.

 

 

Downloads

Facing problem with your embedded, electronics or robotics project? We are here to help!
Post a help request.

Avinash

Avinash Gupta is solely focused on free and high quality tutorial to make learning embedded system fun !

More Posts - Website

Follow Me:
FacebookLinkedInGoogle Plus

20 thoughts on “PS2 Keyboard Interface with AVR MCU

  • By Sagar - Reply

    jST TRUELY aWESm Dude…..!!!!!

    LoviN Ur Tuatorial……….it help a lot to me…!!!

    By using this we can create plenty of appliction

    • By anil - Reply

      dude where is init_ps2 function definition,please update that

      • By Avinash -

        @ Anil

        You kids please go an play with your teddy bears! Leave this place for big boys.

        Its defined in the precompiled library file kbm816m.a

        Some other kid recently tried to open and read it ha ha!

        Its closed source so you cannot read it !

        :LOL:

  • By UTTAM DUTTA - Reply

    Thank you Mr. Avinash, again an useful tutorial, I have one question – can we run this application in AVR GCC 4 version, and atmel stdio 6 need separate installation.
    Another question- will “written text” be in the memory of Micro or it will be wentoff after power off.
    Please reply – Regards Uttam Dutta

  • By Ankit Raj - Reply

    Hi Uttam,

    The “Written text” is used for display purpose only and will be erased once the controller is turned off.

    Thanks
    Ankit Raj

  • By ahmed - Reply

    Hi avinash…. Thanks for sharing such a useful project…. i did try the way for creating hex file as u mentioned above,i did all the copy/paste work and adding the libraries but in the end when i build the project it is showing two errors and in both it is saying for avr gcc.exe that ‘no such file or directory found’ … i av downloaded the atmel studio 6 from atmel site and have run other c projects on atmel 6…please help me finding the error

  • By siddhartha - Reply

    Hi avinash,
    i wanted to interface the keyboard to another MCU.Can you please provide .c file of your library functions.

    thanks
    siddhartha

  • By Nasim Imtiaz Khan - Reply

    Hi avinash,
    Another excellent piece of work. Your tutorial is self explanatory.

    I have a question though. Can i change Kbd Data: PC0 to PD0? Please tell me what i need to do for it.

    Nasim

    • By Avinash - Reply

      @Nasim

      No you cannot change the i/o pins connection.

  • By rozek - Reply

    is it only me being handicapped, or there is ps2.c file missing though i cant run this example project?

    compilers writes there is undefined reference to
    //Initialize Keyboard library
    InitPS2();

  • By Hi avinish , - Reply

    really i want to appreciate your work u are engineering guider i have visited last day on your web site i really can’t belive that this information for the learner..thanks Avinish sir
    ..praveen

  • By Amit Kumar - Reply

    Sir, i am unable to read the code of this libkbm816.a file. As all library files could be read but it is not n readable format. I want to understand the code. As this library file is not opening, i am not getting the code. Could you tell me, how can i get this library file in readable format.

  • By Avinash - Reply

    @Amit Kumar, Ha ha ha 😀 😆 You are talking like a kid asking for a lollipop! Do you know anything about engineering ??? Its .a file in gnu area are pre compiled libraries ! The human readable code is called the “source code” ! This project is not “open-souce” so I have not made the sources public ! Why should I ?
    Ha ha silly !

  • By ISSA BEHESHTI - Reply

    Thank you Mr. Avinash

  • By akshay - Reply

    can i use atmega8- 8pu for this project without changing hex file

  • By Panagiotis - Reply

    Hello Avinash,
    do you also have a tutorial for ps/2 mouse interface? I’m trying to make a mouse work with an avr but can’t quite make it to work.

  • By M.M.M - Reply

    Hello, thanks for your awesome tutorials. Can you make a tutorial for how to send ps/2 commands? (or USB, today most keyboards have usb port.) since I am too lazy (:D) I want to make an external Ctrl+C button 😛
    and again, thank’s a lot.

  • By JA? - Reply

    avr-objdump helps!

  • By Vl - Reply

    Function InitPS2() is located in kbm816m library, am I right?
    It’s only for reading keypress. But I want to make ps/2 keyboard controller and tramsmit data to the host. How to make that?

  • By Anil Reddy - Reply

    Can we use atmega16 with the same procedure ?

Leave a Reply

Your email address will not be published. Required fields are marked *


5 + = thirteen

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>