视频来源:https://www.youtube.com/watch?v=OiKgWRRWj4c&list=PLdckmk1Jf8MYOED98iY13wdGi52h-O69X&index=12
bilibili:https://www.bilibili.com/video/BV1YW411Z76E?t=33&p=11
原理介绍
74HC59芯片电路原理图如下:
除去VCC,GND两个引脚外,共有8个输出引脚,在使用时,OE引脚接地,MR引脚置高位,DS,ST_CP,SH_CP为信号输入引脚
为了更好的去理解这个原件的工作逻辑,可将DS,ST_CP,SH_CP三个引脚分别比作“资料块”,“大平台”,“活塞”
其工作原理其实也很简单,当ST_CP也就是我们的大平台置低电位时,可以写入数据,通过给DS赋值,并使用SH_CP将其写入序列,之后再将ST_CP置高位,就可以的到自己想要的数据输出了。
代码演示V1.0
跟着原理来敲一遍代码:
/*74HC59实例*/
int big = 2;
int push = 3;
int datain = 4;
void setup() {
pinMode(big,OUTPUT);
pinMode(push,OUTPUT);
pinMode(datain,OUTPUT);
}
void loop() {
digitalWrite(big,LOW);
//No.1
digitalWrite(push,LOW);
digitalWrite(datain,1);
digitalWrite(push,HIGH);
//No.2
digitalWrite(push,LOW);
digitalWrite(datain,0);
digitalWrite(push,HIGH);
//No.3
digitalWrite(push,LOW);
digitalWrite(datain,1);
digitalWrite(push,HIGH);
//No.4
digitalWrite(push,LOW);
digitalWrite(datain,0);
digitalWrite(push,HIGH);
//No.5
digitalWrite(push,LOW);
digitalWrite(datain,1);
digitalWrite(push,HIGH);
//No.6
digitalWrite(push,LOW);
digitalWrite(datain,0);
digitalWrite(push,HIGH);
//No.7
digitalWrite(push,LOW);
digitalWrite(datain,1);
digitalWrite(push,HIGH);
//No.8
digitalWrite(push,LOW);
digitalWrite(datain,0);
digitalWrite(push,HIGH);
digitalWrite(big,HIGH);
}
代码优化V2.0
使用函数对代码进行优化
/*74HC59实例*/
int big = 2;
int push = 3;
int datain = 4;
void setup() {
pinMode(big,OUTPUT);
pinMode(push,OUTPUT);
pinMode(datain,OUTPUT);
}
void loop() {
digitalWrite(big,LOW);
//No.1
putOne();
//No.2
putZero();
//No.3
putOne();
//No.4
putZero();
//No.5
putOne();
//No.6
putZero();
//No.7
putOne();
//No.8
putZero();
digitalWrite(big,HIGH);
}
void putOne(){
digitalWrite(push,LOW);
digitalWrite(datain,1);
digitalWrite(push,HIGH);
}
void putZero(){
digitalWrite(push,LOW);
digitalWrite(datain,0);
digitalWrite(push,HIGH);
}
代码优化V3.0
int big = 2;
int push = 3;
int datain = 4;
void setup() {
pinMode(big,OUTPUT);
pinMode(push,OUTPUT);
pinMode(datain,OUTPUT);
}
void loop() {
digitalWrite(big,LOW);
//No.1
putnumber(1);
//No.2
putnumber(0);
//No.3
putnumber(1);
//No.4
putnumber(0);
//No.5
putnumber(1);
//No.6
putnumber(0);
//No.7
putnumber(1);
//No.8
putnumber(0);
digitalWrite(big,HIGH);
}
void putnumber(int number){
digitalWrite(push,LOW);
digitalWrite(datain,number);
digitalWrite(push,HIGH);
}
代码优化v4.0
使用数组+for循环+自建函数
int big = 2;
int push = 3;
int datain = 4;
int datainput[8] = {1,0,1,0,1,0,1,0};
void setup() {
pinMode(big,OUTPUT);
pinMode(push,OUTPUT);
pinMode(datain,OUTPUT);
}
void loop() {
digitalWrite(big,LOW);
for(int i=0;i<8;i++){
putmunber(datainput[i]);
}
}
void putnumber(int number){
digitalWrite(push,LOW);
digitalWrite(datain,number);
digitalWrite(push,HIGH);
}