Simulate decoders and priority encoders and implement logic functions with a decoder
Click the inputs to change them. The highlighted output is the selected one.
Activate several inputs at once: the highest-numbered one is encoded.
Click the values in column F to define the function.
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:
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.
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:
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.
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.
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.
| IC | Function | Notes |
|---|---|---|
| 74HC139 | Two 2 → 4 decoders | Active-low outputs, one enable per decoder |
| 74HC138 | 3 → 8 decoder | Active-low outputs, enables G1, G2A and G2B |
| 74HC238 | 3 → 8 decoder | Like the 138 with active-high outputs |
| 74HC154 | 4 → 16 decoder | 24-pin package |
| 74HC42 / CD4028 | BCD to decimal | Four inputs, ten outputs |
| 74HC148 | 8 → 3 priority encoder | Active-low inputs and outputs, GS, EI and EO |
| 74HC147 | 10 → 4 priority encoder | Decimal 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.