[高級輸入/輸出]
說明
一次移出一個字節的數據。從最高(即最左邊)或最低(最右邊)有效位開始。每個位依次寫入數據引腳,之後時鍾引腳被脈衝化(先為高電平,然後為低電平)以指示該位可用。
注意 - 如果您正在與由上升沿計時的設備連接,您需要確保在調用 shiftOut()
之前時鍾引腳為低電平,例如調用 digitalWrite(clockPin, LOW)
。
這是一個軟件實現;另請參見 SPI library ,它提供了一種更快但僅適用於特定引腳的硬件實現。
用法
shiftOut(dataPin, clockPin, bitOrder, value)
參數
dataPin
:輸出每個位的引腳。允許的數據類型:int
.
clockPin
:一旦將 dataPin 設置為正確的值,該引腳就會切換。允許的數據類型:int
.
bitOrder
:移出位的順序; MSBFIRST 或 LSBFIRST。 (最高有效位優先,或最低有效位優先)。
value
: 要移出的數據。允許的數據類型:byte
.
返回
無
示例代碼
//**************************************************************//
// 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 short用法及代碼示例
- Arduino sq()用法及代碼示例
- Arduino static用法及代碼示例
- Arduino scope用法及代碼示例
- Arduino setWireTimeout()用法及代碼示例
- Arduino serialEvent()用法及代碼示例
- Arduino setup()用法及代碼示例
- Arduino switch...case用法及代碼示例
- Arduino string用法及代碼示例
- Arduino sizeof()用法及代碼示例
- Arduino long用法及代碼示例
- Arduino Arduino_EMBRYO_2 - setLengthXY()用法及代碼示例
- Arduino ~用法及代碼示例
- Arduino ArduinoBLE - bleDevice.advertisedServiceUuidCount()用法及代碼示例
- Arduino const用法及代碼示例
- Arduino Ethernet - server.begin()用法及代碼示例
- Arduino ArduinoBLE - BLEService()用法及代碼示例
- Arduino digitalWrite()用法及代碼示例
- Arduino ArduinoBLE - bleCharacteristic.subscribe()用法及代碼示例
- Arduino Servo - attach()用法及代碼示例
- Arduino write()用法及代碼示例
- Arduino Arduino_LSM9DS1 - readGyroscope()用法及代碼示例
- Arduino ArduinoSound - FFTAnalyzer.input()用法及代碼示例
- Arduino MKRGSM - gprs.attachGPRS()用法及代碼示例
- Arduino WiFiNINA - WiFi.config()用法及代碼示例
注:本文由純淨天空篩選整理自arduino.cc大神的英文原創作品 shiftOut()。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。