A
Arduino•2mo ago
Onigiri_Cloud

Need help optimizing 74hc595 updates

Hi i need a way to make my 595 update faster currently it takes about 750 to 850 ms for 8bytes (8bits per iteration) to transfer using shiftout neeh help with to either make shiftout faster or update 595 over spi
37 Replies
Onigiri_Cloud
Onigiri_CloudOP•2mo ago
board: pro micro with atmega32u4
AnonEngineering
AnonEngineering•2mo ago
might be a code issue, Googling indicates shiftOut takes about 150 us (on a much slower Uno)
Onigiri_Cloud
Onigiri_CloudOP•2mo ago
i am assuming u talking about per byte i am doing this 8 times per loop so it gets little high like 8 times that
AnonEngineering
AnonEngineering•2mo ago
8 x 150 us is still only about a millisecond
Onigiri_Cloud
Onigiri_CloudOP•2mo ago
well i still require it being faster the delay of that adds up with other things making it like 3 miliseconds which is lot! and shift out adds the most of it to whole runtime
AnonEngineering
AnonEngineering•2mo ago
800 seemed long, 3 ms I can picture 🙂
Onigiri_Cloud
Onigiri_CloudOP•2mo ago
so.... any idea how to make it faster?
AnonEngineering
AnonEngineering•2mo ago
apparently folks have gained some speed driving the 595 with SPI, I haven't ever done it myself
Onigiri_Cloud
Onigiri_CloudOP•2mo ago
that seemed to be the most obvious answer but it's not working 595 output pins act kinda when i do spi and i am not sure if am doing it right or some bug? cause work fine with shiftout
#include <SPI.h>

const int latchPin = 14;

void setup() {
pinMode(latchPin, OUTPUT);
digitalWrite(latchPin, LOW);

SPI.begin();
}

void loop() {

sendToShiftRegister(0b10101010);
delay(500);
sendToShiftRegister(0b01010101);
delay(500);
}

// Function to send one byte to the 74HC595 via SPI
void sendToShiftRegister(uint8_t data) {
digitalWrite(latchPin, LOW);
SPI.transfer(data); digitalWrite(latchPin, HIGH);
}
#include <SPI.h>

const int latchPin = 14;

void setup() {
pinMode(latchPin, OUTPUT);
digitalWrite(latchPin, LOW);

SPI.begin();
}

void loop() {

sendToShiftRegister(0b10101010);
delay(500);
sendToShiftRegister(0b01010101);
delay(500);
}

// Function to send one byte to the 74HC595 via SPI
void sendToShiftRegister(uint8_t data) {
digitalWrite(latchPin, LOW);
SPI.transfer(data); digitalWrite(latchPin, HIGH);
}
this is what i did
pseud0
pseud0•2mo ago
How did you connect each pin?
No description
Onigiri_Cloud
Onigiri_CloudOP•2mo ago
VCC to vcc SER to arduino 16 as data OE to ground RCLK to 14 as latch SRCLK to 15 as clock SRCLR to VCC
No description
pseud0
pseud0•2mo ago
And what about pins like master reset
AnonEngineering
AnonEngineering•2mo ago
isn't that SRCLR?
pseud0
pseud0•2mo ago
oh right that's your serial clear. GND to GNd technically.
Onigiri_Cloud
Onigiri_CloudOP•2mo ago
it is the same wait going to try the same code again
AnonEngineering
AnonEngineering•2mo ago
yeah, i just changed the latch pin
Onigiri_Cloud
Onigiri_CloudOP•2mo ago
i think... it got updated once or twice not sure... it's on 0b01010101
AnonEngineering
AnonEngineering•2mo ago
in other words your real life circuit isn't like the sim?
Onigiri_Cloud
Onigiri_CloudOP•2mo ago
void loop() {

sendToShiftRegister(0b10101010);
Serial.println("0b10101010");
delay(1000);
sendToShiftRegister(0b01010101);
Serial.println("0b01010101");
delay(1000);
}
void loop() {

sendToShiftRegister(0b10101010);
Serial.println("0b10101010");
delay(1000);
sendToShiftRegister(0b01010101);
Serial.println("0b01010101");
delay(1000);
}
yea can say that too also installed the spark fun promicro board defination just to make sure it wasn't the cause of issue
AnonEngineering
AnonEngineering•2mo ago
how about not using 14 for latch (it's MISO)
Onigiri_Cloud
Onigiri_CloudOP•2mo ago
it has pinMode(latchPin, OUTPUT); think that disables 14 as miso? but yea will try that wow that fixed it! so turns out... this not true thanks for all the help! so i might have lost my last digital io pin but spi works! didn't have any plans of using that as for now so.. guess its fine?
AnonEngineering
AnonEngineering•2mo ago
so it was using MISO as latch that was slowing it down?
wtf
wtf•2mo ago
use SPI oh you are
Onigiri_Cloud
Onigiri_CloudOP•2mo ago
i think? latch definitely wasn't working right with miso as latch
AnonEngineering
AnonEngineering•2mo ago
but it runs at milliseconds now rather than 100's of ms?
Onigiri_Cloud
Onigiri_CloudOP•2mo ago
i am using the pin the same way as before just changed the physical pin to 10 and 8bytes takes 80us now allows my whole to to run in about 1ms after further optimizations ofc i also had to speead up analog read clock a little
AnonEngineering
AnonEngineering•2mo ago
ok, 3 orders of magnitude is a good improvement 😉
Onigiri_Cloud
Onigiri_CloudOP•2mo ago
it's actually more cause in the previous 3ms there were not analog reads involved the current code at slow speed as before would surely be more
AnonEngineering
AnonEngineering•2mo ago
well, thanks for bringing it up, i'd only ever used shiftOut with 595s before, TIL 🙂 can't figure out how to change the bit order though...
Onigiri_Cloud
Onigiri_CloudOP•2mo ago
shift out works great ofc but if your are doing things that are like time critical it's has to be on spi i wanted my code to run under 1 ms so that does the job great think u just do MSBFIRST or LSBFIRST? for most/least significant bit never tried it myself it was always on MSBFIRST
AnonEngineering
AnonEngineering•2mo ago
yes, i was trying SPI.beginTransaction(SPISettings(15000000, LSBFIRST, SPI_MODE0)); but it seems to always do MSB
Onigiri_Cloud
Onigiri_CloudOP•2mo ago
weird manually reverse all the bits u want to send? xD
AnonEngineering
AnonEngineering•2mo ago
there's always that!
Onigiri_Cloud
Onigiri_CloudOP•2mo ago
it's a dirty way ofc but when nothing works yea
AnonEngineering
AnonEngineering•2mo ago
it works fine with shiftOut the SPI docs on Arduino are all over the place, seems the core changed in 2014 but most docs out there pre-date that one day, if i need the speed, and LSB, i'll dig more
Onigiri_Cloud
Onigiri_CloudOP•2mo ago
yea i coudn't find much about it this was huge help

Did you find this page helpful?