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!.
 
Download mikroBasic and search for "keyboard" or "PS/2" with the help file search tool.

It has a complete set of integrated tools for PS/2 keyboard control
 
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

 
Last edited:
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…