Shift register help.

I have a 7-segment display and a 74HC595N npx shift register, both on a breadboard. Below is my hook up. I am confused on how to send a byte out to serial. I need to keep OE on shift register low so all outputs are enabled and SRCLK high or else all data will be cleared. I only need 8 bits so I looked at the arduino APL reference and found this " byte b = B10010; // "B" is the binary formatter (B10010 = 18 decimal) " but that is confusing because I only need 8 bits not 18? Or am I reading it wrong? Any help would be appreciated! (Also I am assuming I would somehow send the byte stored in the variable out through serial print or something)

7-seg display to shift register (out) hookup:
7 segment display:
A to QA
B to QB
C to QC
D to QD
E to QE
F to QF
G to QG
D.P to QH
common cathodes connected with resistor to ground

Shift register to arduino:
Ser to RX
GND to shift register ground
Vcc to 5v

Use the Arduino's SPI hardware:

#Include<SPI.h>
byte ssPIN = 10;
byte testData = 0xAA;  // test data
void setup(){
pinMode (ssPIN,  OUTPUT);
digitalWrite (ssPin, HIGH);
}
void loop(){
digitalWrite (ssPin, LOW); // RCLK
SPI.transfer(testData); // SCK to SRCLK, MOSI to SER In. OE Low, MRCLR HIGH
digitalWrite (ssPin, HIGH);
}

Is the 0xAA hex? So it would be sending 10101010?

Yes. Just some data to start.

It is saying "ssPin not declared in scope"? (Sorry if It seems if I am just trying to get code without work, for me to really understand the shift register I just need to get it working and see what each element does.)

Also MOSI and MISO get connected together right? So MOSI is arduino (arduino is master), shift reg is MISO. and what does slave select do (SS pin)? and what about SCLK? Is there another way to do this as well as SPI? Just curious so I can learn as much as I can and can go read about it!

Yes, variables are case sensitive, make sure they all match. Looks I had that one a bit different. Make them all the same.
MOSI & MISO do not connect. MISO is not used in this example.
Arduino's MOSI goes to shift register Serial In
Arduino's SCK is the defualt 4 MHz clock, goes to shift register SRCLK
(can use SPI.divisor command, set to 2, and get 8 MHz. Call it after SPI.begin)
Arduino's ssPins is the slave select, we use it move data from shift register's input register to it's output register, rising edge on shift register's RCLK pin.
(Many devices need ssPin low to tell the slave it is being commanded. Here the data goes in the input register, and goes no where fit if RCLK is not toggled.)

Any other method uses software commands to 'manually' (i.e. bit bang) control the SCK and MOSI function. That's what shiftOut( ) command does.
Definitely won't get 4 MHz clock with it. Need to use the SPI hardware for that, it's dedicated set of shift registers connected to D11-D12-D13 pins. D10 must be an output to be a master device (vs a slave, watching ssPin to tell it when to communicate).

Change

#Include<SPI.h>

to

#include <SPI.h>

It will compile but I can’t promise it will work! :wink:

Same error :frowning:

ilovearduinosomuch:
Same error :frowning:

#Include<SPI.h>

So I see.

To CrossRoads,

I connected pin 13 (SCK) to SCLK on shift register, pin 11 (MOSI) to ser in on shift register, pin 10 to OE ,

I changed the code to this and I get "SPI was not declared in this scope"

-- START Questions, not related to code:
Also MOSI = Master out, slave in. so basically output of master (the main device that is in control) and MISO = Master in, slave out. So it is the input on the master and the output of the slave. the ss pin basically when turned high moves the 8 bits in the input register to the output then you put the RCLK (latch pin it sends out the output to control whatever device you have connected to the shift registers outputs? Below is what I think each pin does.

SHIFT REG:
SER - input where data goes in (MOSI)
OE - (output enable) enables output when High and disables when low
RCLK - this is the registers clock, must be pulled high to reshift (reset) new data to their new positions.
SRCLK - serial clock, shifts data when pulled high
SRCLR - serial clear, when pulled low clears all data from register, must be kept high to keep data from clearing.
END Questions, not related to code--

#Include<SPI.h>
byte ssPin = 10;
byte testData = 0xAA;  // test data
void setup(){
pinMode (ssPin,  OUTPUT);
digitalWrite (ssPin, HIGH);
}
void loop(){
digitalWrite (ssPin, LOW); // RCLK
SPI.transfer(testData); // SCK to SRCLK, MOSI to SER In. OE Low, MRCLR HIGH
digitalWrite (ssPin, HIGH);
}
#include <SPI.h>
byte ssPin = 10;
byte testData = 0xAA;  // test data
void setup(){
pinMode (ssPin,  OUTPUT);
digitalWrite (ssPin, HIGH);
}
void loop(){
digitalWrite (ssPin, LOW); // RCLK
SPI.transfer(testData); // SCK to SRCLK, MOSI to SER In. OE Low, MRCLR HIGH
digitalWrite (ssPin, HIGH);
}

Connect Arduino SCK (13) to SRCLK
Connect Arduino MOSI (11) SerData in
Connect Arduino SS (10) to RCLK.
Connect OE/ to GND
Connect SRCLR to +5
Just like the notes I have in the code.

It works somewhat, it is displaying things. I just need to fine tune a few things like making sure all of the correct inputs are paired with outputs etc. On the OE and SRCLR. Hypothetically I could hook OE to a pin and make it low, and the SRCLR set it high right? Also what are the two different chips? The SPI is a synchronous serial data protocol that the atmega has that allows for serial comms. then what controls the rx and tx on the board? Are they two different parts of the MCU or are they totally different components? Thanks :slight_smile:

OE can be a PWM output for brightness control, lower # = brighter, 255 = full off.
SRCLR, just connect that to 5V.
2 different chips?
SPI is supported by one set of internal hardware, Rx/Tx is supported by another set, I2C by yet another set. All are on different IO pins so all can be connected/used together without interference.

It works somewhat, it is displaying things.

This is rather vague. Can you take a video and upload it YOUTUBE and post the link ?

Ok, so basically it is all built in to the atmel 328p in the different blocks . 255? decimal 255? I'll make a quick video and include a link. Thank you for the help :slight_smile:

decimal 255 = 0b11111111 = 0xFF
I'm usually concerned about bit positions, so I use 0b format a lot, and 0x format for less typing of obvious stuff, like walking a 1 across a byte: 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80

I think the reason it isn't working is because my hookup is wrong, I hooked up QA-QH hooked up in order with the inputs on the seg display, so I hooked QA to segment A, QB to segment b and so on.