|
|
Part: AN434
Category:
Description:
Company:
Datasheet: Download AN434 datasheet File size : 50 kB
Request For quote: Find where to buy AN434
Datasheet text preview:
Philips Semiconductors Microcontroller Products
Application note
Connecting a PC keyboard to the I2C-bus
AN434
CONNECTING A PC KEYBOARD TO THE I2C BUS
This application note illustrates the use of a low-cost 8-bit microcontroller--the 8XC751--to interface a standard PC/AT keyboard to the I2C bus. The 8XC751 (83C751 = ROM-version, 87C571 = EPROM-version) is ideally suited for the task thanks to its built-in I2C interface, small form-factor (24-pin DIP or 28-pin PLCC) and low power consumption (11mA typical @12 MHz; see Figure 1). The application software easily fits within the 2K bytes code and 64 bytes data memory provided on the 8XC751.
Keyboard-to-I2C Hardware (Figure 4)
The 8XC751 on-chip I2C interface allows direct connection of the SDA (Serial Data) and SCL (Serial Clock) pins to the corresponding I2C bus lines. Since the I2C bus is open collector (allowing multimasters), 10K resistors are used to pull the lines to the idle state between keypresses. The PC/AT keyboard interface is equally simple. The CLK output from the keyboard is used to generate an interrupt (INT0). In response, the 8XC751 interrupt service routine samples the keyboard serial DATA connected to port 0 bit 2 (P0.2). When used with a PC, the keyboard implements a bidirectional communication protocol by exploiting the fact that both the keyboard and PC can drive the open collector CLK and DATA lines. However, bidirectional communication is not required for basic keyboard operation and in this application, the keyboard is treated as an `input-only' device.
example, a keyboard SHIFT-MAKE, `A'-MAKE, SHIFT-BREAK, `A'-BREAK sequence is converted to the ASCII code for uppercase `A' (41H). The flowchart in Figure 5 depicts the keyboard data capture and code conversion process. The 8XC751 operates as an I2C slave. When the master issues a read command, the 8XC751 returns the converted ASCII character. The seven least significant bits are used for the ASCII code, while the most significant bit is used as a NEW flag (0 = new, 1 = old). The key code remains marked as new until the master issues a write to the 8XC751 at which point it is marked as old and will be overwritten by the next key processed. The keyboard-to-I2C software is shown immediately following Figure 5. Less than half the code space available on the 8XC751 is used, leaving room for extra features such as parity checking and more complete keyboard control state mapping using additional look-up tables.
The PC/AT Keyboard
The PC/AT keyboard transmits data in a clocked serial format consisting of a start bit, 8 data bits (LSB first), an odd parity bit and a stop bit as shown in Figure 2. Besides clock and data, the 5-pin connector (Figure 3) also includes power, ground and a no connect. Note that the PS/2 keyboard interface is logically equivalent, though it uses a different connector. (A sixth pin provides an additional no connect). When a key is pressed, the PC/AT keyboard transmits a `make' code and, when the key is released, a `break' code. The make code consists of an 8-bit `scan' code denoting the key pressed. The `break' code (key released) consists of the same 8-bit scan code preceded by a special code--0F0H. A notable difference from a regular ASCII keyboard is the way SHIFT, CTRL, ALT, etc. control keys work. For an ASCII keyboard, the control keys directly modify the code output. For example, a 61H (ASCII code for `a') is output if the `A' key is pressed by itself, while a 41H (ASCII code for `A') is output if the SHIFT and `A' keys are pressed simultaneously. The PC/AT keyboard handles such a key combination as two separate key presses, i.e., SHIFT-MAKE, `A'-MAKE, SHIFT-BREAK, `A'-BREAK. The `A' scan code (1CH) is the same for both the shifted and unshifted state. To determine whether the `A' scan code is interpreted as `A' or `a' the PC must keep track of the presence or absence of a prior SHIFT-MAKE.
Keyboard-to-I2C Software
The keyboard-to-I2C software performs three major functions:
P3.4/A4 1 P3.3/A3 2 P3.2/A2/A10 3 P3.1/A1/A9 4 P3.0/A0/A8 5 P0.2/VPP 6 P0.1/SDA/ 7 OEPGM P0.0/SCL/ 8 ASEL RST 9 DIP 24 V CC 23 P3.5/A5 22 P3.6/A6 21 P3.7/A7 20 P1.7/T0/D7 19 P1.6/INT1/D6 18 P1.5/INT0/D5 17 P1.4/D4 16 P1.3/D3 15 P1.2/D2 14 P1.1/D1 13 P1.0/D0
· Capture the clocked serial data from the
keyboard
· Translate the keyboard data to the
corresponding ASCII code
· Send the ASCII code as an I2C message.
When a key is pressed, the CLK output from the keyboard generates an interrupt via INT0. The 8XC751 shifts in the DATA from the keyboard on P0.2 (port 0, bit 2) and extracts the 8-bit scan code from the 11-bit packet. Next, the scan code is interpreted and converted to the corresponding ASCII code using a look-up table. Keyboard multi-code outputs are converted to single ASCII codes by tracking the state (i.e. shifted vs. unshifted) of the keyboard and using separate look-up tables for each. For
X2 10 X1 11 VS S 12
Figure 1. Pin Configuration
August 26, 1992
1
Philips Semiconductors Microcontroller Products
Application note
Connecting a PC keyboard to the I2C-bus
AN434
DATA CLK
start
D0
D1
D2
D3
D4
D5
D6
D7
parity
stop
20µs typ. 80µs typ. Figure 2. PC/AT Keyboard Timing
RESET
1
5
CLOCK
2 3
4
5 VOLTS
GND
DATA
Figure 3. Keyboard Connections (looking into the connector)
KEYBOARD CONNECTOR
VCC = 5V
1 2 N/C 3 4
5
CLK 1 2 10K 10K 10K 3 4 GND 5 6 7 8 1 µF 9 11.06MHz 10 11 33pF 12 N/C N/C N/C N/C N/C P0.2 SDA SCL RST X1 X2 VSS V CC N/C N/C 24 23 22 21 20 19 18 17 16 15 14 13 10K
DATA
8 7 C 7 5 1
N/C N/C N/C INT0 N/C N/C N/C N/C N/C
SDA SCL PWR
33pF
GND
GND
GND
Figure 4. IBM Keyboard to I2C Bus Format Using the 8XC751
August 26, 1992
2
Philips Semiconductors Microcontroller Products
Application note
Connecting a PC keyboard to the I2C-bus
AN434
Save registers
BIT# = 0 ?
Bit#=1 to 8 ?
Shift keyboard DATA line (P0.2) into variable KEY.
Bit#=9 ?
Parity check (optional)
KEY=SHIFT ?
LASTKEY = BREAK code ?
Set SHIFT flag
Clear SHIFT flag CTRL key handling (optional)
KEY=CTRL ?
KEY=BREAK code ?
LASTKEY=BREAK code
LASTKEY = BREAK code ?
LASTKEY=KEY
SHIFT flag set ?
Translate to ASCII using `shifted' look-up table
Translate to ASCII using `unshifted look-up table
BIT#=0
BIT#=BIT#+1
Restore Registers
Figure 5. Keyboard Data Capture and Conversion August 26, 1992 3
Philips Semiconductors Microcontroller Products
Application note
Connecting a PC keyboard to the I2C-bus
AN434
0001 0002 0003 0004 0005 0006 0007 0008 0009 0010 0011 0012 0013 0014 0015 0016 0017 0018 0019 0020 0001+ 0002+ 0003+ 0004+ 0005+ 0006+ 0007+ 0008+ 0009+ 0010+ 0011+ 0012+ 0013+ 0014+ 0015+ 0016+ 0017+ 0018+ 0019+ 0020+ 0021+ 0022+ 0023+ 0024+ 0025+ 0026+ 0027+ 0028+ 0029+ 0030+ 0031+ 0032+ 0033+ 0034+ 0035+ 0036+ 0037+ 0038+ 0039+ 0040+ 0041+ 0042+ 0043+ 0044+ 0045+ 0046+ 0047+
0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000
;******************************** ; ; ; Copyright Micro AMPS Ltd ; ; & Philips Semiconductors ; ; Dec 1990 ; ; ; ;******************************** ; Read data under interrupt from an IBM keyboard ; Hardware resources: ; Kbd clock on interrupt INT0 P1.5 ; Kbd data on pin P0.2
; This program reads keys in from the keyboard ; and translates them to ASCII
#include equates.51 ; direct addresses for the standard 8051 processor p0 sp dpl dph pcon tcon tmod tl0 th0 th1 p1 scon s0con s0buf p2 p3 ien0 ie psw acc b .equ .equ .equ .equ .equ .equ .equ .equ .equ 80h 81h 82h 83h 87h 88h 89h 8ah 8ch ; ; ; ; ; ; ; ; ; port 0 stack pointer data pointer low data pointer high power timer timer timer timer control control mode 0 low 0 high
.equ 8dh .equ .equ .equ .equ .equ .equ .equ .equ 90h 98h 98h 99h 0a0h 0b0h 0a8h 0a8h
; timer 1 high ; ; ; ; port 1 serial control serial control serial data
; port 2 ; port 3 ; interrupt enable
.equ 0d0h .equ 0e0h .equ 0f0h
; program status word ; accumulator ; b register
; bit addressed flags it0 ie0 it1 ie1 tr0 tf0 tr1 tf1 ri ti ien0.7 p0.0 .equ .equ .equ .equ .equ .equ .equ .equ 88h 89h 8ah 8bh 8ch 8dh 8eh 8fh ; ; ; ; ; ; ; ; int 0 int 0 int 1 int 1 timer timer timer timer edge/level trigger edge detect edge/level trigger edge detect 0 enable/disable 0 overflow detect 1 enable/disable 1 overflow detect
.equ 98h .equ 99h .equ 0afh .equ 080h ; global int enable/disable ; port 0 bit 0
August 26, 1992
4
Philips Semiconductors Microcontroller Products
Application note
Connecting a PC keyboard to the I2C-bus
AN434
0048+ 0049+ 0050+ 0051+ 0052+ 0053+ 0054+ 0055+ 0056+ 0057+ 0058+ 0059+ 0060+ 0061+ 0062+ 0063+ 0064+ 0065+ 0066+ 0067+ 0068+ 0021 0001+ 0002+ 0003+ 0004+ 0005+ 0006+ 0007+ 0008+ 0009+ 0010+ 0011+ 0012+ 0013+ 0014+ 0015+ 0016+ 0017+ 0018+ 0019+ 0020+ 0021+ 0022+ 0023+ 0024+ 0025+ 0026+ 0027+ 0028+ 0029+ 0030+ 0031+ 0032+ 0033+ 0034+ 0035+ 0036+ 0037+ 0038+ 0039+ 0040+ 0041+ 0042+ 0043+ 0044+ 0045+
0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000
b.0 b.1 b.2 b.3 b.4 b.5 b.6 b.7 a.0 a.1 a.2 a.3 a.4 a.5 a.6 a.7 rth rtl
.equ .equ .equ .equ .equ .equ .equ .equ .equ .equ .equ .equ .equ .equ .equ .equ
0f0h 0f1h 0f2h 0f3h 0f4h 0f5h 0f6h 0f7h 0e0h 0e1h 0e2h 0e3h 0e4h 0e5h 0e6h 0e7h
; b reg bits
; accumulator bits
.equ 8dh .equ 8bh
; timer 0 reload high ; timer 0 reload low
#include kbd.h #define reg .equ ; ; 8xc751 special register set ; ; 751 I2C byte registers I2CON I2CFG I2DAT I2STA IE TCON TL TH RTL RTH .equ .equ .equ .equ 098h 0d8h 099h 0f8h ; ; ; ; I2C I2C I2C I2C control configuration data status
.equ 0a8h .equ 088h .equ .equ .equ .equ 08ah 08ch 08bh 08dh
; interrupt enable ; timer/counter control ; ; ; ; timer timer timer timer 0 low 0 high reload low reload high
; 751 I2C bit registers ;I2CNFG SLAVEN MASTRQ TIRUN CT1 CT0 CLRTI RDAT ATN DRDY ARL STR STP MASTER ; I2CON CXA IDLE CDR .equ 09fh .equ 09eh .equ 09dh .equ .equ .equ .equ .equ .equ .equ .equ .equ .equ .equ .equ .equ 0dfh 0deh 0dch 0d9h 0d8h 0ddh 09fh 09eh 09dh 09ch 09bh 09ah 099h
August 26, 1992
5
Others parts begin by an
AN-1 AN-2 AN-3 AN-4 AN-5 AN-6 AN-7 AN-8
|
|
|