Mikroelektronika MIKROE-724 データシート

ページ / 726
mikroBasic PRO for dsPIC30/33 and PIC24
MikroElektronika
431
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
dim keydata, special, down as byte
dim PS2_Data            as sbit at RB0_bit
    PS2_Clock           as sbit at RB1_bit
    PS2_Data_Direction  as sbit at TRISB0_bit
    PS2_Clock_Direction as sbit at TRISB1_bit
main:
  ADPCFG = 0xFFFF            ‘ Configure AN pins as digital I/O
  UART1_Init(19200)          ‘ 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(10)                                      ‘ Debounce period
  wend
end.