User manual

Table Of Contents
mikroC PRO for PIC32
MikroElektronika
399
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
unsigned short keydata , special , down;
sbit PS2_Data at RD0_bit;
sbit PS2_Clock at RD1_bit;
sbit PS2_Data_Direction at TRISD0_bit;
sbit PS2_Clock_Direction at TRISD1_bit;
void main() {
CHECON = 0x32;
AD1PCFG = 0xFFFF; // Congure AN pins as digital I/O
TRISD = 0;
LATD = 0;
UART1_Init(56000); // 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
//UART1_Write(‘\n’); // uncomment this line if usart terminal also
expects line feed
// for new line transition
}
else if (down && !special && keydata) {
UART1_Write(keydata); // Send key to usart terminal
}
}
Delay_ms(1); // Debounce period
} while (1);
}