2018年2月28日 星期三

Shift Register 74HC595 Arduino Library

The Shift Register 74HC595 Arduino Library makes the use of shift registers much easier.

It basically allows you to set single pins of your shift register either high or low, instead of shifting out complicated created bytes. So you can simply increase the number of output pins on your microcontroller without much more work. Source code on GitHub


Get started!


Download the library and install it. Connect the shift register to your Microcontroller as shown in Fig. 1.


Open up a new project in the Arduino IDE and import the library with the following code: 

#include <ShiftRegister74HC595.h>


Create an instance of the ShiftRegister74HC595 class like so:

int numberOfShiftRegisters = 1; // number of shift registers attached in series
int serialDataPin = 0; // DS
int clockPin = 1; // SHCP
int latchPin = 2; // STCP
ShiftRegister74HC595 sr (numberOfShiftRegisters, serialDataPin, clockPin, latchPin); 


If you want to use multiple shift registers in series, connect them as shown in Fig. 2.


You can create multiple instances of the shift register class if necessary. In fact this makes sense if you want to use multiple shift registers parallel and not in series.


Insert the normal setup and loop function.


Congratulations! Now you have additional output pins (Fig. 3 - series resistors are left out for clarify reasons). You can access them with a bunch of methods:

// sets a specific pin of your shift register on high
int pin = 0;
sr.set(pin, HIGH); // equivalent to sr.set(pin, 1);

// sets all pins either on high ...
sr.setAllHigh();
// ... or low
sr.setAllLow();

// sets all pins at once (pin 0 low, pin 1 high, ...)
uint8_t pinValues[] = { B10101010 };
sr.setAll(pinValues); 

// set multiple pins at once when using two shift registers in series
uint8_t pinValues[] = { B00011000, B10101010 };
sr.setAll(pinValues); 


You can also read the current state of a pin:

int pin = 0; 
uint8_t state = sr.get(pin); // 0 = LOW, 1 = HIGH


An example of using the library can be found here.


That's it from me! Feel free to contact me if you have any questions or want to give feedback.

List of all functions


ShiftRegister74HC595(int numberOfShiftRegisters, int serialDataPin, int clockPin, int latchPin);
void setAll(uint8_t * digitalValues);
uint8_t * getAll(); 
void set(int pin, uint8_t value);
void setAllLow();
void setAllHigh(); 
uint8_t get(int pin);



===============測試後 24bit LED ===============

/*
  ShiftRegister74HC595.h - Library for easy control of the 74HC595 shift register.
  Created by Timo Denk (www.simsso.de), Nov 2014.
  Additional information are available on http://shiftregister.simsso.de/
  Released into the public domain.

// sets a specific pin of your shift register on high
int pin = 0;
sr.set(pin, HIGH); // equivalent to sr.set(pin, 1);

// sets all pins either on high ...
sr.setAllHigh();
// ... or low
sr.setAllLow();

// sets all pins at once (pin 0 low, pin 1 high, ...)
uint8_t pinValues[] = { B10101010 };
sr.setAll(pinValues); 

// set multiple pins at once when using two shift registers in series
uint8_t pinValues[] = { B00011000, B10101010 };
sr.setAll(pinValues); 



You can also read the current state of a pin:

int pin = 0; 
uint8_t state = sr.get(pin); // 0 = LOW, 1 = HIGH


*/

// 接 74HC595 的第 12 支接腳
//int latchPin = 5;
// 接 74HC595 的第 11 支接腳
//int clockPin = 4;
// 接 74HC595 的第 14 支接腳
//int dataPin = 16;


#include <ShiftRegister74HC595.h>
// create shift register object (number of shift registers, data pin, clock pin, latch pin)
ShiftRegister74HC595 sr (3, 16, 4, 5); 

void setup() { 
}

void loop() {

  sr.setAllHigh(); // set all pins HIGH
  delay(500);
  
  sr.setAllLow(); // set all pins LOW
  delay(500); 
  
  
  for (int i = 0; i < 23; i++) {
    
    sr.set(i, HIGH); // set single pin HIGH
    delay(250); 
  }
  
  
  // set all pins at once
  byte d1=0x3c;
  byte d2=0xaa;
  byte d3=0x55;
  
  uint8_t pinValues[] = { d1, d2 , d3 };
  sr.setAll(pinValues); 
  delay(10000);

  byte d4=0xc3;
  byte d5=0x55;
  byte d6=0xaa;
  uint8_t pinValues1[] = { d4, d5 , d6 };
  sr.setAll(pinValues1); 
  delay(10000);
  
  
  // read pin (zero based)
  uint8_t stateOfPin5 = sr.get(5);
  uint8_t stateOfPin23 = sr.get(23);
  
  
}




沒有留言:

張貼留言

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> &...