Skip to main content

8255 interface with switches and 7-segment

Short answer

Port A outputs segment codes to a common-anode 7-segment display, port B reads eight SPDT switches (closure gives 0). The 8255 is initialised with control word 82H. The program polls port B, complements the bits, rotates right to find the switch number, and outputs the corresponding code from a table: C0H, F9H, A4H, B0H, 99H, 92H, 82H, F8H for 0–7.

A microprocessor kit has an onboard 8255. Interface to the 8255 eight single-pole-double-throw (SPDT) switches numbered S0 to S7 and a seven segment common anode LED display. Draw the complete circuit setup. Define clearly the functions of all ports, write a program to initialize 8255, detect a switch closure, and display the value of the switch number on the LED display.

Assumptions

8085 kit with the 8255 I/O-mapped at 80H-83H: Port A = 80H, Port B = 81H, Port C = 82H, Control = 83H (A1 and A0 select the register; CS' comes from a decoder of A7-A2, selected when A7 = 1 and A6-A2 = 0). A switch is called "closed" when it is thrown to the GND side.

Circuit

 Each SPDT switch Sn: one throw -> +5 V, other throw -> GND,
 common terminal -> PBn   (S0 -> PB0, S1 -> PB1, ... S7 -> PB7)

 PA0..PA6 --[330 ohm each]--> segments a..g of the common anode display
 Common anode pin ----------> +5 V

 8255: D0-D7 <-> AD0-AD7, RD', WR', A0, A1 from the 8085,
       RESET <- RESET OUT, CS' <- address decoder

A segment glows when its port pin is 0 (common anode).

Port functions

PortModeDirectionFunction
A0Output7-segment code (PA0=a ... PA6=g, PA7=dp)
B0InputSwitches S0-S7 (+5 V side reads 1, closed (GND) side reads 0)
C-Unused-

Control word: D7=1 (mode set), A mode 0, A output, C upper output, B mode 0, B input, C lower output = 1000 0010 = 82H.

Display codes (common anode, dp off)

0 = C0H, 1 = F9H, 2 = A4H, 3 = B0H, 4 = 99H, 5 = 92H, 6 = 82H, 7 = F8H.

Program

        MVI A,82H
        OUT 83H        ; initialise 8255
START:  IN 81H         ; read switches
        CMA            ; closed switch becomes 1
        ORA A          ; set flags, clear carry
        JZ START       ; no switch closed
        MVI C,00H      ; C = switch number
NEXT:   RAR            ; shift lowest bit into carry
        JC FOUND
        INR C
        JMP NEXT
FOUND:  LXI H,TABLE
        MVI B,00H
        DAD B          ; HL = TABLE + number
        MOV A,M        ; get 7-segment code
        OUT 80H        ; display switch number
        JMP START
TABLE:  DB C0H,F9H,A4H,B0H,99H,92H,82H,F8H

Example: if S3 is closed, the port reads 1111 0111. After CMA it is 0000 1000. The first three RAR instructions leave carry = 0 (each is followed by INR C), and the fourth sets carry, so C = 3 and the code B0H shows "3". If more than one switch is closed, the lowest-numbered one is displayed.

✓ Answer checked by Claude Sonnet 5.5

All Instrumentation II chapter 2 questions →