Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

How to read the computer keyboard with PICmicrocontrollers?

Status
Not open for further replies.
Check the EPE website, they did a project on it a number of years ago, or check the PICList, they have code as well - or google, there's plenty of code out there!.
 
From the help file;

This simple example reads values of keys pressed on PS/2 keyboard and sends them via USART.

Code:
program ps2_test
dim keydata, special, down as byte

main:
  CMCON  = $07           ' Disable analog comparators (comment this for P18)
  INTCON = 0             ' Disable all interrupts
  Ps2_Init(PORTA)        ' Init PS/2 Keyboard on PORTA
  Delay_ms(100)          ' Wait for keyboard to finish

  do
    if Ps2_Key_Read(keydata, special, down) = 1 then
      if (down = 1) and (keydata = 16) then  ' Backspace
        ' ...do something with a backspace...
      else
        if (down = 1) and (keydata = 13) then  ' Enter
          Usart_Write(13)
        else
          if (down = 1) and (special = 0) and (keydata <> 1) then
            Usart_Write(keydata)
          end if
        end if
      end if
    end if
    Delay_ms(10)    ' debounce
  loop until FALSE
end.

The PS2_Init function will tell the compiler how the keyboard is connected

Ps2_Init(dim byref port as byte)

Initializes port for work with PS/2 keyboard, with default pin settings. Port pin 0 is Data line, and port pin 1 is Clock line.

You need to call either Ps2_Init or Ps2_Config before using other routines of PS/2 library.
 
Last edited:
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top