PS/2 Keyboard code.

General discussion on mikroBasic PRO for PIC32.
Post Reply
Author
Message
Skyfall
Posts: 32
Joined: 06 Aug 2017 16:31

PS/2 Keyboard code.

#1 Post by Skyfall » 09 Oct 2020 14:10

Hi,
Has anyone had luck running this PS/2 Keyboard code on a PIC32MX470? See below.

Compiler is MikroBASIC pro for PIC32. Have wired up, as per the example. Processor is running above 6MHz (95MHz), with no background interrupts.
Have replaced AD1PCFG with ANSELB = 0. As this line doesn't compile with PIC32.
Looking for code that works. Don't care if it's polling or interrupt driven.

Thank you!


program PS2_Example

dim keydata, special, down as byte

dim PS2_Data as sbit at RD0_bit
PS2_Clock as sbit at RD1_bit
PS2_Data_Direction as sbit at TRISD0_bit
PS2_Clock_Direction as sbit at TRISD1_bit

main:
CHECON = 0x32
AD1PCFG = 0xFFFF ' Configure AN pins as digital I/O

UART1_Init(56000) ' Initialize UART module at 19200 bps

Ps2_Config() ' Init PS/2 Keyboard
Delay_ms(100) ' Wait for keyboard to finish
UART1_Write_Text("Ready") ' Ready
UART1_Write(13) ' Line Feed
UART1_Write(10) ' Carriage return


while TRUE ' Endless loop
if Ps2_Key_Read(keydata, special, down) then ' If data was read from PS/2
if (down <> 0) and (keydata = 16) then ' Backspace read
UART1_Write(0x08) ' Send Backspace to usart terminal
else
if (down <> 0) and (keydata = 13) then ' Enter read
UART1_Write(10) ' Send carriage return to usart terminal
UART1_Write(13) ' Uncomment this line if usart terminal also expects line feed
' for new line transition
else
if (down <> 0) and (special = 0) and (keydata <> 0) then ' Common key read
UART1_Write(keydata) ' Send key to usart terminal
end if
end if
end if
end if
Delay_ms(1) ' Debounce period
wend
end.

User avatar
filip
mikroElektronika team
Posts: 11874
Joined: 25 Jan 2008 09:56

Re: PS/2 Keyboard code.

#2 Post by filip » 13 Oct 2020 09:00

Hi,

Are you sure that the RD0 and RD1 are set as digital and the MCU is running at the correct frequency ?

Regards,
Filip.

Post Reply

Return to “mikroBasic PRO for PIC32 General”