2015年06月03日 星期三 09:25
前面讲过使用74LS47来驱动LED数码管,目的就是减少单片机IO引脚的使用量。如果单片机直接驱动LED数码管,每个数码管至少需要7个IO引脚,如果使用74LS47,那么只需要4个引脚。如果要驱动多个数码管,要么引脚数量倍增,要么采用高频刷新的方式(每增加一位数码管,需要多占用一个引脚,并且数码管可能会有闪烁的现象)。今天介绍的74HC595,可以将引脚数量降低为3个,并且可以串行驱动任意多个数码管,无需再增加IO引脚数量,并且,LED数码管是没有任何闪烁的,效果非常好。
下面是74HC595的物理引脚定义和逻辑引脚标识:
Arduino的代码库中内置了ShiftOut方法,可以很方便的应用于74HC595。在这个示例中,我们使用一个74HC595驱动一个LED数码管,单片机采用Arduino UNO。
电路连接要点如下:
LED数码管(5161AS)的1,2,4,5,6,7,9,10引脚分别连接74HC595的Q0-Q7。
LED数码管的3引脚串接一个200欧的电阻然后接地。
74HC595的OE、GND接地,VCC、MR接电源正极。
74HC595的ST_CP接Arduino的3脚,SH_CP接Arduino的4脚,DS接Arduino的2脚。
我是在面包板上做测试的,很方便,实际连接图如下,其实啥也看不清,大家照着上述的连接方式连即可:
Arduino程序代码如下:
/*
Shift Register Example
Turning on the outputs of a 74HC595 using an array
Hardware:
* 74HC595 shift register
* LEDs attached to each of the outputs of the shift register
*/
//Pin connected to ST_CP of 74HC595
int latchPin = 3;
//Pin connected to SH_CP of 74HC595
int clockPin = 4;
////Pin connected to DS of 74HC595
int dataPin = 2;
//holders for infromation you're going to pass to shifting function
byte data;
byte dataArray[10];
void setup() {
//set pins to output because they are addressed in the main loop
pinMode(latchPin, OUTPUT);
pinMode(clockPin, OUTPUT);
pinMode(dataPin, OUTPUT);
//Binary notation as comment
dataArray[0] = 0xEE;
dataArray[1] = 0x82;
dataArray[2] = 0xCD;
dataArray[3] = 0x6D;
dataArray[4] = 0x2B;
dataArray[5] = 0x67;
dataArray[6] = 0xE7;
dataArray[7] = 0x2E;
dataArray[8] = 0xEF;
dataArray[9] = 0x6F;
}
void loop() {
for (int j = 0; j < 10; j++) {
//load the light sequence you want from array
data = dataArray[j];
//ground latchPin and hold low for as long as you are transmitting
digitalWrite(latchPin, 0);
//move 'em out
shiftOut(dataPin, clockPin, LSBFIRST ,data);
//return the latch pin high to signal chip that it
//no longer needs to listen for information
digitalWrite(latchPin, 1);
delay(300);
}
}
代码运行的效果,就是LED数码管从0到9循环显示。
本次试验参考了74HC595的数据手册和Arduino的官方文档:
http://www.arduino.cc/en/Tutorial/ShiftOut
http://www.datasheetcatalog.com/datasheets_pdf/7/4/H/C/74HC595.shtml
大家可以自行研究,尝试驱动多个LED数码管。
Zeuux © 2025
京ICP备05028076号