2018年2月27日 星期二

使用兩顆 74HC595 和三支腳位控制 16 顆 LED

ESP8266 NodeMcu 與 兩顆 74HC595 和三支腳位控制 16 顆 LED

源自於
http://coopermaa2nd.blogspot.tw/2010/12/arduino-lab12-74hc595-16-led.html


// Lab12 使用2顆 74HC595 和三支腳位控制 16 顆 LED
//static const uint8_t D0   = 16;
//static const uint8_t D1   = 5;
//static const uint8_t D2   = 4;
//static const uint8_t D3   = 0;
//static const uint8_t D4   = 2;
//static const uint8_t D5   = 14;
//static const uint8_t D6   = 12;
//static const uint8_t D7   = 13;
//static const uint8_t D8   = 15;
//static const uint8_t D9   = 3;
//static const uint8_t D10  = 1;
// 接 74HC595 的 ST_CP (pin 12,latch pin)
int latchPin = 5;   
// 接 74HC595 的 SH_CP (pin 11, clock pin)
int clockPin = 4;
// 接 74HC595 的 DS (pin 14)
int dataPin = 16;

void setup() {
  // 將 latchPin, clockPin, dataPin 設置為輸出
  pinMode(latchPin, OUTPUT);
  pinMode(clockPin, OUTPUT);
  pinMode(dataPin, OUTPUT);
}

void loop() {     
  for (int led = 0; led < 16; led++) {
    int numberToDisplay  = 1 << led;
    byte high_Byte = ~ (highByte(numberToDisplay));     //經過反相處理
    byte low_Byte = ~ (lowByte(numberToDisplay));         //經過反相處理
   
    // 送資料前要先把 latchPin 拉成低電位
    digitalWrite(latchPin, LOW);
   
    // 先送高位元組 (Hight Byte), 給離 Arduino 較遠的那顆 74HC595
    shiftOut(dataPin, clockPin, MSBFIRST, high_Byte); 
    // 再送低位元組 (Low Byte), 給離 Arduino 較近的那顆 74HC595
    shiftOut(dataPin, clockPin, MSBFIRST, low_Byte); 

    // 送完資料後要把 latchPin 拉回成高電位
    digitalWrite(latchPin, HIGH);
 
    delay(40);
  }
}


74HC595 是一顆八位元的移位暫存器,同時可以控制八個輸出,我們可以把多顆移位暫存器串接 (Daisy chain) 在一起以擴充腳位,例如: 如果串接兩顆 74HC595 移位暫存器,便可以同時控制 16 個輸出。
74HC595 總共有 16 支接腳,底下這 16 支接腳的圖表說明:
image_thumb4 
腳位編號名稱說明
1-7, 15Q0 ~ Q7輸出腳位
8GND接地
7Q7’序列輸出 (Serial Out)
10MRMaster Reset, 清除所有資料, 低電位有效 (Active low)
11SH_CPSHift register clock pin (Clock Pin)
12ST_CPSTorage register clock pin (Latch Pin)
13OEOutput Enable, 允許輸出,低電位有效 (Active low)
14DS序列資料輸入 (Serial data input)
16Vcc供應電壓
要把兩顆 74HC595 串接起來,方法很簡單,只要把第一顆 74HC595 的 SH_CP (Clock Pin) 和 ST_CP (Latch Pin) 兩支腳位接到第二顆 74HC595 上,接著把第一顆 74HC595 的 Q7’ (序列輸出腳) 接到第二顆的 DS (序列資料輸入) 就可以了。



沒有留言:

張貼留言

2024年4月24日 星期三 Node-Red Dashboard UI Template + AngularJS 參考 AngularJS教學 --2

 2024年4月24日 星期三 Node-Red Dashboard UI Template + AngularJS 參考 AngularJS教學 --2 AngularJS 實例 <!DOCTYPE html> <html> <head> &...