Engineers Garage

  • Projects and Tutorials
    • Circuit Design
    • Electronic Projects
      • 8051
      • Arduino
      • ARM
      • AVR
      • PIC
      • Raspberry pi
      • STM32
    • Tutorials
    • Components
  • Contributions
  • Articles
    • EG Blogs
    • Insight
    • Invention Stories
    • How to
  • What Is
  • Forums
    • EG Forum Archive
    • EDABoard.com
    • Electro-Tech-Online
  • Digi-Key Store
    • Cables, Wires
    • Connectors, Interconnect
    • Discrete
    • Electromechanical
    • Embedded Computers
    • Enclosures, Hardware, Office
    • Integrated Circuits (ICs)
    • Isolators
    • LED/Optoelectronics
    • Passive
    • Power, Circuit Protection
    • Programmers
    • RF, Wireless
    • Semiconductors
    • Sensors, Transducers
    • Test Products
    • Tools
  • News
    • EE Design News
    • DIY Reviews
    • Guest Post
    • Sponsored Content
  • EE Resources
    • DesignFast
    • LEAP Awards
    • Oscilloscope Product Finder
    • White Papers
    • Webinars
  • EE Classrooms
    • Grid Infrastructure
    • Aerospace & Defense
    • Building Automation
    • Power Delivery
    • Factory Automation
    • Motor Drives
    • Medical Technology
  • Video

How To Use PS2 Keyboard To Store Text In SD Card Using Arduino- (Part 42/49)

July 22, 2013 By Ajish Alfred

The PS2 keyboard uses a simple synchronous serial protocol using only two wires for communication. Due to its simplicity the PS2 keyboards are widely used with simple microcontroller based boards also. The PS2 keyboard always acts as a slave device which can send the equivalent ASCII value of the key which has been pressed to its master device which can be a PC or a microcontroller.The SD memory card comes in large storage capacities from 1 GB up to 32 GB are available. They are used mostly in costly devices like digital camera, media players etc. The SD memory cards are interfaced in high end devices using SD bus which provides very high speed data transfer between the device and the memory card.  They also can be interfaced with a device using the low speed and comparatively simple SPI bus and hence they are widely used in microcontroller based system also.

This project explains how to interface a keyboard and memory card with the Arduino board and hence type and store some text using the keyboard into the memory card. The files in an SD memory card are stored using FAT32 or FAT16 and one should use the code or FAT file-system library to access the files from an SD card. The easy prototyping platform Arduino provides a library for accessing the SD memory cards. The Arduino also supports several libraries for interfacing PS2 devices like mouse and keyboard.


 

The AVR microcontroller boards which are provided with all the basic circuitry for the operation of the microcontroller which has been flashed with the arduino boot-loader are called arduino boards. The arduino can communicate with the other devices using its digital I/O, serial port, I2C port, SPI port etc. The arduino IDE is so simple to use that anyone who has basic knowledge of c programming can quickly get started with it. The project on how to get started with the arduino explains about the steps required to get start with an arduino board.The arduino board used in this project is the arduino pro-mini board and the IDE version of the arduino is 1.0.3 for windows. The Arduino pro-mini board has ATMEGA328 microcontroller inside it which has an internal EEPROM memory of 1Kb.

The image of the arduino pro-mini board and the arduino IDE are shown below;

 Typical Arduino Pro-Mini Board

Fig. 2: Typical Arduino Pro-Mini Board

Arduino IDE Software Window

Fig. 3: Arduino IDE Software Window

 

Since the arduino pro-mini board has no circuitary for interfacing it with the serial port or the USB port of the PC, an external USB to TTL converter board is required to connect it with the PC. This hardware helps in programming the arduino board and also helps in the serial communication with the USB port of the PC.

 External USB To TTL Converter Board For Programming Arduino And Serial Communication

Fig. 4: External USB to TTL converter board for programming Arduino and serial communication

It is assumed that the reader has gone through the project how to get started with the arduino and tried out all the things discussed there.The memory card used in this particular project is a 2 GB SD card from Transcend but the code is supposed to work with SD card from all vendors. The SD card operates on 3.3V logic and hence to interface it with a microcontroller which runs on 5V logic one should use a Logic Level Converter. Since the memory card is interfaced using the SPI bus the four channel Logic Level Converter modules which are commonly available in the market can be used. The image of the Logic Level Converter module used in this project is shown in the following image;

Arduino

Fig. 5: Logic Level Converter Module Circuit

The SD memory card and the low voltage side of the Logic Level Converter should be provided with the 3.3V power supply and for that one can use any 3.3V regulator IC. A bi-color LED is suggested to connect across the 3.3V positive and MISO and MOSI lines of the SD card. The image of the memory card and the required circuitry that has been built for this particular project is shown in the following image. In the image one can see a potentiometer which is actually forms the circuit with an SMD variable regulator IC LM117 underneath it. It is recommended to use LM1117 which is a 3.3V regulator and which does not require other components to set the voltage as shown in the circuit diagram of this project.

Arduino

Fig. 6: Interfacing SD Memory card using Level Contoller With Arduino

The Arduino pro-mini board has digital pins marked as 2, 3, 4 up to 13.  Among the digital pins four pins namely 10, 11, 12 and 13 can be configured as SS, MOSI, MISO and SCK. The MISO of the memory card should be connected to the pin number 11, the MOSI should be connected to the pin number 12 and the SCK should be connected to the pin number 13 of the Arduino pro-min. The SS of the SD card should be connected to the pin which is defined as the SS pin of the Arduino in the code written. The previous projects on how to interface an SD card with the Arduino  and how to use SD card to store sensor value  discusses more about the details of interfacing the SD card with the Arduino. 

The project uses the PS2 connector to connect the Keyboard with the Arduino board. The PS2 connector has a pin for Data and another pin for Clock and using only these two pins the keyboard communicates with the host device. The mouse always has 6 pin mini-DIN male connector for PS2 interface and the host device always has the corresponding female pin. The images and the pin-outs of the PS2 male and female connectors are shown in the following image, the only difference between the PS2 keyboard and mouse connectors are in their color.

The image of the PS2 male pin

 6 Pin Mini-DIN Male Connector For PS2 Interface

Fig. 7: 6 pin mini-DIN Male connector for PS2 interface

The image of the PS2 female pin

 6 Pin Mini DIN Female Connector Plug For PS2 Interface

Fig. 8: 6 Pin Mini DIN Female Connector Plug for PS2 interface

The pin-out of the PS2 male and female connectors

 Pin-Out Of PS2 Male And Female Connectors

Fig. 9: Pin-Out Of PS2 Male and Female Connectors

When it comes to connecting the female connector with the circuit board one should be able to identify the pins at the bottom of the PS2 connector and the following image will be helpful.

Bottom Of Mini DIN Female Connector Plug For PS2 Interface

Fig. 10: Bottom of Mini DIN Female Connector Plug for PS2 interface

The code written for this project uses the custom PS2 library file called “PS2Keyboard.h” which has all the necessary routines for accessing a PS2 mouse and the details of how to use this library to interface a PS2 keyboard is already discussed in a previous project on how to interface the PS2 keyboard with the Arduino and how to connect the PS2 keyboard with LCD using Arduino. There are basically three functions which the user can directly make use in their code and are namely “keyboard.begin()”, keyboard.available() and “mouse.report(data)”.

The Arduino IDE provides a library called <SD.h> which has lot of functions to access the SD memory card. The library is able to access the FAT16 or FAT32 filesystem of the SD card using the AVR microcontroller of the Arduino board so that the files can be read, modify or write. The functions used in this particular project are SD.begin(), SD.open(), file. print(), file.read(), and file.close() from the library <SD.h> and the details regarding them are explained in a previous project on how to interface SD card with Arduino. 

As the code runs it first opens a particular file for reading and displays all its content in the Serial Monitor window. It then closes the same file and reopens it in the writing mode. After that the user can type in what all text into the memory card using the PS2 keyboard and they will get saved in it. The next time when the code runs it displays the same data which has been read from the SD card along with the previous data in the same file.

The data are displayed on the Serial monitor window with the help of the functions Serial.begin(),Serial.print() and Serial.println() which are already discussed in previous projects on how to do serial communication with the Arduino, how to send and receive serial data using arduino and how to do serial debugging with the Arduino. 

Before compiling the code make sure that the files “PS2Keyboard.h” and “PS2Keyboard.cpp” are kept in the same folder where the .pde file has been saved. When the coding is finished one can verify and upload the code to the Arduino board as explained in the project how to get started with the Arduino.

 

Project Source Code

###




/*================================= EG LABS =======================================

The demonstration of simple read write using Keyboard into the SD card

 

 The circuit:

 * SD card attached to SPI bus as follows:

 ** MOSI - pin 11

 ** MISO - pin 12

 ** CLK - pin 13

 ** CS - pin 4

 

 Keyboard:

 DATA PIN TO PIN NUMBER 8

 CLOCK PIN TO PIN NUMBER 3

============================== EG LABS ===================================*/


#include <SD.h>

#include "PS2Keyboard.h"


#define DATA_PIN 8


PS2Keyboard keyboard;

File myFile;                                                                  // variable required to hold the file descriptor

const int chipSelect = 6;                                                     // the pin number which is needs to be used as a SS pin

char dat;


void setup()

{

   pinMode(9, OUTPUT);     

   Serial.begin(9600);                                                        // initialize the serial port at baud rate 9600

   Serial.print("Initializing SD card...");                                  

   pinMode(10, OUTPUT);                                      // It is a must to set the hardware SS pin as output eventhough it is not using

  

   while(!SD.begin(chipSelect));                                              // initialize the SD card

   Serial.println("card initialized.");                                       

 

   while(!(myFile = SD.open("new.txt")));                                     // open the file for reading

   while (myFile.available())                                                 // read the file till the last byte

     Serial.write(myFile.read());

   myFile.close();                                                            // close the file


   keyboard.begin(DATA_PIN);                                                   // initialize the PS2 keyboard

   digitalWrite(9, HIGH);  

}


void loop()

{

  if(keyboard.available())

  {

    dat = keyboard.read();                                                     // read the data from the keyboard

    

    while(!(myFile = SD.open("new.txt", FILE_WRITE)));                         // open a file for writing

    myFile.print(dat);                                                         // write the string into the file

    myFile.close();       

    

    Serial.write(dat);

  }else;

}

//*************PS2.h**************//


#ifndef PS2Keyboard_h

#define PS2Keyboard_h

#include <avr/io.h>

#include <avr/interrupt.h>

#include <avr/pgmspace.h>


/*

 * PS2 keyboard "make" codes to check for certain keys.

 */


// Give these codes that aren't used by anything else

// Making all the control key codes above 0x80 makes it simple to check for

// printable characters at the calling level.

#define PS2_KC_BKSP    0x80

#define PS2_KC_UP      0x81

#define PS2_KC_DOWN    0x82

#define PS2_KC_LEFT    0x83

#define PS2_KC_RIGHT   0x84

#define PS2_KC_PGDN    0x85

#define PS2_KC_PGUP    0x86

#define PS2_KC_END     0x87

#define PS2_KC_HOME    0x88

#define PS2_KC_INS     0x89

#define PS2_KC_DEL     0x8A

#define PS2_KC_ESC     0x8B

#define PS2_KC_CLON    0x8C // caps_lock on

#define PS2_KC_CLOFF   0x8D // caps_lock off

#include "binary.h"

typedef uint8_t boolean;

typedef uint8_t byte;


/*

 * This PIN is hardcoded in the init routine later on. If you change this

 * make sure you change the interrupt initialization as well.

 */

#define PS2_INT_PIN 3


/**

 * Purpose: Provides an easy access to PS2 keyboards

 * Author:  Christian Weichel

 */

class PS2Keyboard {


  private:

    int  m_dataPin;

    byte m_charBuffer;


  public:

  /**

  * This constructor does basically nothing. Please call the begin(int)

  * method before using any other method of this class.

  */

  PS2Keyboard();


    /**

     * Starts the keyboard "service" by registering the external interrupt.

     * setting the pin modes correctly and driving those needed to high.

     * The propably best place to call this method is in the setup routine.

     */

    void begin(int dataPin);


    /**

     * Returns true if there is a char to be read, false if not.

     */

    bool available();


    /**

     * Sends a reset command to the keyboard and re-initialises all the control

     * variables within the PS2Keybaord code.

     */

    void reset();


    /**

     * Returns the char last read from the keyboard. If the user has pressed two

     * keys between calls to this method, only the later one will be availble. Once

     * the char has been read, the buffer will be cleared.

     * If there is no char availble, 0 is returned.

     */

    byte read();


    /**

     * Returns the status of the <ctrl> key, the <alt> key, the <shift> key and the

     * caps_lock state. Note that shift and caps_lock are handled within the

     * Ps2Keyboard code (and the return value from read() is already modified), but

     * being able to read them here may be useful.

     * This routine is optional BUT MUST ONLY be read after available() has returned

     * true and BEFORE read() is called to retrieve the character. Reading it after

     * the call to read() will return unpredictable values.

     */

    byte read_extra();


};


#endif
###

 


Project Source Code

###




#include <avr/io.h>

#include <avr/interrupt.h>

#include <avr/pgmspace.h>

#include "Arduino.h"

#include "PS2Keyboard.h"


#include "binary.h"

typedef uint8_t boolean;

typedef uint8_t byte;


/*

 * I do know this is so uncool, but I just don't see a way arround it

 * REALLY BAD STUFF AHEAD

 *

 * The variables are used for internal status management of the ISR. The're

 * not kept in the object instance because the ISR has to be as fast as anyhow

 * possible. So the overhead of a CPP method call is to be avoided.

 *

 * PLEASE DO NOT REFER TO THESE VARIABLES IN YOUR CODE AS THEY MIGHT VANISH SOME

 * HAPPY DAY.

 */

int  ps2Keyboard_DataPin;

byte ps2Keyboard_CurrentBuffer;

volatile byte ps2Keyboard_CharBuffer;

volatile byte ps2Keyboard_BufferPos;


// variables used to remember information about key presses

volatile bool ps2Keyboard_shift;     // indicates shift key is pressed

volatile bool ps2Keyboard_ctrl;      // indicates the ctrl key is pressed

volatile bool ps2Keyboard_alt;       // indicates the alt key is pressed

volatile bool ps2Keyboard_extend;    // remembers a keyboard extended char received

volatile bool ps2Keyboard_release;   // distinguishes key presses from releases

volatile bool ps2Keyboard_caps_lock; // remembers shift lock has been pressed


// vairables used in sending command bytes to the keyboard, eg caps_lock light

volatile boolean cmd_in_progress;

volatile int     cmd_count;

         byte    cmd_value;

volatile byte    cmd_ack_value;

         byte    cmd_parity;

volatile boolean cmd_ack_byte_ok;


// sending command bytes to the keybaord needs proper parity (otherwise the keyboard

// just asks you to repeat the byte)

byte odd_parity(byte val) {

  int i, count = 1;  // start with 0 for even parity

  for (i=0; i<8; i++) {

    if (val&1) count++;

    val = val>>1;

  }

  return count & 1; // bottom bit of count is parity bit

}


void kbd_send_command(byte val) {

  // stop interrupt routine from receiving characters so that we can use it

  // to send a byte

  cmd_in_progress = true;

  cmd_count       = 0;


  // set up the byte to shift out and initialise the ack bit

  cmd_value      = val;

  cmd_ack_value  = 1;    // the kbd will clear this bit on receiving the byte

  cmd_parity     = odd_parity(val);


  // set the data pin as an output, ready for driving

  digitalWrite(ps2Keyboard_DataPin, HIGH);

  pinMode(ps2Keyboard_DataPin, OUTPUT);


  // drive clock pin low - this is going to generate the first

  // interrupt of the shifting out process

  pinMode(PS2_INT_PIN, OUTPUT);

  digitalWrite(PS2_INT_PIN, LOW);


  // wait at least one clock cycle (in case the kbd is mid transmission)

  delayMicroseconds(60);


  // set up the 0 start bit

  digitalWrite(ps2Keyboard_DataPin, LOW);

  // let go of clock - the kbd takes over driving the clock from here

  digitalWrite(PS2_INT_PIN, HIGH);

  pinMode(PS2_INT_PIN, INPUT);


  // wait for interrupt routine to shift out byte, parity and receive ack bit

  while (cmd_ack_value!=0) ;


  // switch back to the interrupt routine receiving characters from the kbd

  cmd_in_progress = false;

}


void PS2Keyboard::reset() {

  kbd_send_command(0xFF);   // send the kbd reset code to the kbd: 3 lights

                            // should flash briefly on the kbd


  // reset all the global variables

  ps2Keyboard_CurrentBuffer = 0;

  ps2Keyboard_CharBuffer    = 0;

  ps2Keyboard_BufferPos     = 0;

  ps2Keyboard_shift         = false;

  ps2Keyboard_ctrl          = false;

  ps2Keyboard_alt           = false;

  ps2Keyboard_extend        = false;

  ps2Keyboard_release       = false;

  ps2Keyboard_caps_lock     = false;

  cmd_in_progress           = false;

  cmd_count                 = 0;

  cmd_value                 = 0;

  cmd_ack_value             = 1;

}


// val : bit_2=caps_lock, bit_1=num_lock, bit_0=scroll_lock

void kbd_set_lights(byte val) {

  // When setting the lights with the 0xED command the keyboard responds

  // with an "ack byte", 0xFA. This is NOT the same as the "ack bit" that

  // follows the succesful shifting of each command byte. See this web

  // page for a good description of all this:

  // http://www.beyondlogic.org/keyboard/keybrd.htm

  cmd_ack_byte_ok = false;   // initialise the ack byte flag

  kbd_send_command(0xED);    // send the command byte

  while (!cmd_ack_byte_ok) ; // ack byte from keyboard sets this flag

  kbd_send_command(val);     // now send the data

}


// The ISR for the external interrupt

// This may look like a lot of code for an Interrupt routine, but the switch

// statements are fast and the path through the routine is only ever a few

// simple lines of code.

void ps2interrupt (void) {

  int value = digitalRead(ps2Keyboard_DataPin);


  // This is the code to send a byte to the keyboard. Actually its 12 bits:

  // a start bit, 8 data bits, 1 parity, 1 stop bit, 1 ack bit (from the kbd)

  if (cmd_in_progress) {

    cmd_count++;          // cmd_count keeps track of the shifting

    switch (cmd_count) {

    case 1: // start bit

      digitalWrite(ps2Keyboard_DataPin,LOW);

      break;

    case 2: case 3: case 4: case 5: case 6: case 7: case 8: case 9:

      // data bits to shift

      digitalWrite(ps2Keyboard_DataPin,cmd_value&1);

      cmd_value = cmd_value>>1;

      break;

    case 10:  // parity bit

      digitalWrite(ps2Keyboard_DataPin,cmd_parity);

      break;

    case 11:  // stop bit

      // release the data pin, so stop bit actually relies on pull-up

      // but this ensures the data pin is ready to be driven by the kbd for

      // for the next bit.

      digitalWrite(ps2Keyboard_DataPin, HIGH);

      pinMode(ps2Keyboard_DataPin, INPUT);

      break;

    case 12: // ack bit - driven by the kbd, so we read its value

      cmd_ack_value = digitalRead(ps2Keyboard_DataPin);

      cmd_in_progress = false;  // done shifting out

    }

    return; // don't fall through to the receive section of the ISR

  }


  // receive section of the ISR

  // shift the bits in

  if(ps2Keyboard_BufferPos > 0 && ps2Keyboard_BufferPos < 11) {

    ps2Keyboard_CurrentBuffer |= (value << (ps2Keyboard_BufferPos - 1));

  }

  ps2Keyboard_BufferPos++; // keep track of shift-in position


  if(ps2Keyboard_BufferPos == 11) { // a complete character received

    switch (ps2Keyboard_CurrentBuffer) {

    case 0xF0: { // key release char

      ps2Keyboard_release = true;

      ps2Keyboard_extend = false;

      break;

    }

    case 0xFA: { // command acknowlegde byte

      cmd_ack_byte_ok = true;

      break;

    }

    case 0xE0: { // extended char set

      ps2Keyboard_extend = true;

      break;

    }

    case 0x12:   // left shift

    case 0x59: { // right shift

      ps2Keyboard_shift = ps2Keyboard_release? false : true;

      ps2Keyboard_release = false;

      break;

    }

    case 0x11: { // alt key (right alt is extended 0x11)

      ps2Keyboard_alt = ps2Keyboard_release? false : true;

      ps2Keyboard_release = false;

      break;

    }

    case 0x14: { // ctrl key (right ctrl is extended 0x14)

      ps2Keyboard_ctrl = ps2Keyboard_release? false : true;

      ps2Keyboard_release = false;

      break;

    }

    case 0x58: { // caps lock key

      if (!ps2Keyboard_release) {

ps2Keyboard_caps_lock = ps2Keyboard_caps_lock? false : true;

// allow caps lock code through to enable light on and off

        ps2Keyboard_CharBuffer = ps2Keyboard_CurrentBuffer;

      }

      else {

ps2Keyboard_release = false;

      }

      break;

    }

    default: { // a real key

      if (ps2Keyboard_release) { // although ignore if its just released

        ps2Keyboard_release = false;

      }

      else { // real keys go into CharBuffer

        ps2Keyboard_CharBuffer = ps2Keyboard_CurrentBuffer;

      }

    }

    }

    ps2Keyboard_CurrentBuffer = 0;

    ps2Keyboard_BufferPos = 0;

  }

}


PS2Keyboard::PS2Keyboard() {

  // nothing to do here 

}


void PS2Keyboard::begin(int dataPin) {

  // Prepare the global variables

  ps2Keyboard_DataPin       = dataPin;

  ps2Keyboard_CurrentBuffer = 0;

  ps2Keyboard_CharBuffer    = 0;

  ps2Keyboard_BufferPos     = 0;

  ps2Keyboard_shift         = false;

  ps2Keyboard_ctrl          = false;

  ps2Keyboard_alt           = false;

  ps2Keyboard_extend        = false;

  ps2Keyboard_release       = false;

  ps2Keyboard_caps_lock     = false;

  cmd_in_progress           = false;

  cmd_count                 = 0;

  cmd_value                 = 0;

  cmd_ack_value             = 1;


  // initialize the pins

  pinMode(PS2_INT_PIN, INPUT);

  digitalWrite(PS2_INT_PIN, HIGH);

  pinMode(dataPin, INPUT);

  digitalWrite(dataPin, HIGH);


  attachInterrupt(1, ps2interrupt, FALLING);

#if 0

  // Global Enable INT1 interrupt

  EIMSK |= ( 1 << INT1);

  // Falling edge triggers interrupt

  EICRA |= (0 << ISC10) | (1 << ISC11);

#endif

}


bool PS2Keyboard::available() {

  return ps2Keyboard_CharBuffer != 0;

}


// This routine allows a calling program to see if other other keys are held

// down when a character is received: ie <ctrl>, <alt>, <shift> or <shift_lock>

// Note that this routine must be called after available() has returned true,

// but BEFORE read(). The read() routine clears the buffer and allows another

// character to be received so these bits can change anytime after the read().

byte PS2Keyboard::read_extra() {

  return (ps2Keyboard_caps_lock<<3) |

         (ps2Keyboard_shift<<2) |

         (ps2Keyboard_alt<<1) |

          ps2Keyboard_ctrl;

}


byte PS2Keyboard::read() {

  byte result;


  // read the raw data from the keyboard

  result = ps2Keyboard_CharBuffer;


  // Use a switch for the code to character conversion.

  // This is fast and actually only uses 4 bytes per simple line

  switch (result) {

  case 0x1C: result = 'a'; break;

  case 0x32: result = 'b'; break;

  case 0x21: result = 'c'; break;

  case 0x23: result = 'd'; break;

  case 0x24: result = 'e'; break;

  case 0x2B: result = 'f'; break;

  case 0x34: result = 'g'; break;

  case 0x33: result = 'h'; break;

  case 0x43: result = 'i'; break;

  case 0x3B: result = 'j'; break;

  case 0x42: result = 'k'; break;

  case 0x4B: result = 'l'; break;

  case 0x3A: result = 'm'; break;

  case 0x31: result = 'n'; break;

  case 0x44: result = 'o'; break;

  case 0x4D: result = 'p'; break;

  case 0x15: result = 'q'; break;

  case 0x2D: result = 'r'; break;

  case 0x1B: result = 's'; break;

  case 0x2C: result = 't'; break;

  case 0x3C: result = 'u'; break;

  case 0x2A: result = 'v'; break;

  case 0x1D: result = 'w'; break;

  case 0x22: result = 'x'; break;

  case 0x35: result = 'y'; break;

  case 0x1A: result = 'z'; break;


    // note that caps lock only used on a-z

  case 0x41: result = ps2Keyboard_shift? '<' : ','; break;

  case 0x49: result = ps2Keyboard_shift? '>' : '.'; break;

  case 0x4A: result = ps2Keyboard_shift? '?' : '/'; break;

  case 0x54: result = ps2Keyboard_shift? '{' : '['; break;

  case 0x5B: result = ps2Keyboard_shift? '}' : ']'; break;

  case 0x4E: result = ps2Keyboard_shift? '_' : '-'; break;

  case 0x55: result = ps2Keyboard_shift? '+' : '='; break;

  case 0x29: result = ' '; break;


  case 0x45: result = ps2Keyboard_shift? ')' : '0'; break;

  case 0x16: result = ps2Keyboard_shift? '!' : '1'; break;

  case 0x1E: result = ps2Keyboard_shift? '@' : '2'; break;

  case 0x26: result = ps2Keyboard_shift? '£' : '3'; break;

  case 0x25: result = ps2Keyboard_shift? '$' : '4'; break;

  case 0x2E: result = ps2Keyboard_shift? '%' : '5'; break;

  case 0x36: result = ps2Keyboard_shift? '^' : '6'; break;

  case 0x3D: result = ps2Keyboard_shift? '&' : '7'; break;

  case 0x3E: result = ps2Keyboard_shift? '*' : '8'; break;

  case 0x46: result = ps2Keyboard_shift? '(' : '9'; break;


  case 0x0D: result = 't'; break;

  case 0x5A: result = 'n'; break;

  case 0x66: result = PS2_KC_BKSP;  break;

  case 0x69: result = ps2Keyboard_extend? PS2_KC_END   : '1'; break;

  case 0x6B: result = ps2Keyboard_extend? PS2_KC_LEFT  : '4'; break;

  case 0x6C: result = ps2Keyboard_extend? PS2_KC_HOME  : '7'; break;

  case 0x70: result = ps2Keyboard_extend? PS2_KC_INS   : '0'; break;

  case 0x71: result = ps2Keyboard_extend? PS2_KC_DEL   : '.'; break;

  case 0x72: result = ps2Keyboard_extend? PS2_KC_DOWN  : '2'; break;

  case 0x73: result = '5'; break;

  case 0x74: result = ps2Keyboard_extend? PS2_KC_RIGHT : '6'; break;

  case 0x75: result = ps2Keyboard_extend? PS2_KC_UP    : '8'; break;

  case 0x76: result = PS2_KC_ESC; break;

  case 0x79: result = '+'; break;

  case 0x7A: result = ps2Keyboard_extend? PS2_KC_PGDN  : '3'; break;

  case 0x7B: result = '-'; break;

  case 0x7C: result = '*'; break;

  case 0x7D: result = ps2Keyboard_extend? PS2_KC_PGUP  : '9'; break;


  case 0x58:

    // setting the keyboard lights is done here. Ideally it would be done

    // in the interrupt routine itself and the key codes associated wth

    // caps lock key presses would never be passed on as characters.

    // However it would make the interrupt routine very messy with lots

    // of extra state associated with the control of a caps_lock

    // key code causing a cmd byte to transmit, causing an ack_byte to

    // be received, then a data byte to transmit. Much easier done here.

    // The downside, however, is that the light going on or off at the

    // right time relies on the calling program to be checking for

    // characters on a regular basis. If the calling program stops

    // polling for characters at any point pressing the caps lock key

    // will not change the state of the caps lock light while polling

    // is not happening.

    result = ps2Keyboard_caps_lock? PS2_KC_CLON : PS2_KC_CLOFF;

    if (ps2Keyboard_caps_lock) kbd_set_lights(4);

    else                       kbd_set_lights(0);

    break;


    // Reset the shift counter for unexpected values, to get back into sink

    // This allows for hot plugging a keyboard in and out

  default:  delay(500); // but wait a bit in case part way through a shift

            ps2Keyboard_BufferPos  = 0;

            ps2Keyboard_shift      = false;

            ps2Keyboard_ctrl       = false;

            ps2Keyboard_alt        = false;

            ps2Keyboard_extend     = false;

            ps2Keyboard_release    = false;

            ps2Keyboard_caps_lock  = false;

  } // end switch(result)


  // shift a-z chars here (less code than in the switch statement)

  if (((result>='a') && (result<='z')) &&

      ((ps2Keyboard_shift && !ps2Keyboard_caps_lock) ||

       (!ps2Keyboard_shift && ps2Keyboard_caps_lock))) {

    result = result + ('A'-'a');

  }


  // done with the character

  ps2Keyboard_CharBuffer = 0;


  return(result);

}

###

 


Circuit Diagrams

Circuit-Diagram-Using-PS2-Keyboard-Store-Text-SD-Card-Using-Arduino

Project Components

  • Arduino Pro Mini
  • LED
  • Resistor

Project Video

Related Articles Read More >

Arduino home security system using Sim900 Gsm module, Pir motion detector and magnetic door contact switch
Fading/Controlling led/brightness using Potentiometer(Variable Resistor) and Arduino Uno
Displaying ASCII Characters on 16×2 lcd using 8051(89c51,89c52) Microcontroller
Fading led with LDR(Light Dependent Resistor) using Arduino uno

Stay Up To Date

Newsletter Signup

Popular Posts

Tic Tac Toe Multiplayer game in c++
Making blinking pattern of leds with 8051 microcontroller
properties of inductors
Basic Electronics 19 – Properties of inductors
GLCD 128×64 graphical lcd(GLCD) interfacing with 8051(89c51,89c52) microcontroller

EE Training Center Classrooms

“ee
“ee

Recent Articles

  • Renesas Electronics secures Bluetooth 5 connections with 32-bit RX23W MCU
  • iBASE introduces signaturePro 12-Port video wall signage player with outstanding capabilities
  • Microcontroller Project: STM32 low power modes analysis
  • Fujitsu starts shipping supercomputer Fugaku
  • RPi Python Programming 03 – Raspberry Pi as Linux System
Engineers Garage
  • Analog IC TIps
  • Connector Tips
  • DesignFast
  • EDABoard Forums
  • EE World Online
  • Electro-Tech-Online Forums
  • Microcontroller Tips
  • Power Electronic Tips
  • Sensor Tips
  • Test and Measurement Tips
  • About Us
  • Contact Us
  • Advertise

Copyright © 2019 WTWH Media LLC. All Rights Reserved. The material on this site may not be reproduced, distributed, transmitted, cached or otherwise used, except with the prior written permission of WTWH Media
Privacy Policy | Advertising | About Us

Search Engineers Garage

  • Projects and Tutorials
    • Circuit Design
    • Electronic Projects
      • 8051
      • Arduino
      • ARM
      • AVR
      • PIC
      • Raspberry pi
      • STM32
    • Tutorials
    • Components
  • Contributions
  • Articles
    • EG Blogs
    • Insight
    • Invention Stories
    • How to
  • What Is
  • Forums
    • EG Forum Archive
    • EDABoard.com
    • Electro-Tech-Online
  • Digi-Key Store
    • Cables, Wires
    • Connectors, Interconnect
    • Discrete
    • Electromechanical
    • Embedded Computers
    • Enclosures, Hardware, Office
    • Integrated Circuits (ICs)
    • Isolators
    • LED/Optoelectronics
    • Passive
    • Power, Circuit Protection
    • Programmers
    • RF, Wireless
    • Semiconductors
    • Sensors, Transducers
    • Test Products
    • Tools
  • News
    • EE Design News
    • DIY Reviews
    • Guest Post
    • Sponsored Content
  • EE Resources
    • DesignFast
    • LEAP Awards
    • Oscilloscope Product Finder
    • White Papers
    • Webinars
  • EE Classrooms
    • Grid Infrastructure
    • Aerospace & Defense
    • Building Automation
    • Power Delivery
    • Factory Automation
    • Motor Drives
    • Medical Technology
  • Video