SN54/74LS138 1-of-8 Decoder/Demultiplexer

used products:

1. Arduino UNO
2. SN74HC138N
3. SN74LS138N

Arduino:

int pins[3] = {2,3,4}; void setup() { for (int j = 0; j < 3; j++) { pinMode(pins[j], OUTPUT); } } void loop() { for (int i = 0; i < 8; i++) { for (int j = 2; j >= 0; j--) { byte state = bitRead(i, j); digitalWrite(pins[j], state); } delay(500); } }

C:

#include <avr/io.h> #include <util/delay.h> const int selectPins[3] = {PD2, PD3, PD4}; int main(void) { for (int i = 0; i < 3; i++) { DDRD |= (1<<selectPins[i]); } while (1) { for (int i = 0; i < 8; i++) { for (int j = 2; j >= 0; j--) { byte state= ((i >> j) & 0x01); if (state) { PORTD |= (1<<selectPins[j]); } else { PORTD &= ~(1<<selectPins[j]); } } _delay_ms(500); } } return 0; }