Use the chip 74hc165 to add an input expansion port to the microcontroller proteus emulation arduino

Our previous blog post " How to expand with few input ports?" "Application of 74hc148 or 74ls148 cascade to realize 16 to 4 in Arduino " introduces that 148 and 148 can be output to the digital tube immediately after input. It can be said that it has its own BCD encoder. The 74hc165 we mainly introduce here today does not have an encoder. Here we use proteus as the simulation environment and Arduino as the compiled code environment.


Original source of the article: https://blog.csdn.net/haigear/article/details/132911752

1. Chip introduction

1. Picture introduction

The conventional 165 chip we get is like this. The pin order and naming method in the chip manual are different from those in proteus (the picture on the left is the picture in the chip manual, and the picture on the right is the picture in proteus). It is
Insert image description here
obvious , we see that pin No. 9 in proteus is SO, while pin No. 9 in the chip manual is Qh. The name not only functions as an output pin.

2. Pin function description

Since we focus on simulation in proteus, we only introduce the functions of the pins in proteus:

Pin name: Function description:
D0…D7 Parallel input pin
INH Parallel load (active low level)
SH/LD That is what we call CP serial clock
CLK Clock enable (active low)
SO Serial output, which we often call DS
QH Inverted serial output (usually not used)
AND Serial input*

2. Schematic diagram

Here we just use a resistor and 8 buttons to test its function and connect them to the parallel input ports D0~D7.
Insert image description here

3. Test code

Let’s explain the definitions of several pins here so that everyone can correspond to the pins listed in the table above. The datapin here is the so pin, the clockpin is the CLK pin, and the latchPin is the SH/LD pin.

1. Code

The code below is just a simple demonstration code, so I won't explain it too much.

/* Main.ino file generated by New Project wizard
 *
 * Created:   周四 9月 14 2023
 * Processor: ATmega328P
 * Compiler:  Arduino AVR (Proteus)
 */

const int dataPin = 2; // 数据引脚
const int clockPin = 3; // 时钟引脚
const int latchPin = 4; // 锁存引脚
const int numRegisters = 1; // 74HC165芯片个数

byte registerData[numRegisters]; // 存储74HC165芯片数据的数组

void setup() {
    
    
  pinMode(dataPin, INPUT);
  pinMode(clockPin, OUTPUT);
  pinMode(latchPin, OUTPUT);
  
  // 初始化串行通信
  Serial.begin(9600);
}

void loop() {
    
    
  // 读取74HC165芯片数据
  readData();
  
  // 打印每个输入引脚状态
  for (int i = 0; i < numRegisters * 8; i++) {
    
    
    Serial.print("Input ");
    Serial.print(i);
    Serial.print(": ");
    Serial.println(bitRead(registerData[i / 8], i % 8));
  }
  
  delay(1000);
}

void readData() {
    
    
  digitalWrite(latchPin, LOW);
  delayMicroseconds(10);
  digitalWrite(latchPin, HIGH);
  
  for (int i = numRegisters - 1; i >= 0; i--) {
    
    
    registerData[i] = shiftIn(dataPin, clockPin, MSBFIRST);
  }
}

For beginners, we see functions such as shiftIn or shiftOut, which are specially customized functions for shift operations that come with Arduino. They can be found in the reference in the Arduino IDE.

2. Driving process

To drive this chip, we divide it into the following steps:
First, set the level status of each input key.
Second, operate the SH/LD pin, which is the Latchpin in the code, to keep it from low level for more than 5 cycles. Set to high level, let the chip detect a rising edge transition.
Third, give the CLK pin, which is the clockpin in the program, a falling edge transition, and every time a falling edge transition is detected, the so port also The datapin will output one bit (this is the legendary shift)

4. Test effect

Insert image description here
With the above basic circuit, we can expand the input ports of Arduino or other stm32 or 8051 microcontrollers. For example, if we add a keyboard matrix or multiple buttons, we no longer have to worry about insufficient input ports. If the number of keyboards is greater than 8, then we can achieve 8xn port expansion by cascading SO and the SI of the next 165 chip. For example, to make an electronic keyboard, we need to cascade a few more to be enough for us to realize multiple scales.

The article may be updated at any time, please indicate the original source: https://blog.csdn.net/haigear/article/details/132911752

Cascade has a blog post for your reference, so I won’t go into details here: https://www.cnblogs.com/F-91/p/14713514.html

Guess you like

Origin blog.csdn.net/haigear/article/details/132911752