User manual

mikroC PRO for dsPIC
MikroElektronika
455
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
sbit PS2_Data at RB0_bit;
sbit PS2_Clock at RB1_bit;
sbit PS2_Data_Direction at TRISB0_bit;
sbit PS2_Clock_Direction at TRISB1_bit;
unsigned short keydata = 0, special = 0, down = 0;
void main() {
ADPCFG = 0xFFFF; // Congure AN pins as digital I/O
UART1_Init(19200); // Initialize UART module at 19200 bps
Ps2_Cong(); // Init PS/2 Keyboard
Delay_ms(100); // Wait for keyboard to nish
UART1_Write_Text("Ready");
UART1_Write(10); // Line Feed
UART1_Write(13); // Carriage return
do {
if (Ps2_Key_Read(&keydata, &special, &down)) {
if (down && (keydata == 16)) { // Backspace
UART1_Write(0x08);
}
else if (down && (keydata == 13)) { // Enter
UART1_Write('r'); // send carriage return to usart
terminal
//Usart_Write('n'); // uncomment this line if usart
terminal also expects line feed
// for new line transition
}
else if (down && !special && keydata) {
UART1_Write(keydata);
}
}
Delay_ms(1); // debounce
} while (1);
}