Interfacing a Microprocessor to a Keyboard

When you press a key on a computer keyboard, you are actually activating a switch. These switches are made in various ways. Below are four main types of keyboard switches explained in detail.

1. Mechanical Key Switches

In a mechanical switch, there are two pieces of metal. When you press a key, these two metal parts touch each other and create an electrical connection. Through this connection, the keyboard tells the computer that the specific key has been pressed.

1.2. What is a Mechanical Switch Made Of?

The construction of a mechanical switch is very strong and durable:

  • Metal Parts: The metal components inside the switch are typically made of phosphor-bronze, a special alloy.
  • Gold Plating: The contact points are coated with gold plating. Gold is an excellent conductor and prevents rust or oxidation, ensuring the switch works well even after long-term use.
  • Spring: Inside the switch, there is a small spring. When you release the key after pressing it, this spring returns the key to its original (top) position.
  • Foam or Rubber: Many switches have a small piece of foam or rubber that helps reduce bouncing (rapid connecting and disconnecting) when the key is pressed.

1.3. Modern Mechanical Switches

Many mechanical switches use a silicone dome instead of metal parts:

  • It looks like a small rubber dome.
  • The underside of this dome has conductive rubber attached to it.
  • When a key is pressed, the rubber dome collapses and the conductive rubber underneath shorts (connects) two traces on the circuit board (PCB). This signals the computer that the key has been pressed.

1.4 Problems or Disadvantages of Mechanical Switches

Although mechanical switches are relatively inexpensive, they have some notable issues:

  1. Contact Bounce: When a key is pressed, the metal parts do not settle immediately. Instead, they rapidly connect and disconnect (bounce) several times within a few milliseconds. This can cause the computer to register a single key press as multiple presses. This problem is fixed through software using debouncing techniques.

  2. Oxidation and Dirt: Over time, the metal parts may develop rust (oxidation) or accumulate dust. This prevents proper electrical connection and can cause keys to stop working.

1.5. Lifespan of Mechanical Switches

Durability depends on the quality of the switch:

  • High-quality metal switches: Typically last about 1 million keystrokes.
  • Silicone dome switches: These are much more durable and last about 25 million keystrokes. This is why silicone dome switches are more commonly used in modern high-quality keyboards.

Examples: Old IBM keyboards, Mechanical gaming keyboards (e.g., Cherry MX switches)

Observed Damage in Macul and Ñuñoa
Mechanical Keyboard

2. Membrane Key Switches

Membrane switches are actually a special type of mechanical switch. However, instead of metal parts, they use thin plastic or rubber layers (membranes).

2.1. Structure of a Membrane Switch

A membrane switch is made like a three-layer “sandwich” :

LayerDescription
Top LayerThin plastic or rubber layer with silver ink conductive lines on the underside. Each key has one line beneath it.
Middle LayerA perforated (hole-filled) insulating layer. Holes are present where keys are located. This layer keeps the top and bottom layers separate.
Bottom LayerHas silver ink conductive lines on its upper side. Each column has one line.
Observed Damage in Macul and Ñuñoa
Membrane Keyboard

2.2. How Does a Membrane Switch Work?

  1. Normal State: The conductive lines of the top and bottom layers remain separate due to the holes in the middle layer. No connection is made.

  2. Key Pressed: When you press a key, the top layer is pushed downward.

  3. Connection Made: The top layer touches the bottom layer through the holes in the middle layer.

  4. Circuit Completes: The conductive lines of the top and bottom layers touch each other, completing an electrical circuit.

  5. Signal Sent: The keyboard controller reads this signal and informs the computer that the key has been pressed.

  6. Key Released: The top layer returns to its original position, breaking the connection.


2.3. Advantages of Membrane Switches

AdvantageDescription
Extremely ThinAs thin as paper, allowing for very slim devices.
SealableCan be completely sealed, protecting against dust, dirt, or liquid ingress.
SilentMakes no noise when pressed.
CheapVery low production cost.
LightweightVery low weight.

2.4. Disadvantages of Membrane Switches

DisadvantageDescription
Low Tactile FeedbackDoes not provide much physical sensation when pressed.
Uncertain LifespanLifespan varies greatly depending on quality.
Difficult to RepairIf one key fails, the entire keyboard usually needs to be replaced.

2.5. Lifespan of Membrane Switches

The lifespan of membrane keyboards varies widely :

TypeApproximate Lifespan
Low Quality1-5 million keystrokes
Medium Quality5-10 million keystrokes
High Quality10-20 million keystrokes

2.7. Membrane vs. Mechanical Switch (Comparison)

FeatureMembrane SwitchMechanical Switch
StructureThree thin layersMetal parts + Spring
CostVery cheapComparatively expensive
NoiseSilentNoisy (click sound)
Tactile FeedbackLowHigh
ThicknessExtremely thinThick
SealableYesDifficult
Lifespan1-20 million1-25 million
RepairDifficult (replace entire keyboard)Individual switches can be replaced

Examples: Laptop keyboards, Phone keypads, Remote controls


3. Capacitive Key Switches

are a modern and advanced keyboard switch technology. Instead of using mechanical contact, they work by detecting changes in capacitance.

3.1. Structure of a Capacitive Switch

Capacitive switches do not use metal contact. Instead, they are made up of:

ComponentDescription
Fixed Metal PlatesTwo small metal plates are placed on the Printed Circuit Board (PCB). These remain stationary.
Movable Metal PlateAnother metal plate is attached to the underside of a foam or rubber piece. This plate moves downward when the key is pressed.
Observed Damage in Macul and Ñuñoa
Capacitive Keyboard

3.2. How Does a Capacitive Switch Work?

Step-by-Step Working Process:

Step 1: Normal State (Key Not Pressed)

  • There is a specific distance between the fixed and movable plates.
  • This distance creates a specific capacitance value.

Step 2: Key Pressed

  • When you press the key, the movable plate moves downward.
  • This reduces the distance between the movable and fixed plates.

Step 3: Capacitance Changes

  • As distance decreases, the capacitance value changes.
  • Formula: C = εA/d (When distance decreases, capacitance increases)

Step 4: Sense Amplifier Generates Signal

  • A special sense amplifier circuit detects this change.
  • It produces a logic level signal (0 or 1).
  • This signal tells the microprocessor that the key has been pressed.

Step 5: Key Released

  • Due to the elasticity of the foam or rubber, the movable plate returns to its original position.
  • The distance returns to normal, and capacitance returns to its original value.

3.3. Simple Diagrammatic Concept

Normal State:                    Key Pressed:
[Movable Plate]                  [Movable Plate]
     |                                ↓ (moved down)
     | Distance = d                   | Distance = d' (less)
[Fixed Plate]                    [Fixed Plate]
[Fixed Plate]                    [Fixed Plate]

Capacitance = C                  Capacitance = C' (higher)

3.4. Advantages of Capacitive Switches

AdvantageDescription
No Mechanical ContactNo metal contact means no oxidation or dirt issues.
Long-lastingNo mechanical wear, so lifespan is very high.
ReliableNo bouncing problems.
Fast ResponseWorks extremely quickly.
SilentMakes no noise when pressed.

3.5. Disadvantages of Capacitive Switches

DisadvantageDescription
Extra Circuitry RequiredNeeds special sense amplifier circuits to detect capacitance changes.
Higher CostDesign and production costs are higher than standard switches.
Complex DesignCircuit design is relatively complex.

Examples: Apple Magic Keyboard, Many high-end laptop keyboards

4. Hall Effect Key Switches

When an electric current flows through a conductor or semiconductor and a perpendicular magnetic field is applied to it, a small voltage is generated perpendicular to the direction of current. This voltage is called the Hall Voltage.

In simple words: When you bring a magnet near an electric current, a small voltage is created.

Observed Damage in Macul and Ñuñoa
Hall-effect Keyboard
Observed Damage in Macul and Ñuñoa
Hall-effect,capactiva and membrane Keyboard

4.2. Structure of a Hall Effect Switch

ComponentDescription
Semiconductor CrystalA special semiconductor crystal (such as silicon or gallium arsenide)
Reference CurrentA fixed electric current is passed through two opposite surfaces of this crystal
MagnetA small magnet that moves closer to the crystal when the key is pressed
Sensing CircuitCircuit that detects and amplifies the generated Hall voltage

4.3. How Does a Hall Effect Switch Work?

Step-by-Step Working Process:

Step 1: Normal State (Key Not Pressed)

  • A reference current flows through the semiconductor crystal.
  • The magnet is far away from the crystal.
  • No Hall voltage is generated.

Step 2: Key Pressed

  • When you press the key, the magnet moves closer to the crystal.
  • The magnet creates a magnetic field on the crystal.

Step 3: Hall Voltage is Generated

  • The magnetic flux lines are perpendicular to the direction of current.
  • This creates a small voltage (Hall voltage) across the other two opposite surfaces of the crystal.

Step 4: Signal is Created

  • This tiny voltage is amplified by an amplifier circuit.
  • The amplified signal tells the microprocessor that the key has been pressed.

Step 5: Key Released

  • The magnet returns to its original position.
  • The magnetic field decreases and the Hall voltage disappears.

4.4. Simple Diagrammatic Concept

Normal State:                    Key Pressed:
[Magnet] ★ Far away              [Magnet] ★ Close (moved near)
     |                                  |
[Semiconductor]                  [Semiconductor]
     | (Current)                      | (Current)
     ↓                                ↓
No Voltage Generated          Hall Voltage is Generated

4.5. Advantages of Hall Effect Switches

AdvantageDescription
No Mechanical ContactNo metal contact means no wear, oxidation, or dirt issues.
Extremely Long-lastingNo mechanical wear, so lifespan is very high.
Highly ReliableNo bouncing problems.
Fast ResponseWorks extremely quickly.
SilentMakes no noise when pressed.

4.6. Disadvantages of Hall Effect Switches

DisadvantageDescription
Higher CostMore expensive than other switches due to complex mechanism.
Complex StructureMaintaining proper positioning of magnet and sensor is difficult.

4.8. Hall Effect vs. Other Switches (Comparison)

FeatureMechanicalMembraneCapacitiveHall Effect
Working PrincipleMetal contactLayer contactCapacitanceMagnet + Hall Voltage
OxidationYesLowNoneNone
BounceYesLowNoneNone
Lifespan (million)1-251-20~20100+
CostCheapVery cheapModerateExpensive
NoiseNoisySilentSilentSilent

4.9. Other Uses of Hall Effect Sensors

Hall Effect sensors are not only used in keyboards but also in many other applications:

  • Electrically Controlled Machines – to detect speed
  • Motor Control – to determine rotation speed and position
  • Automobiles – to measure wheel and engine speed
  • Robotics – to control position and movement

4.10. Where Are Hall Effect Switches Used?

  • High-end Keyboards – where reliability and durability are important
  • Industrial Control Panels – in harsh environments
  • Aircraft and Spacecraft Control Panels – for extreme reliability
  • Medical Devices – where cleanliness is required

Examples: Specialized gaming keyboards, Industrial-use keyboards


Comparison of the Four Methods:

FeatureMechanicalMembraneCapacitiveHall Effect
Working PrincipleMetal contactLayer contactCapacitance changeMagnet + Sensor
CostCheapVery cheapModerateExpensive
SoundNoisySilentSilentSilent
DurabilityGoodModerateGoodExcellent
SpeedGoodSlowFastExtremely fast
UsageGaming keyboardsLaptops, RemotesHigh-end keyboardsSpecialized keyboards

Keyboard Circuit Connections and Interfacing –

Why the Matrix Method?

MethodNumber of WiresExample
Direct Connection100 keys = 100 wiresComplex, expensive, takes more space
Matrix Connection100 keys = 10 rows + 10 columns = 20 wiresSimple, cheap, takes less space

How do Computer Keyboards Work?

1. Three Main Tasks to Get Data from a Keyboard

To get meaningful data from a keyboard, the following three tasks must be completed:

TaskDescriptionWhy Needed
1. Detect a KeypressIdentify whether any key has been pressedThe keyboard needs to know when the user is pressing a key
2. Debounce the KeypressEliminate the bounce generated when a key is pressedTo prevent a single press from being counted as multiple presses
3. Encode the KeypressDetermine the identity (scan code) of the pressed keyTo tell the computer which key has been pressed

2. These three tasks can be done using

  1. hardware
  2. software
  3. combination of both.

Method 1: Pure Hardware Method

All three tasks are done using dedicated electronic circuits – no software needed.

Observed Damage in Macul and Ñuñoa
4*8 Keyboard matrix with AY5-2376

How 4*8 matrix Works:

Step 1: Detect a Keypress

ActionDetails
Set ALL Rows LOWPort A = 0000
Read ColumnsCheck Port B

Result:

  • No key pressed → Port B = 1111 (all HIGH)
  • Key pressed (e.g., Row 1, Column 2) → Port B = 1011 (one column becomes LOW)

Key Detected!


Step 2: Debounce (Wait 20ms)

ActionDetails
First signalColumn becomes LOW
Wait 20msLet mechanical bounce settle
Read Columns AgainCheck Port B once more

Result:

  • If HIGH now → Bounce → Ignore
  • If still LOW → Valid keypress

Step 3: Encode (Find the Key)

Scan one row at a time:

RowPort A ValueAction
Row 01110Read columns → No key
Row 11101Read columns → Column 2 LOW → Key Found!
Row 21011Read columns → Check next
Row 30111Read columns → Check next

Key Found at:

  • Row = 1 (Port A = 1101)
  • Column = 2 (Port B = 1011)

Look-up Table → Scan Code → e.g., 0x06 for key ‘5’

AdvantageDisadvantage
Very fast (nanoseconds)Expensive (many components)
No CPU loadComplex circuit design
Highly reliableCannot be changed easily

Method 2: Pure Software Method

All three tasks are done by microprocessor program – no extra hardware needed.

Observed Damage in Macul and Ñuñoa
Flowchart for get meaningful data from keyboard

This woking Princile of the software based keyboard intrfacing

Block 1: DETECT (Find a Keypress)

StepAction
1Set ALL ROWS to LOW (0V)
2READ COLUMNS – check if any is LOW
3If ALL columns HIGH → No key pressed → Keep waiting
4If ANY column LOW → Key pressed! → Move to DEBOUNCE

Block 2: DEBOUNCE (Remove Bounce)

StepAction
1WAIT 20ms (let mechanical bounce settle)
2READ COLUMNS AGAIN
3If column is HIGH now → It was just noise → Go back to DETECT
4If column is still LOW → Valid keypress → Move to ENCODE

Block 3: ENCODE (Identify & Send Code)

StepAction
1Set ONE ROW to LOW at a time (scan row by row)
2READ COLUMNS for that row
3If column is LOW → KEY FOUND at (Row, Column)
4CONVERT TO HEX (scan code like 0x1E)
5SEND CODE to computer → Go back to DETECT
AdvantageDisadvantage
No extra hardware neededSlower (CPU spends time scanning)
Easy to customizeCPU load is high
Very cheapTiming must be precise

Method 3: Hybrid Method (Hardware + Software)

Some work in hardware, some in software. Most modern keyboards use this.

TaskHow It’s DoneWho Does It
DetectMicrocontroller scans rows/columnsHardware (MCU)
DebounceRC circuit + firmware delayBoth (Hardware + Software)
EncodeFirmware converts scan code to USB HIDSoftware (Firmware)

Advantage and Disadvantage

AdvantageDisadvantage
Best of both worldsMore complex design
Fast and reliableRequires programming (firmware)
Flexible and cheapDevelopment takes time

Which Keyboard Uses Which Method?

Keyboard Brand / ModelMethod UsedWhy
IBM Model M (1980s)Pure HardwareNo microcontroller inside
Apple Magic KeyboardHybridMicrocontroller + firmware
Logitech G SeriesHybridFast gaming response
Arduino 4x4 KeypadPure SoftwareNo extra ICs needed
Cherry Mechanical KeyboardHybridUSB + firmware for RGB
Old PS/2 KeyboardHardware / HybridDepends on model

8086-এর সাথে 8255 ব্যবহার করে 4×4 Keyboard ইন্টারফেসিং এবং ALP

1. System Architecture & Hardware Connection

4×4 ম্যাট্রিক্স কী-এর কাজ করার পদ্ধতি:

4×4 keyboard-এ 16টি কী আছে, যা 4টি সারি (Row) এবং 4টি কলাম (Column)-এ সাজানো। একটি কী চাপলে সংশ্লিষ্ট সারি এবং কলামের মধ্যে সংযোগ তৈরি হয়। এই সংযোগ সনাক্ত করাই হলো keyboard scanning-এর মূল কাজ।

8255 PPI-এর সাথে সংযোগ:

  • Port B (PB0–PB3) → 4টি সারি (Row lines) - এখানে আউটপুট হিসেবে কনফিগার করে ধারাবাহিকভাবে 0 দেওয়া হবে (row scanning)
  • Port C (PC0–PC3) → 4টি কলাম (Column lines) - এখানে ইনপুট হিসেবে কনফিগার করে কলামের অবস্থা পড়া হবে
  • Port A → ব্যবহার না করলে ইনপুট/আউটপুট যেকোনোভাবে রাখা যায়

পোর্ট অ্যাড্রেস নির্ধারণ:

8255-এর Control Register: 86H  (ধরে নিচ্ছি)
Port A: 80H
Port B: 82H
Port C: 84H

সতর্কতা: পোর্ট অ্যাড্রেস আপনার সার্কিটের Chip Select (CS) লজিকের উপর নির্ভর করে পরিবর্তিত হবে。


2. 8255-এর Control Word কনফিগারেশন

Mode 0 (Basic I/O): 8255-কে Mode 0-এ কনফিগার করলে পোর্টগুলো সরল ইনপুট/আউটপুট হিসেবে কাজ করে।

Control Word (CW):

বিট (D7–D0)মানঅর্থ
D71I/O Mode (Active)
D6, D500Mode 0 (Group A)
D40Port A = Output
D3XPort C Upper (PC7–PC4) =不重要 (ব্যবহার হবে না)
D20Mode 0 (Group B)
D10Port B = Output (সারি scan করার জন্য)
D01Port C Lower (PC3–PC0) = Input (কলাম পড়ার জন্য)

Control Word = 10000001₂ = 81H

মনোযোগ: Port B আউটপুট এবং Port C (PC0–PC3) ইনপুট হিসেবে কনফিগার করতে D1=0 এবং D0=1 করতে হবে।


3. Keyboard Scanning Algorithm (Flowchart)

নিচের ধাপগুলো অনুসরণ করে কী প্রেস সনাক্ত করা হয়:

  1. সব সারি Low করুন: Port B-তে 00H আউটপুট দিন (সব সারি 0)।
  2. কলাম পড়ুন: Port C থেকে ইনপুট নিন।
  3. কী প্রেস চেক করুন: যদি Port C-এর নিচের ৪ বিট (PC3–PC0) সব 1 হয় (0FH), তাহলে কোনো কী চাপা নেই → ধাপ 1-এ ফিরে যান।
  4. Debounce (প্রথমবার): 10ms Delay কল করুন (DEBOUNCE routine ব্যবহার করে)।
  5. পুনরায় কলাম পড়ুন: আবার Port C পড়ে নিশ্চিত করুন যে এটি সত্যিই কোনো কী প্রেস, এবং নয়তো ভুল signal (ধাপ 1-এ ফিরে যান)।
  6. কোন কলাম চিহ্নিত করুন: কোন বিট 0 হয়েছে তা দেখে কলাম নম্বর বের করুন।
  7. সারি Scan করুন: এখন একটি একটি করে সারি 0 করে বাকি সারি 1 রেখে Port B-তে আউটপুট দিন:
    • 0EH (PB0=0, বাকি 1) → সারি 0
    • 0DH (PB1=0, বাকি 1) → সারি 1
    • 0BH (PB2=0, বাকি 1) → সারি 2
    • 07H (PB3=0, বাকি 1) → সারি 3
  8. সারি চিহ্নিত করুন: প্রতিবার Port C পড়ুন। যেই সারিতে 0 পাবেন, সেটি চিহ্নিত করুন এবং সারি নম্বর বের করুন।
  9. কী-কোড তৈরি করুন: সারি ও কলাম নম্বর ব্যবহার করে Lookup Table থেকে আসল কী-কোড (যেমন 0–F) বের করুন।
  10. Debounce (দ্বিতীয়বার): কী রিলিজের সময়ও bounce হয়, তাই আবার 10ms Delay দিন এবং নিশ্চিত করুন যে কী রিলিজ হয়েছে।

4. Complete ALP (Assembly Language Program)

;-----------------------------------------------------------
; 4x4 Keyboard Interfacing with 8255 (8086)
; Detects key press, debounces (10ms), returns key code in AL
;-----------------------------------------------------------

DATA SEGMENT
    ; Lookup Table: Key codes for 4x4 keyboard (0-F)
    KEY_TABLE DB 00H, 01H, 02H, 03H
              DB 04H, 05H, 06H, 07H
              DB 08H, 09H, 0AH, 0BH
              DB 0CH, 0DH, 0EH, 0FH
DATA ENDS

CODE SEGMENT
    ASSUME CS:CODE, DS:DATA

START:
    MOV AX, DATA
    MOV DS, AX

    ;-------------------------------------------------------
    ; Step 1: Initialize 8255 in Mode 0
    ; Control Word = 81H (Port B=Output, Port C Lower=Input)
    ;-------------------------------------------------------
    MOV AL, 81H
    OUT 86H, AL          ; Write to Control Register

SCAN_LOOP:
    ;-------------------------------------------------------
    ; Step 2: Check if any key is pressed
    ;-------------------------------------------------------
    MOV AL, 00H          ; All rows = 0 (active low)
    OUT 82H, AL          ; Write to Port B (rows)

    IN AL, 84H           ; Read from Port C (columns)
    AND AL, 0FH          ; Mask only lower 4 bits (PC0-PC3)

    CMP AL, 0FH          ; All columns high? (no key pressed)
    JE SCAN_LOOP         ; Yes, continue polling

    ;-------------------------------------------------------
    ; Step 3: Debounce (First time) - Wait 10ms
    ;-------------------------------------------------------
    CALL DEBOUNCE        ; 10ms delay routine

    ;-------------------------------------------------------
    ; Step 4: Read columns again to confirm key press
    ;-------------------------------------------------------
    IN AL, 84H
    AND AL, 0FH
    CMP AL, 0FH
    JE SCAN_LOOP         ; False trigger, go back

    ;-------------------------------------------------------
    ; Step 5: Identify which column is pressed
    ;-------------------------------------------------------
    MOV CL, 00H          ; CL = Column counter (starting from 0)
    MOV BL, AL           ; Save column status in BL

FIND_COLUMN:
    ROR BL, 1            ; Rotate right to check each bit
    JC COLUMN_FOUND      ; If carry=1, bit was 1 (not pressed)
    INC CL               ; Increment column counter
    CMP CL, 04H          ; Checked all 4 columns?
    JL FIND_COLUMN       ; No, continue

COLUMN_FOUND:
    ; CL now contains column number (0-3)

    ;-------------------------------------------------------
    ; Step 6: Scan rows one by one
    ;-------------------------------------------------------
    MOV DL, 00H          ; DL = Row counter (starting from 0)
    MOV BH, 0EH          ; BH = Row scan pattern (PB0=0 first)

FIND_ROW:
    MOV AL, BH
    OUT 82H, AL          ; Activate current row

    IN AL, 84H           ; Read columns
    AND AL, 0FH
    CMP AL, 0FH
    JNE ROW_FOUND        ; If not all high, row found

    ROL BH, 1            ; Rotate left to activate next row
    INC DL
    CMP DL, 04H
    JL FIND_ROW          ; Check next row

ROW_FOUND:
    ; DL now contains row number (0-3)

    ;-------------------------------------------------------
    ; Step 7: Calculate key code using lookup table
    ; Key Code = Row * 4 + Column
    ;-------------------------------------------------------
    MOV AL, DL           ; AL = Row number
    MOV CL, 02H
    SHL AL, CL           ; Multiply row by 4 (SHL by 2)
    ADD AL, CL           ; Add column number

    ; Now AL = Key index (0-15)
    LEA BX, KEY_TABLE    ; BX points to lookup table
    XLAT                 ; AL = KEY_TABLE[AL] (key code)

    ; AL now contains the actual key code (0-F)

    ;-------------------------------------------------------
    ; Step 8: Debounce on release - Wait for key to be released
    ;-------------------------------------------------------
WAIT_RELEASE:
    MOV AL, 00H
    OUT 82H, AL          ; All rows low

    IN AL, 84H
    AND AL, 0FH
    CMP AL, 0FH
    JNE WAIT_RELEASE     ; Wait until all columns high

    CALL DEBOUNCE        ; 10ms delay for release bounce

    ;-------------------------------------------------------
    ; Step 9: Exit with key code in AL
    ;-------------------------------------------------------
    MOV AH, 4CH          ; DOS exit function (optional)
    INT 21H

;-----------------------------------------------------------
; Subroutine: DEBOUNCE (10ms delay)
; Assumes DEBOUNCE is an available 10ms delay routine
;-----------------------------------------------------------
DEBOUNCE PROC NEAR
    ; If delay routine is already available, call it directly
    ; Otherwise, implement a delay loop (example for 5MHz clock)
    PUSH CX
    MOV CX, 5000H       ; 10ms delay loop count (adjust as needed)
DELAY_LOOP:
    LOOP DELAY_LOOP
    POP CX
    RET
DEBOUNCE ENDP

CODE ENDS
END START

5. ALP-এর ব্যাখ্যা

বিভাগকাজ
Initialize 825581H Control Word দিয়ে 8255 কে Mode 0-এ সেট করা হয়েছে। Port B আউটপুট (সারি scan) এবং Port C ইনপুট (কলাম পড়া)।
Wait for Key Pressসব সারি Low করে (00H) Port C পড়া হচ্ছে। যদি কোনো কলাম 0 হয়, বুঝতে হবে কী চাপা হয়েছে।
Debounce10ms delay (DEBOUNCE routine) কল করে mechanical bounce-এর প্রভাব কমানো হয়েছে।
Identify ColumnPort C-এর 4 বিট পরীক্ষা করে কোন কলামে 0 পাওয়া গেছে তা চিহ্নিত করা হয়েছে।
Scan Rowsধারাবাহিকভাবে প্রতিটি সারি 0 করে বাকি সারি 1 রেখে Port B-তে আউটপুট দিয়ে পড়া হচ্ছে। কোন সারিতে 0 পাওয়া গেছে তা চিহ্নিত করা হয়েছে।
Calculate Key Codeসনাক্তকৃত সারি ও কলাম নম্বর থেকে Lookup Table-এর মাধ্যমে 0–F পর্যন্ত কী-কোড বের করা হয়েছে।
Wait for Releaseকী রিলিজ না হওয়া পর্যন্ত অপেক্ষা করে আবার Debounce করা হয়েছে, যাতে একটি প্রেসে একাধিক সনাক্তকরণ না হয়।
Return Key CodeAL রেজিস্টারে কী-কোড রেখে প্রোগ্রাম শেষ হয়েছে (অথবা main program-এ ফিরে গেছে)।

প্রশ্ন: 8086-এর সাথে 8255 ব্যবহার করে 4×4 Keyboard ইন্টারফেসিং এবং Key Detection-এর জন্য ALP লিখুন।

উত্তর:
4×4 keyboard-এ কী প্রেস সনাক্ত করতে 8255-কে Mode 0-এ কনফিগার করে Port B-কে Output (সারি scan) এবং Port C-কে Input (কলাম পড়া) হিসেবে সেট করতে হবে। Control Word হবে 81H। প্রথমে সব সারি Low করে Port C পড়ে কোনো কী প্রেস আছে কিনা দেখতে হবে। Debounce-এর জন্য 10ms Delay (DEBOUNCE routine) কল করতে হবে। তারপর ধারাবাহিকভাবে প্রতিটি সারি Scan করে কোন সারিতে কী প্রেস হয়েছে তা চিহ্নিত করতে হবে। সনাক্তকৃত সারি ও কলাম নম্বর ব্যবহার করে Lookup Table থেকে কী-কোড বের করে AL রেজিস্টারে রিটার্ন করতে হবে। সবশেষে কী রিলিজ হওয়া পর্যন্ত অপেক্ষা করে আবার Debounce করতে হবে।

Next Topic Universal Serial Bus

Table of Contents