74HC138 Decoder
1. Introduction
When designing MCU circuits, the number of MCU I/O pins is limited and sometimes cannot meet our design requirements. To control more devices, we need peripheral digital chips for pin expansion. A common choice is the 74HC138, also called a three-to-eight (38) decoder.
2. Operating Principle
The three-to-eight (38) decoder, as its name suggests, translates 3 binary input lines into 8 output states. For a digital device pin, a single input has two states, 0 and 1; with two input pins, there are four states—00, 01, 10, and 11; with 3 inputs, there are 8 states. The truth table is shown below.
As shown, the three bits on the left correspond to 0~8, and the right side shows the 8 output states. In any input state, only one output pin is low—keep this in mind. You can also achieve high-level output by connecting inverters.
When in use, connect E2 and E3 to ground. E1 controls decoder enable: 1 for on, 0 for off. A, B, and C correspond to A0, A1, and A2. Connect E1, A, B, and C to 4 MCU pins to control the output states.
Proteus Simulation
1. Objective
Use a 51-series microcontroller to control the selective starting of 12 motors
2. Procedure

- L298N as the stepper motor driver
- CD4066 as the stepper motor gating switch; it conducts when the control pin receives a high level
- 74HC138 decoder+74HC240 inverter for pin expansion, translating MCU signals and sending them to the CD4066 to select motors
3. Results
The motors were sequentially enabled and started as expected.
4. Code
#include<reg52.h>
sbit enable = P3^0;
sbit key = P3^1;
void delay(int i)
{
int j;
for(;i>0;i--)
for(j=114;j>0;j--);
}
void main()
{
unsigned char step[] = {0x01,0x02,0x04,0x08}; //顺时针转动
//unsigned char istep[] = {0x01,0x02,0x04,0x08}; //逆时针转动
unsigned char table[] = {0x80,0x90,0xa0,0xb0,0xc0,0xd0,0xe0,0xf0,0x08,0x09,0x0a,0x0b};
int i=0,num=0;
enable=1;
P1=0x00;
while(1)
{
if(key == 0);
{
delay(10);
if(key == 0)
{
num++;
if(num>=12)
num=-1;
while(!key);
}
}
/*
if(num==-1)
{
P1=0x00;
P0=0x00;
}
else if(num<8)
P1=table1[num];
else
P0=table2[num-8];
*/
if(num==-1)
P1=0x00;
else
P1 = table[num];
for(i=0; i<4; i++)
{
P2 = step[i];
delay(500);
}
}
}
Comments