Go Down

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

PaulRB

#60
Sep 20, 2019, 07:43 am Last Edit: Sep 20, 2019, 09:35 pm by PaulRB
If I were you, I would test the code by wiring up the 6x '595 with 46 LEDs (+46 series resistors). You may have noticed the code I posted uses millis() rather than micros() which means it will run 1000 times slower, enabling you to observe the LEDs and check the sequence. Then you can play around with the wiring, direction of shifting and use of L/MSBFIRST in the code. Once that's working correctly, you can change millis() to micros() before you wire up your transistors.

"const" is short for constant. It indicates a value that does not change in the code. You can treat const variables just like any other, except that if your code accidentally attempts to change the value, you'll get an error message.

marathonMAN1

#61
Sep 20, 2019, 05:39 pm Last Edit: Sep 20, 2019, 08:30 pm by marathonMAN1
PaulRB;
That's what i figured from the moment i saw the code you presented. seeing the Millis() i could test it first before implementation which is the standard way of doing things and yet i am still grateful you brought it up.

i do have a puzzling question though to you Paul and CrossRoads alike, does it matter if two channels are high on the same port and will this have an adverse effect on the chip.?? ie... port D 0, 1 high at the same time or does it have to be another port.??? or does it even matter.
thank you for the const information.

CrossRoads;
Slave Select is only available on certain Arduino boards. using a board that does not have a Slave Select pin for witch i am aware of,  how do i go about using a general pin for Slave Select and or can any pin be used for this.
also error message; "serial" was not declared in this scope. i did not know i had to declare serial.

thank you both for your time and patients to a newbie.

PaulRB

#62
Sep 20, 2019, 08:42 pm Last Edit: Sep 20, 2019, 08:47 pm by PaulRB
You can have as many pins high as you like, it does not matter. What does matter is the current they source (or sink, when low). Each pin has a 40mA absolute limit on AVR chips (less on teensy, I suspect), but you should keep that to 30mA or less for long life. An unconnected pin sources/sinks no current. A pin driving the gate of a FET will source or sink almost zero current. Or rather, the pin must source current for an instant to turn the FET on, and sink current for an instant to turn the FET off again. The gate of a FET acts like a small capacitor. At low switching frequencies, the current is almost zero, but at higher frequencies it can become significant.

There are also current limits for various groups of pins, and for the chip overall. I don't see this as a problem in your circuit. Pins driving the inputs of other chips also draw almost no current.

CrossRoads

If a chip supports SPI, then it will have a Slave Select pin. Some of the Lilypad boards do not make all the pins available and make SPI usage not possible.

A '328P in a DIP package has a 200mA limit on its VCC and Gnd pin. A '328P in a surface mount package has two Vcc pins, thus the board can deal with more current.  The electrical interface section of the data sheet bears this out, as the total current available for each port (there is a current limit per port) totals up to 300mA.

http://ww1.microchip.com/downloads/en/DeviceDoc/ATmega48A-PA-88A-PA-168A-PA-328-P-DS-DS40002061A.pdf

Table 30-1, page 323
3. Although each I/O port can source more than the test conditions (20mA at VCC = 5V, 10mA at VCC = 3V) under steady state conditions (non-transient), the following must be observed: 
ATmega48A/PA/88A/PA/168A/PA/328/P: 
1] The sum of all IOH, for ports C0 - C5, D0- D4, ADC7, RESET should not exceed 150mA. 
2] The sum of all IOH, for ports B0 - B5, D5 - D7, ADC6, XTAL1, XTAL2 should not exceed 150mA. 
If IIOH exceeds the test condition, VOH may exceed the related specification. Pins are not ensured to source current greater than the listed test condition. 
4. Although each I/O port can sink more than the test conditions (20mA at VCC = 5V, 10mA at VCC = 3V) under steady state conditions (non-transient), the following must be observed: 
ATmega48A/PA/88A/PA/168A/PA/328/P: 
1] The sum of all IOL, for ports C0 - C5, ADC7, ADC6 should not exceed 100mA. 
2] The sum of all IOL, for ports B0 - B5, D5 - D7, XTAL1, XTAL2 should not exceed 100mA. 
3] The sum of all IOL, for ports D0 - D4, RESET should not exceed 100mA. 
If IOL exceeds the test condition, VOL may exceed the related specification. Pins are not ensured to sink current greater than the listed test condition.

Are you using Serial.begin(), Serial.Available(), Serial.read(), Serial.write(), Serial.print()?
Notice all use a capital S, not a small s. C++ is case sensitive, cap/no-cap is an easy mistake to make.

Sorry about being in & out on this thread, I've been kind of busy with other stuff.  What board are you using?
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.

marathonMAN1

#64
Sep 21, 2019, 12:40 am Last Edit: Sep 21, 2019, 08:09 pm by marathonMAN1
PaulRB;

"There are also current limits for various groups of pins, and for the chip overall. I don't see this as a problem in your circuit. Pins driving the inputs of other chips also draw almost no current."

That's what i figured also. there is only a few pins active at a time so i forsee no such problems. my only real concern is the load on the 74HC595's which will basically have 50 to 60 ma ruffly total anyways.

CrossRoads;

The guinea pig in this case is an Arduino Leonardo i had for my Aquaponic control system that got destroyed by my ex room mates. Arduino works though.

do not fret the small stuff, i an just very grateful for the help from you two and i realize people have lives also. whether you take a week to respond it is ok with me.
PS. i am still getting an error code SPI_MODEO not declared in setup.

Thank you both very much.

PaulRB

Quote
PS. i am still getting an error code SPI_MODEO not declared in setup.
Well, that's not right. Post your code.

marathonMAN1

This error is with CrossRoads code. i am working on it so i will post when i get done troubleshooting it.
PaulRB i am wondering about the non pin assignment in your code. i have added the pins in the declaration and in after void set up. is this correct as it still compiles correctly.?

PaulRB

Quote
the non pin assignment
???

You can declare pin constants anywhere you want, as long as they are outside of any function (otherwise they would be local to that function and not visible outside it) and before they are used.

lastchancename

Honestly, no disrespect to OP, but please remember you're planning to switch mains voltages & currents into an inductive load.
This may not be a suitable beginners project
See if you can find a buddy that lives nearby, or buy someone in the Gigs & Collaboration section.
Experienced responders have a nose for laziness, (they were beginners once)... Sure, there are trolls, chest-beaters, and pretenders - but the help you'll get here is about as good as it gets - if you try to help youself!.

marathonMAN1

#69
Sep 23, 2019, 03:13 am Last Edit: Sep 23, 2019, 03:27 am by marathonMAN1
Here is my problem blatant with no disrespect intended.
1. i did not ask you for your opinion but thanks anyways, i might take it under consideration some day just not today.
2. i have been working on this project for 6 years and i really know this device top to bottom especially my Inductor controller and what it is capable of.
3.  if you would of read from the start this device does NOT use main AC it uses DC 100 volts @ 1 amp peaking to 1.4 amps. well i might not have stated that but i did now and if that is the case i do apologize for my lack of awareness to the fact .
if you feel like helping on the coding i would very much appreciate it but i have the device very much covered thank you.

it uses Inductive Reactance to control current flow which is probably not within your realm of understanding of what an Inductor can actually do. the mechanical controller is almost done and i am here to seek advice from experienced coders to convert the controller to electronic to make it totally non moving.
it is one Inductor split into two with two opposing magnetic fields at the rotating brush which allows two separate feeds on either end of the Inductor to control current 180 degrees out from each other. one increasing one  decreasing, vice versa, in complete unison as the brush rotates.

if you care to lean what an active Inductor can really do and how it operates just let me know as i have a ton of research on the matter that i have posted on MY forum.

Regards,
DL
MM

marathonMAN1

PaqulRB;

  I was wondering if i use const for the pin assignment of the remaining pins or do i use just regular pinMode and it if is the previous case how to i go about it.?

i tried to use digitalWriteFast and pinModeFast but it would not compile at all for the Arduino Leonardo.

Coding, ARGE !

lastchancename

REPLIES #5 and 7 pointed you in the right direction, but you don't like the thoughts proposed.
OK by me.
Experienced responders have a nose for laziness, (they were beginners once)... Sure, there are trolls, chest-beaters, and pretenders - but the help you'll get here is about as good as it gets - if you try to help youself!.

marathonMAN1

lastchancename;

  I am already pursuing two different forms of SPI coding and i can not handle a third just yet as i am new to coding and very new to SPI.

PaulRB;

  I figured out why it was not compiling. i downloaded the new digitalWrieFast-Master, followed instruction and presto it compiled. it was having a tissy with the installed Arduino file.

Here is the file, please tell me if i made mistakes.

Code: [Select]

  #include <digitalWriteFast.h>
  #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 = 180UL;
  const byte latchPin = (9);   // SS or CS output to STCK latch pin 12 on 74HC595
  const int clockPin = (11);  // SCk to Seial Clock SHCP Shift Clock pin 11 on 74HC595
  const int dataPin =  (12);  // Mosi to DS in pin 14 on 74HC595

  void setup() {
  Serial.begin(115200);
  SPI.begin();
  SPI.beginTransaction(SPISettings(10000000, MSBFIRST, SPI_MODE0));
  pinModeFast(latchPin, OUTPUT);
  pinModeFast(clockPin, OUTPUT);
  pinModeFast(dataPin,  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
  digitalWriteFast(latchPin, HIGH); //Latch the updated values into the register's output pins
  digitalWriteFast(latchPin, LOW);
  }
}



 

PaulRB

#73
Sep 23, 2019, 05:42 pm Last Edit: Sep 23, 2019, 05:55 pm by PaulRB
These lines are not needed. The SPI clock (SCK) and data (MOSI) pins are fixed and not configurable. They are on the SPI header and not duplicated on any of the Lenonardo's edge pins. They don't seem to have Arduino pin numbers on Leonardo. You don't need to specify them, and if you try to specify them and choose different pins, it won't work on those other pins, it will continue to work on the SPI header pins. You don't need to set them to be outputs, SPI.begin() will do that.
Code: [Select]

  const int clockPin = (11);  // SCk to Seial Clock SHCP Shift Clock pin 11 on 74HC595
  const int dataPin =  (12);  // Mosi to DS in pin 14 on 74HC595

  pinModeFast(clockPin, OUTPUT);
  pinModeFast(dataPin,  OUTPUT);



marathonMAN1

#74
Sep 24, 2019, 01:12 am Last Edit: Sep 24, 2019, 01:46 am by marathonMAN1
Awe !, that is something i very much overlooked, they are constant and dedicated . that just goes to show you do not try to code at 3 am in the morning. isn't the SS or CS pin also not configurable. ?? but i guess since it isn't in the group it isn't static.?

I guess the title of this thread needs to be changes to Arduino SPI to control 74HC595 shift register.

Thank you PaulRB much appreciated.

Go Up