Mikroelektronika MIKROE-350 Datenbogen

Seite von 526
Library Example
This simple example reads values of the pressed keys on the PS/2 keyboard and
sends them via UART.
program PS2_Example
dim keydata, special, down as byte
dim PS2_Data          as sbit at PINC.B0
PS2_Clock_Input   
as sbit at PINC.B1
PS2_Clock_Output  
as sbit at PORTC.B1
PS2_Data_Direction  
as sbit at DDRC.B0
PS2_Clock_Direction 
as sbit at DDRC.B1
main:
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("R")         ' Ready
while TRUE                                                   '
Endless loop
if (Ps2_Key_Read(keydata, special, down) <> 0) then
' If
data was read from PS/2
if (((down <> 0) and (keydata = 16)) <> 0) then
'
Backspace read
UART1_Write(0x08)                                      '
Send Backspace to USART terminal
else if (((down <> 0) and (keydata = 13)) <> 0) 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)) <> 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.
306
MIKROELEKTRONIKA
- SOFTWARE AND HARDWARE SOLUTIONS FOR EMBEDDED WORLD
Libraries
mikroBasic PRO for AVR
CHAPTER 6