Arduino实验–2个移位寄存器

在项目Arduino实验–移位寄存器中,我们再连接上另一个二进制寄存器,就是我们今天做的Arduino实验。

下面是我们要准备的元器件:

74HC595 直插芯片*2595

红色直插LED*16(可以用其他LED灯代替):led

220Ω直插电阻*16电阻

我们按照如图所示的电路图连接:

0001

0002

如果图片看不清楚的话,我们按照下面的数字编号来连接:

74HC595

两个74HC595的编号引脚为:1-16,,1’-16‘,然后连接的说明如下:

1-7,1’-7‘,15,15’这些引脚是为了数据输出,所以连接的是16个LED灯珠。

9连接14‘,从第一个74HC595穿行数据输出管脚连接到第二个74HC595的数据线引脚。这样保证了数据的传输

第一个74HC595第14号引脚连接arduino的11号引脚。

两个74HC595的第13号引脚连接GND

两个74HC595的第12号相互连接,连接至arduino的8号引脚

两个74HC595的11号引脚SCK相互连接,并连接至arduino的12号引脚

两个74HC595的10号引脚SCLR相互连接,并连接至arduino的3.3v电源

 

连接好以后,输入如下代码,就可以实现一组LED从 0到255计数(二进制),与此同时另一组的LED从255到0进行倒计数:

int latchPin = 8;  //连接的74HC595 (Latch)编号为12上的引脚
int clockPin = 12; //连接的74HC595 (Clock)编号为11上的引脚
int dataPin = 11;  //连接的74HC595 (Data)编号为14上的引脚

void setup() {
  	
        pinMode(latchPin, OUTPUT);
  	pinMode(clockPin, OUTPUT);
  	pinMode(dataPin, OUTPUT);
}

void loop() {
            for (int i = 0; i < 255; i++) {     //从0到255计数
                digitalWrite(latchPin, LOW); //设置latchPin引脚为LOW,允许数据输入
                shiftOut(dataPin, clockPin, LSBFIRST, i);  // 输出第一个8位数字
                shiftOut(dataPin, clockPin, LSBFIRST, 255-i); // 输出第二个8位数字
                digitalWrite(latchPin, HIGH); //设置latchPin引脚为HIGH,允许输入数据
    		delay(250 );
  	}
}

按照之前的电路图连接后,我想做一个16位的计数器,而不是8位,也就是说能够计数达到65535,这样一来我们就必须将两个595看成一个整体。但是在实验的过程中发现了几点注意的点:

1.我们传输的数据是先输入第二个595芯片,然后等第二个装满后再输入第一个595芯片。(这跟我看的书《arduino从基础到实践》里面的不同。)

2.输出数据的时候,要注意输出位的顺序。

下面就是我编写的16位计数器的代码:

int latchPin = 8;  //Pin connected to Pin 12 of 74HC595 (Latch)
int clockPin = 12; //Pin connected to Pin 11 of 74HC595 (Clock)
int dataPin = 11;  //Pin connected to Pin 14 of 74HC595 (Data)

void setup() {
	//set pins to output 
	pinMode(latchPin, OUTPUT);
	pinMode(clockPin, OUTPUT);
	pinMode(dataPin, OUTPUT);
}

void loop() {
	for (int i = 0; i < 256; i++) {     //count from 0 to 255
		for (int j = 0; j < 256; j++)
		{
		digitalWrite(latchPin, LOW); //set latchPin low to allow data flow
		shiftOut(dataPin, clockPin, LSBFIRST, j);
		shiftOut(dataPin, clockPin, LSBFIRST, i);
		digitalWrite(latchPin, HIGH);
		delay(50);
		}
	}
}

书中留了一个练习题:使用与之前相同的电路,产生跑马灯效果,使16个LED在首尾之间来回一次点亮:

1.从一边点亮到另一边,然后又从头开始,代码如下:

// Project 18 - Dual Binary Counters

int latchPin = 8;  //Pin connected to Pin 12 of 74HC595 (Latch)
int clockPin = 12; //Pin connected to Pin 11 of 74HC595 (Clock)
int dataPin = 11;  //Pin connected to Pin 14 of 74HC595 (Data)

void setup() {
	//set pins to output 
	pinMode(latchPin, OUTPUT);
	pinMode(clockPin, OUTPUT);
	pinMode(dataPin, OUTPUT);
}

void loop() {
	for (int i = 1; i < 256; i*=2) {     //count from 0 to 255

		digitalWrite(latchPin, LOW); //set latchPin low to allow data flow
		shiftOut(dataPin, clockPin, LSBFIRST, i);
		shiftOut(dataPin, clockPin, LSBFIRST, 0);
		digitalWrite(latchPin, HIGH);
		delay(250);
	}
	for (int i = 1; i < 256; i *= 2) {     //count from 0 to 255

		digitalWrite(latchPin, LOW); //set latchPin low to allow data flow
		shiftOut(dataPin, clockPin, LSBFIRST, 0);
		shiftOut(dataPin, clockPin, LSBFIRST, i);
		digitalWrite(latchPin, HIGH);
		delay(250);
	}
}

2.从头点亮在从尾返回,代码如下:

// Project 18 - Dual Binary Counters

int latchPin = 8;  //Pin connected to Pin 12 of 74HC595 (Latch)
int clockPin = 12; //Pin connected to Pin 11 of 74HC595 (Clock)
int dataPin = 11;  //Pin connected to Pin 14 of 74HC595 (Data)

void setup() {
	//set pins to output 
	pinMode(latchPin, OUTPUT);
	pinMode(clockPin, OUTPUT);
	pinMode(dataPin, OUTPUT);
}

void loop() {
	for (int i = 1; i < 256; i*=2) {     //count from 0 to 255

		digitalWrite(latchPin, LOW); //set latchPin low to allow data flow
		shiftOut(dataPin, clockPin, LSBFIRST, i);
		shiftOut(dataPin, clockPin, LSBFIRST, 0);
		digitalWrite(latchPin, HIGH);
		delay(250);
	}
	for (int i = 1; i < 256; i *= 2) { //count from 0 to 255 digitalWrite(latchPin, LOW); //set latchPin low to allow data flow shiftOut(dataPin, clockPin, LSBFIRST, 0); shiftOut(dataPin, clockPin, LSBFIRST, i); digitalWrite(latchPin, HIGH); delay(250); } for (int i = 64; i >0; i /= 2) {     //count from 0 to 255
		digitalWrite(latchPin, LOW); //set latchPin low to allow data flow
		shiftOut(dataPin, clockPin, LSBFIRST, 0);
		shiftOut(dataPin, clockPin, LSBFIRST, i);
		digitalWrite(latchPin, HIGH);
		delay(250);
	}
	for (int i = 128; i >1; i /= 2) {     //count from 0 to 255
		digitalWrite(latchPin, LOW); //set latchPin low to allow data flow
		shiftOut(dataPin, clockPin, LSBFIRST, i);
		shiftOut(dataPin, clockPin, LSBFIRST, 0);
		digitalWrite(latchPin, HIGH);
		delay(250);
	}
}

3.从两头开始依次点亮,然后相遇后各自返回。代码如下:

// Project 18 - Dual Binary Counters

int latchPin = 8;  //Pin connected to Pin 12 of 74HC595 (Latch)
int clockPin = 12; //Pin connected to Pin 11 of 74HC595 (Clock)
int dataPin = 11;  //Pin connected to Pin 14 of 74HC595 (Data)

void setup() {
	//set pins to output 
	pinMode(latchPin, OUTPUT);
	pinMode(clockPin, OUTPUT);
	pinMode(dataPin, OUTPUT);
}

void loop() {
	for (int i = 1; i < 256; i*=2) {     //count from 0 to 255

		digitalWrite(latchPin, LOW); //set latchPin low to allow data flow
		shiftOut(dataPin, clockPin, LSBFIRST, i);
		shiftOut(dataPin, clockPin, MSBFIRST, i);
		digitalWrite(latchPin, HIGH);
		delay(250);
	}
	for (int i = 1; i < 256; i *= 2) {     //count from 0 to 255

		digitalWrite(latchPin, LOW); //set latchPin low to allow data flow
		shiftOut(dataPin, clockPin, MSBFIRST, i);
		shiftOut(dataPin, clockPin, LSBFIRST, i);
		digitalWrite(latchPin, HIGH);
		delay(250);
	}
	
}

其实在做arduino的项目的额过程中,我越来越感觉到自己的编程能力有限了,所以我要尽快学好我的编程能力,尽快将我的学习笔记做完。

0 Comments
Leave a Reply