User manual
mikroPascal PRO for dsPIC30/33 and PIC24
MikroElektronika
430
Library Example
This simple example reads values of the pressed keys on the PS/2 keyboard and sends them via UART.
Copy Code To Clipboard
program PS2_Example;
var keydata, special, down : byte;
var PS2_Data : sbit at RB0_bit;
PS2_Clock : sbit at RB1_bit;
PS2_Data_Direction : sbit at TRISB0_bit;
PS2_Clock_Direction : sbit at TRISB1_bit;
begin
ADPCFG := 0xFFFF; // Congure AN pins as digital I/O
UART1_Init(19200); // Initialize UART module at 19200 bps
Ps2_Cong(); // Init PS/2 Keyboard
Delay_ms(100); // Wait for keyboard to nish
UART1_Write_Text(‘Ready’); // Ready
UART1_Write(13); // Line Feed
UART1_Write(10); // Carriage return
while TRUE do // Endless loop
begin
if Ps2_Key_Read(keydata, special, down) then // If data was read from PS/2
begin
if (down <> 0) and (keydata = 16) then // Backspace
begin
UART1_Write(0x08); // Send Backspace to usart terminal
end
else if (down <> 0) and (keydata = 13) then // Enter
begin
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
end
else if (down <> 0) and (special = 0) and (keydata <> 0) then // Common key
read
begin
UART1_Write(keydata); // Send key to usart terminal
end;
end;
Delay_ms(1); // Debounce period
end;
end.