PS/2 Keyboard Interface

Questions and Comments on All Tutorials

ThePyroElectro
PyroElectro Admin
Posts: 1181
Joined: Mon Nov 12, 2007 9:24 pm
Location: Earth
Contact:

PS/2 Keyboard Interface

Postby ThePyroElectro » Thu Sep 22, 2011 6:20 am

PS/2 Keyboard Interface

This article describes the basics of the PS/2 communication protocol using an example design of a PIC microcontroller and PS/2 keyboard. The PIC translates the PS/2 keyboard keypress inputs and displays the key as output on a 7-segment LED display.



Questions & Comments?

kiranjose
Newbie Pyro
Posts: 1
Joined: Thu Oct 27, 2011 11:23 pm
Location: Newcastle, UK

I think #pragma OSC=HSPLL needed

Postby kiranjose » Thu Oct 27, 2011 11:30 pm

I added #pragma OSC=HSPLL to make it work. It was not working with the code provided. :idea:

david toronto
Newbie Pyro
Posts: 4
Joined: Thu Nov 17, 2011 6:00 am

Re: PS/2 Keyboard Interface

Postby david toronto » Thu Nov 17, 2011 6:15 am

Wonderful tutorial, thank you. I have one question, and I apologize if it seems a bit simple but I'm just starting out and my first project will be like this one except even simpler, minus the lcd screen. I just want the keyboard to work! From my close reading of your tutorial, I believe that the ps/2 keyboard is NOT receiving any incoming data from the ps/2 port, just power. And with power alone it is able to send data out. It is not like a usb keyboard which needs to be enumerated by a usb host in order to behave like a keyboard. A ps/2 keyboard will send clock and scan codes regardless of whether it is attached to a PIC or a computer. Just give it 5V and it will behave like a keyboard. And if it is not, it is either broken or simply not getting enough voltage. Is this correct?

Thanks in advance!

david toronto
Newbie Pyro
Posts: 4
Joined: Thu Nov 17, 2011 6:00 am

Re: PS/2 Keyboard Interface

Postby david toronto » Thu Nov 17, 2011 6:25 am

I see that your battery pack is 8 x AA. I think my problem is that I am using 6 x AA. I will try again tomorrow!

david toronto
Newbie Pyro
Posts: 4
Joined: Thu Nov 17, 2011 6:00 am

Re: PS/2 Keyboard Interface

Postby david toronto » Thu Nov 17, 2011 6:29 am

I also am looking for a compact keyboard like the one you used for this tute. Where did you get yours?

david toronto
Newbie Pyro
Posts: 4
Joined: Thu Nov 17, 2011 6:00 am

Re: PS/2 Keyboard Interface

Postby david toronto » Thu Nov 17, 2011 6:46 am

"PS/2 is a serial protocol and only uses 2 pins to communication information." should read "...communicate information". Just want to help tidy up an excellent tutorial!

ThePyroElectro
PyroElectro Admin
Posts: 1181
Joined: Mon Nov 12, 2007 9:24 pm
Location: Earth
Contact:

Re: PS/2 Keyboard Interface

Postby ThePyroElectro » Thu Nov 17, 2011 4:48 pm

david toronto wrote:"PS/2 is a serial protocol and only uses 2 pins to communication information." should read "...communicate information". Just want to help tidy up an excellent tutorial!


Fixed that.

Thank you David!

nuetron
Newbie Pyro
Posts: 2
Joined: Thu Dec 15, 2011 8:10 pm

Re: PS/2 Keyboard Interface

Postby nuetron » Thu Dec 15, 2011 9:03 pm

Hi from nuetron at FourThreeOh!,

Great tutorial, wanted to help with some of your code. Instead of using so many #define's for the 7-seg display, you could use an array like this:

Code: Select all

#include "MSP430G2252.h"

const char CHAR[] = {
   0x3F, // 0
   0x06, // 1
   0x5B, // 2
   0x4F, // 3
   0x66, // 4
   0x6D, // 5
   0x7D, // 6
   0x07, // 7
   0x7F, // 8
   0x6F, // 9
   0x77, // A
   0x7C, // b
   0x39, // c
   0x5E, // d
   0x79, // E
   0x71, // F
   0x3D, // G
   0x76, // h
   0x30, // I
   0x1E, // j
   0x38, // l
   0x5C, // o
   0x73, // P
   0x60, // r
   0x1C, // u
   0x53, // ?
   0x00, // BLANK
};
const char BIT[8] = {1,2,4,8,16,32,64,128};
char DIGI[7] = {CHAR[12],CHAR[0],CHAR[15],CHAR[15],CHAR[14],CHAR[14],CHAR[24]};
int NUMBER=0;
int DIGIT=0;
int c = 0;

void main( void ){
   // Stop watchdog timer to prevent time out reset
   WDTCTL = WDTPW + WDTHOLD;
   BCSCTL1 = CALBC1_1MHZ;
   DCOCTL = CALDCO_1MHZ;

     P1OUT = 0x7F;
   P1DIR = 0x7F;
    P2SEL = 0x00;      // allow 2.6-2.7 to output
   P2OUT = 0x7F;
   P2DIR = 0x7F;
   P1IES = BIT7;
    P1IFG = 0;
    P1IE = BIT7;
   P2IES = BIT7;
    P2IFG = 0;
    P2IE = BIT7;
   DIGIT=NUMBER=0;
   __enable_interrupt();
   for(;;){
      while(c < 7){
        P1OUT = DIGI[c];
      P2OUT = ~BIT[c];
      __delay_cycles(5);
        P2OUT = 0xFF;
      c++;
      }
   c = 0;
   }
}

#pragma vector=PORT1_VECTOR
__interrupt void PORT1_ISR(void){
   NUMBER++;
   if(NUMBER > 10) NUMBER = 0;
   DIGI[DIGIT] = CHAR[NUMBER];
   P1IFG &= ~BIT7;
}

#pragma vector=PORT2_VECTOR
__interrupt void PORT2_ISR(void){
   DIGIT++;
   if(DIGIT == 7)
      DIGIT = 0;
   P2IFG &= ~BIT7;
}


This code is for a Texas Instruments MSP430G2252 microcontroller (see pdf), with a 7-seg-7-digit common-cathode display.

ThePyroElectro
PyroElectro Admin
Posts: 1181
Joined: Mon Nov 12, 2007 9:24 pm
Location: Earth
Contact:

Re: PS/2 Keyboard Interface

Postby ThePyroElectro » Fri Dec 16, 2011 3:42 am

nuetron wrote:Hi from nuetron at FourThreeOh!,

Great tutorial, wanted to help with some of your code. Instead of using so many #define's for the 7-seg display, you could use an array like this:

..
..
..

This code is for a Texas Instruments MSP430G2252 microcontroller (see pdf), with a 7-seg-7-digit common-cathode display.


Hi nuetron,

Thanks for the FYI. There's definitely a lot of different ways to implement it. The main reason I used #define's was because when you use them later in the code it's easier to tell which character you're displaying on the 7-seg led.

An array is definitely a better way to do it, in terms of quality code.

nuetron
Newbie Pyro
Posts: 2
Joined: Thu Dec 15, 2011 8:10 pm

Re: PS/2 Keyboard Interface

Postby nuetron » Fri Dec 16, 2011 8:57 pm

K.
Also, what are you using to communicate with the KBD? I don't see any reference to a UART or USI in your code... (or maybe I'm just blind...), unless you are bit-banging it, which I don't see either...

Anyway, I'm trying to write a program to put on an MSP430 that will receive the codes from a KBD, translate them into ASCII, and present them on a parallel bus, for another micro to read and print on a 16x2 character display. Page 401 of this datasheet describes the built-in shift register and control registers for the USI of an MSP.

If I've left anything out, please let me know, I have a habit of not finishing my thoughts.

Thanks ahead of time for any help rendered! :D

ThePyroElectro
PyroElectro Admin
Posts: 1181
Joined: Mon Nov 12, 2007 9:24 pm
Location: Earth
Contact:

Re: PS/2 Keyboard Interface

Postby ThePyroElectro » Fri Dec 16, 2011 9:30 pm

Hi nuetron,

What you described is pretty much all you need to do to get PS/2 communication with a keyboard running. The way I have done it in this tutorial is passive, the microcontroller only receives and doesn't transmit any ps/2 data. Some keyboards require that you send ps/2 commands to them before they start-up, so double check how your keyboard functions.

My code works by connecting the clock signal to an external interrupt pin and then triggering an interrupt with clock transitions. When the correct transition is detected, the current data signal (either 0 or 1) is shifted into a buffer. After all clock cycles pass, the buffer data is put onto a stack for the main process to evaluate. Each key press causes the keyboard to send multiple responses, so you need to build an algorithm to properly filter out the response to get at the data you want (probably the keyboard scancode of the key pressed).

My method is definitely not the best way, but I was shooting for simplicity.

joand8
Newbie Pyro
Posts: 1
Joined: Sun Dec 18, 2011 8:25 am

Re: PS/2 Keyboard Interface

Postby joand8 » Sun Dec 18, 2011 8:33 am

Hi!

Good tutorial, thank you, but I have a question
As I understand, you made schematic in the Proteus (right?) How do you add PS/2 port to the schematic? I searched this port in the standard devices, but I could not find it. Or, if you can, please upload schematic file. I need it for my work.
Thanks
And sorry for my English ))

ThePyroElectro
PyroElectro Admin
Posts: 1181
Joined: Mon Nov 12, 2007 9:24 pm
Location: Earth
Contact:

Re: PS/2 Keyboard Interface

Postby ThePyroElectro » Sun Dec 18, 2011 10:37 pm

joand8 wrote:Hi!

Good tutorial, thank you, but I have a question
As I understand, you made schematic in the Proteus (right?) How do you add PS/2 port to the schematic? I searched this port in the standard devices, but I could not find it. Or, if you can, please upload schematic file. I need it for my work.
Thanks
And sorry for my English ))


Hi,

I didn't use Proteus, I used a generic schematic tool and used their part creator to make a schematic symbol, there's no layout tool to go with it.

You can find PS/2 part libraries for Proteus on the internet on many websites.

r1ley
Newbie Pyro
Posts: 2
Joined: Mon Mar 25, 2013 4:30 am

Re: PS/2 Keyboard Interface

Postby r1ley » Mon Mar 25, 2013 4:33 am

does anyone know how I can configure this to display on a 16-segment display?

and use the other outputs like RB and RC?

any help would be great.

Thanks

ThePyroElectro
PyroElectro Admin
Posts: 1181
Joined: Mon Nov 12, 2007 9:24 pm
Location: Earth
Contact:

Re: PS/2 Keyboard Interface

Postby ThePyroElectro » Tue Mar 26, 2013 4:22 pm

r1ley wrote:does anyone know how I can configure this to display on a 16-segment display?

and use the other outputs like RB and RC?

any help would be great.

Thanks


You can wire up the 7 wires from the original design to the 16-seg or if you want to use all 16 segments, you would need to redefine the characters in the source code. Remap what output pins are +5v and +0v given which keyboard scan-code is detected.


Return to “Tutorials”

Who is online

Users browsing this forum: No registered users and 2 guests