
The example shows the connection of a device from the dsPIC30F family to a standard PS/2 keyboard. It is important to note that all pins of the PS/2 keyboard connected to the dsPIC30F device are connected to the power supply by the pull-up resistors. The realization is carried out by using the mikroC compiler for dsPIC30F microcontrollers.
unsigned int
keydata = 0, special = 0, down = 0;
void main() {
ADPCFG = 0xFFFF;
Uart1_Init(9600);
Ps2_Init(&PORTC); // Init PS/2 Keyboard on PORTC
// pin 13 is connected to Data line
// pin 14 is connected to Clock line
Delay_ms(100); // Wait for keyboard to finish
Uart1_Write_Char('s'); Uart1_Write_Char('t'); Uart1_Write_Char('a');
Uart1_Write_Char('r'); Uart1_Write_Char('t'); Uart1_Write_Char('!');
do {
if(Ps2_Key_Read(&keydata, &special, &down)) {
if(down && (keydata == 16)) {// Backspace
Uart1_Write_Char(0x08);
}
else if(down && (keydata == 13)) {// Enter
Uart1_Write_Char('r'); // send carriage return to usart terminal
//Uart1_Write_Char('n');//uncomment this line if usart terminal also expects line feed
// for new line transition
}
else if(down && !special && keydata) {
//Uart1_Write_Char(keydata >> 8);
Uart1_Write_Char(keydata);
}
}
Delay_ms(1); // debounce
} while(1);
}//~