Go Down

Topic: Using Arduino to control 74HC595 not CD40110BE in cascade using SPI. (Read 1 time) previous topic - next topic


PaulRb, Crossroads i have search the net for two days and i have found nothing worth a darn about << >> unsigned long long  or SCK . everything i found just wanted to describe it or it's function with no coding or sketch as an example. i am completely lost in that field.
i can design the circuit, design the board on DipTrace with the transistor board to boot but beyond simple coding i am stuck like chuck.

any advice will be well taken.


Sep 16, 2019, 05:03 am Last Edit: Sep 16, 2019, 05:04 am by CrossRoads
Put your data into an array, and then shift the data into 75HC595, or TPIC6C595 or TPIC6B595 if you need more current drive.

This code shows just doing one pass of data, you can change it to output bytes 0 to 5, then 1 to 6, 2 to 7, etc.

Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.


Sep 16, 2019, 07:31 am Last Edit: Sep 16, 2019, 09:37 pm by marathonMAN1

 Thank you so much, i will download and study till my eyes fall out.
one thing though, i thought it was an 8 bit per port or is that depend on pin ability as some can not be used.  can you explain the bit group count, do you mean to overlap.???

another thing is i see you daisy chained them. in the original circuit i had it used pin 14 of each 595 to Arduino separately and worked fine. will this be able to shift 0 to 46 and back to zero with additional line of code. ?

with this programming will i be able to overlap like the timing drawing i posted??? overlap believe it or not in this situation is a must in the controller.
sorry for the question.
so with a quick glance it looks like a standard Arduino should run a sketch with 595's interfacing with a logic NPN MosFet switching PNP MosFet on the high side.

another thing is why is pin 13 in your schematic from all 595's going to the Arduino??  A HIGH on OE causes the outputs to assume a high-impedance OFF-state which will kill the output.
your the best and thanks again.

EDIT; found a lot of info on Gammon.com.au forum concerning SPI.


I've had a go at some example code for you. Not properly tested!
Code: [Select]
#include <SPI.h>

unsigned long long int registerBuffer = 0b11;
const unsigned long long int bit45 = 1ULL<<45;
unsigned long int lastUpdate;
const unsigned long updatePeriod = 177UL;
const byte latchPin = 10;

void setup() {
  SPI.beginTransaction(SPISettings(10000000, MSBFIRST, SPI_MODE0));
  pinMode(latchPin, OUTPUT);

void loop() {
  if (millis() - lastUpdate > updatePeriod) { //Is it time to update the registers?
    lastUpdate = millis();
    byte wrap = (registerBuffer & bit45) != 0; //Copy bit 45 from the buffer
    registerBuffer <<= 1; //Shift all bits in the buffer 1 place to the right
    if (wrap) registerBuffer |= 1; //Paste the copied value from bit 45 into bit 0
    unsigned long long int temp = registerBuffer; //Take a copy of the buffer
    SPI.transfer(&temp, 6); //Send the buffer to the registers
    digitalWrite(latchPin, HIGH); //Latch the updated values into the register's output pins
    digitalWrite(latchPin, LOW);

@CrossRoads, @dougp, @TomGeorge, @Paul__B, @lastchancename, @johnwasser please check this code for me!


Sep 18, 2019, 02:10 am Last Edit: Sep 18, 2019, 04:49 pm by marathonMAN1
thank you so much PaulRb. between you and CrossRoads you both have a different approach to the same outcome which is very good. i also thought digitalWrite was to slow for something like this. is there a serialWrite () or is that a wrong usage of serialWrite.

i am curious as to direct port manipulation. see the example shown and tell me what you think about the feasibility of such an approach. is millis(); used instead of delay microseconds, i looks like i would need a lot of channels to satisfy this beast. even though this looks easier i think the code from both of you will be better in the long run, i hope.

Code: [Select]

PORTD =0b00000001; // Bit 0
PORTD =0b00000011; // Bit 0,1
PORTD =0b00000110; // Bit 1,2
PORTD =0b00001100; // Bit 2,3
PORTD =0b00011000; // Bit 3,4
PORTD =0b00110000; // Bit 4,5
PORTD =0b01100000; // Bit 5,6
PORTD =0b11000000; // Bit 6,7
PORTD =0b10000000; // Bit 7
PORTB =0b00000001; // Bit 0  bit 8 = PORTB bit 0)
PORTB =0b00000011; // Bit 0,1
PORTD =0b00000110; // Bit 1,2
PORTD =0b00001100; // Bit 2,3


Sep 18, 2019, 07:42 am Last Edit: Sep 18, 2019, 08:04 am by PaulRB
Yes, I think this is also an approach to consider. Obviously you would need a Mega for this. One downside to the approach is that, if you find you need to move to a faster processor, your code would need to be rewritten, because the direct port manipulation code above is specific to AVR processors. There will probably be equivalent commands for other MCU, but for example their ports may be 16 bits wide, not 8.

Never heard of serialWrite. Did you mean Serial.write() ? If so, that's not for setting output pins, it's similar to Serial.print().


I Figured as much for the top part. i was researching it last night and came to the same conclusion that it would have to be modded per chip ports.
you are correct about SerialWrite, i went back to take another peak.

i do have a Tensy 3.6 with a Tall Dog break out but i need 5 volts output for the logic level NPN's so the Tensy would "just" turn it on and that is no good. is there any way to use Serial, SPI or I2C  two Arduino's together to get my 46 channels and 5 volts in a master slave situation. i am just asking as a last resort as i will try both your and CrossRoad's way to time the board.

it is to bad the STP16C596 doesn't have shift capabilities at 16 channels it sure would of made thing easier to design. all the output are on one end with the rest on the other.

Thanks again PaulRB and you to CrossRoads for the wonderful help you both have given me.


Tall Dog break out
No idea what that is, please post a link.

but i need 5 volts output for the logic level NPN's
I thought we had covered this earlier in the thread, but maybe I'm getting confused with another thread. NPN transistors are not switched by voltage, they are switched by current. 3.3V vs 5V is not a problem, you simply compensate with a lower value resistor in series with the base.

is there any way to use Serial, SPI or I2C  two Arduino's together
Yes, but almost always a bad idea. Certainly a bad idea in this case. I2C is slow. SPI would be fast enough, but why use another Arduino, which needs coding, debugging etc. when you can just use simple shift registers?

it is to bad the STP16C596 doesn't have shift capabilities at 16 channels it sure would of made thing easier to design. all the output are on one end with the rest on the other.
Lost me there. No-one has suggested STP16C596. It does have 16 channels. But the max voltage is 16V, so cannot be exposed to your 100V+. Cannot drive NPN because the outputs can only sink current, not source it. No idea what you mean by "all the output are on one end with the rest on the other".


Sep 19, 2019, 04:29 am Last Edit: Sep 19, 2019, 04:31 am by marathonMAN1
Here is the link to the Tensy 3.6 Tall Dog breakout board.

it is really nice having all pin outs easily available and not have to fiddle with the underside. easily fits in the palm of your hand.

i was referring to Logic level enhanced Mosfet NPN not a current controlled bjt for which the Mosfet i have connected to an enhanced Mosfet PNP for the grunt work.
thanks for the info about the different interfaces, good to know about i2c being slow.

as for the STP16C596, all the outputs are on the lower half of the ic on both sides and the control pins are on the top half making it really nice to trace out on a board. i was just stating that it would be nice if the ic had latch capabilities and not sink only.

i have a question about the code, do the interrupts needs to be turned off as to not interfere with the SPI function and correct timing or is that not a problem. interrupts can add time to the sketch  throwing the time off right.?

Thank you much.


Sep 19, 2019, 08:13 am Last Edit: Sep 19, 2019, 08:25 am by PaulRB
Thanks for the link to the link to the tall boy thing. Not something I would choose, but each to their own.

Can you post a link to these "NPN MOSFETs" please. I've only heard of "n-channel MOSFETs", so would be interested to understand the difference.

You don't need to turn interrupts off for SPI. Exact timing is not as critical with SPI as with some other types of serial communications, because there are separate clock and data lines and the correct timing is whatever the master device says it is. So if software SPI were used and interrupts caused slightly inconsistent timing, that would not matter. But for your application you need to use hardware SPI, and that wouldn't be affected by interrupts during the transmission of each byte of data, although it could cause minor delays between bytes, but again thats not a problem. Are you planning to use interrupts? I am not aware of any need to do that for your project.

STP16C596 does have latch capability. Its outputs can sink but cannot source current.


Unfortunately i have a bad habit of using NPN, yes we are talking about the same thing, N channel MosFets.
i was just curious as i have been studying up on SPI and came across the data that suggested possible interference issues so i thought i would bring it up to you as you have MUCH more experience then i do at this sort of thing, which by the way thank you for the info. no i do not plan on using interrupts as they are not needed in my situation.
as for the STI it can shift but as you said it has no latch and can only sink. i have the data sheet on my computer with a some guy's program that uses it for a LED bar graph that is why i brought it up. no i do not plan to use it, only the 595.

any luck on getting the sketch a second eye.?

again thank you and everyone else for the help. when i am finally finished with my active inductor i will post link to it in action and i will also post you guy's as the leading contributors in making this a reality.


as you said it has no latch
No, I said the opposite of that.

I can't imagine the SPI circuit causing interference, but it (and the rest of the circuit) might be affected by interference. You will be switching high voltages and currents and creating fast moving magnetic fields. That's a recipy for interference problems! I'm not an expert in how to minimise that risk.


Bad eyes all i can say,  LE Latch pin 4 sorry.

concerning an Byte Array how do you shift the bytes from 0 byte to 45 and back to 0 and would it matter if it was MSB or LSB.?

also in the set up you used const int LatchPin, clockPin, and dataPin, when i declare them as outputs do i still use pinMode in the setup.?

Remember i am a newbie at coding so don't byte.


There's no byte array in the code I suggested. Are you asking @CrossRoads?

Regarding L/MSBFIRST, yes, as I said, not properly tested, some corrections will almost certainly be needed!


My apologies PaulRB, i did get the two mixed up, i have labeled them accordingly.
it seams the two of you are taking such different routs and it is a little confusing.
i actually am trying to pursue both so now i have separated them into different folders with the research also.
as i stated earlier does it matter if LSB or MSB i would like to know if it changes anything. i am sure the program can start with either way Right.?
can you explain the usage of const int and how do i declare the pins.?

Go Up