ShiftOut()
来自YFRobotwiki
描述
一次移位一位数据一位。 从最多(即最左边)或最少(最右边)有效位开始。 每个位依次写入数据引脚,之后时钟引脚被脉冲(高电平,然后为低电平)以指示该位可用。
注意:如果您正在与上升沿时钟的器件进行接口,那么在调用shiftOut()之前,需要确保时钟引脚为低电平。 调用digitalWrite(clockPin,LOW)。
这是一个软件实现; 另见SPI库,它提供了一个更快的硬件实现,但仅适用于特定的引脚。
语法
shiftOut(dataPin, clockPin, bitOrder, value)
参数
- dataPin:要在其上输出每个位的引脚(int)
- clockPin:一旦dataPin被设置为正确的值(int),该引脚就会切换
- bitOrder:哪个顺序移出位? MSBFIRST或LSBFIRST。(最高有效位优先,或最低有效位优先)
- value: 将数据移出。(字节)
返回
None
注意
必须通过调用pinMode()将dataPin和clockPin配置为输出。
shiftOut当前被写入输出1个字节(8位),因此需要两步操作来输出大于255的值。
// Do this for MSBFIRST serial int data = 500; // shift out highbyte shiftOut(dataPin, clock, MSBFIRST, (data >> 8)); // shift out lowbyte shiftOut(dataPin, clock, MSBFIRST, data); // Or do this for LSBFIRST serial data = 500; // shift out lowbyte shiftOut(dataPin, clock, LSBFIRST, data); // shift out highbyte shiftOut(dataPin, clock, LSBFIRST, (data >> 8));
示例
有关相关电路,请参阅有关控制74HC595移位寄存器的教程。
//**************************************************************//
// Name : shiftOutCode, Hello World //
// Author : Carlyn Maw,Tom Igoe //
// Date : 25 Oct, 2006 //
// Version : 1.0 //
// Notes : Code for using a 74HC595 Shift Register //
// : to count from 0 to 255 //
//****************************************************************
//Pin connected to ST_CP of 74HC595
int latchPin = 8;
//Pin connected to SH_CP of 74HC595
int clockPin = 12;
////Pin connected to DS of 74HC595
int dataPin = 11;
void setup() {
//set pins to output because they are addressed in the main loop
pinMode(latchPin, OUTPUT);
pinMode(clockPin, OUTPUT);
pinMode(dataPin, OUTPUT);
}
void loop() {
//count up routine
for (int j = 0; j < 256; j++) {
//ground latchPin and hold low for as long as you are transmitting
digitalWrite(latchPin, LOW);
shiftOut(dataPin, clockPin, LSBFIRST, j);
//return the latch pin high to signal chip that it
//no longer needs to listen for information
digitalWrite(latchPin, HIGH);
delay(1000);
}
}
扩展阅读
更多建议和问题欢迎反馈至 YFRobot论坛