Warning: Trying to access array offset on value of type bool in /home/cycos/cammy.co.jp/public_html/technical/wp-content/themes/cocoon-master/lib/utils.php on line 2604

Warning: Trying to access array offset on value of type bool in /home/cycos/cammy.co.jp/public_html/technical/wp-content/themes/cocoon-master/lib/utils.php on line 2604

Warning: Trying to access array offset on value of type bool in /home/cycos/cammy.co.jp/public_html/technical/wp-content/themes/cocoon-master/lib/utils.php on line 2632

shiftOut


指定したピンに接続されたシリアルパラレル変換IC(74HC595など)に順番に1ビットずつ信号をシフトアウトする関数です。

※本関数は未検証です。
※翻訳と関連した様々な記事から情報を整理しました。

書き方 shiftOut(dataPin,clockPin,bitOrder)
戻り値 (byte)readData….読み取ったデータ
引数 (int)dataPin … 出力したいデータピン番号
(int)clockPin … クロック出力のピン番号。クロックをHIGHにした後、ビットを読み取り、その後クロックピンをLOWにする。
(int)bitOrder … 上位から(MSBFIRST)読むか下位から(LSBFIRST)読むかを指定する

※たち上がりクロックのデバイスを利用するときは、shiftIn()を呼ぶ前にクロックピンをdigitalWriteでLOWにしておく。

プログラム例

公式Arduinoページより転記しました。

//**************************************************************//
//  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より大きい値を出力するには2ステップの操作が必要です。

// 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));
タイトルとURLをコピーしました