shiftOut using SPI isn't working I made a sample using two of them using Arduino UNO.
So I did that like this:


(There aren't any resistor on that image, but there are 220ohm resistor on every LEDs.)
And uploaded this code.

//1.6us+62.5ns
#include &lt;SPI.h&gt;

#define sbi(port, bit) (port) |= (1 &lt;&lt; (bit))
#define cbi(port, bit) (port) &amp;= ~(1 &lt;&lt; (bit))

int latchPin = 12;
int clockPin = 13;
int dataPin = 11;

byte data[]={ 0b10101010,0b11001100 };

void _595_out() {
    cbi(PORTB, 4);
    SPI.transfer(data[0] );
    SPI.transfer( data[1] );
    sbi(PORTB, 4);
}

void setup() {
    pinMode(latchPin, OUTPUT);
    pinMode(clockPin, OUTPUT);
    pinMode(dataPin, OUTPUT);

    SPI.setClockDivider(SPI_CLOCK_DIV2);
    SPI.setBitOrder(MSBFIRST);
    SPI.setDataMode(SPI_MODE0);
    SPI.begin();
}

void loop() {
    _595_out();
}


And this worked as I expected. But, when I changed sketch like this,

//1.6us+62.5ns
#include &lt;SPI.h&gt;

#define sbi(port, bit) (port) |= (1 &lt;&lt; (bit))
#define cbi(port, bit) (port) &amp;= ~(1 &lt;&lt; (bit))
#define CHIPNO 2
int latchPin = 12;
int clockPin = 13;
int dataPin = 11;

byte data[]={ 0b10101010,0b11001100 };

void _595_out() {
    cbi(PORTB, 4);
    SPI.transfer(data[0]);
    SPI.transfer(data[1]);
    sbi(PORTB, 4);
}

void setup() {
    pinMode(latchPin, OUTPUT);
    pinMode(clockPin, OUTPUT);
    pinMode(dataPin, OUTPUT);

    SPI.setClockDivider(SPI_CLOCK_DIV2);
    SPI.setBitOrder(MSBFIRST);
    SPI.setDataMode(SPI_MODE0);
    SPI.begin();
    Serial.begin(9600);
}

void loop() {
    for(unsigned int z = 0; z &lt; 65535; z++) {
        data[0] = z &amp; 255;
        data[1] = (z &amp; 65280) &gt;&gt; 8;
        Serial.println("A");
        Serial.print(data[0]);
        Serial.print("/");
        Serial.println(data[1]);
        Serial.println("B");
        _595_out();
        Serial.println("C");
        delay(500);
    }
}


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?
int latchPin = 12;


You use pin 12 as the latch pin. Once you activate SPI the hardware takes over control of the pins MOSI, MISO and SCK. Pin 12 is MISO and thus attempting to change it in your code will have no effect.

You should use some other pin as the latch pin (pin 10 is often used).




  And shiftOut using SPI is working when I use first code


shiftOut is a software-based method of clocking out bits. It doesn't use hardware and thus it doesn't take control of pin 12.



Note: Answered originally on the Arduino Forum