当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Arduino shiftOut()用法及代码示例


[高级输入/输出]

说明

一次移出一个字节的数据。从最高(即最左边)或最低(最右边)有效位开始。每个位依次写入数据引脚,之后时钟引脚被脉冲化(先为高电平,然后为低电平)以指示该位可用。

注意 - 如果您正在与由上升沿计时的设备连接,您需要确保在调用 shiftOut() 之前时钟引脚为低电平,例如调用 digitalWrite(clockPin, LOW)

这是一个软件实现;另请参见 SPI library ,它提供了一种更快但仅适用于特定引脚的硬件实现。

用法

shiftOut(dataPin, clockPin, bitOrder, value)

参数

dataPin:输出每个位的引脚。允许的数据类型:int.
clockPin:一旦将 dataPin 设置为正确的值,该引脚就会切换。允许的数据类型:int.
bitOrder:移出位的顺序; MSBFIRST 或 LSBFIRST。 (最高有效位优先,或最低有效位优先)。
value: 要移出的数据。允许的数据类型:byte.

返回

示例代码

有关随附电路,请参阅tutorial on controlling a 74HC595 shift register

//**************************************************************//
//  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);
  }
}

注意事项和警告

dataPin 和 clockPin 必须已通过调用 pinMode() 配置为输出。

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));

相关用法


注:本文由纯净天空筛选整理自arduino.cc大神的英文原创作品 shiftOut()。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。