EN

Decoders and encoders

Simulate decoders and priority encoders and implement logic functions with a decoder

1. Decoder

Decoder

Click the inputs to change them. The highlighted output is the selected one.

Truth table

Equations

2. Priority encoder

Encoder

Activate several inputs at once: the highest-numbered one is encoded.

Equations (positive logic)

3. Logic function with a decoder

Click the values in column F to define the function.

Function

What a decoder does

A decoder with n inputs has 2n outputs and activates only one: the one matching the binary number at the input. With A2 A1 A0 = 101, Y5 is activated and the rest stay idle. Each output is one of the minterms of the input variables, multiplied by the enable:

\[ Y_i = EN \cdot m_i \qquad Y_5 = EN \cdot A_2\,\overline{A_1}\,A_0 \]

Most real ICs have active-low outputs: the chosen output goes to 0 and the rest stay at 1. That is the practical choice, because the chip-select (CS) inputs of memories and peripherals are also activated by a 0. The 74HC238 is the version of the 74HC138 with active-high outputs.

Enables and cascading

The 74HC138 has three enable inputs: G1 (active high) and G2A and G2B (active low). It only decodes when G1 = 1 and G2A = G2B = 0; otherwise all outputs stay inactive. These enables make it possible to expand the size without extra gates:

Typical use: selecting chips on a bus

In a microprocessor system, memory and peripherals share the address and data buses. The decoder looks at the highest address lines and activates the CS of the chip that address belongs to. With a 16-bit bus and a 74HC138 on A15 A14 A13, the 64 KB space is split into 8 blocks of 8 KB: Y0 activates the chip at 0000h–1FFFh, Y1 the one at 2000h–3FFFh, and so on up to Y7. That allocation is the memory map.

Other uses: turning on one of several rows of a keypad or LED matrix, enabling the digits of a multiplexed display, or generating the select lines of several SPI devices with few microcontroller pins.

Encoder and priority encoder

An encoder does the opposite: it has 2n inputs and outputs, in binary, the number of the active input. A simple encoder is just a set of OR gates and assumes there is only one active input; if two are activated, the output is the OR of their codes and makes no sense (activate I2 and I5 in the simulator and compare). The priority encoder solves this: if several are active, it encodes the highest-numbered one and ignores the rest.

Besides A2 A1 A0, the 74HC148 has a GS output (some input active) that distinguishes "input 0 active" from "none active", since the code is 000 in both cases, and an EI input and EO output to chain two ICs into a 16 → 4 encoder. It is used to read keypads, to decide which interrupt has priority or, in the 74HC147, to convert a key from 0 to 9 into BCD.

Logic functions with a decoder

Since each output is a minterm, any function of n variables can be built with an n-input decoder and a single gate. With active-high outputs, the outputs of the minterms where the function is 1 are joined with an OR. With active-low outputs, a NAND is used, which is the OR of the inverted signals. If the function has more ones than zeros, it is shorter to join the outputs of the zeros with a NOR (or with an AND in negative logic). One decoder can serve several functions of the same variables at once: this is the basis of a full adder with a 74HC138 and two NANDs.

Common ICs

ICFunctionNotes
74HC139Two 2 → 4 decodersActive-low outputs, one enable per decoder
74HC1383 → 8 decoderActive-low outputs, enables G1, G2A and G2B
74HC2383 → 8 decoderLike the 138 with active-high outputs
74HC1544 → 16 decoder24-pin package
74HC42 / CD4028BCD to decimalFour inputs, ten outputs
74HC1488 → 3 priority encoderActive-low inputs and outputs, GS, EI and EO
74HC14710 → 4 priority encoderDecimal to BCD

Related: BCD to 7-segment decoder, multiplexers and demultiplexers (a demultiplexer is a decoder whose enable acts as the data input), logic gate simulator and Karnaugh map.