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