74HC595s aren't working. Any LEDs aren't on.
Printing Serial works well. Only 74HC595s aren't working.
And Removing all Serial-related thing doesn't solved this problem.
Where did I do wrong?
Even if you don't use pin 10 for chip select, it must be set to OUTPUT for SPI to work as a master.
All AVR based boards have an SS pin that is useful when they act as a slave controlled by an external master. Since this library supports only master mode, this pin should be set always as OUTPUT otherwise the SPI interface could be put automatically into slave mode by hardware, rendering the library inoperative.
Try this. I simplified it some, got rid of the default SPI settings, put the SPI clock after SPI.begin, made D10 an output (needed to be SPI master), and got rid of pin assignments that SPI.begin takes care of. You should have the shift register clock (SRCLK) on D13, the data to the first device on D11, and the output latch (SRCK) on D4, but I would use D10 as it has to be an output anyway, why not use it?
//1.6us+62.5ns
#include <SPI.h>
byte ssPin = 10; // must be output for SPI master
byte latchPin = 4;
byte data[]={ 0b10101010,0b11001100 };
void _595_out() {
PORTD = PORTD & 0b11101111; // D4 low
SPI.transfer(data[0]);
SPI.transfer(data[1]);
PORTD = PORTD | 0b00010000; // D4 high
}
void setup() {
pinMode(latchPin, OUTPUT);
pinMode (ssPin, OUTPUT);
SPI.begin();
SPI.setClockDivider(SPI_CLOCK_DIV2); // these follow SPI.begin().
Serial.begin(9600);
}
void loop() {
for(unsigned int z = 0; z < 65535; z++) {
data[0] = z & 255;
data[1] = (z & 65280) >> 8;
Serial.println("A");
Serial.print(data[0]);
Serial.print("/");
Serial.println(data[1]);
Serial.println("B");
_595_out();
Serial.println("C");
delay(500);
}
}
groundfungus, That's quite important information to me
As johnwasser and CrossRoads said, changing latch to Pin10(PORTB 2) worked well.
And also I simplified the code thanks to CrossRoads.
74HC595 is not really an SPI device? Sure it is. I use SPI.transfer() with 595 shift registers exclusively, it is much faster to send data out than shiftOut().
Especially at 8 MHz:
SPI.setClockDivider(SPI_CLOCK_DIV2);
shiftOut() can't touch that.
CrossRoads:
74HC595 is not really an SPI device? Sure it is. I use SPI.transfer() with 595 shift registers exclusively, it is much faster to send data out than shiftOut().
Especially at 8 MHz:
SPI.setClockDivider(SPI_CLOCK_DIV2);
shiftOut() can't touch that.
Yea, you’re right. I was incorrectly thinking of more specialized SPI protocols where specific data needs to be shifted into MISO right after commands go out on MOSI.