Skip to content
Tutorials Center
My learning

Topics

Lesson 52 of 67 ยท Input/Output

Interfacing: 8255 PPI, 8254 Timer and a Microcomputer System

See how a microprocessor, memory and I/O chips form a microcomputer: address decoding, the 8255 PPI control word and modes, and the 8254 timer, with Python models.

A processor on its own cannot do anything useful. A real microcomputer system has three parts around the CPU: memory for programs and data, I/O devices for the outside world, and glue logic that connects them to the buses. The classic way to teach this is with the Intel 8085/8086 family and its support chips, because the chips are simple enough to understand completely. Two of them show up in nearly every textbook: the 8255 programmable peripheral interface (parallel ports) and the 8254 programmable interval timer (counters and timers).

You will see the shape of a microcomputer system, how address decoding gives each chip a window in the address space, how the 8255 is configured with a control word, and how the 8254 counts. The chips are old but the ideas (registers, control words, modes, decoding) are exactly how modern peripherals are programmed. Read I/O Organization and I/O Interfaces and Handshaking first.

The microcomputer system

flowchart LR
  CPU["Microprocessor<br/>(MPU)"] --- AB["Address bus"]
  CPU --- DB["Data bus"]
  CPU --- CB["Control bus<br/>RD, WR, IO/M"]
  AB --- DEC["Address decoder"]
  DEC --> ROM["ROM / RAM"]
  DEC --> PPI["8255 PPI"]
  DEC --> PIT["8254 timer"]
  DB --- ROM
  DB --- PPI
  DB --- PIT
  CB --- ROM
  CB --- PPI
  CB --- PIT
  PPI --- DEV["Switches, LEDs,<br/>keypad, display"]
  PIT --- OUT["Clock outputs,<br/>interrupts"]

Every chip shares the three buses; the decoder picks which one answers.

The MPU runs the program. Memory (ROM for the boot code, RAM for data) holds instructions. The I/O chips translate between the CPU's bus and the pins of real devices. All chips connect to the same address, data and control buses; the address decoder ensures only one chip responds to any given address. The same signals you saw in Memory-Mapped vs Isolated I/O apply here: in an I/O-mapped system the CPU uses IN and OUT with an IO/M control signal, and in a memory-mapped system the chips sit in the memory address space and are reached with ordinary loads and stores.

The 8085 microprocessor: pins, registers and flags

The classic textbook MPU for this kind of system is the Intel 8085: an 8-bit processor with a 16-bit address bus (64 KB of memory), a 40-pin package and a single +5 V supply. Its design shows every idea above in a small space.

Pins

Pins Purpose
A8 to A15 Upper half of the address, output only.
AD0 to AD7 Multiplexed low address byte and data byte.
ALE Address latch enable: high when AD0 to AD7 carry an address.
RD, WR, IO/M Read and write strobes, and a signal that tells I/O cycles from memory cycles. S0 and S1 give the machine-cycle status.
READY Lets a slow memory or device ask the CPU to wait.
HOLD, HLDA A DMA controller asks for the buses; the CPU grants them.
INTR, INTA General interrupt request and its acknowledge.
TRAP, RST 5.5, RST 6.5, RST 7.5 Vectored interrupt requests; see vectored and non-vectored interrupts.
SID, SOD Serial input and serial output data, one bit at a time.
RESET IN, RESET OUT Restart the CPU (the program counter goes to 0000H) and tell the peripherals a reset happened.
X1, X2, CLK OUT Crystal connection (the internal clock is half the crystal frequency) and a clock for other chips.
Vcc, Vss +5 V supply and ground.

Using one set of pins for the low address byte and the data byte saves eight pins. The price is an external address latch: during the first clock state ALE goes high and the latch stores AD0 to AD7 as the low address; afterwards the same pins carry data.

Registers

  • Accumulator (A): 8 bits, holds one operand and the result of most arithmetic and logic instructions.
  • General registers B, C, D, E, H, L: 8 bits each, usable singly or as the 16-bit pairs BC, DE and HL. The HL pair usually serves as a memory pointer (register M).
  • Program counter (PC) and stack pointer (SP): 16 bits each.
  • Temporary registers W and Z: used internally by the CPU and not visible to the programmer.
  • Instruction register and flag register, discussed below.

The five flags

Flag Name Set to 1 when
S Sign bit 7 of the result is 1 (negative in two's complement)
Z Zero the result is zero
AC Auxiliary carry there is a carry from bit 3 to bit 4 (used for BCD correction)
P Parity the result has an even number of 1 bits
CY Carry the operation carried out of bit 7, or borrowed
Python Flags after an 8-bit addition, and ALE demultiplexing

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

For the sum 3AH + 7FH the result B9H has bit 7 set, so S=1, and a carry from the low nibble (A + F is more than 15), so AC=1. Its five 1 bits give odd parity, P=0. The second line shows why Z and CY often appear together: FFH + 01H wraps to zero and carries out. The flags are what the conditional jumps (JZ, JNC, JP, JPE and so on) test, as in Register Contents and Flag Status.

Strengths and limits

The 8085 is cheap, needs only one supply voltage and has built-in serial and interrupt features, which made it popular in teaching kits and simple controllers. Its limits are an 8-bit data path (slow for large arithmetic), a 64 KB address space, no hardware support for multiprocessing or memory protection, and the extra latch that multiplexing requires.

Address decoding for I/O chips

The 8255 has four internal registers (port A, port B, port C and the control register), selected by the two lowest address lines A1 and A0. The remaining address lines go to the decoder, whose output drives the chip's chip-select (CS) pin. If the decoder uses A7..A2 to produce CS, the chip answers at four consecutive ports.

A1 A0 Register
00 Port A
01 Port B
10 Port C
11 Control register

If CS is active when A7..A2 = 100000 (hex 80..83 once A1A0 are added), the 8255 sits at I/O ports 80H, 81H, 82H and 83H. The 8254 has the same layout (counter 0, counter 1, counter 2, control word) and might be placed at 84H..87H with its own decoder output.

Python I/O address decoder for two chips

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The 8255 programmable peripheral interface

The 8255 gives the CPU 24 parallel I/O lines organized as three 8-bit ports: A, B and C. Port C can also be split into two 4-bit halves, upper and lower. Each port is programmed as an input or an output by writing a control word to the control register.

Operating modes

Mode Name What it does
0 Basic I/O Simple input or output. Outputs are latched, inputs are not. Ports A, B and both halves of C work independently.
1 Strobed I/O Ports A and B transfer data with handshake signals taken from port C, with interrupt support.
2 Bidirectional Only port A. 8 bits both ways on the same lines, with handshaking on 5 lines of port C.

The mode-set control word

If bit 7 of the control word is 1, the word sets the modes:

Text
D7  D6 D5  D4   D3   D2   D1   D0
1   Group A mode  A    C-up  Group B mode  B    C-low
    (00,01,1x)    1=in 1=in  (0 or 1)      1=in 1=in
  • D6-D5: mode of group A (port A and upper port C): 00 mode 0, 01 mode 1, 1x mode 2.
  • D4: port A direction (1 = input, 0 = output).
  • D3: port C upper direction.
  • D2: group B mode (0 = mode 0, 1 = mode 1).
  • D1: port B direction.
  • D0: port C lower direction.

For example, 1001 1010 = 0x9A makes everything mode 0, with ports A and B and the upper half of C as inputs and the lower half of C as output. 1000 0000 = 0x80 is "all outputs, mode 0", the most common setup for driving LEDs. 1001 1011 = 0x9B makes all three ports inputs.

Bit set/reset mode

If bit 7 is 0, the word changes a single bit of port C without touching the others (handy for toggling a control line):

Text
D7=0   D6 D5 D4 (unused)   D3 D2 D1 = bit number 0-7   D0 = 1 set, 0 reset

For example, 0000 0111 = 0x07 sets bit PC3 (bit number 011, D0 = 1), and 0000 0110 = 0x06 resets PC3.

Python Decode an 8255 control word

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

A typical use: switches in, LEDs out

To read 8 switches on port A and show them on LEDs on port B, the program writes 0x90 (A in, B out, C out, mode 0) to the control port, then loops: IN from port A, OUT to port B. In assembly:

Text
MOV AL, 90H
OUT 83H, AL      ; control register
again:
IN  AL, 80H      ; read switches (port A)
OUT 81H, AL      ; drive LEDs (port B)
JMP again

A model of that exchange follows.

Python Model an 8255 in mode 0

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The 8254 programmable interval timer

The 8254 contains three independent 16-bit down counters (counter 0, 1 and 2). Each has a CLK input, a GATE input that can enable or pause counting, and an OUT output. You load a number, the counter counts down on each clock pulse, and the output changes in a way set by the mode.

The control word selects the counter (bits D7-D6), the read/write format (D5-D4), the mode (D3-D1) and binary or BCD counting (D0).

Mode Name Behaviour of OUT
0 Interrupt on terminal count Goes low on load, goes high when the count reaches 0 (one shot)
1 Hardware retriggerable one-shot Low pulse after a gate trigger
2 Rate generator Low for one clock every N clocks (divide by N)
3 Square-wave generator Square wave with period N clocks
4 Software-triggered strobe One low pulse after N clocks
5 Hardware-triggered strobe One low pulse after a gate trigger

Typical uses: a real-time tick that raises an interrupt every 10 ms, a square wave for a speaker (PCs drive the beeper from a timer channel), and a baud-rate clock. To get a frequency f out of an input clock fclk in mode 3, load N = fclk / f.

Python 8254 modes 0, 2 and 3 as a counting model

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Putting it together

A small application, such as a traffic-light controller or a data logger, uses the three kinds of chip together: ROM holds the program, RAM holds variables, the 8255 reads buttons and drives lamps, and the 8254 makes the time base. The programmer's job is: (1) know the address map, (2) write control words to configure each chip, (3) use IN/OUT (or loads/stores) to talk to the data registers. Modern systems replace the 8255 and 8254 with GPIO and timer blocks inside a microcontroller, but they are programmed the same way: a handful of registers at fixed addresses, a configuration write, then reads and writes.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. Which address lines select one of the four internal registers of an 8255?

    A1 and A0 choose port A, port B, port C or the control register; the other address lines go to the decoder that drives chip select.
  2. 2. Which control word makes every 8255 port an output in mode 0?

    Bit 7 = 1 (mode set), all mode and direction bits = 0 gives 0x80. 0x9B makes all ports inputs; 0x00 would be a bit set/reset word.
  3. 3. What does a control word with bit 7 = 0 do?

    Bit 7 = 0 selects the bit set/reset (BSR) format, which sets or clears a single port C bit.
  4. 4. An 8254 channel in mode 3 receives a 1 MHz clock and is loaded with N = 1000. What is the output?

    Mode 3 divides the input frequency by N: 1 MHz / 1000 = 1 kHz.
  5. 5. Why does a microcomputer need an address decoder?

    All chips share the buses, so a decoder turns the high address bits into chip-select signals that enable exactly one chip.

Summary

  • A microcomputer is a microprocessor plus memory and I/O chips connected by the address, data and control buses, with an address decoder generating chip selects.
  • In I/O-mapped systems chips are reached with IN/OUT; in memory-mapped systems with ordinary loads and stores.
  • The 8255 has ports A, B and C (24 lines), selected by A1A0, and is configured by a control word: bit 7 = 1 sets modes and directions, bit 7 = 0 sets or resets one bit of port C.
  • 8255 modes: 0 basic I/O, 1 strobed I/O with handshaking, 2 bidirectional on port A.
  • The 8254 has three 16-bit down counters with CLK, GATE and OUT, six modes (one-shot, rate generator, square wave, strobes), and a control word per counter.
  • Frequency output in mode 3 is the clock frequency divided by the loaded count.

Next: Parallel Processing and Flynn's Taxonomy.