My Schematic is like above
Hi , this program work fine to one register 74HC595, How can I control two 74HC595 inside this?
const int latchPin = 27;
const int clockPin = 14;
const int dataPin = 23;
int i = 25;
byte leds = 0;
void updateShiftRegister()
{
digitalWrite(latchPin, LOW);
shiftOut(dataPin, clockPin, LSBFIRST, leds);
digitalWrite(latchPin, HIGH);
}
void setup()
{
pinMode(latchPin, OUTPUT);
pinMode(dataPin, OUTPUT);
pinMode(clockPin, OUTPUT);
Serial.begin(115200);
Serial.println("setup");
}
void loop() {
if (Serial.available() > 0) {
int inByte = Serial.read();
switch (inByte) {
case 'a':
i = 0;
break;
case 'b':
i = 1;
break;
case 'h':
i = 7;
break;
case 'j':
i = 9;
break;
case 'L':
i = 10;
break;
default:
i = 25;
}
}
if ( i == 9) {
bitSet(leds, 0);
bitSet(leds, 1);
bitSet(leds, 2);
bitSet(leds, 3);
bitSet(leds, 4);
bitSet(leds, 5);
bitSet(leds, 6);
bitSet(leds, 7);
bitSet(leds, 8);
updateShiftRegister();
}
if ( i == 10) {
bitClear(leds, 0);
bitClear(leds, 1);
bitClear(leds, 2);
bitClear(leds, 3);
bitClear(leds, 4);
bitClear(leds, 5);
bitClear(leds, 6);
bitClear(leds, 7);
bitClear(leds, 8);
updateShiftRegister();
}
if ( i == 0) {
bitSet(leds, 0);
updateShiftRegister();
Serial.println("0 on");
}
if ( i == 1) {
bitSet(leds, 1);
updateShiftRegister();
Serial.println("1 on");
}
if ( i == 7) {
bitSet(leds, 7);
updateShiftRegister();
Serial.println("7 on");
}
}
Please use [code] tags when posting code.
Here's what I use: SPI-Blink.ino
It allows you to use the standard digitalWrite() function to control the pins of any number of shift registers.
The example above uses the SPI bus, which is the fastest and most efficient method. If you really want to use normal IO pins, you can use the bit-banged version: BitBankg-Blink.ino
For example:
#include <Arduino_Helpers.h> // https://github.com/tttapa/Arduino-Helpers
#include <AH/Hardware/ExtendedInputOutput/SPIShiftRegisterOut.hpp>
// Instantiate a shift register with the SPI slave select pin as latch pin, most
// significant bit first, and a total of 16 outputs.
SPIShiftRegisterOut<16> sreg = {SS, MSBFIRST};
void setup() {
sreg.begin(); // Initialize the shift registers
}
void loop() {
// Turn each of the 16 pins of the shift registers
// on and off in sequence.
for (pin_t pin = 0; pin < sreg.getLength(); ++pin) {
sreg.digitalWrite(pin, HIGH);
delay(100);
sreg.digitalWrite(pin, LOW);
}
}
If you want to keep using your code, you could use 16-bit integers to store your LED states, and add a second shiftOut call to the updateShiftRegister function, one for the least significant byte and one for the most significant byte (while keeping the latch pin low).
Pieter
PieterP:
Please use[code]tags when posting code.
Here's what I use: SPI-Blink.ino
It allows you to use the standarddigitalWrite()function to control the pins of any number of shift registers.
The example above uses the SPI bus, which is the fastest and most efficient method. If you really want to use normal IO pins, you can use the bit-banged version: BitBankg-Blink.inoFor example:
#include <Arduino_Helpers.h> // https://github.com/tttapa/Arduino-Helpers
#include <AH/Hardware/ExtendedInputOutput/SPIShiftRegisterOut.hpp>
// Instantiate a shift register with the SPI slave select pin as latch pin, most
// significant bit first, and a total of 16 outputs.
SPIShiftRegisterOut<16> sreg = {SS, MSBFIRST};
void setup() {
sreg.begin(); // Initialize the shift registers
}
void loop() {
// Turn each of the 16 pins of the shift registers
// on and off in sequence.
for (pin_t pin = 0; pin < sreg.getLength(); ++pin) {
sreg.digitalWrite(pin, HIGH);
delay(100);
sreg.digitalWrite(pin, LOW);
}
}
If you want to keep using your code, you could use 16-bit integers to store your LED states, and add a second shiftOut call to the updateShiftRegister function, one for the least significant byte and one for the most significant byte (while keeping the latch pin low). Pieter [/code]
Thank you by support PieeterP,
I tried apply your code, but I don't understood very well where should I put the pin?
Like this?
const int latchPin = 27;
const int clockPin = 14;
const int dataPin = 23;
aleubc1:
Thank you by support PieeterP,I tried apply your code, but I don't understood very well where should I put the pin?
Like this?
const int latchPin = 27;
const int clockPin = 14;
const int dataPin = 23;
That would be how the bit-banged version works. SPI doesn't have that because it has a fixed assignment on certain MCU pins. Only the SS function is assignable to almost any pin.
aleubc1:
I tried apply your code, but I don't understood very well where should I put the pin?Like this?
const int latchPin = 27;
const int clockPin = 14;
const int dataPin = 23;
You'll find similar constants in the the bit-banged example, you can replace those by your own pin numbers:
const pin_t latchPin = 10; // Pin connected to ST_CP of 74HC595
const pin_t dataPin = 11; // Pin connected to DS of 74HC595
const pin_t clockPin = 13; // Pin connected to SH_CP of 74HC595
// Instantiate a shift register on the correct pins, most significant bit first,
// and a total of 8 outputs.
ShiftRegisterOut<8> sreg = {dataPin, clockPin, latchPin, MSBFIRST};
If you're using SPI, you have to use the MOSI pin as data pin and the CLK pin as clock pin. You're free to choose the latch pin, the example uses the SS (slave select) pin as latch pin, because you usually have to use that pin as output anyway when using SPI. You can find which pins are the MOSI and CLK pins in the pinout for your specific Arduino board (you forgot to mention which board you're using).
Hello
I would like control each separeted pin of register (74HC595). For example control the pin 15. This program not worked to dual 74HC595.
Sorry by my mistakes , because I'm new bie to program.
/* ESP32
05 -> SS: 74HC595 ST_CP
23 -> MOSI: 74HC595 DS
18 -> SCK: 74HC595 SH_CP
*/
#include <Arduino_Helpers.h> // https://github.com/tttapa/Arduino-Helpers
#include <AH/Hardware/ExtendedInputOutput/SPIShiftRegisterOut.hpp>
// Instantiate a shift register with the SPI slave select pin as latch pin, most
// significant bit first, and a total of 16 outputs.
SPIShiftRegisterOut<16> sreg = {SS, MSBFIRST};
void setup() {
sreg.begin(); // Initialize the shift registers
}
void loop() {
pin_t pin = 15;
sreg.digitalWrite(pin, HIGH);
delay(1100);
sreg.digitalWrite(pin, LOW);
}
05 -> SS: 74HC595 ST_CP
The SS constant in the Arduino IDE is not always pin 5, it depends on which ESP32 development board you're using.
Try specifying pin 5 explicitly:
SPIShiftRegisterOut<16> sreg = {5, MSBFIRST};
sreg.digitalWrite(pin, HIGH);
delay(1100);
sreg.digitalWrite(pin, LOW);
This turns of the LED and immediately turns it on again. You need a second delay.
I've just tried this code on a Sparkfun ESP32 Thing with two daisy-chained 74HC595 shift registers and it works just fine:
/* ESP32
05 -> SS: 74HC595 ST_CP
23 -> MOSI: 74HC595 DS
18 -> SCK: 74HC595 SH_CP
*/
#include <Arduino_Helpers.h> // https://github.com/tttapa/Arduino-Helpers
#include <AH/Hardware/ExtendedInputOutput/SPIShiftRegisterOut.hpp>
// Instantiate a shift register with pin 5 as latch pin, most
// significant bit first, and a total of 16 outputs.
SPIShiftRegisterOut<16> sreg = {5, MSBFIRST};
void setup() {
sreg.begin(); // Initialize the shift registers
}
void loop() {
pin_t pin = 15;
sreg.digitalWrite(pin, HIGH);
delay(1100);
sreg.digitalWrite(pin, LOW);
delay(1100);
}