0% found this document useful (0 votes)
27 views30 pages

Lec9 Credit KeyBoard

The document discusses how keyboards are interfaced with CPUs. It explains that keyboards use a matrix of rows and columns, and that pressing a key creates a connection between a specific row and column. It then summarizes some of the key points about how PC keyboards work, including how they use an 8042 microcontroller to continuously scan keys and present scan codes to the motherboard CPU to identify which key was pressed.

Uploaded by

Yousef Elsawy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views30 pages

Lec9 Credit KeyBoard

The document discusses how keyboards are interfaced with CPUs. It explains that keyboards use a matrix of rows and columns, and that pressing a key creates a connection between a specific row and column. It then summarizes some of the key points about how PC keyboards work, including how they use an 8042 microcontroller to continuously scan keys and present scan codes to the motherboard CPU to identify which key was pressed.

Uploaded by

Yousef Elsawy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 30

Keyboard

Problem 1: How to connect many input wires to a system?


INTERFACING KEYBOARD TO CPU

• Keyboards are organized in a matrix of rows and


columns, and the CPU accesses rows & columns
through ports.
– With two 8-bit ports, an 8 x 8 matrix of keys can be
connected to a microprocessor.
• On a keypress, a row & column make contact.
– Otherwise, there is no connection between rows/columns.
INTERFACING KEYBOARD TO CPU

4 x 4 matrix connected
to two ports.
If no key has been pressed,
the input port will yield 1s for
all columns, since they are
all connected to high. (Vcc)

If all the rows are grounded


and a key is pressed, one of
the columns will have 0.
The key pressed provides
the path to ground.

Matrix Keyboard Connection to Ports


INTERFACING KEYBOARD TO CPU
grounding rows and reading the columns
• To detect the key pressed, the processor grounds
all rows by providing 0 to the output latch, and then
reads the columns.
– If data read from the columns is D3–D0 = 1111, no key
has been pressed
• The process continues until a key press is detected.
– If one of the column bits has a zero, a key press has
occurred.
INTERFACING KEYBOARD TO CPU
grounding rows and reading the columns
• To identify the key, the microprocessor, starting with
the top row, grounds it by providing a low to row D0
only, then it reads the columns.
– If the data read is all 1s, no key in that row is activated.
• The process is moved to the next row.
– This process continues until the row is identified.
INTERFACING KEYBOARD TO CPU
grounding rows and reading the columns
• After identification of the row, the next task is to find
out which column the pressed key belongs to.
– Easy, since the CPU knows at any time which row and
column are being accessed.
Problem 2: Could we reduce it again?

•In PC keyboards, a single microcontroller takes care of


hardware & software interfacing of the keyboard.
– It scans keys continuously, identifies which one has
been activated & presents it to the motherboard CPU.
PC KEYBOARD INTERFACING/PROGRAMMING

• The keyboard microcontroller used widely in the PC


and compatibles is Intel's 8042. (or some variation)
– A scan code is assigned to each key, and the controller
provides the code for the pressed key to the motherboard.
• IBM PC/AT keyboards use the following data frame
to send scan code serially to the motherboard.
– For each scan code, a total of 11 bits are transferred.
• One start bit (always 0)
• 8 bits for scan code
• Odd parity bit
• One stop bit (always 1)
PC KEYBOARD INTERFACING/PROGRAMMING
make and break
• In the IBM PC, the key press and release are
represented by two different scan codes.
– The key press is referred to as a make, for which
the keyboard sends a scan code.
– Release of the same key is called a break, for which
another scan code is sent.
• The scan code for the break is always 127 decimal (80H)
larger than the make scan code.
PC KEYBOARD INTERFACING/PROGRAMMING
IBM PC scan codes
• The original IBM PC keyboard had 83 keys in three
major groupings:
– 1. The standard typewriter keys
– 2. Ten function keys, F1 to F10
– 3. 15-key keypad

IBM later introduced


the the enhanced
keyboard, with the
number of keys
increased to 101.
PC KEYBOARD INTERFACING/PROGRAMMING
INT 09 interrupt service
• To understand fully principles in the PC keyboard,
it is necessary to know how INT 09 works.
– The PC keyboard communicates with the motherboard
through hardware interrupt IRQ1 of the 8259.

Keyboard Cable Jack for the PC


PC KEYBOARD INTERFACING/PROGRAMMING
INT 09 interrupt service sequence
1. The keyboard microcontroller scans the keyboard
matrix continuously.
– When a key is pressed (a make)…
• It is identified and its scan code is sent serially to the
motherboard through the keyboard cable.
– The circuitry on the motherboard…
• Receives the serial bits.
• Gets rid of the frame bits
• Makes one byte (scan code) with its serial-in-parallel-out
shift register
• Presents this 8-bit scan code to port A of 8255 at
I/O address 60H.
• Activates IRQ1.
8255 Scan code

(60)

IRQ0 IRQ7
IRQ1

Interrupt controller
Execute interrupt service routine 9 (ISR 9)
PC KEYBOARD INTERFACING/PROGRAMMING
INT 09 interrupt service sequence
2. Since IRQ1 is set to INT 09, its interrupt service
routine (ISR) residing in BIOS ROM is invoked.
3. ISR of INT 09 reads the scan code from port 60H.
4. ISR of INT 09 tests the scan code to see if it is
the Right or Left Shift, Alt, Ctrl keys, etc.
– If so, the appropriate bit of the keyboard status
bytes in BIOS 0040:0017H and 0018H are set.
• It will not write the scan code to the keyboard buffer.
5. Before returning from INT 09, ISR will issue EOI
to unmask IRQ1, followed by the IRET instruction.
– This allows IRQ1 activation to be responded to again.
PC KEYBOARD INTERFACING/PROGRAMMING
INT 09 interrupt service sequence
6. When the key is released (a break), the keyboard
generates the second scan code by adding 80H to
it and sends it to the motherboard.
7. ISR of INT 09 checks the scan code to see if there
is 80H difference between this code and the one.
– If D7 is high, it is interpreted as meaning the key has
been released & the system ignores the 2nd scan code.
– If the key is held down more than 0.5 seconds, it is
interpreted as a new key and INT 09 will write it into
the keyboard buffer next to the preceding one.
• Commonly referred to as typematic in IBM literature,
which means repeating the same key.
PC KEYBOARD INTERFACING/PROGRAMMING
keyboard overrun
• On the keyboard side, the 8042 must serialize the
scan code & send it by cable to the motherboard.
• On the motherboard side, circuits get the serial
data & make a single byte of scan code out of
the bit streams, and holds it for the CPU to read.
• The CPU can fall behind, and fail to keep up with
the number of keystrokes, called keyboard overrun.
– The motherboard beeps the speaker when overrun occurs.
• BIOS ROM on the motherboard is responsible for
beeping the speaker in the event of keyboard
overrun.
How to save
data in
memory?
Problem 3: The memory is limited.
How to save data and reuse the same locations
PC KEYBOARD INTERFACING/PROGRAMMING
BIOS keyboard buffer
• INT 09 gets the scan code from the keyboard and
stores it in memory locations in the BIOS data area.
– Referred to as the BIOS keyboard buffer, it should not
be confused with the buffer in the keyboard itself.
• 32 bytes (16 words) of BIOS data memory is set
aside, at addresses 40:001EH - 40:003DH.
– Physical addresses 0041EH and 0043DH.
• Each two consecutive locations are used for a
single character.
– One for the scan code, the other for the ASCII code (if
any) of the character.
buffer pointers - head pointer

• As INT 16H reads a character from the keyboard


buffer, it advances the head pointer, which is held
by memory locations 41AH and 41BH.
– As INT 09 inserts the character into the keyboard buffer,
it advances the tail.
– As INT 16H reads the character from the keyboard buffer
it advances the head.
PC KEYBOARD INTERFACING/PROGRAMMING
buffer pointers
• As INT 16H reads a character from the keyboard
buffer, it advances the head pointer, which is held
by memory locations 41AH and 41BH.
– When they come to the end of the keyboard buffer, they
both wrap around, creating a ring of 16 words where the
head is continuously chasing the tail.
– If the buffer is empty, head address equals tail address.

Keyboard Buffer Head and Tail


What about
Shift/CTRL
keys?
Problem 4: How to save them in the buffer
and how to consume them?
They are not independent key
PC KEYBOARD INTERFACING/PROGRAMMING
IBM PC scan codes
• The same scan code is used for a given lowercase
letter & capital, as for all the keys with dual labels.
– location 0040:0017H holds the shift status byte.

When a key is pressed, the interrupt service routine of INT 9


receives the scan code and stores it in a memory location
called a keyboard buffer, located in the BIOS data area.
PC KEYBOARD INTERFACING/PROGRAMMING
IBM PC scan codes
• The same scan code is used for a given lowercase
letter & capital, as for all the keys with dual labels.
– location 0040:0018H holds the second status byte.

Some of the bits are used for the 101-key enhanced keyboards.
To relieve programmers from details of keyboard and
motherboard interaction, IBM provides INT 16H.
PC KEYBOARD INTERFACING/PROGRAMMING
IBM PC scan codes
• INT 16H, AH = 0 (read a character) - checks the
keyboard buffer for a character.
– If a character is available, it returns its scan code in AH
and its ASCII code in AL.
– If no character is available in the buffer, it waits for a
key press and returns it.
– For characters for which there is no ASCII code, it provides the
scan code in AH and AL = 0.
• Such as F1–F10.
PC KEYBOARD INTERFACING/PROGRAMMING
IBM PC scan codes
• INT 16H, AH = 01 (find if a character is available) -
checks the keyboard buffer for a character.
– If a character is available, it returns its scan code in AH, its
ASCII code in AL, and sets ZF = 0.
– If no character is available in the buffer, it does not wait for
a key press, and simply makes ZF = 1.
PC KEYBOARD INTERFACING/PROGRAMMING
IBM PC scan codes
• INT 16H, AH = 02 (return current keyboard status
byte) - provides keyboard status in register AL.
– The keyboard status byte (also referred to as the
keyboard flag byte) is located in the BIOS data area
memory location 0040:0017H.

Fig. 18-3 First Keyboard Status Byte


PC KEYBOARD INTERFACING/PROGRAMMING
IBM PC scan codes
• INT 16H, AH = 02 (return current keyboard status
byte) - provides keyboard status in register AL.
PC KEYBOARD INTERFACING/PROGRAMMING
PC keyboard technology
• The keyboard shown is 4 x 4 matrix connected
a hard contact keyboard. to two ports.
– When a key is pressed,
a physical contact in the
row & column causes
the column to be pulled
to ground.
– The alternative to hard contact
keyboards are capacitive
keyboards.
• No physical contact.
• There is a capacitor for
each point of the matrix.

Matrix Keyboard Connection to Ports

You might also like