可以使用序列的方式傳入Q0~Q7的狀態(high or low)到內部register,然後輸出時chip依照register值去拉Q0~Q7的狀態
可以使用少數腳位一次控制Q0~Q7的狀態,此外由於此chip可以串接,所以可以一次控制更多腳位
下列圖來源: http://arduino.cc/en/uploads/Tutorial/595datasheet.pdf
74HCT595N腳位:
- VCC:5V
- GND
- Ds: serial data in
- Q0~Q7: 8根輸出pin
- Q7': storage register對應到Q7的bit,最為多顆chip 串接用
- MR: (active low) 清空shift register
- OE: (active low)
- 若OE拉low,STcp raising時,將storage輸出至Q0~Q7
- 若OE拉high,Q0~Q7輸出為高阻抗(相當於沒接上這顆chip)
- SHcp: raising edge trigger, 觸發shift register shift 1 bit(新的1bit資料由Ds取)
- STcp: raising edge trigger
從文件可以知道,74HCT595N有兩個register(8bit):
- shift register - 序列資料輸入時使用
- storage register - 控制Q0~Q7輸出時為high or low
從文件可以知道,SHcp/STcp最高可運作在100Mhz(每個clock 10ns)
- 由此可得知下面範例SHcp/STcp拉high/low時,每次delay只要5ns,不過由於Arduino delay最小只能用1 micro second,所以設定delayMicroseconds(1)即可
基本原理
- 透過序列的方式將資料傳入shift register
- 將shift register資料傳入storage register
- 每個SHcp raising edge,資料會一直輸入進去chip的shift register
- Q0'~Q7'代表shift register的值
- Serial date寫入方向:
- Ds > Q0' > Q1' > Q2' > Q3' > Q4' > Q5' > Q6' > Q7'
- 輸出到Q0~Q7
範例:
- 連接
- chip 接上VCC/GND
- micro controller gpio 接上Ds/MR/OE/SHcp/STcp
- 初始化:
- Ds/MR/OE/SHcp/STcp全部拉low
- 此時由於MR為low,shift register全部清為0
- STcp LOW->HIGH->LOW,將shift register的值寫入storage register
- 此時由於OE為LOW,storage register的值馬上會輸出至Q0~Q7
- 資料寫至shift register
- 將MR 拉HIGH
- 將Q7的值設到Ds, SHcp LOW->HIGN->LOW將資料移入shift register
- 將Q6的值設到Ds, SHcp LOW->HIGN->LOW將資料移入shift register
- 將Q5的值設到Ds, SHcp LOW->HIGN->LOW將資料移入shift register
- 將Q4的值設到Ds, SHcp LOW->HIGN->LOW將資料移入shift register
- 將Q3的值設到Ds, SHcp LOW->HIGN->LOW將資料移入shift register
- 將Q2的值設到Ds, SHcp LOW->HIGN->LOW將資料移入shift register
- 將Q1的值設到Ds, SHcp LOW->HIGN->LOW將資料移入shift register
- 將Q0的值設到Ds, SHcp LOW->HIGN->LOW將資料移入shift register
- 將資料從shift register移到storage register
- STcp LOW->HIGH->LOW
- 此時由於OE為LOW,storage register的值馬上會輸出至Q0~Q7
- 使用MTADRL293D 電機驅動模組,其中
- Ds: Arduino pin 8
- MR: 固定拉high
- OE: Arduino pin 7
- SHcp: Arduino pin 4
- STcp: Arduino pin 12
Arduino可使用我寫的library操作74HCT595N:
- https://github.com/cy-arduino/arduino_lib_MTADRL293D <== 74HCT595N 控制的library包含在此
簡單控制74HCT595N的範例程式碼:
const int DS = 8; const int OE = 7; const int SHCP = 4; const int STCP = 12; void setup(){ Serial.begin(115200); while (!Serial){ ; } delay(1000); Serial.println("=== setup() ==="); pinMode(DS, OUTPUT); pinMode(OE, OUTPUT); pinMode(SHCP, OUTPUT); pinMode(STCP, OUTPUT); digitalWrite(DS, LOW); digitalWrite(OE, HIGH); //Q0~Q7 維持高阻抗 digitalWrite(SHCP, LOW); digitalWrite(STCP, LOW); } void loop(){ Serial.println("=== loop() ==="); delay(1000); //serial data input into serial register //Q0~Q7: 00001111 Serial.println("##serial data input into serial register"); digitalWrite(DS, HIGH);//Q7 digitalWrite(SHCP, HIGH); delayMicroseconds(1); digitalWrite(SHCP, LOW); delayMicroseconds(1); digitalWrite(DS, HIGH);//Q6 digitalWrite(SHCP, HIGH); delayMicroseconds(1); digitalWrite(SHCP, LOW); delayMicroseconds(1); digitalWrite(DS, HIGH);//Q5 digitalWrite(SHCP, HIGH); delayMicroseconds(1); digitalWrite(SHCP, LOW); delayMicroseconds(1); digitalWrite(DS, HIGH);//Q4 digitalWrite(SHCP, HIGH); delayMicroseconds(1); digitalWrite(SHCP, LOW); delayMicroseconds(1); digitalWrite(DS, LOW);//Q3 digitalWrite(SHCP, HIGH); delayMicroseconds(1); digitalWrite(SHCP, LOW); delayMicroseconds(1); digitalWrite(DS, LOW);//Q2 digitalWrite(SHCP, HIGH); delayMicroseconds(1); digitalWrite(SHCP, LOW); delayMicroseconds(1); digitalWrite(DS, LOW);//Q1 digitalWrite(SHCP, HIGH); delayMicroseconds(1); digitalWrite(SHCP, LOW); delayMicroseconds(1); digitalWrite(DS, LOW);//Q0 digitalWrite(SHCP, HIGH); delayMicroseconds(1); digitalWrite(SHCP, LOW); delayMicroseconds(1); delay(1000); //serial register -> storage register Serial.println("##serial register -> storage register"); digitalWrite(STCP, HIGH); delay(10); digitalWrite(STCP, LOW); delay(10); delay(1000); //storage register -> Q0~Q7 Serial.println("##storage register -> Q0~Q7"); digitalWrite(OE, LOW); delay(1000); Serial.println("##finish"); while(true){ ; } }
========================================================
相關連結
- http://ruten-proteus.blogspot.tw/2012/11/io-74hc595-ic.html
- http://arduino.cc/en/tutorial/ShiftOut#.Uw19LvmSyXo
- http://arduino.tw/articlesindex/extend-io/213-74hc595.html
- http://arduino.cc/en/uploads/Tutorial/595datasheet.pdf
待續
沒有留言:
張貼留言