0

I made a sample using two of them using Arduino UNO.
So I did that like this:

Arduino with 74HC595 (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 <SPI.h>

#define sbi(port, bit) (port) |= (1 << (bit))
#define cbi(port, bit) (port) &= ~(1 << (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 <SPI.h>

#define sbi(port, bit) (port) |= (1 << (bit))
#define cbi(port, bit) (port) &= ~(1 << (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 < 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);
    }
}

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?

  • Did you do a Serial.begin()? Please post the entire sketch that doesn't work. – Nick Gammon Jan 13 '17 at 8:12
  • 1
    shiftOut using SPI isn't working - And this worked as I expected. - so SPI is in fact working? – Nick Gammon Jan 13 '17 at 8:14
  • @NickGammon I just changed the loop function like that and added Serial.begin(9600); ath the end of the setup function. Everything except these is same as the first code, so that's why I didn't upload the entire sketch... And shiftOut using SPI is working when I use first code, but isn't working when I change the code as I said. – Lee M.U. Jan 13 '17 at 10:17
  • @NickGammon What I trying to do is changing outputs of 74HC595 every 0.5sec at very fast speed and that's why I'm using SPI. On first code, it's sending same outputs every loop, and it worked so I just changed that code to send different value every loop. But now it doesn't work. That's what it happened :-P – Lee M.U. Jan 13 '17 at 10:30
  • Why are you wanting to drive AY-3-8910 chips with shift registers...? It sounds like you are thinking of having one AY-3-8910 per shift register pair, which is just plain silly. Do some research about how these kind of chips interface to a CPU bus, which is what they are designed to do. – Majenko Jan 13 '17 at 10:31
0
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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.