From 6167c83dca5b6085445a314c08d4afeb2cba7b3c Mon Sep 17 00:00:00 2001 From: Arif Balik Date: Wed, 28 Oct 2020 18:18:36 +0300 Subject: [PATCH 01/14] setNoUpdate and get functions updated Everytime setNoUpdate is called, it calculates quite a bit, so using pointers seems much faster. --- src/ShiftRegister74HC595.hpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/ShiftRegister74HC595.hpp b/src/ShiftRegister74HC595.hpp index cb88886..76402d3 100644 --- a/src/ShiftRegister74HC595.hpp +++ b/src/ShiftRegister74HC595.hpp @@ -89,7 +89,7 @@ void ShiftRegister74HC595::updateRegisters() template void ShiftRegister74HC595::setNoUpdate(const uint8_t pin, const uint8_t value) { - (value) ? bitSet(_digitalValues[pin / 8], pin % 8) : bitClear(_digitalValues[pin / 8], pin % 8); + bitWrite(*_digitalValues, pin, value); } // Returns the state of the given pin. @@ -97,7 +97,7 @@ void ShiftRegister74HC595::setNoUpdate(const uint8_t pin, const uint8_t va template uint8_t ShiftRegister74HC595::get(const uint8_t pin) { - return (_digitalValues[pin / 8] >> (pin % 8)) & 1; + return bitRead(*_digitalValues, pin); } // Sets all pins of all shift registers to HIGH (1). From 93879fe78c19105b8d8a11c7ac3a159adb2e8906 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mindaugas=20=C5=BDvirblis?= Date: Mon, 28 Dec 2020 14:00:27 +0200 Subject: [PATCH 02/14] editable bit order --- src/ShiftRegister74HC595.h | 2 ++ src/ShiftRegister74HC595.hpp | 8 +++++++- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/src/ShiftRegister74HC595.h b/src/ShiftRegister74HC595.h index ac78fdf..12309e8 100755 --- a/src/ShiftRegister74HC595.h +++ b/src/ShiftRegister74HC595.h @@ -26,8 +26,10 @@ class ShiftRegister74HC595 void setAllLow(); void setAllHigh(); uint8_t get(const uint8_t pin); + void order(uint8_t o); private: + uint8_t bo = MSBFIRST; uint8_t _clockPin; uint8_t _serialDataPin; uint8_t _latchPin; diff --git a/src/ShiftRegister74HC595.hpp b/src/ShiftRegister74HC595.hpp index cb88886..2aba1a0 100644 --- a/src/ShiftRegister74HC595.hpp +++ b/src/ShiftRegister74HC595.hpp @@ -77,7 +77,7 @@ template void ShiftRegister74HC595::updateRegisters() { for (int i = Size - 1; i >= 0; i--) { - shiftOut(_serialDataPin, _clockPin, MSBFIRST, _digitalValues[i]); + shiftOut(_serialDataPin, _clockPin, bo, _digitalValues[i]); } digitalWrite(_latchPin, HIGH); @@ -119,3 +119,9 @@ void ShiftRegister74HC595::setAllLow() } updateRegisters(); } + +template +void ShiftRegister74HC595::order(uint8_t o) +{ + bo = o; +} \ No newline at end of file From e26455d3a9af596221d8de42980bb2d6e086e834 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mindaugas=20=C5=BDvirblis?= Date: Fri, 18 Mar 2022 16:05:08 +0200 Subject: [PATCH 03/14] default shift order --- src/ShiftRegister74HC595.hpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/ShiftRegister74HC595.hpp b/src/ShiftRegister74HC595.hpp index 2aba1a0..26d34e4 100644 --- a/src/ShiftRegister74HC595.hpp +++ b/src/ShiftRegister74HC595.hpp @@ -15,6 +15,8 @@ ShiftRegister74HC595::ShiftRegister74HC595(const uint8_t serialDataPin, co _serialDataPin = serialDataPin; _latchPin = latchPin; + bo = MSBFIRST; + // define pins as outputs pinMode(clockPin, OUTPUT); pinMode(serialDataPin, OUTPUT); @@ -124,4 +126,4 @@ template void ShiftRegister74HC595::order(uint8_t o) { bo = o; -} \ No newline at end of file +} From abe7d8b66385e204b1e79a87f287be7df3623370 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mindaugas=20=C5=BDvirblis?= Date: Sat, 19 Mar 2022 11:15:12 +0200 Subject: [PATCH 04/14] Revert "setNoUpdate and get functions updated" This reverts commit 6167c83dca5b6085445a314c08d4afeb2cba7b3c. --- src/ShiftRegister74HC595.hpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/ShiftRegister74HC595.hpp b/src/ShiftRegister74HC595.hpp index 34cdbe9..26d34e4 100644 --- a/src/ShiftRegister74HC595.hpp +++ b/src/ShiftRegister74HC595.hpp @@ -91,7 +91,7 @@ void ShiftRegister74HC595::updateRegisters() template void ShiftRegister74HC595::setNoUpdate(const uint8_t pin, const uint8_t value) { - bitWrite(*_digitalValues, pin, value); + (value) ? bitSet(_digitalValues[pin / 8], pin % 8) : bitClear(_digitalValues[pin / 8], pin % 8); } // Returns the state of the given pin. @@ -99,7 +99,7 @@ void ShiftRegister74HC595::setNoUpdate(const uint8_t pin, const uint8_t va template uint8_t ShiftRegister74HC595::get(const uint8_t pin) { - return bitRead(*_digitalValues, pin); + return (_digitalValues[pin / 8] >> (pin % 8)) & 1; } // Sets all pins of all shift registers to HIGH (1). From d4fa490e884d1121af67339c3632e90729c606b5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mindaugas=20=C5=BDvirblis?= Date: Sat, 19 Mar 2022 11:24:53 +0200 Subject: [PATCH 05/14] implemented SPI --- README.md | 6 +++++ src/ShiftRegister74HC595.cpp | 3 ++- src/ShiftRegister74HC595.h | 8 ++++++ src/ShiftRegister74HC595.hpp | 48 +++++++++++++++++++++++++++++++++++- 4 files changed, 63 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index c5e116a..b3ff30f 100644 --- a/README.md +++ b/README.md @@ -8,3 +8,9 @@ sr.set(1, HIGH) Please find the detailed **documentation** at https://timodenk.com/blog/shift-register-arduino-library/. An **example** sketch can be found in this repository at [/examples/example/example.ino](https://github.com/Simsso/ShiftRegister74HC595/blob/master/examples/example/example.ino). + + +New: + +to shift data via SPI define `SHIFT_REGISTER_USES_SPI_WITH_FREQUENCY 20000000UL` before you include ShiftRegister74HC595.h +Arduino UNO uses pin 13 for CLOCK and pin 11 for MOSI (data) - latch pin - configurable. \ No newline at end of file diff --git a/src/ShiftRegister74HC595.cpp b/src/ShiftRegister74HC595.cpp index a3a27c1..653136c 100755 --- a/src/ShiftRegister74HC595.cpp +++ b/src/ShiftRegister74HC595.cpp @@ -4,6 +4,7 @@ Additional information is available at https://timodenk.com/blog/shift-register-arduino-library/ Released into the public domain. */ - +#define SHIFT_REGISTER_USES_SPI_WITH_FREQUENCY 20000000 #include #include "ShiftRegister74HC595.h" + diff --git a/src/ShiftRegister74HC595.h b/src/ShiftRegister74HC595.h index 12309e8..5ae948d 100755 --- a/src/ShiftRegister74HC595.h +++ b/src/ShiftRegister74HC595.h @@ -9,11 +9,19 @@ #include +#if defined(SHIFT_REGISTER_USES_SPI_WITH_FREQUENCY) +#include +#endif + template class ShiftRegister74HC595 { public: +#if !defined(SHIFT_REGISTER_USES_SPI_WITH_FREQUENCY) ShiftRegister74HC595(const uint8_t serialDataPin, const uint8_t clockPin, const uint8_t latchPin); +#else + ShiftRegister74HC595(const uint8_t latchPin); +#endif void setAll(const uint8_t * digitalValues); #ifdef __AVR__ diff --git a/src/ShiftRegister74HC595.hpp b/src/ShiftRegister74HC595.hpp index 26d34e4..97952a8 100644 --- a/src/ShiftRegister74HC595.hpp +++ b/src/ShiftRegister74HC595.hpp @@ -5,6 +5,9 @@ Released into the public domain. */ + +#if !defined(SHIFT_REGISTER_USES_SPI_WITH_FREQUENCY) + // ShiftRegister74HC595 constructor // Size is the number of shiftregisters stacked in serial template @@ -33,6 +36,34 @@ ShiftRegister74HC595::ShiftRegister74HC595(const uint8_t serialDataPin, co updateRegisters(); // reset shift register } +#else + +// ShiftRegister74HC595 constructor +// Size is the number of shiftregisters stacked in serial +template +ShiftRegister74HC595::ShiftRegister74HC595(const uint8_t latchPin) +{ + _latchPin = latchPin; + + bo = MSBFIRST; + + // define pins as outputs + pinMode(latchPin, OUTPUT); + + // set pins low + digitalWrite(latchPin, LOW); + + // allocates the specified number of bytes and initializes them to zero + memset(_digitalValues, 0, Size * sizeof(uint8_t)); + + SPI.begin(SPISettings(SHIFT_REGISTER_USES_SPI_WITH_FREQUENCY, bo, SPI_MODE0);) + + updateRegisters(); // reset shift register +} + + +#endif + // Set all pins of the shift registers at once. // digitalVAlues is a uint8_t array where the length is equal to the number of shift registers. template @@ -73,6 +104,8 @@ void ShiftRegister74HC595::set(const uint8_t pin, const uint8_t value) updateRegisters(); } + +#if !defined(SHIFT_REGISTER_USES_SPI_WITH_FREQUENCY) // Updates the shift register pins to the stored output values. // This is the function that actually writes data into the shift registers of the 74HC595. template @@ -85,7 +118,20 @@ void ShiftRegister74HC595::updateRegisters() digitalWrite(_latchPin, HIGH); digitalWrite(_latchPin, LOW); } - +#else +// Updates the shift register pins to the stored output values. +// This is the function that actually writes data into the shift registers of the 74HC595. +template +void ShiftRegister74HC595::updateRegisters() +{ + for (int i = Size - 1; i >= 0; i--) { + SPI.transfer(digitalValues[i]); + } + + digitalWrite(_latchPin, HIGH); + digitalWrite(_latchPin, LOW); +} +#endif // Equivalent to set(int pin, uint8_t value), except the physical shift register is not updated. // Should be used in combination with updateRegisters(). template From 796091d1526d06b693569df5a4573b32ac89d247 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mindaugas=20=C5=BDvirblis?= Date: Sat, 19 Mar 2022 11:26:18 +0200 Subject: [PATCH 06/14] typo --- src/ShiftRegister74HC595.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ShiftRegister74HC595.hpp b/src/ShiftRegister74HC595.hpp index 97952a8..62cc934 100644 --- a/src/ShiftRegister74HC595.hpp +++ b/src/ShiftRegister74HC595.hpp @@ -125,7 +125,7 @@ template void ShiftRegister74HC595::updateRegisters() { for (int i = Size - 1; i >= 0; i--) { - SPI.transfer(digitalValues[i]); + SPI.transfer(_digitalValues[i]); } digitalWrite(_latchPin, HIGH); From 31de6262410813292c5406ac59135410daff5cdc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mindaugas=20=C5=BDvirblis?= Date: Sat, 19 Mar 2022 11:30:40 +0200 Subject: [PATCH 07/14] mistakes... --- src/ShiftRegister74HC595.hpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/ShiftRegister74HC595.hpp b/src/ShiftRegister74HC595.hpp index 62cc934..d5c65e6 100644 --- a/src/ShiftRegister74HC595.hpp +++ b/src/ShiftRegister74HC595.hpp @@ -56,7 +56,8 @@ ShiftRegister74HC595::ShiftRegister74HC595(const uint8_t latchPin) // allocates the specified number of bytes and initializes them to zero memset(_digitalValues, 0, Size * sizeof(uint8_t)); - SPI.begin(SPISettings(SHIFT_REGISTER_USES_SPI_WITH_FREQUENCY, bo, SPI_MODE0);) + SPI.begin(); + SPI.beginTransaction(SPISettings(SHIFT_REGISTER_USES_SPI_WITH_FREQUENCY, bo, SPI_MODE0)); updateRegisters(); // reset shift register } From 37a41597de243295afb2028dc9c2a4d1ba1d1144 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mindaugas=20=C5=BDvirblis?= Date: Sat, 19 Mar 2022 11:35:49 +0200 Subject: [PATCH 08/14] removed hardcode definition --- src/ShiftRegister74HC595.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ShiftRegister74HC595.cpp b/src/ShiftRegister74HC595.cpp index 653136c..cd5d864 100755 --- a/src/ShiftRegister74HC595.cpp +++ b/src/ShiftRegister74HC595.cpp @@ -4,7 +4,7 @@ Additional information is available at https://timodenk.com/blog/shift-register-arduino-library/ Released into the public domain. */ -#define SHIFT_REGISTER_USES_SPI_WITH_FREQUENCY 20000000 + #include #include "ShiftRegister74HC595.h" From 50eba09f7c154a19498a2d5c46af6a392e83c5a9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mindaugas=20=C5=BDvirblis?= Date: Sat, 19 Mar 2022 11:38:19 +0200 Subject: [PATCH 09/14] Readme --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index b3ff30f..a2697f3 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,9 @@ Please find the detailed **documentation** at https://timodenk.com/blog/shift-re An **example** sketch can be found in this repository at [/examples/example/example.ino](https://github.com/Simsso/ShiftRegister74HC595/blob/master/examples/example/example.ino). + New: to shift data via SPI define `SHIFT_REGISTER_USES_SPI_WITH_FREQUENCY 20000000UL` before you include ShiftRegister74HC595.h + Arduino UNO uses pin 13 for CLOCK and pin 11 for MOSI (data) - latch pin - configurable. \ No newline at end of file From f92e2b45bd21801c7a32a72ddb93ec24b997094c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mindaugas=20=C5=BDvirblis?= Date: Sat, 19 Mar 2022 12:08:15 +0200 Subject: [PATCH 10/14] added spi example and bumped lib version --- examples/spi_example/example.ino | 53 ++++++++++++++++++++++++++++++++ library.properties | 2 +- 2 files changed, 54 insertions(+), 1 deletion(-) create mode 100644 examples/spi_example/example.ino diff --git a/examples/spi_example/example.ino b/examples/spi_example/example.ino new file mode 100644 index 0000000..456f759 --- /dev/null +++ b/examples/spi_example/example.ino @@ -0,0 +1,53 @@ +/* + ShiftRegister74HC595 - Library for simplified control of 74HC595 shift registers. + Developed and maintained by Timo Denk and contributers, since Nov 2014. + Additional information is available at https://timodenk.com/blog/shift-register-arduino-library/ + Released into the public domain. +*/ + +// 20000000UL <- your chip speed. if arduino cand handle that, it will work at max it can. On uno up to 8mhz. 20000000UL means 20Mhz +#define SHIFT_REGISTER_USES_SPI_WITH_FREQUENCY 20000000UL +#include + +// create a global shift register object +// parameters: (data pin, clock pin, latch pin) +ShiftRegister74HC595<1> sr(7); + +void setup() { +} + +void loop() { + + // setting all pins at the same time to either HIGH or LOW + sr.setAllHigh(); // set all pins HIGH + delay(500); + + sr.setAllLow(); // set all pins LOW + delay(500); + + + // setting single pins + for (int i = 0; i < 8; i++) { + + sr.set(i, HIGH); // set single pin HIGH + delay(250); + } + + + // set all pins at once + uint8_t pinValues[] = { B10101010 }; + sr.setAll(pinValues); + delay(1000); + + + // read pin (zero based, i.e. 6th pin) + uint8_t stateOfPin5 = sr.get(5); + sr.set(6, stateOfPin5); + + + // set pins without immediate update + sr.setNoUpdate(0, HIGH); + sr.setNoUpdate(1, LOW); + // at this point of time, pin 0 and 1 did not change yet + sr.updateRegisters(); // update the pins to the set values +} diff --git a/library.properties b/library.properties index 0b311d5..d31452c 100644 --- a/library.properties +++ b/library.properties @@ -1,5 +1,5 @@ name=ShiftRegister74HC595 -version=1.3.1 +version=1.3.2 author=Timo Denk (timodenk.com) maintainer=Timo Denk (timodenk.com) sentence=Simplifies usage of shift registers, designed for the 74HC595. From 131262ade199f38381642184e4f29ef406fb9e7e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mindaugas=20=C5=BDvirblis?= Date: Sat, 19 Mar 2022 12:10:48 +0200 Subject: [PATCH 11/14] updated spi example comments --- examples/spi_example/example.ino | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/examples/spi_example/example.ino b/examples/spi_example/example.ino index 456f759..9c9f668 100644 --- a/examples/spi_example/example.ino +++ b/examples/spi_example/example.ino @@ -5,12 +5,13 @@ Released into the public domain. */ -// 20000000UL <- your chip speed. if arduino cand handle that, it will work at max it can. On uno up to 8mhz. 20000000UL means 20Mhz +// 20000000UL <- your chip speed. if arduino cant handle that, it will work at max it can. On uno up to 8mhz. 20000000UL means 20Mhz +// MOSI to shift register DATA pin, SCLK to CLock pin, Latch pin configurable #define SHIFT_REGISTER_USES_SPI_WITH_FREQUENCY 20000000UL #include // create a global shift register object -// parameters: (data pin, clock pin, latch pin) +// parameters: (latch pin) ShiftRegister74HC595<1> sr(7); void setup() { From a6e4ccf45624ce269fd639dd71367ad0d8064dbc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mindaugas=20=C5=BDvirblis?= Date: Wed, 17 Aug 2022 16:03:32 +0300 Subject: [PATCH 12/14] efficiency --- src/ShiftRegister74HC595.h | 3 +++ src/ShiftRegister74HC595.hpp | 8 +++++--- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/src/ShiftRegister74HC595.h b/src/ShiftRegister74HC595.h index 5ae948d..28100f8 100755 --- a/src/ShiftRegister74HC595.h +++ b/src/ShiftRegister74HC595.h @@ -43,6 +43,9 @@ class ShiftRegister74HC595 uint8_t _latchPin; uint8_t _digitalValues[Size]; + + uint8_t *_port; + uint8_t _pinMask; }; #include "ShiftRegister74HC595.hpp" diff --git a/src/ShiftRegister74HC595.hpp b/src/ShiftRegister74HC595.hpp index d5c65e6..821bb19 100644 --- a/src/ShiftRegister74HC595.hpp +++ b/src/ShiftRegister74HC595.hpp @@ -45,6 +45,9 @@ ShiftRegister74HC595::ShiftRegister74HC595(const uint8_t latchPin) { _latchPin = latchPin; + _pinMask = digitalPinToBitMask(latchPin); + _port = portOutputRegister(digitalPinToPort(latchPin)); + bo = MSBFIRST; // define pins as outputs @@ -125,12 +128,11 @@ void ShiftRegister74HC595::updateRegisters() template void ShiftRegister74HC595::updateRegisters() { + *_port &= ~_pinMask; for (int i = Size - 1; i >= 0; i--) { SPI.transfer(_digitalValues[i]); } - - digitalWrite(_latchPin, HIGH); - digitalWrite(_latchPin, LOW); + *_port |= _pinMask; } #endif // Equivalent to set(int pin, uint8_t value), except the physical shift register is not updated. From 129303d6cab1262737e2a62a8d90f27ddd11efae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mindaugas=20=C5=BDvirblis?= Date: Thu, 15 Sep 2022 00:24:11 +0300 Subject: [PATCH 13/14] check for avr testing on stm32f1 --- src/ShiftRegister74HC595.hpp | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/ShiftRegister74HC595.hpp b/src/ShiftRegister74HC595.hpp index 821bb19..eaad840 100644 --- a/src/ShiftRegister74HC595.hpp +++ b/src/ShiftRegister74HC595.hpp @@ -45,8 +45,10 @@ ShiftRegister74HC595::ShiftRegister74HC595(const uint8_t latchPin) { _latchPin = latchPin; +#ifdef __AVR__ _pinMask = digitalPinToBitMask(latchPin); _port = portOutputRegister(digitalPinToPort(latchPin)); +#endif bo = MSBFIRST; @@ -128,11 +130,16 @@ void ShiftRegister74HC595::updateRegisters() template void ShiftRegister74HC595::updateRegisters() { - *_port &= ~_pinMask; for (int i = Size - 1; i >= 0; i--) { SPI.transfer(_digitalValues[i]); } + #ifdef __AVR__ *_port |= _pinMask; + *_port &= ~_pinMask; + #else + digitalWrite(_latchPin, HIGH); + digitalWrite(_latchPin, LOW); + #endif } #endif // Equivalent to set(int pin, uint8_t value), except the physical shift register is not updated. From ba8c23934a594ee330780f075218637fac6dc743 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mindaugas=20=C5=BDvirblis?= Date: Thu, 15 Sep 2022 00:26:34 +0300 Subject: [PATCH 14/14] adapting to stm32 --- src/ShiftRegister74HC595.h | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/ShiftRegister74HC595.h b/src/ShiftRegister74HC595.h index 28100f8..93ad7d0 100755 --- a/src/ShiftRegister74HC595.h +++ b/src/ShiftRegister74HC595.h @@ -37,7 +37,11 @@ class ShiftRegister74HC595 void order(uint8_t o); private: + #ifdef __AVR__ uint8_t bo = MSBFIRST; + #else + BitOrder bo = MSBFIRST; + #endif uint8_t _clockPin; uint8_t _serialDataPin; uint8_t _latchPin;